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 #ifndef CARP_PARAMS
41 #define CARP_PARAMS
42 #include "openCARP_p.h"
43 #include "openCARP_d.h"
44 #endif
45 
46 #include "IGBheader.h"
47 
48 
49 namespace opencarp {
50 
53 
56 #define DATAOUT_NONE 0
57 #define DATAOUT_SURF 1
58 #define DATAOUT_VOL 2
59 #define DATAOUT_VTX 3
60 
61 namespace user_globals {
65  extern std::map<mesh_t, sf_mesh> mesh_reg;
67  extern std::map<SF::quadruple<int>, SF::index_mapping<int> > map_reg;
69  extern std::map<physic_t, Basic_physic*> physics_reg;
71  extern timer_manager* tm_manager;
73  extern std::map<datavec_t, sf_vec*> datavec_reg;
75  extern FILE* petsc_error_fd;
77  extern bool using_legacy_stimuli;
79  extern MPI_Comm IO_Intercomm;
80 }
81 
88 void parse_params_cpy(int argc, char** argv);
89 
93 void register_physics();
97 void initialize_physics();
101 void destroy_physics();
102 
106 void simulate();
107 
109 void post_process();
110 
117 void parse_mesh_types();
118 
125 void setup_meshes();
126 
130 void output_meshes();
131 
132 // flags to go with solution type
133 #define SOLV_METH(A,B) (B & A##_FLAG)
134 #define MONODOMAIN_FLAG 1
135 #define ITERATE_FLAG 2
136 #define CN_PARAB_FLAG 4
137 #define PURKINJE_FLAG 8
138 #define O2dT_PARAB_FLAG 16
139 #define PSEUDO_BIDM_FLAG 32
140 
141 // simulation modes
142 #define MONODOMAIN 0
143 #define BIDOMAIN 1
144 #define PSEUDO_BIDM 2
145 
146 // post processting options
147 // keep in jive with post_processing_opts in carp.prm
148 #define RECOVER_PHIE 1
149 #define OPTICAL_MAP 2
150 #define ACTIVATING_FUNCTION 4
151 #define AXIAL_CURRENTS 8
152 #define FILAMENTS 16
153 #define CURRENT_DENSITIES 32
154 
155 // experiment defs, keep in jive with carp.prm
156 #define EXP_NORMAL 0
157 #define EXP_OUTPUT_FEM 1
158 #define EXP_LAPLACE 2
159 #define EXP_SETUP_MESH 3
160 #define EXP_POSTPROCESS 4
161 
168 unsigned int classify_soln_methods();
169 
183 
185 void setup_petsc_err_log();
186 
201 void set_io_dirs(char *sim_ID, char *pp_ID, IO_t init);
202 
203 bool setup_IO(int argc, char **argv);
208 void update_cwd();
215 int set_dir(IO_t dest);
216 
222 void basic_timer_setup();
223 
228 int plot_protocols(const char *);
229 
237 Basic_physic* get_physics(physic_t p, bool error_if_missing = true);
238 
240 short get_mesh_dim(mesh_t id);
241 
248 void register_data(sf_vec* dat, datavec_t d);
249 
250 [[noreturn]] void cleanup_and_exit();
251 
252 // return the direcotry path of a given file
253 char* get_file_dir(const char* file);
262 
264 struct prog_stats {
265  double op;
266  double start;
267  double last;
268  double curr;
269 };
270 
271 struct sync_io_item {
275  bool elem_flag;
277 };
278 
281  int IO_id;
284 };
286 {
287  private:
292  sf_vec* fill_output_buffer(const sync_io_item & it);
293 
294 
295  void register_output_async(sf_vec* inp_data,
296  const mesh_t inp_meshid,
297  const int dpn,
298  const char* name,
299  const char* units,
300  const SF::vector<mesh_int_t>* idx,
301  bool elem_data);
302 
303  public:
307  std::map<SF::mixed_tuple<mesh_t, int>, sf_vec*> buffmap;
308  std::map<SF::mixed_tuple<mesh_t, int>, sf_vec*> buffmap_elem;
309 
310  void register_output_sync(sf_vec* inp_data,
311  const mesh_t inp_meshid,
312  const int dpn,
313  const char* name,
314  const char* units,
315  const SF::vector<mesh_int_t>* idx = NULL,
316  bool elem_data = false);
317 
329  void register_output(sf_vec* inp_data,
330  const mesh_t inp_meshid,
331  const int dpn,
332  const char* name,
333  const char* units,
334  const SF::vector<mesh_int_t>* idx = NULL,
335  bool elem_data = false);
336 
338  void write_data();
339 
348  IGBheader* get_igb_header(const sf_vec* vec);
349 
351  void close_files_and_cleanup();
352 };
353 
364 void output_parameter_file(const char *fname, int argc, char **argv);
365 
367 void savequit();
368 
370 void show_build_info();
371 
372 } // namespace opencarp
373 
374 #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:275
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:52
void output_parameter_file(const char *fname, int argc, char **argv)
Definition: sim_utils.cc:1517
void set_io_dirs(char *sim_ID, char *pp_ID, IO_t init)
Definition: sim_utils.cc:317
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:282
tagreg_t
tag regions types. must be in line with carp.prm
Definition: sim_utils.h:55
double op
output period
Definition: sim_utils.h:265
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:754
void post_process()
do postprocessing
Definition: sim_utils.cc:802
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:307
SF::vector< mesh_int_t > restr_petsc_idx
pointer to index vector with nodal indices we restrict to.
Definition: sim_utils.h:283
double last
last output wallclock time
Definition: sim_utils.h:267
void setup_petsc_err_log()
set up error logs for PETSc, so that it doesnt print errors to stderr.
Definition: sim_utils.cc:1240
void initialize_physics()
Initialize all physics in the registry.
Definition: sim_utils.cc:100
void savequit()
save state and quit simulator
Definition: sim_utils.cc:1602
IGBheader igb
pointer to index vector used for restricting output.
Definition: sim_utils.h:274
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:227
char * get_file_dir(const char *file)
Definition: sim_utils.cc:1231
Timers and timer manager.
for display execution progress and statistical data of electrical solve
Definition: sim_utils.h:264
bool setup_IO(int argc, char **argv)
Definition: sim_utils.cc:388
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:266
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:1216
void parse_mesh_types()
Parse the phys_type CLI parameters and set up (empty) SF::meshdata meshes.
Definition: sim_utils.cc:854
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:268
void destroy_physics()
Destroy all physics in the registry.
Definition: sim_utils.cc:124
const SF::vector< mesh_int_t > * restr_idx
pointer to data registered for output
Definition: sim_utils.h:273
std::map< SF::mixed_tuple< mesh_t, int >, sf_vec * > buffmap_elem
Definition: sim_utils.h:308
void basic_timer_setup()
Here we set up the timers that we always want to have, independent of physics.
Definition: sim_utils.cc:458
int plot_protocols(const char *fname)
plot simulation protocols (I/O timers, stimuli, boundary conditions, etc)
Definition: sim_utils.cc:536
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:986
void show_build_info()
show the build info, exit if -buildinfo was provided. This code runs before MPI_Init().
Definition: sim_utils.cc:219
SF::abstract_vector< SF_int, SF_real > sf_vec
Definition: sf_interface.h:49
void output_meshes()
Definition: sim_utils.cc:1164
void register_data(sf_vec *dat, datavec_t d)
Register a data vector in the global registry.
Definition: sim_utils.cc:844
Basic_physic * get_physics(physic_t p, bool error_if_missing)
Convinience function to get a physics.
Definition: sim_utils.cc:818
Interface to SlimFem.
SF::mixed_tuple< mesh_t, int > spec
flag whether the data is elements-wise
Definition: sim_utils.h:276
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:441
int set_dir(IO_t dest)
Definition: sim_utils.cc:446
short get_mesh_dim(mesh_t id)
get (lowest) dimension of the mesh used in the experiment
Definition: sim_utils.cc:1255
SF::vector< async_io_item > async_IOs
Definition: sim_utils.h:305
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:304
sf_vec * get_data(datavec_t d)
Retrieve a petsc data vector from the data registry.
Definition: sim_utils.cc:834
int IO_id
pointer to data registered for output
Definition: sim_utils.h:281
SF::scatter_registry scatter_reg
Registry for the different scatter objects.
Definition: main.cc:44
The scatterer registry class.
Basic physics types.