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;
123  parabolic_t parab_tech = CN;
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 
225  void output_initial_activations();
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:
246  enum grid_t {intra_grid = 0, extra_grid};
247  enum linsys_t {elliptic_sys, parabolic_sys};
248 
250  MaterialType mtype[2];
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
304  compute_time -= ion.compute_time;
305  initialize_time -= ion.initialize_time;
306 
307  // now we call the base class timings output for Electrics
309  // and then for ionics
310  ion.output_timings();
311  }
312 
313  ~Electrics();
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:
377  MaterialType mtype[2];
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 
423 } // namespace opencarp
424 
425 #endif
int nacts
number of events detected over time step
Definition: electrics.h:183
SF_real gBath
Bath conductivity.
Definition: electrics.h:234
bool have_dbc_stims(const SF::vector< stimulus > &stimuli)
return wheter any stimuli require dirichlet boundary conditions
Definition: electrics.cc:826
dbc_manager * dbc
dbcs require a dbc manager
Definition: electrics.h:58
igb_output_manager output_manager
class handling the igb output
Definition: electrics.h:270
gvec_data gvec
datastruct holding global IMP state variable output
Definition: electrics.h:267
description of materal properties in a mesh
Definition: fem_types.h:110
void setup_phie_recovery_data(phie_recovery_data &data)
Definition: electrics.cc:2008
void recover_phie_std(sf_vec &vm, phie_recovery_data &rcv)
Definition: electrics.cc:1869
SF::vector< mesh_real_t > pts
The phie recovery locations.
Definition: electrics.h:230
sf_vec * currtmp
temp vector for phiesrc
Definition: electrics.h:46
cond_t
description of electrical tissue properties
Definition: fem_types.h:42
void solve(sf_mat &Ki, sf_vec &Vmv, sf_vec &tmp_i)
Definition: electrics.cc:1020
SF::vector< stimulus > stimuli
the electrical stimuli
Definition: electrics.h:379
elliptic_solver ellip_solver
Solver for the elliptic bidomain equation.
Definition: electrics.h:259
lin_solver_stats stats
Definition: electrics.h:55
SF::index_mapping< mesh_int_t > petsc_to_nodal
Definition: electrics.h:209
int all
determine all or first instants of activation
Definition: electrics.h:168
SF::vector< Activation > acts
Definition: electrics.h:208
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:2164
PotType measurand
quantity being monitored
Definition: electrics.h:181
generic_timing_stats IO_stats
Definition: electrics.h:275
void rebuild_mass(FILE_SPEC logger)
Definition: electrics.cc:983
int mode
toggle mode from standard to reverse
Definition: electrics.h:167
sf_mat * phie_mat
lhs matrix to solve elliptic
Definition: electrics.h:49
igb_output_manager output_manager
class handling the igb output
Definition: electrics.h:384
parabolic_solver parab_solver
Solver for the parabolic bidomain equation.
Definition: electrics.h:261
Electrics()
Most of the initialization is done with initialize()
Definition: electrics.h:280
event detection data structures
Definition: electrics.h:164
for analysis of the #iterations to solve CG
Definition: timers.h:34
The abstract physics interface we can use to trigger all physics.
Definition: physics_types.h:59
int init
true if intialized
Definition: electrics.h:169
ActMethod method
method to check whether activation occured
Definition: electrics.h:165
sf_sol * lin_solver
petsc or ginkgo lin_solver
Definition: electrics.h:52
float threshold
threshold for detection of activation
Definition: electrics.h:166
sentinel for checking activity in the tissue
Definition: electrics.h:187
int max_it
maximum number of iterations
Definition: electrics.h:63
manager for dirichlet boundary conditions
Definition: stimulate.h:198
Electrical ionics functions and LIMPET wrappers.
sf_vec * phie
phi_e
Definition: electrics.h:43
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
void set_cond_type(MaterialType &m, cond_t type)
Definition: electrics.cc:803
elliptic_solver ellip_solver
Solver for the elliptic bidomain equation.
Definition: electrics.h:382
Interface to SlimFem.
int offset
node number offset (used for PS)
Definition: electrics.h:182
SF::vector< stimulus > stimuli
the electrical stimuli
Definition: electrics.h:252
File descriptor struct.
Definition: basics.h:133
virtual void output_timings()
Definition: physics_types.h:76
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 * phiesrc
I_e.
Definition: electrics.h:45
void rebuild_stiffness(MaterialType *mtype, SF::vector< stimulus > &stimuli, FILE_SPEC logger)
Definition: electrics.cc:923
Electrical stimulation functions.
lin_solver_stats stats
Definition: electrics.h:118
A vector storing arbitrary data.
Definition: SF_vector.h:42
grid_t
An electrics grid identifier to distinguish between intra and extra grids.
Definition: electrics.h:246
void balance_electrode(SF::vector< stimulus > &stimuli, int balance_from, int balance_to)
Definition: electrics.cc:366
sf_vec * phie_i
phi_e on intracellular grid
Definition: electrics.h:44
for analysis of the #iterations to solve CG
Definition: timers.h:11
LAT_detector lat
the activation time detector
Definition: electrics.h:264
FILE_SPEC fout
output file
Definition: electrics.h:177
void rebuild_matrices(MaterialType *mtype, SF::vector< stimulus > &stimuli, FILE_SPEC logger)
Definition: electrics.cc:912
double SF_real
Use the general double as real type.
Definition: SF_globals.h:38
void apply_stim_to_vector(const stimulus &s, sf_vec &vec, bool add)
Definition: electrics.cc:427
int stimidx_from_timeridx(const SF::vector< stimulus > &stimuli, const int timer_id)
determine link between timer and stimulus
Definition: electrics.cc:751
Simulator-level utility execution control functions.
phie_recovery_data phie_rcv
struct holding helper data for phie recovery
Definition: electrics.h:273
int postproc_recover_phie()
Definition: electrics.cc:1929
Basic physics types.