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  sf_vec* Diff_term = nullptr;
115 
116  // the linear solver
117  sf_sol* lin_solver = nullptr;
118 
119  // linear solver stats
121 
122  // solver config
123  double tol = 1e-8;
124  int max_it = 100;
125  parabolic_t parab_tech = CN;
126 
127  // solver statistics
128  double final_residual = -1.0;
129  int niter = -1;
130 
132  {
133  if (lin_solver) delete lin_solver;
134  // matrices
135  if (u_mass_i) delete u_mass_i;
136  if (mass_i) delete mass_i;
137  if (rhs_parab) delete rhs_parab;
138  if (lhs_parab) delete lhs_parab;
139  if (phie_recov_mat) delete phie_recov_mat;
140  // vectors
141  // IIon and Vmv are shallow copies of global vectors, do not delete them
142  if (old_vm) delete old_vm;
143  if (kappa_i) delete kappa_i;
144  if (tmp_i1) delete tmp_i1;
145  if (tmp_i2) delete tmp_i2;
146  if (Irhs) delete Irhs;
147  if (inv_mass_diag) delete inv_mass_diag;
148  }
149 
150  void init();
151  void rebuild_matrices(MaterialType* mtype, limpet::MULTI_IF & miif, FILE_SPEC logger);
152  void solve(sf_vec & phie_i);
153 
154  private:
155  void setup_linear_solver(FILE_SPEC logger);
156 
157  void solve_CN(sf_vec & phie_i);
158  void solve_O2dT(sf_vec & phie_i);
159  void solve_EF(sf_vec & phie_i);
160 };
161 
162 enum PotType {VM, PHIE};
164 
166 struct Activation {
168  float threshold;
169  int mode;
170  int all;
171  int init;
172  sf_vec* phi = nullptr;
173  sf_vec* phip = nullptr;
174  int *ibuf = nullptr;
175  double *actbuf = nullptr;
176  sf_vec* tm = nullptr;
177  sf_vec* dvp0 = nullptr;
178  sf_vec* dvp1 = nullptr;
180  char *fname = nullptr;
181  char *ID = nullptr;
182  char *prv_fname = nullptr;
184  int offset;
185  int nacts;
186 };
187 
189 struct Sentinel {
190  bool activated = false;
191  double t_start = -1.0;
192  double t_window = -1.0;
193  double t_quiesc = -1.0;
194  int ID = -1;
195 };
196 
198 {
199  private:
201  int check_cross_threshold(sf_vec & vm, sf_vec & vmp, double tm,
202  int *ibuf, double *actbuf, float threshold, int mode);
203 
205  int check_mx_derivative(sf_vec & vm, sf_vec & vmp, double tm,
206  int *ibuf, double *actbuf, sf_vec & dvp0, sf_vec & dvp1,
207  float threshold, int mode);
208 
209  public:
213 
215  LAT_detector();
216 
218  void init(sf_vec & vm, sf_vec & phie, int offset, enum physic_t = elec_phys);
219 
221  int check_acts(double tm);
222 
224  int check_quiescence(double tm, double dt);
225 
227  void output_initial_activations();
228 };
229 
231 {
233  sf_vec* phie_rec = nullptr;
234  sf_vec* Im = nullptr;
235  sf_vec* dphi = nullptr;
237 };
238 
239 class Electrics : public Basic_physic
240 {
241  public:
248  enum grid_t {intra_grid = 0, extra_grid};
249  enum linsys_t {elliptic_sys, parabolic_sys};
250 
252  MaterialType mtype[2];
255 
259 
264 
267 
270 
273 
276 
278 
283  {
284  name = "Electrics";
285  }
286 
295  void initialize();
296 
297  void destroy();
298 
299  // This funcs from the Basic_physic interface are currently empty
300  void compute_step();
301  void output_step();
302 
303  inline void output_timings()
304  {
305  // since ionics are included in electrics, we substract ionic timings from electric timings
306  compute_time -= ion.compute_time;
307  initialize_time -= ion.initialize_time;
308 
309  // now we call the base class timings output for Electrics
311  // and then for ionics
312  ion.output_timings();
313  }
314 
315  ~Electrics();
316 
318  double timer_val(const int timer_id);
319 
321  std::string timer_unit(const int timer_id);
322 
323 
324  private:
325 
327  void setup_stimuli();
328 
330  void stimulate_intracellular();
333  void clamp_Vm();
334 
336  void stimulate_extracellular();
337 
339  void setup_solvers();
340 
342  void setup_mappings();
343 
345  void setup_output();
346 
348  void dump_matrices();
349 
350  void checkpointing();
351 
352  void balance_electrodes();
353 
354  void prepace();
355 };
356 
364 
366 bool have_dbc_stims(const SF::vector<stimulus> & stimuli);
367 
369 int stimidx_from_timeridx(const SF::vector<stimulus> & stimuli, const int timer_id);
370 
372 void recover_phie_std(sf_vec & vm, phie_recovery_data & rcv);
374 
375 class Laplace : public Basic_physic
376 {
377  public:
379  MaterialType mtype[2];
382 
387 
389  {
390  name = "Laplace solver";
391  }
392 
393  void initialize();
394  void destroy();
395  void compute_step();
396  void output_step();
398  double timer_val(const int timer_id);
400  std::string timer_unit(const int timer_id);
401 };
402 
414  sf_mat & mass_i,
415  sf_mat & mass_e,
416  limpet::MULTI_IF *miif,
417  FILE_SPEC logger);
418 
419 void balance_electrode(SF::vector<stimulus> & stimuli, int balance_from, int balance_to);
420 
421 void apply_stim_to_vector(const stimulus & s, sf_vec & vec, bool add);
422 
423 void set_cond_type(MaterialType & m, cond_t type);
424 
427 const char* get_tsav_ext(double time);
428 
431 void setup_dataout(const int dataout, std::string dataout_vtx, mesh_t grid,
432  SF::vector<mesh_int_t>* & restr, bool async = false);
433 
434 void compute_restr_idx_async(sf_mesh & mesh,
435  SF::vector<mesh_int_t> & inp_idx,
436  SF::vector<mesh_int_t> & idx);
437 
438 void compute_restr_idx(sf_mesh & mesh,
439  SF::vector<mesh_int_t> & inp_idx,
440  SF::vector<mesh_int_t> & idx);
441 
442 } // namespace opencarp
443 
444 #endif
int nacts
number of events detected over time step
Definition: electrics.h:185
SF_real gBath
Bath conductivity.
Definition: electrics.h:236
bool have_dbc_stims(const SF::vector< stimulus > &stimuli)
return wheter any stimuli require dirichlet boundary conditions
Definition: electrics.cc:822
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:272
gvec_data gvec
datastruct holding global IMP state variable output
Definition: electrics.h:269
description of materal properties in a mesh
Definition: fem_types.h:110
void setup_phie_recovery_data(phie_recovery_data &data)
Definition: electrics.cc:2019
void recover_phie_std(sf_vec &vm, phie_recovery_data &rcv)
Definition: electrics.cc:1870
SF::vector< mesh_real_t > pts
The phie recovery locations.
Definition: electrics.h:232
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:1014
SF::vector< stimulus > stimuli
the electrical stimuli
Definition: electrics.h:381
elliptic_solver ellip_solver
Solver for the elliptic bidomain equation.
Definition: electrics.h:261
lin_solver_stats stats
Definition: electrics.h:55
SF::index_mapping< mesh_int_t > petsc_to_nodal
Definition: electrics.h:211
int all
determine all or first instants of activation
Definition: electrics.h:170
SF::vector< Activation > acts
Definition: electrics.h:210
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:2183
void balance_electrode(elliptic_solver &ellip, SF::vector< stimulus > &stimuli, int balance_from, int balance_to)
Definition: electrics.cc:369
mesh_t
The enum identifying the different meshes we might want to load.
Definition: sf_interface.h:58
physic_t
Identifier for the different physics we want to set up.
Definition: physics_types.h:51
PotType measurand
quantity being monitored
Definition: electrics.h:183
generic_timing_stats IO_stats
Definition: electrics.h:277
void rebuild_mass(FILE_SPEC logger)
Definition: electrics.cc:977
int mode
toggle mode from standard to reverse
Definition: electrics.h:169
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:386
parabolic_solver parab_solver
Solver for the parabolic bidomain equation.
Definition: electrics.h:263
Electrics()
Most of the initialization is done with initialize()
Definition: electrics.h:282
event detection data structures
Definition: electrics.h:166
for analysis of the #iterations to solve CG
Definition: timers.h:69
The abstract physics interface we can use to trigger all physics.
Definition: physics_types.h:59
int init
true if intialized
Definition: electrics.h:171
ActMethod method
method to check whether activation occured
Definition: electrics.h:167
sf_sol * lin_solver
petsc or ginkgo lin_solver
Definition: electrics.h:52
float threshold
threshold for detection of activation
Definition: electrics.h:168
sentinel for checking activity in the tissue
Definition: electrics.h:189
const char * get_tsav_ext(double time)
Definition: electrics.cc:831
int max_it
maximum number of iterations
Definition: electrics.h:63
manager for dirichlet boundary conditions
Definition: stimulate.h:192
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:799
elliptic_solver ellip_solver
Solver for the elliptic bidomain equation.
Definition: electrics.h:384
Interface to SlimFem.
int offset
node number offset (used for PS)
Definition: electrics.h:184
void compute_restr_idx(sf_mesh &mesh, SF::vector< mesh_int_t > &inp_idx, SF::vector< mesh_int_t > &idx)
Definition: electrics.cc:520
SF::vector< stimulus > stimuli
the electrical stimuli
Definition: electrics.h:254
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:917
Electrical stimulation functions.
lin_solver_stats stats
Definition: electrics.h:120
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:248
sf_vec * phie_i
phi_e on intracellular grid
Definition: electrics.h:44
for analysis of the #iterations to solve CG
Definition: timers.h:13
LAT_detector lat
the activation time detector
Definition: electrics.h:266
FILE_SPEC fout
output file
Definition: electrics.h:179
void rebuild_matrices(MaterialType *mtype, SF::vector< stimulus > &stimuli, FILE_SPEC logger)
Definition: electrics.cc:906
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:428
void setup_dataout(const int dataout, std::string dataout_vtx, mesh_t grid, SF::vector< mesh_int_t > *&restr, bool async)
Definition: electrics.cc:588
int stimidx_from_timeridx(const SF::vector< stimulus > &stimuli, const int timer_id)
determine link between timer and stimulus
Definition: electrics.cc:747
Simulator-level utility execution control functions.
phie_recovery_data phie_rcv
struct holding helper data for phie recovery
Definition: electrics.h:275
void compute_restr_idx_async(sf_mesh &mesh, SF::vector< mesh_int_t > &inp_idx, SF::vector< mesh_int_t > &idx)
Definition: electrics.cc:553
int postproc_recover_phie()
Definition: electrics.cc:1936
Basic physics types.