openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
MULTI_ION_IF.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 
132 #ifndef MULTI_ION_IF_H
133 #define MULTI_ION_IF_H
134 
135 #include <ctime>
136 #include <cstdarg>
137 #include <cstddef>
138 #include <string>
139 #include <sys/resource.h>
140 
141 #include "ION_IF.h"
142 
143 #include "timer_utils.h"
144 #include "fem_types.h"
145 #include "sf_interface.h"
146 
147 #define FARADAY 96485
148 
149 namespace limpet {
150 
151 typedef double Real;
152 
153 // Format history:
154 // 2 - per-IMP state written as whole data-layout-optimized SoA blocks keyed by
155 // the block leader's canonical id. Partition-dependent: a cross-rank restart
156 // with dlo_vector_size() > 1 regroups nodes into different blocks and reads
157 // mismatched (or never-written) slots -> corrupt state. Still read for
158 // backwards compatibility (correct only when restored on the same layout).
159 // 3 - per-IMP state de-interleaved to one contiguous per-node record keyed by the
160 // node's own canonical id, like the global data. Partition-independent. Each
161 // IMP/plugin header also stores a fingerprint of its state-variable layout
162 // (ordered field names and per-lane sizes); on restore a mismatch marks the
163 // region incompatible instead of misreading a changed model field-for-field.
164 static const unsigned int MIIF_Format = 3;
165 static const char *Magic_MIIF_ID = "Dump_MIIF_ID";
166 
171 struct SV_DUMP {
172  int active;
173  int n;
175  char** fn;
176  int* reg;
177  char** svnames;
178  double intv;
179  double t_dump;
180  int n_dumps;
181  long nwr;
182  int* offset;
183  int* size;
184  int* dtype;
186  void** svtab;
187  size_t* svsize;
188  int* dlo_vs;
189 };
190 
195 struct Trace_Info {
196  bool found;
197  bool ignored;
199  int region;
201 };
202 
206 class MULTI_IF {
207 
208  public:
209  std::string name;
210  bool doppel;
213  std::vector<IonIfBase*> IIF;
215  GlobalData_t* procdata[NUM_IMP_DATA_TYPES];
217  bool* contiguous;
219  bool extUpdateVm;
220  int* numplugs;
222  int N_IIF;
224  std::vector<Target> targets;
226  std::vector<IonTypeList> plugtypes;
227  opencarp::sf_vec* gdata[NUM_IMP_DATA_TYPES];
229 
230  double dt;
231  int numSubDt;
232 
233 #ifdef HDF5
234  hid_t file;
235 #endif // ifdef HDF5
236 
237  MULTI_IF() : doppel(false), N_Nodes(NULL), NodeLists(NULL), IIF({}), extUpdateVm(false), logger(NULL)
238  {
239  zero_data();
240  }
241 
243 
244  void zero_data() {
245  name = "";
246  N_Nodes = NULL;
247  NodeLists = NULL;
248  IIF = {};
249  STRUCT_ZERO(svd);
251  ldata = NULL;
252  contiguous = NULL;
253  trace_info = NULL;
254  numplugs = NULL;
255  iontypes = {};
256  IIFmask = NULL;
257  plugtypes = {};
259  }
260 
261  void initialize_MIIF();
262  void initialize_currents(double, int);
263 
264  void compute_ionic_current(bool flag_send = 1, bool flag_receive = 1); // pMIIF
265 
266  void free_MIIF(); // pMIIF
267 
268  void sv_dump_add(int, const IonType&, int, int, int, const char *, const char *);
269  int sv_dump_add_by_name(int, char *, char *, char *, char *);
270  void sv_dump_add_by_name_list(int, char *, char *, char *, char *,
271  char *, double, double);
272  size_t dump_svs(opencarp::base_timer *);
273  void close_svs_dumps();
274  void dump_luts_MIIF(bool);
275 
276  void dump_state(char *, float, opencarp::mesh_t gid, bool, unsigned int);
277  void transmem_stim_species(float, const char *, float, int *, int);
278  float restore_state(const char *, opencarp::mesh_t gid, bool);
279  void releaseRealData();
281  void getRealData();
282  int adjust_MIIF_variables(const char* variable,
283  const SF::vector<SF_int> & indices,
284  const SF::vector<SF_real> & values);
285  void MIIF_change_dt(double);
286  bool use_stretch();
287 
289 };
290 
292 
293 void doppel_MIIF(MULTI_IF *, MULTI_IF *);
294 void doppel_update(MULTI_IF *, MULTI_IF *);
295 void free_doppel(MULTI_IF *);
297 
299 int get_plug_flag(char *, int *, IonTypeList&);
300 int IMPdataLabel2Index(const char *);
301 char* tokstr_r(char *s1, const char *s2, char **lasts);
302 
303 void open_trace(MULTI_IF *MIIF, int n_traceNodes, int *traceNodes, int *label, opencarp::sf_mesh* imesh);
304 void dump_trace(MULTI_IF *MIIF, limpet::Real time);
305 
306 
312 #define USED_DAT(I, F) ( (I)->get_reqdat()&F || (I)->get_moddat()&F)
313 
325 #define get_plug_params(M, R, P) (P ## _Params *)get_IIF_plugparam(*(M.IIF[R]), P ## _ID)
326 
327 
337 #define SVD_add(I, R, T, M) {T ## _state *sv; int dtype = 0; sv_dump_add(&I, R, \
338  T ## _ID, offsetof(T ## _state, \
339  M), \
340  sizeof(sv->M), dtype, # R "." # T "." # M, # T, \
341  # R); }
342 } // namespace limpet
343 
344 #endif // MULTI_ION_IF_H
#define STRUCT_ZERO(S)
Definition: basics.h:53
Represents the ionic model and plug-in (IMP) data structure.
Definition: ION_IF.h:142
Abstract class representing an ionic model type.
Definition: ion_type.h:59
bool extUpdateVm
flag indicating update function for Vm
Definition: MULTI_ION_IF.h:219
std::vector< IonIfBase * > IIF
array of IIF's
Definition: MULTI_ION_IF.h:213
opencarp::sf_vec * gdata[NUM_IMP_DATA_TYPES]
data used by all IMPs
Definition: MULTI_ION_IF.h:227
void sv_dump_add_by_name_list(int, char *, char *, char *, char *, char *, double, double)
node_count_t numNode
local number of nodes
Definition: MULTI_ION_IF.h:221
size_t dump_svs(opencarp::base_timer *)
int * numplugs
number of plugins for each region
Definition: MULTI_ION_IF.h:220
std::vector< Target > targets
target for each region
Definition: MULTI_ION_IF.h:224
std::vector< IonTypeList > plugtypes
plugins types for each region
Definition: MULTI_ION_IF.h:226
IonTypeList iontypes
type for each region
Definition: MULTI_ION_IF.h:223
void dump_state(char *, float, opencarp::mesh_t gid, bool, unsigned int)
SV_DUMP svd
state variable dump
Definition: MULTI_ION_IF.h:214
void sv_dump_add(int, const IonType &, int, int, int, const char *, const char *)
void * thread_initialize_MIIF()
void transmem_stim_species(float, const char *, float, int *, int)
void initialize_currents(double, int)
GlobalData_t *** ldata
data local to each IMP
Definition: MULTI_ION_IF.h:216
float restore_state(const char *, opencarp::mesh_t gid, bool)
int N_IIF
how many different IIF's
Definition: MULTI_ION_IF.h:222
void compute_ionic_current(bool flag_send=1, bool flag_receive=1)
GPU kernel to emulate the add_scaled call made to adjust the Vm values when the update to Vm is not m...
int sv_dump_add_by_name(int, char *, char *, char *, char *)
Trace_Info * trace_info
Information about traces.
Definition: MULTI_ION_IF.h:218
double dt
time step (ms)
Definition: MULTI_ION_IF.h:230
node_count_t * N_Nodes
#nodes for each IMP
Definition: MULTI_ION_IF.h:211
GlobalData_t * procdata[NUM_IMP_DATA_TYPES]
data for this processor
Definition: MULTI_ION_IF.h:215
void dump_luts_MIIF(bool)
bool * contiguous
whether a region is contiguously numbered
Definition: MULTI_ION_IF.h:217
opencarp::FILE_SPEC logger
Definition: MULTI_ION_IF.h:228
int numSubDt
number of sub-dt time steps
Definition: MULTI_ION_IF.h:231
node_index_t ** NodeLists
local partitioned node lists for each IMP stored
Definition: MULTI_ION_IF.h:212
void MIIF_change_dt(double)
IIF_Mask_t * IIFmask
region for each node
Definition: MULTI_ION_IF.h:225
int adjust_MIIF_variables(const char *variable, const SF::vector< SF_int > &indices, const SF::vector< SF_real > &values)
std::string name
name for MIIF region
Definition: MULTI_ION_IF.h:209
bool doppel
is this a shallow clone?
Definition: MULTI_ION_IF.h:210
void releaseRealDataDuringInit()
Structs and types for (electric) FEM.
void doppel_MIIF(MULTI_IF *orig, MULTI_IF *miif_doppel)
int get_plug_flag(char *plgstr, int *out_num_plugins, IonTypeList &out_plugins)
void doppel_update(MULTI_IF *orig, MULTI_IF *miif_doppel)
double Real
Definition: MULTI_ION_IF.h:151
int IMPdataLabel2Index(const char *sv)
SF_real GlobalData_t
Definition: limpet_types.h:27
FILE_SPEC _nc_logf
Definition: ION_IF.cc:85
std::vector< std::reference_wrapper< IonType > > IonTypeList
Definition: ion_type.h:291
void dup_IMP_node_state(IonIfBase &IF, node_index_t from, node_index_t to, GlobalData_t **localdata)
opencarp::local_index_t node_count_t
Definition: limpet_types.h:29
void dump_trace(MULTI_IF *MIIF, limpet::Real time)
void open_trace(MULTI_IF *MIIF, int n_traceNodes, int *traceNodes, int *label, opencarp::sf_mesh *imesh)
Set up ionic model traces at some global node numbers.
char IIF_Mask_t
Definition: ion_type.h:50
void free_doppel(MULTI_IF *m)
opencarp::local_index_t node_index_t
Definition: limpet_types.h:28
char * tokstr_r(char *s1, const char *s2, char **lasts)
Definition: ION_IF.cc:89
void * get_IIF_plugparam(IonIfBase &, int)
mesh_t
The enum identifying the different meshes we might want to load.
Definition: sf_interface.h:59
Interface to SlimFem.
data structure to manage state variable file dumps
Definition: MULTI_ION_IF.h:171
size_t * svsize
state variable sizes
Definition: MULTI_ION_IF.h:187
char ** fn
array to store file names
Definition: MULTI_ION_IF.h:175
double intv
time interval for sv dumps
Definition: MULTI_ION_IF.h:178
int * dtype
data type
Definition: MULTI_ION_IF.h:184
int n_dumps
keep track of number of dumped time slices
Definition: MULTI_ION_IF.h:180
node_count_t * num
number of nodes
Definition: MULTI_ION_IF.h:185
long nwr
keep track of number of written tokens
Definition: MULTI_ION_IF.h:181
int * size
sizes of SV to dump
Definition: MULTI_ION_IF.h:183
void ** svtab
state variable tables
Definition: MULTI_ION_IF.h:186
int * reg
array to store region ids
Definition: MULTI_ION_IF.h:176
double t_dump
next instant for sv dump
Definition: MULTI_ION_IF.h:179
char ** svnames
array to store sv names
Definition: MULTI_ION_IF.h:177
int n
#state variables we want to dump
Definition: MULTI_ION_IF.h:173
int * offset
offsets into structure for SV
Definition: MULTI_ION_IF.h:182
opencarp::FILE_SPEC * hdls
array of file handles to sv output files
Definition: MULTI_ION_IF.h:174
data structure to manage trace dumps. Should eventually be combined with the state variable dumps,...
Definition: MULTI_ION_IF.h:195
bool ignored
globally not found
Definition: MULTI_ION_IF.h:197
node_index_t node_idx
local node number
Definition: MULTI_ION_IF.h:198
bool found
found on this node
Definition: MULTI_ION_IF.h:196
opencarp::FILE_SPEC file
Definition: MULTI_ION_IF.h:200
File descriptor struct.
Definition: basics.h:135
Timers and timer manager.