openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
emi.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2020 openCARP project
3 //
4 // This program is licensed under the openCARP Academic Public License (APL)
5 // v1.0: You can use and redistribute it and/or modify it in non-commercial
6 // academic environments under the terms of APL as published by the openCARP
7 // project v1.0, or (at your option) any later version. Commercial use requires
8 // a commercial license (info@opencarp.org).
9 //
10 // This program is distributed without any warranty; see the openCARP APL for
11 // more details.
12 //
13 // You should have received a copy of the openCARP APL along with this program
14 // and can find it online: http://www.opencarp.org/license
15 // ----------------------------------------------------------------------------
16 
24 #if WITH_EMI_MODEL
25 
26 #ifndef _EMI_H
27 #define _EMI_H
28 
29 #include "electrics.h"
30 #include "ionicsOnFace.h"
31 
32 namespace opencarp {
33 
34 class parabolic_solver_emi
35 {
36  public:
37 
38  // Has to be kept in line with param_globals::parab_solve
39  enum parabolic_t {SEMI_IMPLICIT = 0};
40  //
41  // There are three surface meshes in EMI model:
42 
43  // - `emi_surface_msh`:
44  // Contains face/counter-face information only when the extracellular and intracellular sides are not located on the same MPI rank, i.e., when the membrane interface is split across ranks and a counter-face representation is required.
45  // This mesh is associated with the vector `one_face`.
46 
47  // - `emi_surface_counter_msh`:
48  // Contains both faces and counter-faces for all membrane interfaces, i.e., regardless of whether the extracellular and intracellular parts are located on the same MPI rank or on different ranks.
49  // This mesh is associated with the vector `both_face`.
50 
51  // - `emi_surface_unique_face_msh`:
52  // Contains only unique faces, where each physical membrane face appears exactly once, independent of whether its two sides are located on the same MPI rank or on different ranks.
53  // This mesh is associated with the vector `unique_face`.
54  //
55  sf_vec* ui = nullptr;
56  sf_vec* dui = nullptr;
57  sf_vec* ui_pre = nullptr;
58  sf_vec* vb = nullptr;
59  sf_vec* vb_one_face = nullptr;
60  sf_vec* vb_both_face = nullptr;
61  sf_vec* vb_unique_face = nullptr;
62  sf_vec* Ib = nullptr;
63  sf_vec* Ib_one_face = nullptr;
64  sf_vec* Ib_both_face = nullptr;
65  sf_vec* Ib_unique_face = nullptr;
66  sf_vec* Iij_stim = nullptr;
67  sf_vec* Iij_temp = nullptr;
68  sf_vec* Irhs = nullptr;
69 
70  sf_mat* mass_emi = nullptr;
71  sf_mat* mass_surf_emi = nullptr;
72  sf_mat* lhs_emi = nullptr;
73  sf_mat* stiffness_emi = nullptr;
74  sf_mat* B = nullptr;
75  sf_mat* Bi = nullptr;
76  sf_mat* BsM = nullptr;
77  sf_mat* Vm_myocyte_emi = nullptr;
78 
79  // Direct operators (unique <-> both, skipping intermediate one-face)
80  sf_mat* operator_unique_to_both_faces = nullptr;
81  sf_mat* operator_both_to_unique_face = nullptr;
82 
83 
85  std::pair<SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>,
86  SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>>> line_face;
88  std::pair<SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>,
89  SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>>> tri_face;
91  std::pair<SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>,
92  SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>>> quad_face;
93 
95  hashmap::unordered_map<std::pair<mesh_int_t,mesh_int_t>, std::pair<mesh_int_t,mesh_int_t>> map_vertex_tag_to_dof_petsc;
96 
100 
101  hashmap::unordered_set<int> extra_tags;
102  hashmap::unordered_set<int> intra_tags;
103 
104  std::set<std::pair<mesh_int_t,mesh_int_t>> membraneFace_default;
105  std::set<std::pair<mesh_int_t,mesh_int_t>> gapjunctionFace_default;
106 
107  // here for any element, we set 1 for membrane and 2 for gap-junction
108  SF::vector<mesh_int_t> elemTag_emi_mesh;
109  SF::vector<mesh_int_t> elemTag_surface_mesh;
110  SF::vector<mesh_int_t> elemTag_surface_w_counter_mesh;
111 
112 
113  // map via vector from emi_surfmesh_w_counter_face element indices to emi_surfmesh (both -> one)
114  SF::vector<mesh_int_t> vec_both_to_one_face;
115 
116  hashmap::unordered_map<mesh_int_t, std::pair<SF::emi_index_rank<mesh_int_t>, SF::emi_index_rank<mesh_int_t>>> map_elem_uniqueFace_to_elem_oneface;
117 
118 
119  // Direct mapping (unique -> both face indices, no intermediate)
120  hashmap::unordered_map<mesh_int_t, std::pair<SF::emi_index_rank<mesh_int_t>, SF::emi_index_rank<mesh_int_t>>> map_elem_uniqueFace_to_elem_bothface;
121 
123  // the linear solver
124  sf_sol* lin_solver = nullptr;
125 
126  // linear solver stats
127  lin_solver_stats stats;
128 
130  dbc_manager* dbc = nullptr;
131  bool phie_mat_has_nullspace = false;
132 
133  // solver config
134  double tol = 1e-8;
135  int max_it = 100;
136  parabolic_t parab_tech = SEMI_IMPLICIT;
137 
138  // solver statistics
139  double final_residual = -1.0;
140  int niter = -1;
141 
142  ~parabolic_solver_emi()
143  {
144  if(dbc) delete dbc;
145  if (lin_solver) delete lin_solver;
146  // matrices
147  if (mass_emi) delete mass_emi;
148  if (mass_surf_emi) delete mass_surf_emi;
149  if (lhs_emi) delete lhs_emi;
150  if (stiffness_emi) delete stiffness_emi;
151  if (B) delete B;
152  if (Bi) delete Bi;
153  if (BsM) delete BsM;
154  if (Vm_myocyte_emi) delete Vm_myocyte_emi;
155 
156  if (operator_unique_to_both_faces) delete operator_unique_to_both_faces;
157  if (operator_both_to_unique_face) delete operator_both_to_unique_face;
158 
159  // vectors
160  if (ui) delete ui;
161  if (dui) delete dui;
162  if (ui_pre) delete ui_pre;
163  if (vb) delete vb;
164 
165  if (vb_both_face) delete vb_both_face;
166  if (vb_unique_face) delete vb_unique_face;
167  if (Ib) delete Ib;
168 
169  if (Ib_both_face) delete Ib_both_face;
170  if (Ib_unique_face) delete Ib_unique_face;
171  if (Iij_stim) delete Iij_stim;
172  if (Iij_temp) delete Iij_temp;
173  if (Irhs) delete Irhs;
174 
175  line_face.clear();
176  tri_face.clear();
177  quad_face.clear();
178  }
179 
180  void init();
181  void rebuild_matrices(MaterialType* mtype, limpet::MULTI_IF & miif, SF::vector<stimulus> & stimuli, FILE_SPEC logger);
182  void solve();
183 
184  private:
185  void setup_linear_solver(FILE_SPEC logger);
186 
187  void solve_semiImplicit();
188 };
189 
190 class EMI : public Basic_physic
191 {
192  public:
193 
195  MaterialType mtype_vol[1];
196  MaterialType_EMI mtype_face;
198  SF::vector<stimulus> stimuli;
199 
202  IonicsOnFace ion;
203 
205  parabolic_solver_emi parab_solver;
206 
208  gvec_data_OnFace gvec;
209 
211  igb_output_manager output_manager;
212 
214  phie_recovery_data phie_rcv;
215 
216  generic_timing_stats IO_stats;
217 
222 
226  EMI() : ion(emi_surface_unique_face_msh)
227  {
228  name = "EMI";
229  }
230 
239  void initialize();
240 
241  void destroy();
242 
243  // This funcs from the Basic_physic interface are currently empty
244  void compute_step();
245  void output_step();
246 
247  inline void output_timings()
248  {
249  // since ionics are included in electrics, we substract ionic timings from electric timings
250  compute_time -= ion.compute_time;
251  initialize_time -= ion.initialize_time;
252 
253  // now we call the base class timings output for Electrics
255  // and then for ionics
256  ion.output_timings();
257  }
258 
259  ~EMI()
260  {
261  };
262 
264  double timer_val(const int timer_id);
265 
267  std::string timer_unit(const int timer_id);
268 
269  private:
308  void setup_stimuli();
309 
343  void apply_current_stimulus();
344 
364  void apply_dbc_stimulus();
365 
401  void balance_electrodes();
402 
455  void scale_total_stimulus_current(SF::vector<stimulus>& stimuli,
456  sf_mat& mass_vol,
457  sf_mat& mass_surf,
458  FILE_SPEC logger);
459 
461  void setup_solvers();
462 
464  void setup_mappings();
465 
467  void setup_output();
468 
470  void dump_matrices();
471 
472  void checkpointing();
473 
475  void setup_EMI_mesh();
476 };
477 
484 void set_elec_tissue_properties_emi_volume(MaterialType* mtype, FILE_SPEC logger);
485 
487 void extract_unique_tag(SF::vector<mesh_int_t>& unique_tags);
488 
490 void compute_tags_per_rank(int num_tags, SF::vector<mesh_int_t>& num_tags_per_rank);
491 
493 void distribute_elements_based_tags(SF::meshdata<mesh_int_t, mesh_real_t>& mesh, SF::vector<mesh_int_t> & unique_tags);
494 
496 void partition_based_tags(int num_tags, SF::vector<mesh_int_t> tag, SF::vector<mesh_int_t> unique_tags, SF::vector<mesh_int_t> num_tags_per_rank, SF::vector<mesh_int_t>& part);
497 
499 void map_tags_to_rank(int size, const SF::vector<mesh_int_t> & unique_tags, const SF::vector<mesh_int_t> & num_tags_per_rank, hashmap::unordered_map<mesh_int_t, mesh_int_t> &tags_to_rank_map);
500 
503 bool load_partitions_from_file(hashmap::unordered_map<mesh_int_t, mesh_int_t>& tags_to_rank_map, MPI_Comm comm);
504 
506 void permute_mesh_locally_based_on_tag_elemIdx(SF::meshdata<mesh_int_t, mesh_real_t>& mesh);
507 
508 } // namespace opencarp
509 
510 
511 #endif
512 #endif
int mesh_int_t
Definition: SF_container.h:46
virtual void output_timings()
Definition: physics_types.h:76
Tissue level electrics, main Electrics physics class.
Electrical ionics functions (gap junction + ionic current) on the face of interface mesh for EMI mesh...
SF::abstract_linear_solver< SF_int, SF_real > sf_sol
Definition: sf_interface.h:54
@ emi_surface_unique_face_msh
Definition: sf_interface.h:68
SF::abstract_vector< SF_int, SF_real > sf_vec
Definition: sf_interface.h:50
SF::abstract_matrix< SF_int, SF_real > sf_mat
Definition: sf_interface.h:52
file_desc * FILE_SPEC
Definition: basics.h:138