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<int> > 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 processting 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 
167 // experiment defs, keep in jive with carp.prm
168 #define EXP_NORMAL 0
169 #define EXP_OUTPUT_FEM 1
170 #define EXP_LAPLACE 2
171 #define EXP_SETUP_MESH 3
172 #define EXP_POSTPROCESS 4
173 
180 unsigned int classify_soln_methods();
181 
194 
196 void setup_petsc_err_log();
197 
212 void set_io_dirs(char *sim_ID, char *pp_ID, IO_t init);
213 
214 bool setup_IO(int argc, char **argv);
219 void update_cwd();
226 int set_dir(IO_t dest);
227 
233 void basic_timer_setup();
234 
235 #ifdef WITH_POWERCAPPING
240 void basic_powercapping_setup();
241 #endif
242 
247 int plot_protocols(const char *);
248 
256 Basic_physic* get_physics(physic_t p, bool error_if_missing = true);
257 
259 short get_mesh_dim(mesh_t id);
260 
267 void register_data(sf_vec* dat, datavec_t d);
268 
269 [[noreturn]] void cleanup_and_exit();
270 
271 // return the direcotry path of a given file
272 char* get_file_dir(const char* file);
281 
283 struct prog_stats {
284  double op;
285  double start;
286  double last;
287  double curr;
288 };
289 
290 struct sync_io_item {
294  bool elem_flag;
296 };
297 
300  int IO_id;
303 };
305 {
306  private:
311  sf_vec* fill_output_buffer(const sync_io_item & it);
312 
313 
314  void register_output_async(sf_vec* inp_data,
315  const mesh_t inp_meshid,
316  const int dpn,
317  const char* name,
318  const char* units,
319  const SF::vector<mesh_int_t>* idx,
320  bool elem_data);
321 
322  public:
326  std::map<SF::mixed_tuple<mesh_t, int>, sf_vec*> buffmap;
327  std::map<SF::mixed_tuple<mesh_t, int>, sf_vec*> buffmap_elem;
328 
329  void register_output_sync(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 
348  void register_output(sf_vec* inp_data,
349  const mesh_t inp_meshid,
350  const int dpn,
351  const char* name,
352  const char* units,
353  const SF::vector<mesh_int_t>* idx = NULL,
354  bool elem_data = false);
355 
357  void write_data();
358 
367  IGBheader* get_igb_header(const sf_vec* vec);
368 
371 };
372 
383 void output_parameter_file(const char *fname, int argc, char **argv);
384 
386 void savequit();
387 
389 void show_build_info();
390 
391 } // namespace opencarp
392 
393 #endif
Index mapping class. This is a bijective mapping.
Definition: SF_container.h:220
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:327
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:1595
void write_data()
write registered data to disk
Definition: sim_utils.cc:1524
SF::vector< async_io_item > async_IOs
Definition: sim_utils.h:324
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:1360
std::map< SF::mixed_tuple< mesh_t, int >, sf_vec * > buffmap
map data spec -> PETSc vector buffer
Definition: sim_utils.h:326
void close_files_and_cleanup()
close file descriptors
Definition: sim_utils.cc:1571
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:1491
SF::vector< sync_io_item > sync_IOs
Definition: sim_utils.h:323
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< int > > 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:917
void output_parameter_file(const char *fname, int argc, char **argv)
Definition: sim_utils.cc:1605
void initialize_physics()
Initialize all physics in the registry.
Definition: sim_utils.cc:114
bool setup_IO(int argc, char **argv)
Definition: sim_utils.cc:436
void parse_params_cpy(int argc, char **argv)
Initialize input parameters on a copy of the real command line parameters.
Definition: sim_utils.cc:50
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:494
void cleanup_and_exit()
Definition: sim_utils.cc:1304
void register_physics()
Register physics to the physics registry.
Definition: sim_utils.cc:93
void post_process()
do postprocessing
Definition: sim_utils.cc:885
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:241
short get_mesh_dim(mesh_t id)
get (lowest) dimension of the mesh used in the experiment
Definition: sim_utils.cc:1343
void show_build_info()
show the build info, exit if -buildinfo was provided. This code runs before MPI_Init().
Definition: sim_utils.cc:233
void savequit()
save state and quit simulator
Definition: sim_utils.cc:1690
void set_io_dirs(char *sim_ID, char *pp_ID, IO_t init)
Definition: sim_utils.cc:365
void register_data(sf_vec *dat, datavec_t d)
Register a data vector in the global registry.
Definition: sim_utils.cc:927
void basic_timer_setup()
Here we set up the timers that we always want to have, independent of physics.
Definition: sim_utils.cc:506
char * get_file_dir(const char *file)
Definition: sim_utils.cc:1319
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:591
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:1072
void output_meshes()
Definition: sim_utils.cc:1252
Basic_physic * get_physics(physic_t p, bool error_if_missing)
Convinience function to get a physics.
Definition: sim_utils.cc:901
unsigned int classify_soln_methods()
void destroy_physics()
Destroy all physics in the registry.
Definition: sim_utils.cc:138
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:489
void setup_petsc_err_log()
set up error logs for PETSc, so that it doesnt print errors to stderr.
Definition: sim_utils.cc:1328
void simulate()
Main simulate loop.
Definition: sim_utils.cc:809
void parse_mesh_types()
Parse the phys_type CLI parameters and set up (empty) SF::meshdata meshes.
Definition: sim_utils.cc:937
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:301
SF::vector< mesh_int_t > restr_petsc_idx
pointer to index vector with nodal indices we restrict to.
Definition: sim_utils.h:302
int IO_id
pointer to data registered for output
Definition: sim_utils.h:300
for display execution progress and statistical data of electrical solve
Definition: sim_utils.h:283
double curr
current output wallclock time
Definition: sim_utils.h:287
double start
output start wallclock time
Definition: sim_utils.h:285
double last
last output wallclock time
Definition: sim_utils.h:286
double op
output period
Definition: sim_utils.h:284
bool elem_flag
igb header we use for output
Definition: sim_utils.h:294
IGBheader igb
pointer to index vector used for restricting output.
Definition: sim_utils.h:293
const SF::vector< mesh_int_t > * restr_idx
pointer to data registered for output
Definition: sim_utils.h:292
SF::mixed_tuple< mesh_t, int > spec
flag whether the data is elements-wise
Definition: sim_utils.h:295
Timers and timer manager.