openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
emi.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
2 // SPDX-License-Identifier: Apache-2.0
3 
11 #if WITH_EMI_MODEL
12 
13 #ifndef _EMI_H
14 #define _EMI_H
15 
16 #include "electrics.h"
17 #include "ionicsOnFace.h"
18 
19 namespace opencarp {
20 
21 class parabolic_solver_emi
22 {
23  public:
24 
25  // Has to be kept in line with param_globals::parab_solve
26  enum parabolic_t {SEMI_IMPLICIT = 0};
27  //
28  // There are three surface meshes in the EMI model:
29 
30  // - `emi_surface_msh`:
31  // Contains the local one-sided interface representation used by the ionic
32  // model and surface output. Each local element corresponds to the interface
33  // side owned by this rank.
34 
35  // - `emi_surface_counter_msh`:
36  // Contains both faces and counter-faces for all membrane interfaces,
37  // regardless of whether the two sides are on the same MPI rank or on
38  // different ranks. This is the row space for the barycentric both-face
39  // vectors and for B/Bi.
40 
41  // - `emi_surface_unique_face_msh`:
42  // Contains one representative face per physical membrane/gap-junction
43  // interface. This is the vector layout used by the ionic model.
44  //
45  sf_vec* ui = nullptr;
46  sf_vec* dui = nullptr;
47  sf_vec* ui_pre = nullptr;
48  sf_vec* vb = nullptr;
49  sf_vec* vb_one_face = nullptr;
50  sf_vec* vb_both_face = nullptr;
51  sf_vec* vb_unique_face = nullptr;
52  sf_vec* Ib = nullptr;
53  sf_vec* Ib_one_face = nullptr;
54  sf_vec* Ib_both_face = nullptr;
55  sf_vec* Ib_unique_face = nullptr;
56  sf_vec* Iij_stim = nullptr;
57  sf_vec* Iij_temp = nullptr;
58  sf_vec* Irhs = nullptr;
59 
60  sf_mat* mass_emi = nullptr;
61  sf_mat* mass_surf_emi = nullptr;
62  sf_mat* lhs_emi = nullptr;
63  sf_mat* stiffness_emi = nullptr;
64  sf_mat* B = nullptr;
65  sf_mat* Bi = nullptr;
66  sf_mat* BsM = nullptr;
67 
68  // Direct transfer operators between unique-face and both-face layouts.
69  sf_mat* operator_unique_to_both_faces = nullptr;
70  sf_mat* operator_both_to_unique_face = nullptr;
71 
72 
74  std::pair<SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>,
75  SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>>> line_face;
77  std::pair<SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>,
78  SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>>> tri_face;
80  std::pair<SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>,
81  SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>>> quad_face;
82 
84  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;
85 
89 
90  hashmap::unordered_set<int> extra_tags;
91  hashmap::unordered_set<int> intra_tags;
92 
93  // Element-side markers: 1 for extracellular/membrane side, 2 for intracellular/gap-junction side.
94  SF::vector<mesh_int_t> elemTag_emi_mesh;
95  SF::vector<mesh_int_t> elemTag_surface_mesh;
96  SF::vector<mesh_int_t> elemTag_surface_w_counter_mesh;
97 
98 
99  // map via vector from emi_surfmesh_w_counter_face element indices to emi_surfmesh (both -> one)
100  SF::vector<mesh_int_t> vec_both_to_one_face;
101 
102  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;
103 
104 
105  // Direct mapping (unique -> both face indices, no intermediate)
106  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;
107 
109  // the linear solver
110  sf_sol* lin_solver = nullptr;
111 
112  // linear solver stats
113  lin_solver_stats stats;
114 
116  dbc_manager* dbc = nullptr;
117  bool phie_mat_has_nullspace = false;
118  bool fem_matrices_exact_preallocated = false;
119 
120  // solver config
121  double tol = 1e-8;
122  int max_it = 100;
123  parabolic_t parab_tech = SEMI_IMPLICIT;
124 
125  // solver statistics
126  double final_residual = -1.0;
127  int niter = -1;
128 
129  ~parabolic_solver_emi()
130  {
131  if(dbc) delete dbc;
132  if (lin_solver) delete lin_solver;
133  // matrices
134  if (mass_emi) delete mass_emi;
135  if (mass_surf_emi) delete mass_surf_emi;
136  if (lhs_emi) delete lhs_emi;
137  if (stiffness_emi) delete stiffness_emi;
138  if (B) delete B;
139  if (Bi) delete Bi;
140  if (BsM) delete BsM;
141 
142  if (operator_unique_to_both_faces) delete operator_unique_to_both_faces;
143  if (operator_both_to_unique_face) delete operator_both_to_unique_face;
144 
145  // vectors
146  if (ui) delete ui;
147  if (dui) delete dui;
148  if (ui_pre) delete ui_pre;
149  if (vb) delete vb;
150 
151  if (vb_both_face) delete vb_both_face;
152  if (vb_unique_face) delete vb_unique_face;
153  if (Ib) delete Ib;
154 
155  if (Ib_both_face) delete Ib_both_face;
156  if (Ib_unique_face) delete Ib_unique_face;
157  if (Iij_stim) delete Iij_stim;
158  if (Iij_temp) delete Iij_temp;
159  if (Irhs) delete Irhs;
160 
161  line_face.clear();
162  tri_face.clear();
163  quad_face.clear();
164  }
165 
167  void init();
168 
170  void rebuild_matrices(MaterialType* mtype, limpet::MULTI_IF & miif, SF::vector<stimulus> & stimuli, FILE_SPEC logger);
171 
173  void solve();
174 
175  private:
176  void setup_linear_solver(FILE_SPEC logger);
177 
178  void solve_semiImplicit();
179 };
180 
181 class EMI : public Basic_physic
182 {
183  public:
184 
186  MaterialType mtype_vol[1];
187  MaterialType_EMI mtype_face;
189  SF::vector<stimulus> stimuli;
190 
193  IonicsOnFace ion;
194 
196  parabolic_solver_emi parab_solver;
197 
199  gvec_data_OnFace gvec;
200 
202  igb_output_manager output_manager;
203 
205  hashmap::unordered_set<int> output_tags;
206  SF::vector<mesh_int_t> phie_output_idx;
207  SF::vector<mesh_int_t> vm_output_idx;
208 
210  phie_recovery_data phie_rcv;
211 
212  generic_timing_stats IO_stats;
213 
218 
222  EMI() : ion(emi_surface_unique_face_msh)
223  {
224  name = "EMI";
225  }
226 
235  void initialize();
236 
237  void destroy();
238 
239  // This funcs from the Basic_physic interface are currently empty
240  void compute_step();
241  void output_step();
242 
243  inline void output_timings()
244  {
245  // since ionics are included in electrics, we subtract ionic timings from electric timings
246  compute_time -= ion.compute_time;
247  initialize_time -= ion.initialize_time;
248 
249  // now we call the base class timings output for Electrics
251  // and then for ionics
252  ion.output_timings();
253  }
254 
255  ~EMI()
256  {
257  };
258 
260  double timer_val(const int timer_id);
261 
263  std::string timer_unit(const int timer_id);
264 
265  private:
304  void setup_stimuli();
305 
338  void apply_current_stimulus();
339 
359  void apply_dbc_stimulus();
360 
396  void balance_electrodes();
397 
450  void scale_total_stimulus_current(SF::vector<stimulus>& stimuli,
451  sf_mat& mass_vol,
452  sf_mat& mass_surf,
453  FILE_SPEC logger);
454 
456  void setup_solvers();
457 
459  void setup_mappings();
460 
462  void setup_output();
463 
465  void dump_matrices();
466 
467  void checkpointing();
468 
471  void dump_field_state(const char* roe_fnm);
472 
475  void restore_field_state(const char* roe_fnm);
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:31
virtual void output_timings()
Definition: physics_types.h:61
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:39
SF::meshdata< mesh_int_t, mesh_real_t > sf_mesh
Definition: sf_interface.h:33
@ emi_surface_unique_face_msh
Definition: sf_interface.h:53
SF::abstract_vector< SF_int, SF_real > sf_vec
Definition: sf_interface.h:35
SF::abstract_matrix< SF_int, SF_real > sf_mat
Definition: sf_interface.h:37
file_desc * FILE_SPEC
Definition: basics.h:125