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 the EMI model:
42 
43  // - `emi_surface_msh`:
44  // Contains the local one-sided interface representation used by the ionic
45  // model and surface output. Each local element corresponds to the interface
46  // side owned by this rank.
47 
48  // - `emi_surface_counter_msh`:
49  // Contains both faces and counter-faces for all membrane interfaces,
50  // regardless of whether the two sides are on the same MPI rank or on
51  // different ranks. This is the row space for the barycentric both-face
52  // vectors and for B/Bi.
53 
54  // - `emi_surface_unique_face_msh`:
55  // Contains one representative face per physical membrane/gap-junction
56  // interface. This is the vector layout used by the ionic model.
57  //
58  sf_vec* ui = nullptr;
59  sf_vec* dui = nullptr;
60  sf_vec* ui_pre = nullptr;
61  sf_vec* vb = nullptr;
62  sf_vec* vb_one_face = nullptr;
63  sf_vec* vb_both_face = nullptr;
64  sf_vec* vb_unique_face = nullptr;
65  sf_vec* Ib = nullptr;
66  sf_vec* Ib_one_face = nullptr;
67  sf_vec* Ib_both_face = nullptr;
68  sf_vec* Ib_unique_face = nullptr;
69  sf_vec* Iij_stim = nullptr;
70  sf_vec* Iij_temp = nullptr;
71  sf_vec* Irhs = nullptr;
72 
73  sf_mat* mass_emi = nullptr;
74  sf_mat* mass_surf_emi = nullptr;
75  sf_mat* lhs_emi = nullptr;
76  sf_mat* stiffness_emi = nullptr;
77  sf_mat* B = nullptr;
78  sf_mat* Bi = nullptr;
79  sf_mat* BsM = nullptr;
80 
81  // Direct transfer operators between unique-face and both-face layouts.
82  sf_mat* operator_unique_to_both_faces = nullptr;
83  sf_mat* operator_both_to_unique_face = nullptr;
84 
85 
87  std::pair<SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>,
88  SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>>> line_face;
90  std::pair<SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>,
91  SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>>> tri_face;
93  std::pair<SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>,
94  SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>>> quad_face;
95 
97  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;
98 
102 
103  hashmap::unordered_set<int> extra_tags;
104  hashmap::unordered_set<int> intra_tags;
105 
106  // Element-side markers: 1 for extracellular/membrane side, 2 for intracellular/gap-junction side.
107  SF::vector<mesh_int_t> elemTag_emi_mesh;
108  SF::vector<mesh_int_t> elemTag_surface_mesh;
109  SF::vector<mesh_int_t> elemTag_surface_w_counter_mesh;
110 
111 
112  // map via vector from emi_surfmesh_w_counter_face element indices to emi_surfmesh (both -> one)
113  SF::vector<mesh_int_t> vec_both_to_one_face;
114 
115  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;
116 
117 
118  // Direct mapping (unique -> both face indices, no intermediate)
119  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;
120 
122  // the linear solver
123  sf_sol* lin_solver = nullptr;
124 
125  // linear solver stats
126  lin_solver_stats stats;
127 
129  dbc_manager* dbc = nullptr;
130  bool phie_mat_has_nullspace = false;
131  bool fem_matrices_exact_preallocated = 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 
155  if (operator_unique_to_both_faces) delete operator_unique_to_both_faces;
156  if (operator_both_to_unique_face) delete operator_both_to_unique_face;
157 
158  // vectors
159  if (ui) delete ui;
160  if (dui) delete dui;
161  if (ui_pre) delete ui_pre;
162  if (vb) delete vb;
163 
164  if (vb_both_face) delete vb_both_face;
165  if (vb_unique_face) delete vb_unique_face;
166  if (Ib) delete Ib;
167 
168  if (Ib_both_face) delete Ib_both_face;
169  if (Ib_unique_face) delete Ib_unique_face;
170  if (Iij_stim) delete Iij_stim;
171  if (Iij_temp) delete Iij_temp;
172  if (Irhs) delete Irhs;
173 
174  line_face.clear();
175  tri_face.clear();
176  quad_face.clear();
177  }
178 
180  void init();
181 
183  void rebuild_matrices(MaterialType* mtype, limpet::MULTI_IF & miif, SF::vector<stimulus> & stimuli, FILE_SPEC logger);
184 
186  void solve();
187 
188  private:
189  void setup_linear_solver(FILE_SPEC logger);
190 
191  void solve_semiImplicit();
192 };
193 
194 class EMI : public Basic_physic
195 {
196  public:
197 
199  MaterialType mtype_vol[1];
200  MaterialType_EMI mtype_face;
202  SF::vector<stimulus> stimuli;
203 
206  IonicsOnFace ion;
207 
209  parabolic_solver_emi parab_solver;
210 
212  gvec_data_OnFace gvec;
213 
215  igb_output_manager output_manager;
216 
218  phie_recovery_data phie_rcv;
219 
220  generic_timing_stats IO_stats;
221 
226 
230  EMI() : ion(emi_surface_unique_face_msh)
231  {
232  name = "EMI";
233  }
234 
243  void initialize();
244 
245  void destroy();
246 
247  // This funcs from the Basic_physic interface are currently empty
248  void compute_step();
249  void output_step();
250 
251  inline void output_timings()
252  {
253  // since ionics are included in electrics, we subtract ionic timings from electric timings
254  compute_time -= ion.compute_time;
255  initialize_time -= ion.initialize_time;
256 
257  // now we call the base class timings output for Electrics
259  // and then for ionics
260  ion.output_timings();
261  }
262 
263  ~EMI()
264  {
265  };
266 
268  double timer_val(const int timer_id);
269 
271  std::string timer_unit(const int timer_id);
272 
273  private:
312  void setup_stimuli();
313 
346  void apply_current_stimulus();
347 
367  void apply_dbc_stimulus();
368 
404  void balance_electrodes();
405 
458  void scale_total_stimulus_current(SF::vector<stimulus>& stimuli,
459  sf_mat& mass_vol,
460  sf_mat& mass_surf,
461  FILE_SPEC logger);
462 
464  void setup_solvers();
465 
467  void setup_mappings();
468 
470  void setup_output();
471 
473  void dump_matrices();
474 
475  void checkpointing();
476 
478  void setup_EMI_mesh();
479 };
480 
488 void log_mesh_local_element_ranges(const sf_mesh& emi_mesh,
489  const sf_mesh& emi_surfmesh_w_counter_face,
490  const sf_mesh& emi_surfmesh_unique_face);
491 
493 void extract_unique_tag(SF::vector<mesh_int_t>& unique_tags);
494 
496 void compute_tags_per_rank(int num_tags, SF::vector<mesh_int_t>& num_tags_per_rank);
497 
499 void distribute_elements_based_tags(SF::meshdata<mesh_int_t, mesh_real_t>& mesh,
500  int total_num_tags);
501 
503 void partition_based_tags(int num_tags,
505  SF::vector<mesh_int_t> num_tags_per_rank,
506  SF::vector<mesh_int_t>& part);
507 
509 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);
510 
513 bool load_partitions_from_file(hashmap::unordered_map<mesh_int_t, mesh_int_t>& tags_to_rank_map,
514  int expected_num_tags,
515  MPI_Comm comm);
516 
518 void permute_mesh_locally_based_on_tag_elemIdx(SF::meshdata<mesh_int_t, mesh_real_t>& mesh);
519 
520 } // namespace opencarp
521 
522 
523 #endif
524 #endif
opencarp::local_index_t mesh_int_t
Definition: SF_container.h:46
virtual void output_timings()
Definition: physics_types.h:76
Tissue level electrics, main Electrics physics class.
LIMPET ionics and gap-junction models on the EMI unique-face interface mesh.
SF::abstract_linear_solver< SF_int, SF_real > sf_sol
Definition: sf_interface.h:54
SF::meshdata< mesh_int_t, mesh_real_t > sf_mesh
Definition: sf_interface.h:48
@ 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:140