openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
sim_utils.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // openCARP is an open cardiac electrophysiology simulator.
3 //
4 // Copyright (C) 2020 openCARP project
5 //
6 // This program is licensed under the openCARP Academic Public License (APL)
7 // v1.0: You can use and redistribute it and/or modify it in non-commercial
8 // academic environments under the terms of APL as published by the openCARP
9 // project v1.0, or (at your option) any later version. Commercial use requires
10 // a commercial license (info@opencarp.org).
11 //
12 // This program is distributed without any warranty; see the openCARP APL for
13 // more details.
14 //
15 // You should have received a copy of the openCARP APL along with this program
16 // and can find it online: http://www.opencarp.org/license
17 // ----------------------------------------------------------------------------
18 
27 #ifndef _SIM_UTILS_H
28 #define _SIM_UTILS_H
29 
30 #include <cstdio>
31 #include <cstdlib>
32 #include <cstring>
33 
34 #include "Typedefs.h"
35 #include "sf_interface.h"
36 #include "timer_utils.h"
37 #include "physics_types.h"
38 #include "build_info.h"
39 
40 
41 #ifndef CARP_PARAMS
42 #define CARP_PARAMS
43 #include "openCARP_p.h"
44 #include "openCARP_d.h"
45 #endif
46 
47 #include "IGBheader.h"
48 
49 
50 namespace opencarp {
51 
54 
57 #define DATAOUT_NONE 0
58 #define DATAOUT_SURF 1
59 #define DATAOUT_VOL 2
60 #define DATAOUT_VTX 3
61 
62 namespace user_globals {
66  extern std::map<mesh_t, sf_mesh> mesh_reg;
68  extern std::map<SF::quadruple<int>, SF::index_mapping<int> > map_reg;
70  extern std::map<physic_t, Basic_physic*> physics_reg;
72  extern timer_manager* tm_manager;
74  extern std::map<datavec_t, sf_vec*> datavec_reg;
76  extern FILE* petsc_error_fd;
78  extern bool using_legacy_stimuli;
80  extern MPI_Comm IO_Intercomm;
81 }
82 
89 void parse_params_cpy(int argc, char** argv);
90 
94 void register_physics();
98 void initialize_physics();
102 void destroy_physics();
103 
107 void simulate();
108 
110 void post_process();
111 
118 void parse_mesh_types();
119 
126 void setup_meshes();
127 
131 void output_meshes();
132 
133 // flags to go with solution type
134 #define SOLV_METH(A,B) (B & A##_FLAG)
135 #define MONODOMAIN_FLAG 1
136 #define ITERATE_FLAG 2
137 #define CN_PARAB_FLAG 4
138 #define PURKINJE_FLAG 8
139 #define O2dT_PARAB_FLAG 16
140 #define PSEUDO_BIDM_FLAG 32
141 
142 // simulation modes
143 #define MONODOMAIN 0
144 #define BIDOMAIN 1
145 #define PSEUDO_BIDM 2
146 
147 // post processting options
148 // keep in jive with post_processing_opts in carp.prm
149 #define RECOVER_PHIE 1
150 #define OPTICAL_MAP 2
151 #define ACTIVATING_FUNCTION 4
152 #define AXIAL_CURRENTS 8
153 #define FILAMENTS 16
154 #define CURRENT_DENSITIES 32
155 
156 // experiment defs, keep in jive with carp.prm
157 #define EXP_NORMAL 0
158 #define EXP_OUTPUT_FEM 1
159 #define EXP_LAPLACE 2
160 #define EXP_SETUP_MESH 3
161 #define EXP_POSTPROCESS 4
162 
169 unsigned int classify_soln_methods();
170 
184 
186 void setup_petsc_err_log();
187 
202 void set_io_dirs(char *sim_ID, char *pp_ID, IO_t init);
203 
204 bool setup_IO(int argc, char **argv);
209 void update_cwd();
216 int set_dir(IO_t dest);
217 
223 void basic_timer_setup();
224 
229 int plot_protocols(const char *);
230 
238 Basic_physic* get_physics(physic_t p, bool error_if_missing = true);
239 
241 short get_mesh_dim(mesh_t id);
242 
249 void register_data(sf_vec* dat, datavec_t d);
250 
251 [[noreturn]] void cleanup_and_exit();
252 
253 // return the direcotry path of a given file
254 char* get_file_dir(const char* file);
263 
265 struct prog_stats {
266  double op;
267  double start;
268  double last;
269  double curr;
270 };
271 
272 struct sync_io_item {
276  bool elem_flag;
278 };
279 
282  int IO_id;
285 };
287 {
288  private:
293  sf_vec* fill_output_buffer(const sync_io_item & it);
294 
295 
296  void register_output_async(sf_vec* inp_data,
297  const mesh_t inp_meshid,
298  const int dpn,
299  const char* name,
300  const char* units,
301  const SF::vector<mesh_int_t>* idx,
302  bool elem_data);
303 
304  public:
308  std::map<SF::mixed_tuple<mesh_t, int>, sf_vec*> buffmap;
309  std::map<SF::mixed_tuple<mesh_t, int>, sf_vec*> buffmap_elem;
310 
311  void register_output_sync(sf_vec* inp_data,
312  const mesh_t inp_meshid,
313  const int dpn,
314  const char* name,
315  const char* units,
316  const SF::vector<mesh_int_t>* idx = NULL,
317  bool elem_data = false);
318 
330  void register_output(sf_vec* inp_data,
331  const mesh_t inp_meshid,
332  const int dpn,
333  const char* name,
334  const char* units,
335  const SF::vector<mesh_int_t>* idx = NULL,
336  bool elem_data = false);
337 
339  void write_data();
340 
349  IGBheader* get_igb_header(const sf_vec* vec);
350 
352  void close_files_and_cleanup();
353 };
354 
365 void output_parameter_file(const char *fname, int argc, char **argv);
366 
368 void savequit();
369 
371 void show_build_info();
372 
373 } // namespace opencarp
374 
375 #endif
void register_physics()
Register physics to the physics registry.
Definition: sim_utils.cc:89
bool elem_flag
igb header we use for output
Definition: sim_utils.h:276
std::map< physic_t, Basic_physic * > physics_reg
the physics
Definition: main.cc:50
IO_t
The different output (directory) types.
Definition: sim_utils.h:53
void output_parameter_file(const char *fname, int argc, char **argv)
Definition: sim_utils.cc:1561
void set_io_dirs(char *sim_ID, char *pp_ID, IO_t init)
Definition: sim_utils.cc:354
const SF::vector< mesh_int_t > * restr_idx
when using asyncIO, here we store the different IDs associated to the vectors we output ...
Definition: sim_utils.h:283
tagreg_t
tag regions types. must be in line with carp.prm
Definition: sim_utils.h:56
double op
output period
Definition: sim_utils.h:266
std::map< mesh_t, sf_mesh > mesh_reg
Registry for the different meshes used in a multi-physics simulation.
Definition: main.cc:46
void simulate()
Main simulate loop.
Definition: sim_utils.cc:791
void post_process()
do postprocessing
Definition: sim_utils.cc:848
timer_manager * tm_manager
a manager for the various physics timers
Definition: main.cc:52
std::map< SF::mixed_tuple< mesh_t, int >, sf_vec * > buffmap
map data spec -> PETSc vector buffer
Definition: sim_utils.h:308
SF::vector< mesh_int_t > restr_petsc_idx
pointer to index vector with nodal indices we restrict to.
Definition: sim_utils.h:284
double last
last output wallclock time
Definition: sim_utils.h:268
void setup_petsc_err_log()
set up error logs for PETSc, so that it doesnt print errors to stderr.
Definition: sim_utils.cc:1284
void initialize_physics()
Initialize all physics in the registry.
Definition: sim_utils.cc:103
void savequit()
save state and quit simulator
Definition: sim_utils.cc:1646
IGBheader igb
pointer to index vector used for restricting output.
Definition: sim_utils.h:275
mesh_t
The enum identifying the different meshes we might want to load.
Definition: sf_interface.h:58
physic_t
Identifier for the different physics we want to set up.
Definition: physics_types.h:51
void check_and_convert_params()
Here we want to put all parameter checks, conversions and modifications that have been littered throu...
Definition: sim_utils.cc:230
char * get_file_dir(const char *file)
Definition: sim_utils.cc:1275
Timers and timer manager.
for display execution progress and statistical data of electrical solve
Definition: sim_utils.h:265
bool setup_IO(int argc, char **argv)
Definition: sim_utils.cc:425
bool using_legacy_stimuli
flag storing whether legacy stimuli are used
Definition: main.cc:58
FILE * petsc_error_fd
file descriptor for petsc error output
Definition: main.cc:56
double start
output start wallclock time
Definition: sim_utils.h:267
std::map< SF::quadruple< int >, SF::index_mapping< int > > map_reg
Registriy for the inter domain mappings.
Definition: main.cc:48
void cleanup_and_exit()
Definition: sim_utils.cc:1260
void parse_mesh_types()
Parse the phys_type CLI parameters and set up (empty) SF::meshdata meshes.
Definition: sim_utils.cc:900
void parse_params_cpy(int argc, char **argv)
Initialize input parameters on a copy of the real command line parameters.
Definition: sim_utils.cc:46
double curr
current output wallclock time
Definition: sim_utils.h:269
void destroy_physics()
Destroy all physics in the registry.
Definition: sim_utils.cc:127
const SF::vector< mesh_int_t > * restr_idx
pointer to data registered for output
Definition: sim_utils.h:274
std::map< SF::mixed_tuple< mesh_t, int >, sf_vec * > buffmap_elem
Definition: sim_utils.h:309
void basic_timer_setup()
Here we set up the timers that we always want to have, independent of physics.
Definition: sim_utils.cc:495
int plot_protocols(const char *fname)
plot simulation protocols (I/O timers, stimuli, boundary conditions, etc)
Definition: sim_utils.cc:573
void setup_meshes()
Read in the reference mesh and use its data to populate all meshes registered in the mesh registry...
Definition: sim_utils.cc:1030
void show_build_info()
show the build info, exit if -buildinfo was provided. This code runs before MPI_Init().
Definition: sim_utils.cc:222
SF::abstract_vector< SF_int, SF_real > sf_vec
Definition: sf_interface.h:49
void output_meshes()
Definition: sim_utils.cc:1208
void register_data(sf_vec *dat, datavec_t d)
Register a data vector in the global registry.
Definition: sim_utils.cc:890
Basic_physic * get_physics(physic_t p, bool error_if_missing)
Convinience function to get a physics.
Definition: sim_utils.cc:864
Interface to SlimFem.
SF::mixed_tuple< mesh_t, int > spec
flag whether the data is elements-wise
Definition: sim_utils.h:277
MPI_Comm IO_Intercomm
Communicator between IO and compute worlds.
Definition: main.cc:60
Index mapping class. This is a bijective mapping.
Definition: SF_container.h:207
unsigned int classify_soln_methods()
void update_cwd()
save the current working directory to curdir so that we can switch back to it if needed.
Definition: sim_utils.cc:478
int set_dir(IO_t dest)
Definition: sim_utils.cc:483
short get_mesh_dim(mesh_t id)
get (lowest) dimension of the mesh used in the experiment
Definition: sim_utils.cc:1299
SF::vector< async_io_item > async_IOs
Definition: sim_utils.h:306
std::map< datavec_t, sf_vec * > datavec_reg
important solution vectors from different physics
Definition: main.cc:54
std::map< int, std::string > units
Definition: stimulate.cc:41
datavec_t
Enum used to adress the different data vectors stored in the data registry.
Definition: physics_types.h:99
SF::vector< sync_io_item > sync_IOs
Definition: sim_utils.h:305
sf_vec * get_data(datavec_t d)
Retrieve a petsc data vector from the data registry.
Definition: sim_utils.cc:880
int IO_id
pointer to data registered for output
Definition: sim_utils.h:282
SF::scatter_registry scatter_reg
Registry for the different scatter objects.
Definition: main.cc:44
The scatterer registry class.
Basic physics types.