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 "sf_interface.h"
35 #include "timer_utils.h"
36 #include "physics_types.h"
37 #include "build_info.h"
38 
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 #ifdef WITH_POWERCAPPING
49 #endif
50 
51 namespace opencarp {
52 
55 
58 #define DATAOUT_NONE 0
59 #define DATAOUT_SURF 1
60 #define DATAOUT_VOL 2
61 #define DATAOUT_VTX 3
62 
63 #ifdef WITH_POWERCAPPING
64 class powercapping_manager;
65 #endif
66 
67 namespace user_globals {
71  extern std::map<mesh_t, sf_mesh> mesh_reg;
73  extern std::map<SF::quadruple<int>, SF::index_mapping<mesh_int_t> > map_reg;
75  extern std::map<physic_t, Basic_physic*> physics_reg;
77  extern timer_manager* tm_manager;
79  extern std::map<datavec_t, sf_vec*> datavec_reg;
81  extern FILE* petsc_error_fd;
83  extern bool using_legacy_stimuli;
85  extern MPI_Comm IO_Intercomm;
86 #ifdef WITH_POWERCAPPING
88  extern powercapping_manager *pc_manager;
89 #endif
90 }
91 
98 void parse_params_cpy(int argc, char** argv);
99 
103 void register_physics();
107 void initialize_physics();
111 void destroy_physics();
112 
116 void simulate();
117 
119 void post_process();
120 
127 void parse_mesh_types();
128 
136 void setup_meshes(bool require_fibers);
137 
141 void output_meshes();
142 
150 
159  const hashmap::unordered_set<int>& output_tags,
160  SF::vector<mesh_int_t>& output_idx,
161  bool async);
162 
170  const SF::vector<mesh_t>& mesh_ids);
171 
176  const SF::vector<mesh_int_t>& selected_idx);
177 
178 // flags to go with solution type
179 #define SOLV_METH(A,B) (B & A##_FLAG)
180 #define MONODOMAIN_FLAG 1
181 #define ITERATE_FLAG 2
182 #define CN_PARAB_FLAG 4
183 #define PURKINJE_FLAG 8
184 #define O2dT_PARAB_FLAG 16
185 #define PSEUDO_BIDM_FLAG 32
186 
187 // simulation modes
188 #define MONODOMAIN 0
189 #define BIDOMAIN 1
190 #define PSEUDO_BIDM 2
191 
192 // post processing options
193 // keep in jive with post_processing_opts in carp.prm
194 #define RECOVER_PHIE 1
195 #define OPTICAL_MAP 2
196 #define ACTIVATING_FUNCTION 4
197 #define AXIAL_CURRENTS 8
198 #define FILAMENTS 16
199 #define CURRENT_DENSITIES 32
200 #define LEADFIELD 64
201 
202 // experiment defs, keep in jive with carp.prm
203 #define EXP_NORMAL 0
204 #define EXP_OUTPUT_FEM 1
205 #define EXP_LAPLACE 2
206 #define EXP_SETUP_MESH 3
207 #define EXP_POSTPROCESS 4
208 
215 unsigned int classify_soln_methods();
216 
229 
231 void setup_petsc_err_log();
232 
247 void set_io_dirs(char *sim_ID, char *pp_ID, IO_t init);
248 
249 bool setup_IO(int argc, char **argv);
254 void update_cwd();
261 int set_dir(IO_t dest);
262 
268 void basic_timer_setup();
269 
270 #ifdef WITH_POWERCAPPING
275 void basic_powercapping_setup();
276 #endif
277 
282 int plot_protocols(const char *);
283 
291 Basic_physic* get_physics(physic_t p, bool error_if_missing = true);
292 
294 short get_mesh_dim(mesh_t id);
295 
302 void register_data(sf_vec* dat, datavec_t d);
303 
304 [[noreturn]] void cleanup_and_exit();
305 
306 // return the direcotry path of a given file
307 char* get_file_dir(const char* file);
316 
318 struct prog_stats {
319  double op;
320  double start;
321  double last;
322  double curr;
323 };
324 
325 struct sync_io_item {
330  bool elem_flag;
332 };
333 
336  int IO_id;
339 };
341 {
342  private:
347  sf_vec* fill_output_buffer(const sync_io_item & it);
348 
349 
350  void register_output_async(sf_vec* inp_data,
351  const mesh_t inp_meshid,
352  const int dpn,
353  const char* name,
354  const char* units,
355  const SF::vector<mesh_int_t>* idx,
356  bool elem_data);
357 
358  public:
362  std::map<SF::mixed_tuple<mesh_t, int>, sf_vec*> buffmap;
363  std::map<SF::mixed_tuple<mesh_t, int>, sf_vec*> buffmap_elem;
364 
365  void register_output_sync(sf_vec* inp_data,
366  const mesh_t inp_meshid,
367  const int dpn,
368  const char* name,
369  const char* units,
370  const SF::vector<mesh_int_t>* idx = NULL,
371  bool elem_data = false);
372 
384  void register_output(sf_vec* inp_data,
385  const mesh_t inp_meshid,
386  const int dpn,
387  const char* name,
388  const char* units,
389  const SF::vector<mesh_int_t>* idx = NULL,
390  bool elem_data = false);
391 
393  void write_data();
394 
403  IGBheader* get_igb_header(const sf_vec* vec);
404 
407 };
408 
419 void output_parameter_file(const char *fname, int argc, char **argv);
420 
422 void savequit();
423 
425 void show_build_info();
426 
427 } // namespace opencarp
428 
429 #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:363
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:2930
void write_data()
write registered data to disk
Definition: sim_utils.cc:2850
SF::vector< async_io_item > async_IOs
Definition: sim_utils.h:360
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:2676
std::map< SF::mixed_tuple< mesh_t, int >, sf_vec * > buffmap
map data spec -> PETSc vector buffer
Definition: sim_utils.h:362
void close_files_and_cleanup()
close file descriptors
Definition: sim_utils.cc:2906
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:2817
SF::vector< sync_io_item > sync_IOs
Definition: sim_utils.h:359
std::map< int, std::string > units
Definition: stimulate.cc:41
MPI_Comm IO_Intercomm
Communicator between IO and compute worlds.
Definition: main.cc:63
FILE * petsc_error_fd
file descriptor for petsc error output
Definition: main.cc:59
timer_manager * tm_manager
a manager for the various physics timers
Definition: main.cc:55
std::map< datavec_t, sf_vec * > datavec_reg
important solution vectors from different physics
Definition: main.cc:57
std::map< SF::quadruple< int >, SF::index_mapping< mesh_int_t > > map_reg
Registriy for the inter domain mappings.
Definition: main.cc:51
bool using_legacy_stimuli
flag storing whether legacy stimuli are used
Definition: main.cc:61
SF::scatter_registry scatter_reg
Registry for the different scatter objects.
Definition: main.cc:47
std::map< mesh_t, sf_mesh > mesh_reg
Registry for the different meshes used in a multi-physics simulation.
Definition: main.cc:49
std::map< physic_t, Basic_physic * > physics_reg
the physics
Definition: main.cc:53
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:2047
void output_parameter_file(const char *fname, int argc, char **argv)
Definition: sim_utils.cc:2940
void initialize_physics()
Initialize all physics in the registry.
Definition: sim_utils.cc:1203
bool setup_IO(int argc, char **argv)
Definition: sim_utils.cc:1525
void parse_params_cpy(int argc, char **argv)
Initialize input parameters on a copy of the real command line parameters.
Definition: sim_utils.cc:1134
SF::meshdata< mesh_int_t, mesh_real_t > sf_mesh
Definition: sf_interface.h:48
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:1583
void cleanup_and_exit()
Definition: sim_utils.cc:2617
void register_physics()
Register physics to the physics registry.
Definition: sim_utils.cc:1181
void post_process()
do postprocessing
Definition: sim_utils.cc:1992
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:1330
short get_mesh_dim(mesh_t id)
get (lowest) dimension of the mesh used in the experiment
Definition: sim_utils.cc:2659
void show_build_info()
show the build info, exit if -buildinfo was provided. This code runs before MPI_Init().
Definition: sim_utils.cc:1318
void savequit()
save state and quit simulator
Definition: sim_utils.cc:3025
void set_io_dirs(char *sim_ID, char *pp_ID, IO_t init)
Definition: sim_utils.cc:1454
void register_data(sf_vec *dat, datavec_t d)
Register a data vector in the global registry.
Definition: sim_utils.cc:2057
void basic_timer_setup()
Here we set up the timers that we always want to have, independent of physics.
Definition: sim_utils.cc:1595
char * get_file_dir(const char *file)
Definition: sim_utils.cc:2635
IO_t
The different output (directory) types.
Definition: sim_utils.h:54
@ POSTPROC
Definition: sim_utils.h:54
@ CURDIR
Definition: sim_utils.h:54
@ OUTPUT
Definition: sim_utils.h:54
void intersect_output_restriction(SF::vector< mesh_int_t > *&restr, const SF::vector< mesh_int_t > &selected_idx)
Intersect an existing output restriction with another local index set.
Definition: sim_utils.cc:2490
int plot_protocols(const char *fname)
plot simulation protocols (I/O timers, stimuli, boundary conditions, etc)
Definition: sim_utils.cc:1687
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:2202
bool parse_gridout_tags(hashmap::unordered_set< int > &output_tags)
Parse the gridout_tags idset into a unique set of region IDs.
Definition: sim_utils.cc:2401
void validate_gridout_tags(hashmap::unordered_set< int > &output_tags, const SF::vector< mesh_t > &mesh_ids)
Warn about selected gridout tags that are absent from the relevant meshes.
Definition: sim_utils.cc:2440
void output_meshes()
Definition: sim_utils.cc:2520
Basic_physic * get_physics(physic_t p, bool error_if_missing)
Convinience function to get a physics.
Definition: sim_utils.cc:2031
unsigned int classify_soln_methods()
void destroy_physics()
Destroy all physics in the registry.
Definition: sim_utils.cc:1227
tagreg_t
tag regions types. must be in line with carp.prm
Definition: sim_utils.h:57
@ tagreg_cylinder
Definition: sim_utils.h:57
@ tagreg_list
Definition: sim_utils.h:57
@ tagreg_sphere
Definition: sim_utils.h:57
@ tagreg_block
Definition: sim_utils.h:57
void build_tagged_nodal_output_restriction(sf_mesh &mesh, const hashmap::unordered_set< int > &output_tags, SF::vector< mesh_int_t > &output_idx, bool async)
Build a local output-vector restriction from mesh element tags.
Definition: sim_utils.cc:2423
void update_cwd()
save the current working directory to curdir so that we can switch back to it if needed.
Definition: sim_utils.cc:1578
void setup_petsc_err_log()
set up error logs for PETSc, so that it doesnt print errors to stderr.
Definition: sim_utils.cc:2644
void simulate()
Main simulate loop.
Definition: sim_utils.cc:1905
void parse_mesh_types()
Parse the phys_type CLI parameters and set up (empty) SF::meshdata meshes.
Definition: sim_utils.cc:2067
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:337
SF::vector< mesh_int_t > restr_petsc_idx
pointer to index vector with nodal indices we restrict to.
Definition: sim_utils.h:338
int IO_id
pointer to data registered for output
Definition: sim_utils.h:336
for display execution progress and statistical data of electrical solve
Definition: sim_utils.h:318
double curr
current output wallclock time
Definition: sim_utils.h:322
double start
output start wallclock time
Definition: sim_utils.h:320
double last
last output wallclock time
Definition: sim_utils.h:321
double op
output period
Definition: sim_utils.h:319
SF::vector< mesh_int_t > restr_global_idx
pointer to index vector used for restricting output.
Definition: sim_utils.h:328
bool elem_flag
igb header we use for output
Definition: sim_utils.h:330
IGBheader igb
global canonical indices matching restr_idx.
Definition: sim_utils.h:329
const SF::vector< mesh_int_t > * restr_idx
pointer to data registered for output
Definition: sim_utils.h:327
SF::mixed_tuple< mesh_t, int > spec
flag whether the data is elements-wise
Definition: sim_utils.h:331
Timers and timer manager.