openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
electrics.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
2 // SPDX-License-Identifier: LicenseRef-APL-1.1
3 
12 #ifndef _ELECTRICS_H
13 #define _ELECTRICS_H
14 
15 #include "physics_types.h"
16 #include "sim_utils.h"
17 #include "stimulate.h"
18 
19 #include "sf_interface.h"
20 #include "timers.h"
21 #include "ionics.h"
22 
23 #define DUMP_NONE 0
24 #define DUMP_IC 1
25 #define DUMP_IVOL 2
26 #define DUMP_IACT 4
27 
28 namespace opencarp {
29 
31 {
32  public:
33  sf_vec* phie = nullptr;
34  sf_vec* phie_i = nullptr;
35  sf_vec* phiesrc = nullptr;
36  sf_vec* currtmp = nullptr;
37 
38  sf_mat* mass_e = nullptr;
39  sf_mat* phie_mat = nullptr;
40 
41  // the linear solver
42  sf_sol* lin_solver = nullptr;
43 
44  // linear solver stats
46 
48  dbc_manager* dbc = nullptr;
49  bool phie_mat_has_nullspace = false;
50 
51  // solver config
52  double tol = 1e-8;
53  int max_it = 100;
54 
55  void init();
56  void rebuild_matrices(MaterialType* mtype, SF::vector<stimulus> & stimuli, FILE_SPEC logger);
57  void rebuild_stiffness(MaterialType* mtype, SF::vector<stimulus> & stimuli, FILE_SPEC logger);
58  void rebuild_mass(FILE_SPEC logger);
59  void solve(sf_mat & Ki, sf_vec & Vmv, sf_vec & tmp_i);
60  void solve_laplace();
61 
63  {
64  if(dbc) delete dbc;
65  if(lin_solver) delete lin_solver;
66  // matrices
67  if(phie_mat) delete phie_mat;
68  if(mass_e) delete mass_e;
69  // vectors
70  if(phie) delete phie;
71  if(phie_i) delete phie_i;
72  if(phiesrc) delete phiesrc;
73  if(currtmp) delete currtmp;
74  }
75 
76  private:
77  void enforce_dbc();
78  void setup_linear_solver(FILE_SPEC logger);
79 };
80 
82 {
83  public:
84 
85  // Has to be kept in line with param_globals::parab_solve
86  enum parabolic_t { EXPLICIT = 0, CN = 1, O2dT = 2};
87 
88  sf_vec* IIon = nullptr;
89  sf_vec* Vmv = nullptr;
90 
91  sf_vec* Ic = nullptr;
92  sf_vec* Ivol = nullptr;
93  sf_vec* Iact = nullptr;
94 
95  sf_vec* old_vm = nullptr;
96  sf_vec* kappa_i = nullptr;
97  sf_vec* tmp_i1 = nullptr;
98  sf_vec* tmp_i2 = nullptr;
99  sf_vec* Irhs = nullptr;
100  sf_vec* inv_mass_diag = nullptr;
101 
102  sf_mat* u_mass_i = nullptr;
103  sf_mat* mass_i = nullptr;
104  sf_mat* rhs_parab = nullptr;
105  sf_mat* lhs_parab = nullptr;
106  sf_mat* phie_recov_mat = nullptr;
107 
108  sf_vec* Diff_term = nullptr;
109 
110  // the linear solver
111  sf_sol* lin_solver = nullptr;
112 
113  // linear solver stats
115 
116  // solver config
117  double tol = 1e-8;
118  int max_it = 100;
120 
121  // solver statistics
122  double final_residual = -1.0;
123  int niter = -1;
124 
126  {
127  if (lin_solver) delete lin_solver;
128  // matrices
129  if (u_mass_i) delete u_mass_i;
130  if (mass_i) delete mass_i;
131  if (rhs_parab) delete rhs_parab;
132  if (lhs_parab) delete lhs_parab;
133  if (phie_recov_mat) delete phie_recov_mat;
134  // vectors
135  // IIon and Vmv are shallow copies of global vectors, do not delete them
136  if (old_vm) delete old_vm;
137  if (kappa_i) delete kappa_i;
138  if (tmp_i1) delete tmp_i1;
139  if (tmp_i2) delete tmp_i2;
140  if (Irhs) delete Irhs;
141  if (inv_mass_diag) delete inv_mass_diag;
142  }
143 
144  void init();
145  void rebuild_matrices(MaterialType* mtype, limpet::MULTI_IF & miif, FILE_SPEC logger);
146  void solve(sf_vec & phie_i);
147 
148  private:
149  void setup_linear_solver(FILE_SPEC logger);
150 
151  void solve_CN(sf_vec & phie_i);
152  void solve_O2dT(sf_vec & phie_i);
153  void solve_EF(sf_vec & phie_i);
154 };
155 
156 enum PotType {VM, PHIE};
158 
160 struct Activation {
162  float threshold;
163  double start = 0.;
164  int mode;
165  int all;
166  int init;
167  sf_vec* phi = nullptr;
168  sf_vec* phip = nullptr;
169  int *ibuf = nullptr;
170  double *actbuf = nullptr;
171  sf_vec* tm = nullptr;
172  sf_vec* dvp0 = nullptr;
173  sf_vec* dvp1 = nullptr;
175  char *fname = nullptr;
176  char *ID = nullptr;
177  char *prv_fname = nullptr;
179  int offset;
180  int nacts;
181 };
182 
184 struct Sentinel {
185  bool activated = false;
186  double t_start = -1.0;
187  double t_window = -1.0;
188  double t_quiesc = -1.0;
189  int ID = -1;
190 };
191 
193 {
194  private:
196  int check_cross_threshold(sf_vec & vm, sf_vec & vmp, double tm,
197  int *ibuf, double *actbuf, float threshold, int mode);
198 
200  int check_mx_derivative(sf_vec & vm, sf_vec & vmp, double tm,
201  int *ibuf, double *actbuf, sf_vec & dvp0, sf_vec & dvp1,
202  float threshold, int mode);
203 
204  public:
208 
210  LAT_detector();
211 
213  void init(sf_vec & vm, sf_vec & phie, int offset, enum physic_t = elec_phys);
214 
216  int check_acts(double tm);
217 
219  int check_quiescence(double tm, double dt);
220 
223 };
224 
226 {
228  sf_vec* phie_rec = nullptr;
229  sf_vec* Im = nullptr;
230  sf_vec* dphi = nullptr;
232 };
233 
234 
235 class Electrics : public Basic_physic
236 {
237  public:
246 
251 
255 
260 
263 
266 
269 
272 
274 
279  {
280  name = "Electrics";
281  }
282 
291  void initialize();
292 
293  void destroy();
294 
295  // This funcs from the Basic_physic interface are currently empty
296  void compute_step();
297  void output_step();
298 
299  inline void output_timings()
300  {
301  // since ionics are included in electrics, we substract ionic timings from electric timings
304 
305  // now we call the base class timings output for Electrics
307  // and then for ionics
309  }
310 
312 
314  double timer_val(const int timer_id);
315 
317  std::string timer_unit(const int timer_id);
318 
319 
320  private:
321 
323  void setup_stimuli();
324 
326  void stimulate_intracellular();
329  void clamp_Vm();
330 
332  void stimulate_extracellular();
333 
335  void setup_solvers();
336 
338  void setup_mappings();
339 
341  void setup_output();
342 
344  void dump_matrices();
345 
346  void checkpointing();
347 
348  void balance_electrodes();
349 
350  void prepace();
351 };
352 
360 
362 bool have_dbc_stims(const SF::vector<stimulus> & stimuli);
363 
365 int stimidx_from_timeridx(const SF::vector<stimulus> & stimuli, const int timer_id);
366 
368 void recover_phie_std(sf_vec & vm, phie_recovery_data & rcv);
370 // Leadfield post-processing lives in physics/leadfield.{h,cc}
371 
372 class Laplace : public Basic_physic
373 {
374  public:
379 
384 
386  {
387  name = "Laplace solver";
388  }
389 
390  void initialize();
391  void destroy();
392  void compute_step();
393  void output_step();
395  double timer_val(const int timer_id);
397  std::string timer_unit(const int timer_id);
398 };
399 
426  sf_mat & mass_i,
427  sf_mat & mass_e,
428  limpet::MULTI_IF *miif,
429  FILE_SPEC logger);
430 
447 void balance_electrode(SF::vector<stimulus> & stimuli, int balance_from, int balance_to);
448 
449 void apply_stim_to_vector(const stimulus & s, sf_vec & vec, bool add);
450 
451 void set_cond_type(MaterialType & m, cond_t type);
452 
455 const char* get_tsav_ext(double time);
456 
460 void setup_dataout(const int dataout, std::string dataout_vtx, mesh_t grid,
461  SF::vector<mesh_int_t>* & restr, bool async = false,
462  const hashmap::unordered_set<int>* output_tags = NULL);
463 
464 void compute_restr_idx_async(sf_mesh & mesh,
465  SF::vector<mesh_int_t> & inp_idx,
466  SF::vector<mesh_int_t> & idx);
467 
468 void compute_restr_idx(sf_mesh & mesh,
469  SF::vector<mesh_int_t> & inp_idx,
470  SF::vector<mesh_int_t> & idx);
471 
472 } // namespace opencarp
473 
474 #endif
opencarp::real_t SF_real
Global scalar type.
Definition: SF_globals.h:18
A vector storing arbitrary data.
Definition: SF_vector.h:28
The abstract physics interface we can use to trigger all physics.
Definition: physics_types.h:44
virtual void output_timings()
Definition: physics_types.h:61
const char * name
The name of the physic, each physic should have one.
Definition: physics_types.h:47
SF::vector< stimulus > stimuli
the electrical stimuli
Definition: electrics.h:250
LAT_detector lat
the activation time detector
Definition: electrics.h:262
grid_t
An electrics grid identifier to distinguish between intra and extra grids.
Definition: electrics.h:244
phie_recovery_data phie_rcv
struct holding helper data for phie recovery
Definition: electrics.h:271
generic_timing_stats IO_stats
Definition: electrics.h:273
void destroy()
Currently we only need to close the file logger.
Definition: electrics.cc:356
gvec_data gvec
datastruct holding global IMP state variable output
Definition: electrics.h:265
elliptic_solver ellip_solver
Solver for the elliptic bidomain equation.
Definition: electrics.h:257
MaterialType mtype[2]
the material types of intra_grid and extra_grid grids.
Definition: electrics.h:248
std::string timer_unit(const int timer_id)
figure out units of a signal linked to a given timer
Definition: electrics.cc:828
parabolic_solver parab_solver
Solver for the parabolic bidomain equation.
Definition: electrics.h:259
double timer_val(const int timer_id)
figure out current value of a signal linked to a given timer
Definition: electrics.cc:812
void initialize()
Initialize the Electrics.
Definition: electrics.cc:29
igb_output_manager output_manager
class handling the igb output
Definition: electrics.h:268
Electrics()
Most of the initialization is done with initialize()
Definition: electrics.h:278
SF::index_mapping< mesh_int_t > petsc_to_nodal
Definition: electrics.h:206
int check_quiescence(double tm, double dt)
check for quiescence
Definition: electrics.cc:1790
void output_initial_activations()
output one nodal vector of initial activation time
Definition: electrics.cc:1905
void init(sf_vec &vm, sf_vec &phie, int offset, enum physic_t=elec_phys)
initializes all datastructs after electric solver setup
Definition: electrics.cc:1599
int check_acts(double tm)
check activations at sim time tm
Definition: electrics.cc:1722
SF::vector< Activation > acts
Definition: electrics.h:205
LAT_detector()
constructor, sets up basic datastructs from global_params
Definition: electrics.cc:1467
SF::vector< stimulus > stimuli
the electrical stimuli
Definition: electrics.h:378
elliptic_solver ellip_solver
Solver for the elliptic bidomain equation.
Definition: electrics.h:381
double timer_val(const int timer_id)
figure out current value of a signal linked to a given timer
Definition: electrics.cc:2320
std::string timer_unit(const int timer_id)
figure out units of a signal linked to a given timer
Definition: electrics.cc:2330
MaterialType mtype[2]
the material types of intra_grid and extra_grid grids.
Definition: electrics.h:376
igb_output_manager output_manager
class handling the igb output
Definition: electrics.h:383
manager for dirichlet boundary conditions
Definition: stimulate.h:191
sf_mat * phie_mat
lhs matrix to solve elliptic
Definition: electrics.h:39
void rebuild_stiffness(MaterialType *mtype, SF::vector< stimulus > &stimuli, FILE_SPEC logger)
Definition: electrics.cc:1016
void rebuild_matrices(MaterialType *mtype, SF::vector< stimulus > &stimuli, FILE_SPEC logger)
Definition: electrics.cc:1004
lin_solver_stats stats
Definition: electrics.h:45
sf_vec * phie_i
phi_e on intracellular grid
Definition: electrics.h:34
void solve(sf_mat &Ki, sf_vec &Vmv, sf_vec &tmp_i)
Definition: electrics.cc:1121
sf_vec * phie
phi_e
Definition: electrics.h:33
sf_sol * lin_solver
petsc or ginkgo lin_solver
Definition: electrics.h:42
sf_mat * mass_e
mass matrix for RHS elliptic calc
Definition: electrics.h:38
double tol
CG stopping tolerance.
Definition: electrics.h:52
sf_vec * currtmp
temp vector for phiesrc
Definition: electrics.h:36
dbc_manager * dbc
dbcs require a dbc manager
Definition: electrics.h:48
int max_it
maximum number of iterations
Definition: electrics.h:53
sf_vec * phiesrc
I_e.
Definition: electrics.h:35
void rebuild_mass(FILE_SPEC logger)
Definition: electrics.cc:1077
sf_vec * Ivol
global Vm vector
Definition: electrics.h:92
double tol
CG stopping tolerance.
Definition: electrics.h:117
sf_vec * Iact
global Vm vector
Definition: electrics.h:93
sf_vec * Diff_term
Diffusion current.
Definition: electrics.h:108
double final_residual
Holds the residual after convergence.
Definition: electrics.h:122
sf_mat * rhs_parab
rhs matrix to solve parabolic
Definition: electrics.h:104
sf_vec * kappa_i
scaling vector for intracellular mass matrix, M
Definition: electrics.h:96
lin_solver_stats stats
Definition: electrics.h:114
void rebuild_matrices(MaterialType *mtype, limpet::MULTI_IF &miif, FILE_SPEC logger)
Definition: electrics.cc:1246
parabolic_t parab_tech
manner in which parabolic equations are solved
Definition: electrics.h:119
void solve(sf_vec &phie_i)
Definition: electrics.cc:1360
int niter
number of iterations
Definition: electrics.h:123
sf_vec * inv_mass_diag
inverse diagonal of mass matrix, for EXPLICIT solving
Definition: electrics.h:100
sf_mat * mass_i
lumped for parabolic problem
Definition: electrics.h:103
sf_vec * Ic
global Vm vector
Definition: electrics.h:91
sf_mat * u_mass_i
unscaled mass matrix, M
Definition: electrics.h:102
sf_vec * tmp_i2
scratch vector for i-grid
Definition: electrics.h:98
int max_it
maximum number of iterations
Definition: electrics.h:118
sf_mat * phie_recov_mat
rhs for phie recovery with pseudo bidomain
Definition: electrics.h:106
sf_vec * tmp_i1
scratch vector for i-grid
Definition: electrics.h:97
sf_mat * lhs_parab
lhs matrix (CN) to solve parabolic
Definition: electrics.h:105
sf_vec * Vmv
global Vm vector
Definition: electrics.h:89
sf_vec * Irhs
weighted transmembrane currents
Definition: electrics.h:99
sf_vec * old_vm
older Vm needed for 2nd order dT
Definition: electrics.h:95
sf_sol * lin_solver
petsc or ginkgo lin_solver
Definition: electrics.h:111
sf_vec * IIon
ionic currents
Definition: electrics.h:88
Electrical ionics functions and LIMPET wrappers.
physic_t
Identifier for the different physics we want to set up.
Definition: physics_types.h:36
int stimidx_from_timeridx(const SF::vector< stimulus > &stimuli, const int timer_id)
determine link between timer and stimulus
Definition: electrics.cc:842
void set_cond_type(MaterialType &m, cond_t type)
Definition: electrics.cc:894
cond_t
description of electrical tissue properties
Definition: fem_types.h:27
void compute_restr_idx_async(sf_mesh &mesh, SF::vector< mesh_int_t > &inp_idx, SF::vector< mesh_int_t > &idx)
Definition: electrics.cc:563
void apply_stim_to_vector(const stimulus &s, sf_vec &vec, bool add)
Definition: electrics.cc:438
void recover_phie_std(sf_vec &vm, phie_recovery_data &rcv)
Definition: electrics.cc:2020
@ ACT_THRESH
Definition: electrics.h:157
bool have_dbc_stims(const SF::vector< stimulus > &stimuli)
return wheter any stimuli require dirichlet boundary conditions
Definition: electrics.cc:919
void constant_total_stimulus_current(SF::vector< stimulus > &stimuli, sf_mat &mass_i, sf_mat &mass_e, limpet::MULTI_IF *miif, FILE_SPEC logger)
Scales stimulus current to maintain constant total current across affected regions.
Definition: electrics.cc:2338
int postproc_recover_phie()
Definition: electrics.cc:2087
void balance_electrode(elliptic_solver &ellip, SF::vector< stimulus > &stimuli, int balance_from, int balance_to)
Definition: electrics.cc:372
void set_elec_tissue_properties(MaterialType *mtype, Electrics::grid_t g, FILE_SPEC logger)
Fill the RegionSpec of an electrics grid with the associated inputs from the param parameters.
Definition: electrics.cc:94
void compute_restr_idx(sf_mesh &mesh, SF::vector< mesh_int_t > &inp_idx, SF::vector< mesh_int_t > &idx)
Definition: electrics.cc:530
mesh_t
The enum identifying the different meshes we might want to load.
Definition: sf_interface.h:44
@ intra_elec_msh
Definition: sf_interface.h:45
void setup_phie_recovery_data(phie_recovery_data &data)
Definition: electrics.cc:2170
void setup_dataout(const int dataout, std::string dataout_vtx, mesh_t grid, SF::vector< mesh_int_t > *&restr, bool async, const hashmap::unordered_set< int > *output_tags)
Definition: electrics.cc:598
const char * get_tsav_ext(double time)
Definition: electrics.cc:928
SF::abstract_matrix< SF_int, SF_real > sf_mat
Definition: sf_interface.h:37
file_desc * FILE_SPEC
Definition: basics.h:125
Basic physics types.
Interface to SlimFem.
Simulator-level utility execution control functions.
Electrical stimulation functions.
event detection data structures
Definition: electrics.h:160
char * ID
ID used to name output file.
Definition: electrics.h:176
sf_vec * dvp0
additional vector for derivative
Definition: electrics.h:172
sf_vec * phi
signal
Definition: electrics.h:167
sf_vec * dvp1
additional vector for derivative
Definition: electrics.h:173
char * prv_fname
file name of previous run when restarting
Definition: electrics.h:177
int nacts
number of events detected over time step
Definition: electrics.h:180
int all
determine all or first instants of activation
Definition: electrics.h:165
ActMethod method
method to check whether activation occured
Definition: electrics.h:161
PotType measurand
quantity being monitored
Definition: electrics.h:178
FILE_SPEC fout
output file
Definition: electrics.h:174
sf_vec * tm
activation times
Definition: electrics.h:171
int init
true if intialized
Definition: electrics.h:166
int * ibuf
buffer indices where activation occured
Definition: electrics.h:169
int mode
toggle mode from standard to reverse
Definition: electrics.h:164
double start
activations earlier than this time (ms) are not recorded
Definition: electrics.h:163
int offset
node number offset (used for PS)
Definition: electrics.h:179
float threshold
threshold for detection of activation
Definition: electrics.h:162
double * actbuf
buffer activation times if method==all
Definition: electrics.h:170
char * fname
output file name
Definition: electrics.h:175
sf_vec * phip
previous value of signal
Definition: electrics.h:168
description of materal properties in a mesh
Definition: fem_types.h:106
sentinel for checking activity in the tissue
Definition: electrics.h:184
bool activated
flag sentinel activation
Definition: electrics.h:185
int ID
ID of LAT detector used as sentinel.
Definition: electrics.h:189
double t_start
start of observation window
Definition: electrics.h:186
double t_window
duration of observation window
Definition: electrics.h:187
double t_quiesc
measure current duration of quiescence
Definition: electrics.h:188
File descriptor struct.
Definition: basics.h:120
for analysis of the #iterations to solve CG
Definition: timers.h:72
for analysis of the #iterations to solve CG
Definition: timers.h:16
sf_vec * dphi
Auxiliary vectors.
Definition: electrics.h:230
sf_vec * phie_rec
The phie recovery output vector buffer.
Definition: electrics.h:228
SF::vector< mesh_real_t > pts
The phie recovery locations.
Definition: electrics.h:227
SF_real gBath
Bath conductivity.
Definition: electrics.h:231