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 #ifdef WITH_POWERCAPPING
50 #endif
51 
52 namespace opencarp {
53 
56 
59 #define DATAOUT_NONE 0
60 #define DATAOUT_SURF 1
61 #define DATAOUT_VOL 2
62 #define DATAOUT_VTX 3
63 
64 #ifdef WITH_POWERCAPPING
65 class powercapping_manager;
66 #endif
67 
68 namespace user_globals {
72  extern std::map<mesh_t, sf_mesh> mesh_reg;
74  extern std::map<SF::quadruple<int>, SF::index_mapping<mesh_int_t> > map_reg;
76  extern std::map<physic_t, Basic_physic*> physics_reg;
78  extern timer_manager* tm_manager;
80  extern std::map<datavec_t, sf_vec*> datavec_reg;
82  extern FILE* petsc_error_fd;
84  extern bool using_legacy_stimuli;
86  extern MPI_Comm IO_Intercomm;
87 #ifdef WITH_POWERCAPPING
89  extern powercapping_manager *pc_manager;
90 #endif
91 }
92 
99 void parse_params_cpy(int argc, char** argv);
100 
104 void register_physics();
108 void initialize_physics();
112 void destroy_physics();
113 
117 void simulate();
118 
120 void post_process();
121 
128 void parse_mesh_types();
129 
137 void setup_meshes(bool require_fibers);
138 
142 void output_meshes();
143 
144 // flags to go with solution type
145 #define SOLV_METH(A,B) (B & A##_FLAG)
146 #define MONODOMAIN_FLAG 1
147 #define ITERATE_FLAG 2
148 #define CN_PARAB_FLAG 4
149 #define PURKINJE_FLAG 8
150 #define O2dT_PARAB_FLAG 16
151 #define PSEUDO_BIDM_FLAG 32
152 
153 // simulation modes
154 #define MONODOMAIN 0
155 #define BIDOMAIN 1
156 #define PSEUDO_BIDM 2
157 
158 // post processing options
159 // keep in jive with post_processing_opts in carp.prm
160 #define RECOVER_PHIE 1
161 #define OPTICAL_MAP 2
162 #define ACTIVATING_FUNCTION 4
163 #define AXIAL_CURRENTS 8
164 #define FILAMENTS 16
165 #define CURRENT_DENSITIES 32
166 #define LEADFIELD 64
167 
168 // experiment defs, keep in jive with carp.prm
169 #define EXP_NORMAL 0
170 #define EXP_OUTPUT_FEM 1
171 #define EXP_LAPLACE 2
172 #define EXP_SETUP_MESH 3
173 #define EXP_POSTPROCESS 4
174 
181 unsigned int classify_soln_methods();
182 
195 
197 void setup_petsc_err_log();
198 
213 void set_io_dirs(char *sim_ID, char *pp_ID, IO_t init);
214 
215 bool setup_IO(int argc, char **argv);
220 void update_cwd();
227 int set_dir(IO_t dest);
228 
234 void basic_timer_setup();
235 
236 #ifdef WITH_POWERCAPPING
241 void basic_powercapping_setup();
242 #endif
243 
248 int plot_protocols(const char *);
249 
257 Basic_physic* get_physics(physic_t p, bool error_if_missing = true);
258 
260 short get_mesh_dim(mesh_t id);
261 
268 void register_data(sf_vec* dat, datavec_t d);
269 
270 [[noreturn]] void cleanup_and_exit();
271 
272 // return the direcotry path of a given file
273 char* get_file_dir(const char* file);
282 
284 struct prog_stats {
285  double op;
286  double start;
287  double last;
288  double curr;
289 };
290 
291 struct sync_io_item {
295  bool elem_flag;
297 };
298 
301  int IO_id;
304 };
306 {
307  private:
312  sf_vec* fill_output_buffer(const sync_io_item & it);
313 
314 
315  void register_output_async(sf_vec* inp_data,
316  const mesh_t inp_meshid,
317  const int dpn,
318  const char* name,
319  const char* units,
320  const SF::vector<mesh_int_t>* idx,
321  bool elem_data);
322 
323  public:
327  std::map<SF::mixed_tuple<mesh_t, int>, sf_vec*> buffmap;
328  std::map<SF::mixed_tuple<mesh_t, int>, sf_vec*> buffmap_elem;
329 
330  void register_output_sync(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 
349  void register_output(sf_vec* inp_data,
350  const mesh_t inp_meshid,
351  const int dpn,
352  const char* name,
353  const char* units,
354  const SF::vector<mesh_int_t>* idx = NULL,
355  bool elem_data = false);
356 
358  void write_data();
359 
368  IGBheader* get_igb_header(const sf_vec* vec);
369 
372 };
373 
384 void output_parameter_file(const char *fname, int argc, char **argv);
385 
387 void savequit();
388 
390 void show_build_info();
391 
392 } // namespace opencarp
393 
394 #endif
The scatterer registry class.
The abstract physics interface we can use to trigger all physics.
Definition: physics_types.h:59
std::map< SF::mixed_tuple< mesh_t, int >, sf_vec * > buffmap_elem
Definition: sim_utils.h:328
IGBheader * get_igb_header(const sf_vec *vec)
Get the pointer to the igb header for a vector that was registered for output.
Definition: sim_utils.cc:1655
void write_data()
write registered data to disk
Definition: sim_utils.cc:1584
SF::vector< async_io_item > async_IOs
Definition: sim_utils.h:325
void register_output_sync(sf_vec *inp_data, const mesh_t inp_meshid, const int dpn, const char *name, const char *units, const SF::vector< mesh_int_t > *idx=NULL, bool elem_data=false)
Definition: sim_utils.cc:1420
std::map< SF::mixed_tuple< mesh_t, int >, sf_vec * > buffmap
map data spec -> PETSc vector buffer
Definition: sim_utils.h:327
void close_files_and_cleanup()
close file descriptors
Definition: sim_utils.cc:1631
void register_output(sf_vec *inp_data, const mesh_t inp_meshid, const int dpn, const char *name, const char *units, const SF::vector< mesh_int_t > *idx=NULL, bool elem_data=false)
Register a data vector for output.
Definition: sim_utils.cc:1551
SF::vector< sync_io_item > sync_IOs
Definition: sim_utils.h:324
std::map< int, std::string > units
Definition: stimulate.cc:41
MPI_Comm IO_Intercomm
Communicator between IO and compute worlds.
Definition: main.cc:66
FILE * petsc_error_fd
file descriptor for petsc error output
Definition: main.cc:62
timer_manager * tm_manager
a manager for the various physics timers
Definition: main.cc:58
std::map< datavec_t, sf_vec * > datavec_reg
important solution vectors from different physics
Definition: main.cc:60
std::map< SF::quadruple< int >, SF::index_mapping< mesh_int_t > > map_reg
Registriy for the inter domain mappings.
Definition: main.cc:54
bool using_legacy_stimuli
flag storing whether legacy stimuli are used
Definition: main.cc:64
SF::scatter_registry scatter_reg
Registry for the different scatter objects.
Definition: main.cc:50
std::map< mesh_t, sf_mesh > mesh_reg
Registry for the different meshes used in a multi-physics simulation.
Definition: main.cc:52
std::map< physic_t, Basic_physic * > physics_reg
the physics
Definition: main.cc:56
physic_t
Identifier for the different physics we want to set up.
Definition: physics_types.h:51
sf_vec * get_data(datavec_t d)
Retrieve a petsc data vector from the data registry.
Definition: sim_utils.cc:957
void output_parameter_file(const char *fname, int argc, char **argv)
Definition: sim_utils.cc:1665
void initialize_physics()
Initialize all physics in the registry.
Definition: sim_utils.cc:119
bool setup_IO(int argc, char **argv)
Definition: sim_utils.cc:445
void parse_params_cpy(int argc, char **argv)
Initialize input parameters on a copy of the real command line parameters.
Definition: sim_utils.cc:54
datavec_t
Enum used to adress the different data vectors stored in the data registry.
Definition: physics_types.h:99
int set_dir(IO_t dest)
Definition: sim_utils.cc:503
void cleanup_and_exit()
Definition: sim_utils.cc:1364
void register_physics()
Register physics to the physics registry.
Definition: sim_utils.cc:97
void post_process()
do postprocessing
Definition: sim_utils.cc:901
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:250
short get_mesh_dim(mesh_t id)
get (lowest) dimension of the mesh used in the experiment
Definition: sim_utils.cc:1403
void show_build_info()
show the build info, exit if -buildinfo was provided. This code runs before MPI_Init().
Definition: sim_utils.cc:238
void savequit()
save state and quit simulator
Definition: sim_utils.cc:1750
void set_io_dirs(char *sim_ID, char *pp_ID, IO_t init)
Definition: sim_utils.cc:374
void register_data(sf_vec *dat, datavec_t d)
Register a data vector in the global registry.
Definition: sim_utils.cc:967
void basic_timer_setup()
Here we set up the timers that we always want to have, independent of physics.
Definition: sim_utils.cc:515
char * get_file_dir(const char *file)
Definition: sim_utils.cc:1379
IO_t
The different output (directory) types.
Definition: sim_utils.h:55
@ POSTPROC
Definition: sim_utils.h:55
@ CURDIR
Definition: sim_utils.h:55
@ OUTPUT
Definition: sim_utils.h:55
int plot_protocols(const char *fname)
plot simulation protocols (I/O timers, stimuli, boundary conditions, etc)
Definition: sim_utils.cc:600
mesh_t
The enum identifying the different meshes we might want to load.
Definition: sf_interface.h:59
void setup_meshes(bool require_fibers=true)
Read in the reference mesh and use its data to populate all meshes registered in the mesh registry.
Definition: sim_utils.cc:1112
void output_meshes()
Definition: sim_utils.cc:1312
Basic_physic * get_physics(physic_t p, bool error_if_missing)
Convinience function to get a physics.
Definition: sim_utils.cc:941
unsigned int classify_soln_methods()
void destroy_physics()
Destroy all physics in the registry.
Definition: sim_utils.cc:143
tagreg_t
tag regions types. must be in line with carp.prm
Definition: sim_utils.h:58
@ tagreg_cylinder
Definition: sim_utils.h:58
@ tagreg_list
Definition: sim_utils.h:58
@ tagreg_sphere
Definition: sim_utils.h:58
@ tagreg_block
Definition: sim_utils.h:58
void update_cwd()
save the current working directory to curdir so that we can switch back to it if needed.
Definition: sim_utils.cc:498
void setup_petsc_err_log()
set up error logs for PETSc, so that it doesnt print errors to stderr.
Definition: sim_utils.cc:1388
void simulate()
Main simulate loop.
Definition: sim_utils.cc:818
void parse_mesh_types()
Parse the phys_type CLI parameters and set up (empty) SF::meshdata meshes.
Definition: sim_utils.cc:977
Basic physics types.
Interface to SlimFem.
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:302
SF::vector< mesh_int_t > restr_petsc_idx
pointer to index vector with nodal indices we restrict to.
Definition: sim_utils.h:303
int IO_id
pointer to data registered for output
Definition: sim_utils.h:301
for display execution progress and statistical data of electrical solve
Definition: sim_utils.h:284
double curr
current output wallclock time
Definition: sim_utils.h:288
double start
output start wallclock time
Definition: sim_utils.h:286
double last
last output wallclock time
Definition: sim_utils.h:287
double op
output period
Definition: sim_utils.h:285
bool elem_flag
igb header we use for output
Definition: sim_utils.h:295
IGBheader igb
pointer to index vector used for restricting output.
Definition: sim_utils.h:294
const SF::vector< mesh_int_t > * restr_idx
pointer to data registered for output
Definition: sim_utils.h:293
SF::mixed_tuple< mesh_t, int > spec
flag whether the data is elements-wise
Definition: sim_utils.h:296
Timers and timer manager.