openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
electrics.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 _ELECTRICS_H
28 #define _ELECTRICS_H
29 
30 #include "physics_types.h"
31 #include "sim_utils.h"
32 #include "stimulate.h"
33 
34 #include "sf_interface.h"
35 #include "timers.h"
36 #include "ionics.h"
37 
38 namespace opencarp {
39 
41 {
42  public:
43  sf_vec* phie = nullptr;
44  sf_vec* phie_i = nullptr;
45  sf_vec* phiesrc = nullptr;
46  sf_vec* currtmp = nullptr;
47 
48  sf_mat* mass_e = nullptr;
49  sf_mat* phie_mat = nullptr;
50 
51  // the linear solver
52  sf_sol* lin_solver = nullptr;
53 
54  // linear solver stats
56 
58  dbc_manager* dbc = nullptr;
59  bool phie_mat_has_nullspace = false;
60 
61  // solver config
62  double tol = 1e-8;
63  int max_it = 100;
64 
65  void init();
66  void rebuild_matrices(MaterialType* mtype, SF::vector<stimulus> & stimuli, FILE_SPEC logger);
67  void rebuild_stiffness(MaterialType* mtype, SF::vector<stimulus> & stimuli, FILE_SPEC logger);
68  void rebuild_mass(FILE_SPEC logger);
69  void solve(sf_mat & Ki, sf_vec & Vmv, sf_vec & tmp_i);
70  void solve_laplace();
71 
73  {
74  if(dbc) delete dbc;
75  if(lin_solver) delete lin_solver;
76  // matrices
77  if(phie_mat) delete phie_mat;
78  if(mass_e) delete mass_e;
79  // vectors
80  if(phie) delete phie;
81  if(phie_i) delete phie_i;
82  if(phiesrc) delete phiesrc;
83  if(currtmp) delete currtmp;
84  }
85 
86  private:
87  void enforce_dbc();
88  void setup_linear_solver(FILE_SPEC logger);
89 };
90 
92 {
93  public:
94 
95  // Has to be kept in line with param_globals::parab_solve
96  enum parabolic_t { EXPLICIT = 0, CN = 1, O2dT = 2};
97 
98  sf_vec* IIon = nullptr;
99  sf_vec* Vmv = nullptr;
100 
101  sf_vec* old_vm = nullptr;
102  sf_vec* kappa_i = nullptr;
103  sf_vec* tmp_i1 = nullptr;
104  sf_vec* tmp_i2 = nullptr;
105  sf_vec* Irhs = nullptr;
106  sf_vec* inv_mass_diag = nullptr;
107 
108  sf_mat* u_mass_i = nullptr;
109  sf_mat* mass_i = nullptr;
110  sf_mat* rhs_parab = nullptr;
111  sf_mat* lhs_parab = nullptr;
112  sf_mat* phie_recov_mat = nullptr;
113 
114  // the linear solver
115  sf_sol* lin_solver = nullptr;
116 
117  // linear solver stats
119 
120  // solver config
121  double tol = 1e-8;
122  int max_it = 100;
124 
125  // solver statistics
126  double final_residual = -1.0;
127  int niter = -1;
128 
130  {
131  if (lin_solver) delete lin_solver;
132  // matrices
133  if (u_mass_i) delete u_mass_i;
134  if (mass_i) delete mass_i;
135  if (rhs_parab) delete rhs_parab;
136  if (lhs_parab) delete lhs_parab;
137  if (phie_recov_mat) delete phie_recov_mat;
138  // vectors
139  // IIon and Vmv are shallow copies of global vectors, do not delete them
140  if (old_vm) delete old_vm;
141  if (kappa_i) delete kappa_i;
142  if (tmp_i1) delete tmp_i1;
143  if (tmp_i2) delete tmp_i2;
144  if (Irhs) delete Irhs;
145  if (inv_mass_diag) delete inv_mass_diag;
146  }
147 
148  void init();
149  void rebuild_matrices(MaterialType* mtype, limpet::MULTI_IF & miif, FILE_SPEC logger);
150  void solve(sf_vec & phie_i);
151 
152  private:
153  void setup_linear_solver(FILE_SPEC logger);
154 
155  void solve_CN(sf_vec & phie_i);
156  void solve_O2dT(sf_vec & phie_i);
157  void solve_EF(sf_vec & phie_i);
158 };
159 
160 enum PotType {VM, PHIE};
162 
164 struct Activation {
166  float threshold;
167  int mode;
168  int all;
169  int init;
170  sf_vec* phi = nullptr;
171  sf_vec* phip = nullptr;
172  int *ibuf = nullptr;
173  double *actbuf = nullptr;
174  sf_vec* tm = nullptr;
175  sf_vec* dvp0 = nullptr;
176  sf_vec* dvp1 = nullptr;
178  char *fname = nullptr;
179  char *ID = nullptr;
180  char *prv_fname = nullptr;
182  int offset;
183  int nacts;
184 };
185 
187 struct Sentinel {
188  bool activated = false;
189  double t_start = -1.0;
190  double t_window = -1.0;
191  double t_quiesc = -1.0;
192  int ID = -1;
193 };
194 
196 {
197  private:
199  int check_cross_threshold(sf_vec & vm, sf_vec & vmp, double tm,
200  int *ibuf, double *actbuf, float threshold, int mode);
201 
203  int check_mx_derivative(sf_vec & vm, sf_vec & vmp, double tm,
204  int *ibuf, double *actbuf, sf_vec & dvp0, sf_vec & dvp1,
205  float threshold, int mode);
206 
207  public:
211 
213  LAT_detector();
214 
216  void init(sf_vec & vm, sf_vec & phie, int offset);
217 
219  int check_acts(double tm);
220 
222  int check_quiescence(double tm, double dt);
223 
226 };
227 
229 {
231  sf_vec* phie_rec = nullptr;
232  sf_vec* Im = nullptr;
233  sf_vec* dphi = nullptr;
235 };
236 
237 class Electrics : public Basic_physic
238 {
239  public:
248 
253 
257 
262 
265 
268 
271 
274 
276 
281  {
282  name = "Electrics";
283  }
284 
293  void initialize();
294 
295  void destroy();
296 
297  // This funcs from the Basic_physic interface are currently empty
298  void compute_step();
299  void output_step();
300 
301  inline void output_timings()
302  {
303  // since ionics are included in electrics, we substract ionic timings from electric timings
306 
307  // now we call the base class timings output for Electrics
309  // and then for ionics
311  }
312 
314 
316  double timer_val(const int timer_id);
317 
319  std::string timer_unit(const int timer_id);
320 
321 
322  private:
323 
325  void setup_stimuli();
326 
328  void stimulate_intracellular();
331  void clamp_Vm();
332 
334  void stimulate_extracellular();
335 
337  void setup_solvers();
338 
340  void setup_mappings();
341 
343  void setup_output();
344 
346  void dump_matrices();
347 
348  void checkpointing();
349 
350  void balance_electrodes();
351 
352  void prepace();
353 };
354 
362 
364 bool have_dbc_stims(const SF::vector<stimulus> & stimuli);
365 
367 int stimidx_from_timeridx(const SF::vector<stimulus> & stimuli, const int timer_id);
368 
370 void recover_phie_std(sf_vec & vm, phie_recovery_data & rcv);
372 
373 class Laplace : public Basic_physic
374 {
375  public:
380 
385 
387  {
388  name = "Laplace solver";
389  }
390 
391  void initialize();
392  void destroy();
393  void compute_step();
394  void output_step();
396  double timer_val(const int timer_id);
398  std::string timer_unit(const int timer_id);
399 };
400 
412  sf_mat & mass_i,
413  sf_mat & mass_e,
414  limpet::MULTI_IF *miif,
415  FILE_SPEC logger);
416 
417 void balance_electrode(SF::vector<stimulus> & stimuli, int balance_from, int balance_to);
418 
419 void apply_stim_to_vector(const stimulus & s, sf_vec & vec, bool add);
420 
421 void set_cond_type(MaterialType & m, cond_t type);
422 } // namespace opencarp
423 
424 #endif
double SF_real
Use the general double as real type.
Definition: SF_globals.h:38
A vector storing arbitrary data.
Definition: SF_vector.h:43
The abstract physics interface we can use to trigger all physics.
Definition: physics_types.h:59
virtual void output_timings()
Definition: physics_types.h:76
const char * name
The name of the physic, each physic should have one.
Definition: physics_types.h:62
SF::vector< stimulus > stimuli
the electrical stimuli
Definition: electrics.h:252
LAT_detector lat
the activation time detector
Definition: electrics.h:264
grid_t
An electrics grid identifier to distinguish between intra and extra grids.
Definition: electrics.h:246
phie_recovery_data phie_rcv
struct holding helper data for phie recovery
Definition: electrics.h:273
generic_timing_stats IO_stats
Definition: electrics.h:275
void destroy()
Currently we only need to close the file logger.
Definition: electrics.cc:351
gvec_data gvec
datastruct holding global IMP state variable output
Definition: electrics.h:267
elliptic_solver ellip_solver
Solver for the elliptic bidomain equation.
Definition: electrics.h:259
MaterialType mtype[2]
the material types of intra_grid and extra_grid grids.
Definition: electrics.h:250
std::string timer_unit(const int timer_id)
figure out units of a signal linked to a given timer
Definition: electrics.cc:736
parabolic_solver parab_solver
Solver for the parabolic bidomain equation.
Definition: electrics.h:261
double timer_val(const int timer_id)
figure out current value of a signal linked to a given timer
Definition: electrics.cc:720
void initialize()
Initialize the Electrics.
Definition: electrics.cc:36
igb_output_manager output_manager
class handling the igb output
Definition: electrics.h:270
Electrics()
Most of the initialization is done with initialize()
Definition: electrics.h:280
SF::index_mapping< mesh_int_t > petsc_to_nodal
Definition: electrics.h:209
int check_quiescence(double tm, double dt)
check for quiescence
Definition: electrics.cc:1641
void output_initial_activations()
output one nodal vector of initial activation time
Definition: electrics.cc:1750
int check_acts(double tm)
check activations at sim time tm
Definition: electrics.cc:1586
SF::vector< Activation > acts
Definition: electrics.h:208
void init(sf_vec &vm, sf_vec &phie, int offset)
initializes all datastructs after electric solver setup
Definition: electrics.cc:1471
LAT_detector()
constructor, sets up basic datastructs from global_params
Definition: electrics.cc:1343
SF::vector< stimulus > stimuli
the electrical stimuli
Definition: electrics.h:379
elliptic_solver ellip_solver
Solver for the elliptic bidomain equation.
Definition: electrics.h:382
double timer_val(const int timer_id)
figure out current value of a signal linked to a given timer
Definition: electrics.cc:2148
std::string timer_unit(const int timer_id)
figure out units of a signal linked to a given timer
Definition: electrics.cc:2158
MaterialType mtype[2]
the material types of intra_grid and extra_grid grids.
Definition: electrics.h:377
igb_output_manager output_manager
class handling the igb output
Definition: electrics.h:384
manager for dirichlet boundary conditions
Definition: stimulate.h:199
sf_mat * phie_mat
lhs matrix to solve elliptic
Definition: electrics.h:49
void rebuild_stiffness(MaterialType *mtype, SF::vector< stimulus > &stimuli, FILE_SPEC logger)
Definition: electrics.cc:924
void rebuild_matrices(MaterialType *mtype, SF::vector< stimulus > &stimuli, FILE_SPEC logger)
Definition: electrics.cc:911
lin_solver_stats stats
Definition: electrics.h:55
sf_vec * phie_i
phi_e on intracellular grid
Definition: electrics.h:44
void solve(sf_mat &Ki, sf_vec &Vmv, sf_vec &tmp_i)
Definition: electrics.cc:1023
sf_vec * phie
phi_e
Definition: electrics.h:43
sf_sol * lin_solver
petsc or ginkgo lin_solver
Definition: electrics.h:52
sf_mat * mass_e
mass matrix for RHS elliptic calc
Definition: electrics.h:48
double tol
CG stopping tolerance.
Definition: electrics.h:62
sf_vec * currtmp
temp vector for phiesrc
Definition: electrics.h:46
dbc_manager * dbc
dbcs require a dbc manager
Definition: electrics.h:58
int max_it
maximum number of iterations
Definition: electrics.h:63
sf_vec * phiesrc
I_e.
Definition: electrics.h:45
void rebuild_mass(FILE_SPEC logger)
Definition: electrics.cc:986
double tol
CG stopping tolerance.
Definition: electrics.h:121
double final_residual
Holds the residual after convergence.
Definition: electrics.h:126
sf_mat * rhs_parab
rhs matrix to solve parabolic
Definition: electrics.h:110
sf_vec * kappa_i
scaling vector for intracellular mass matrix, M
Definition: electrics.h:102
lin_solver_stats stats
Definition: electrics.h:118
void rebuild_matrices(MaterialType *mtype, limpet::MULTI_IF &miif, FILE_SPEC logger)
Definition: electrics.cc:1137
parabolic_t parab_tech
manner in which parabolic equations are solved
Definition: electrics.h:123
void solve(sf_vec &phie_i)
Definition: electrics.cc:1240
int niter
number of iterations
Definition: electrics.h:127
sf_vec * inv_mass_diag
inverse diagonal of mass matrix, for EXPLICIT solving
Definition: electrics.h:106
sf_mat * mass_i
lumped for parabolic problem
Definition: electrics.h:109
sf_mat * u_mass_i
unscaled mass matrix, M
Definition: electrics.h:108
sf_vec * tmp_i2
scratch vector for i-grid
Definition: electrics.h:104
int max_it
maximum number of iterations
Definition: electrics.h:122
sf_mat * phie_recov_mat
rhs for phie recovery with pseudo bidomain
Definition: electrics.h:112
sf_vec * tmp_i1
scratch vector for i-grid
Definition: electrics.h:103
sf_mat * lhs_parab
lhs matrix (CN) to solve parabolic
Definition: electrics.h:111
sf_vec * Vmv
global Vm vector
Definition: electrics.h:99
sf_vec * Irhs
weighted transmembrane currents
Definition: electrics.h:105
sf_vec * old_vm
older Vm needed for 2nd order dT
Definition: electrics.h:101
sf_sol * lin_solver
petsc or ginkgo lin_solver
Definition: electrics.h:115
sf_vec * IIon
ionic currents
Definition: electrics.h:98
Electrical ionics functions and LIMPET wrappers.
int stimidx_from_timeridx(const SF::vector< stimulus > &stimuli, const int timer_id)
determine link between timer and stimulus
Definition: electrics.cc:750
void set_cond_type(MaterialType &m, cond_t type)
Definition: electrics.cc:802
SF::abstract_vector< mesh_int_t, double > sf_vec
Definition: sf_interface.h:49
cond_t
description of electrical tissue properties
Definition: fem_types.h:45
void constant_total_stimulus_current(SF::vector< stimulus > &stimuli, sf_mat &mass_i, sf_mat &mass_e, limpet::MULTI_IF *miif, FILE_SPEC logger)
Definition: electrics.cc:2166
void apply_stim_to_vector(const stimulus &s, sf_vec &vec, bool add)
Definition: electrics.cc:427
void recover_phie_std(sf_vec &vm, phie_recovery_data &rcv)
Definition: electrics.cc:1871
@ ACT_THRESH
Definition: electrics.h:161
SF::abstract_matrix< mesh_int_t, double > sf_mat
Definition: sf_interface.h:51
bool have_dbc_stims(const SF::vector< stimulus > &stimuli)
return wheter any stimuli require dirichlet boundary conditions
Definition: electrics.cc:825
int postproc_recover_phie()
Definition: electrics.cc:1931
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:100
@ intra_elec_msh
Definition: sf_interface.h:61
void setup_phie_recovery_data(phie_recovery_data &data)
Definition: electrics.cc:2010
void balance_electrode(SF::vector< stimulus > &stimuli, int balance_from, int balance_to)
Definition: electrics.cc:366
file_desc * FILE_SPEC
Definition: basics.h:138
Basic physics types.
Interface to SlimFem.
Simulator-level utility execution control functions.
Electrical stimulation functions.
event detection data structures
Definition: electrics.h:164
char * ID
ID used to name output file.
Definition: electrics.h:179
sf_vec * dvp0
additional vector for derivative
Definition: electrics.h:175
sf_vec * phi
signal
Definition: electrics.h:170
sf_vec * dvp1
additional vector for derivative
Definition: electrics.h:176
char * prv_fname
file name of previous run when restarting
Definition: electrics.h:180
int nacts
number of events detected over time step
Definition: electrics.h:183
int all
determine all or first instants of activation
Definition: electrics.h:168
ActMethod method
method to check whether activation occured
Definition: electrics.h:165
PotType measurand
quantity being monitored
Definition: electrics.h:181
FILE_SPEC fout
output file
Definition: electrics.h:177
sf_vec * tm
activation times
Definition: electrics.h:174
int init
true if intialized
Definition: electrics.h:169
int * ibuf
buffer indices where activation occured
Definition: electrics.h:172
int mode
toggle mode from standard to reverse
Definition: electrics.h:167
int offset
node number offset (used for PS)
Definition: electrics.h:182
float threshold
threshold for detection of activation
Definition: electrics.h:166
double * actbuf
buffer activation times if method==all
Definition: electrics.h:173
char * fname
output file name
Definition: electrics.h:178
sf_vec * phip
previous value of signal
Definition: electrics.h:171
description of materal properties in a mesh
Definition: fem_types.h:155
sentinel for checking activity in the tissue
Definition: electrics.h:187
bool activated
flag sentinel activation
Definition: electrics.h:188
int ID
ID of LAT detector used as sentinel.
Definition: electrics.h:192
double t_start
start of observation window
Definition: electrics.h:189
double t_window
duration of observation window
Definition: electrics.h:190
double t_quiesc
measure current duration of quiescence
Definition: electrics.h:191
File descriptor struct.
Definition: basics.h:133
for analysis of the #iterations to solve CG
Definition: timers.h:34
for analysis of the #iterations to solve CG
Definition: timers.h:11
sf_vec * dphi
Auxiliary vectors.
Definition: electrics.h:233
sf_vec * phie_rec
The phie recovery output vector buffer.
Definition: electrics.h:231
SF::vector< mesh_real_t > pts
The phie recovery locations.
Definition: electrics.h:230
SF_real gBath
Bath conductivity.
Definition: electrics.h:234