openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
MULTI_ION_IF.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
2 // SPDX-License-Identifier: LicenseRef-APL-1.1
3 
117 #ifndef MULTI_ION_IF_H
118 #define MULTI_ION_IF_H
119 
120 #include <ctime>
121 #include <cstdarg>
122 #include <cstddef>
123 #include <string>
124 #include <sys/resource.h>
125 
126 #include "ION_IF.h"
127 
128 #include "timer_utils.h"
129 #include "fem_types.h"
130 #include "sf_interface.h"
131 
132 #define FARADAY 96485
133 
134 namespace limpet {
135 
136 typedef double Real;
137 
138 // Format history:
139 // 2 - per-IMP state written as whole data-layout-optimized SoA blocks keyed by
140 // the block leader's canonical id. Partition-dependent: a cross-rank restart
141 // with dlo_vector_size() > 1 regroups nodes into different blocks and reads
142 // mismatched (or never-written) slots -> corrupt state. Still read for
143 // backwards compatibility (correct only when restored on the same layout).
144 // 3 - per-IMP state de-interleaved to one contiguous per-node record keyed by the
145 // node's own canonical id, like the global data. Partition-independent. Each
146 // IMP/plugin header also stores a fingerprint of its state-variable layout
147 // (ordered field names and per-lane sizes); on restore a mismatch marks the
148 // region incompatible instead of misreading a changed model field-for-field.
149 static const unsigned int MIIF_Format = 3;
150 static const char *Magic_MIIF_ID = "Dump_MIIF_ID";
151 
156 struct SV_DUMP {
157  int active;
158  int n;
160  char** fn;
161  int* reg;
162  char** svnames;
163  double intv;
164  double t_dump;
165  int n_dumps;
166  long nwr;
167  int* offset;
168  int* size;
169  int* dtype;
171  void** svtab;
172  size_t* svsize;
173  int* dlo_vs;
174 };
175 
180 struct Trace_Info {
181  bool found;
182  bool ignored;
184  int region;
186 };
187 
191 class MULTI_IF {
192 
193  public:
194  std::string name;
195  bool doppel;
198  std::vector<IonIfBase*> IIF;
200  GlobalData_t* procdata[NUM_IMP_DATA_TYPES];
202  bool* contiguous;
204  bool extUpdateVm;
205  int* numplugs;
207  int N_IIF;
209  std::vector<Target> targets;
211  std::vector<IonTypeList> plugtypes;
212  opencarp::sf_vec* gdata[NUM_IMP_DATA_TYPES];
214 
215  double dt;
216  int numSubDt;
217 
218 #ifdef HDF5
219  hid_t file;
220 #endif // ifdef HDF5
221 
222  MULTI_IF() : doppel(false), N_Nodes(NULL), NodeLists(NULL), IIF({}), extUpdateVm(false), logger(NULL)
223  {
224  zero_data();
225  }
226 
228 
229  void zero_data() {
230  name = "";
231  N_Nodes = NULL;
232  NodeLists = NULL;
233  IIF = {};
234  STRUCT_ZERO(svd);
236  ldata = NULL;
237  contiguous = NULL;
238  trace_info = NULL;
239  numplugs = NULL;
240  iontypes = {};
241  IIFmask = NULL;
242  plugtypes = {};
244  }
245 
246  void initialize_MIIF();
247  void initialize_currents(double, int);
248 
249  void compute_ionic_current(bool flag_send = 1, bool flag_receive = 1); // pMIIF
250 
251  void free_MIIF(); // pMIIF
252 
253  void sv_dump_add(int, const IonType&, int, int, int, const char *, const char *);
254  int sv_dump_add_by_name(int, char *, char *, char *, char *);
255  void sv_dump_add_by_name_list(int, char *, char *, char *, char *,
256  char *, double, double);
257  size_t dump_svs(opencarp::base_timer *);
258  void close_svs_dumps();
259  void dump_luts_MIIF(bool);
260 
261  void dump_state(char *, float, opencarp::mesh_t gid, bool, unsigned int);
262  void transmem_stim_species(float, const char *, float, int *, int);
263  float restore_state(const char *, opencarp::mesh_t gid, bool);
264  void releaseRealData();
266  void getRealData();
267  int adjust_MIIF_variables(const char* variable,
268  const SF::vector<SF_int> & indices,
269  const SF::vector<SF_real> & values);
270  void MIIF_change_dt(double);
271  bool use_stretch();
272 
274 };
275 
277 
278 void doppel_MIIF(MULTI_IF *, MULTI_IF *);
279 void doppel_update(MULTI_IF *, MULTI_IF *);
280 void free_doppel(MULTI_IF *);
282 
284 int get_plug_flag(char *, int *, IonTypeList&);
285 int IMPdataLabel2Index(const char *);
286 char* tokstr_r(char *s1, const char *s2, char **lasts);
287 
288 void open_trace(MULTI_IF *MIIF, int n_traceNodes, int *traceNodes, int *label, opencarp::sf_mesh* imesh);
289 void dump_trace(MULTI_IF *MIIF, limpet::Real time);
290 
291 
297 #define USED_DAT(I, F) ( (I)->get_reqdat()&F || (I)->get_moddat()&F)
298 
310 #define get_plug_params(M, R, P) (P ## _Params *)get_IIF_plugparam(*(M.IIF[R]), P ## _ID)
311 
312 
322 #define SVD_add(I, R, T, M) {T ## _state *sv; int dtype = 0; sv_dump_add(&I, R, \
323  T ## _ID, offsetof(T ## _state, \
324  M), \
325  sizeof(sv->M), dtype, # R "." # T "." # M, # T, \
326  # R); }
327 } // namespace limpet
328 
329 #endif // MULTI_ION_IF_H
#define STRUCT_ZERO(S)
Definition: basics.h:38
Represents the ionic model and plug-in (IMP) data structure.
Definition: ION_IF.h:168
Abstract class representing an ionic model type.
Definition: ion_type.h:44
bool extUpdateVm
flag indicating update function for Vm
Definition: MULTI_ION_IF.h:204
std::vector< IonIfBase * > IIF
array of IIF's
Definition: MULTI_ION_IF.h:198
opencarp::sf_vec * gdata[NUM_IMP_DATA_TYPES]
data used by all IMPs
Definition: MULTI_ION_IF.h:212
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:206
size_t dump_svs(opencarp::base_timer *)
int * numplugs
number of plugins for each region
Definition: MULTI_ION_IF.h:205
std::vector< Target > targets
target for each region
Definition: MULTI_ION_IF.h:209
std::vector< IonTypeList > plugtypes
plugins types for each region
Definition: MULTI_ION_IF.h:211
IonTypeList iontypes
type for each region
Definition: MULTI_ION_IF.h:208
void dump_state(char *, float, opencarp::mesh_t gid, bool, unsigned int)
SV_DUMP svd
state variable dump
Definition: MULTI_ION_IF.h:199
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:201
float restore_state(const char *, opencarp::mesh_t gid, bool)
int N_IIF
how many different IIF's
Definition: MULTI_ION_IF.h:207
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:203
double dt
time step (ms)
Definition: MULTI_ION_IF.h:215
node_count_t * N_Nodes
#nodes for each IMP
Definition: MULTI_ION_IF.h:196
GlobalData_t * procdata[NUM_IMP_DATA_TYPES]
data for this processor
Definition: MULTI_ION_IF.h:200
void dump_luts_MIIF(bool)
bool * contiguous
whether a region is contiguously numbered
Definition: MULTI_ION_IF.h:202
opencarp::FILE_SPEC logger
Definition: MULTI_ION_IF.h:213
int numSubDt
number of sub-dt time steps
Definition: MULTI_ION_IF.h:216
node_index_t ** NodeLists
local partitioned node lists for each IMP stored
Definition: MULTI_ION_IF.h:197
void MIIF_change_dt(double)
IIF_Mask_t * IIFmask
region for each node
Definition: MULTI_ION_IF.h:210
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:194
bool doppel
is this a shallow clone?
Definition: MULTI_ION_IF.h:195
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:136
int IMPdataLabel2Index(const char *sv)
SF_real GlobalData_t
Definition: limpet_types.h:12
FILE_SPEC _nc_logf
Definition: ION_IF.cc:70
std::vector< std::reference_wrapper< IonType > > IonTypeList
Definition: ion_type.h:276
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:14
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:35
void free_doppel(MULTI_IF *m)
opencarp::local_index_t node_index_t
Definition: limpet_types.h:13
char * tokstr_r(char *s1, const char *s2, char **lasts)
Definition: ION_IF.cc:74
void * get_IIF_plugparam(IonIfBase &, int)
mesh_t
The enum identifying the different meshes we might want to load.
Definition: sf_interface.h:44
Interface to SlimFem.
data structure to manage state variable file dumps
Definition: MULTI_ION_IF.h:156
size_t * svsize
state variable sizes
Definition: MULTI_ION_IF.h:172
char ** fn
array to store file names
Definition: MULTI_ION_IF.h:160
double intv
time interval for sv dumps
Definition: MULTI_ION_IF.h:163
int * dtype
data type
Definition: MULTI_ION_IF.h:169
int n_dumps
keep track of number of dumped time slices
Definition: MULTI_ION_IF.h:165
node_count_t * num
number of nodes
Definition: MULTI_ION_IF.h:170
long nwr
keep track of number of written tokens
Definition: MULTI_ION_IF.h:166
int * size
sizes of SV to dump
Definition: MULTI_ION_IF.h:168
void ** svtab
state variable tables
Definition: MULTI_ION_IF.h:171
int * reg
array to store region ids
Definition: MULTI_ION_IF.h:161
double t_dump
next instant for sv dump
Definition: MULTI_ION_IF.h:164
char ** svnames
array to store sv names
Definition: MULTI_ION_IF.h:162
int n
#state variables we want to dump
Definition: MULTI_ION_IF.h:158
int * offset
offsets into structure for SV
Definition: MULTI_ION_IF.h:167
opencarp::FILE_SPEC * hdls
array of file handles to sv output files
Definition: MULTI_ION_IF.h:159
data structure to manage trace dumps. Should eventually be combined with the state variable dumps,...
Definition: MULTI_ION_IF.h:180
bool ignored
globally not found
Definition: MULTI_ION_IF.h:182
node_index_t node_idx
local node number
Definition: MULTI_ION_IF.h:183
bool found
found on this node
Definition: MULTI_ION_IF.h:181
opencarp::FILE_SPEC file
Definition: MULTI_ION_IF.h:185
File descriptor struct.
Definition: basics.h:120
Timers and timer manager.