openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
ionicsOnFace.cc
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
2 // SPDX-License-Identifier: LicenseRef-APL-1.1
3 
12 #if WITH_EMI_MODEL
13 #include "ionicsOnFace.h"
14 
15 #include "SF_init.h"
16 
17 namespace opencarp {
18 
19 namespace {
20 
21 // Region 0 and 1 are default EMI face regions and are valid when an ionic
22 // model is assigned. User-defined regions 2+ must additionally provide at
23 // least one face-tag pair.
24 bool imp_region_emi_is_assigned(const IMPregion_EMI& region, int idx)
25 {
26  if (!(region.im && strlen(region.im) > 0)) return false;
27  if (idx < 2) return true;
28  return region.num_IDs > 0;
29 }
30 
31 // Ignore trailing unassigned imp_region_emi entries. This lets users keep the
32 // generated default parameter count while only assigning the regions they use.
33 int effective_num_imp_regions_emi()
34 {
35  const int configured_nreg = param_globals::num_imp_regions;
36  int nreg = configured_nreg;
37  while (nreg > 2 && !imp_region_emi_is_assigned(param_globals::imp_region_emi[nreg - 1], nreg - 1)) {
38  --nreg;
39  }
40 
41  if (nreg < configured_nreg) {
42  static bool warned_once = false;
43  int rank = 0;
44  MPI_Comm_rank(SF_COMM, &rank);
45  if (!warned_once && rank == 0) {
46  log_msg(NULL, 1, ECHO,
47  "Warning: num_imp_regions=%d but only %d imp_region_emi entries are assigned; ignoring trailing unassigned regions.\n",
48  configured_nreg, nreg);
49  warned_once = true;
50  }
51  }
52 
53  return nreg;
54 }
55 
56 } // namespace
57 
58 void IonicsOnFace::compute_step()
59 {
60  double t1, t2;
61  get_time(t1);
62 
63  miif->compute_ionic_current();
64 
65  double comp_time = timing(t2, t1);
66  this->compute_time += comp_time;
67 
68  comp_stats.calls++;
69  comp_stats.tot_time += comp_time;
70 
72  comp_stats.log_stats(user_globals::tm_manager->time, false);
73 }
74 
75 void IonicsOnFace::destroy()
76 {
77  miif->free_MIIF();
78 }
79 
80 void IonicsOnFace::output_step()
81 {}
82 
83 void IonicsOnFace::initialize()
84 {
85  double t1, t2;
86  get_time(t1);
87 
88  // imp_region_nodefile assigns regions per mesh node, while EMI assigns them per
89  // interface face, so the override is not supported here
90  if(strlen(param_globals::imp_region_nodefile)) {
91  log_msg(0, 5, 0, "%s error: imp_region_nodefile is not supported with the EMI model. Aborting!",
92  __func__);
93  EXIT(1);
94  }
95 
96  set_dir(OUTPUT);
97 
98  // initialize generic logger for ODE timings per time_dt
99  comp_stats.init_logger("ODE_stats.dat");
100 
101  double tstart = 0.;
102  sf_mesh & mesh = get_mesh(ion_domain);
103  limpet::node_count_t loc_size = mesh.l_numelem;
104 
105  // create ionic current vector and register it
106  sf_vec *IIon;
107  sf_vec *Vmv;
108  SF::init_vector(&IIon, mesh, 1, sf_vec::elemwise);
109  SF::init_vector(&Vmv, mesh, 1, sf_vec::elemwise);
112 
113  // setup miif
114  miif = new limpet::MULTI_IF();
115  // hand down the physics log file, so that what MULTI_IF reports while setting up and
116  // restoring state is recorded rather than only echoed to the console
117  miif->logger = logger;
118  // store IIF_IDs and Plugins in arrays
119  miif->name = "myocardium";
120  miif->gdata[limpet::Vm] = Vmv;
121  miif->gdata[limpet::Iion] = IIon;
122 
123  const int num_imp_regions = effective_num_imp_regions_emi();
124  SF::vector<RegionSpecs_EMI> rs(num_imp_regions);
125 
126  if (rs.size() <2) {
127  log_msg(NULL, 5, ECHO, "\tError: num_imp_regions must be at least 2: the first region is reserved for the ionic model, and the second is designated for the gap junction.\n");
128  log_msg(NULL, 5, ECHO, "set a default ionic model and gapjuntion model in parameter file\\n");
129  exit(1);
130  }
131 
132  // Region 0 and 1 are implicit defaults:
133  // - region 0: default membrane faces
134  // - region 1: default gap-junction faces
135  rs[0].nsubregs = 0;
136  rs[0].subregtags = nullptr;
137  rs[1].nsubregs = 0;
138  rs[1].subregtags = nullptr;
139 
140  // imp_region_emi[2+] assign models to specific face tag pairs like "t1:t2"
141  for (size_t i = 2; i < rs.size(); i++ ) {
142  rs[i].nsubregs = param_globals::imp_region_emi[i].num_IDs;
143  rs[i].subregtags = new std::string[rs[i].nsubregs]; // Allocate memory for the string array
144  for (int j = 0; j < rs[i].nsubregs;j++) {
145  std::string t1t2 = param_globals::imp_region_emi[i].ID[j];
146  size_t colon_pos = t1t2.find(':');
147  if (colon_pos == std::string::npos) {
148  std::cerr << "Error: ':' the face tags are not defined properly in input file, it should be tag1:tag2 as a string" << std::endl;
149  exit(1);
150  }
151 
152  rs[i].subregtags[j] = param_globals::imp_region_emi[i].ID[j]; // Convert char* to std::string.
153  }
154  }
155 
156  SF::vector<int> reg_mask;
157  region_mask_onFace(ion_domain, tags_data, line_face, tri_face, quad_face,
158  map_vertex_tag_to_dof, map_elem_uniqueFace_to_tags, intra_tags,
159  rs, reg_mask, true, "imp_region_emi");
160 
161  for (size_t i = 0; i < rs.size(); i++ ) {
162  delete[] rs[i].subregtags;
163  rs[i].subregtags = nullptr;
164  }
165 
166  int purkfLen = 1;
167  tstart = setup_MIIF(loc_size, num_imp_regions, param_globals::imp_region_emi,
168  reg_mask.data(), param_globals::start_statef, param_globals::num_adjustments,
169  param_globals::adjustment, param_globals::dt, purkfLen > 0);
170 
171  miif->extUpdateVm = !param_globals::operator_splitting;
172 
173  // if we start at a non-zero time (i.e. we have restored a state), we notify the
174  // timer manager
175  if(tstart > 0.) {
176  log_msg(logger, 0, 0, "Changing simulation start time to %.2lf", tstart);
177  user_globals::tm_manager->setup(param_globals::dt, tstart, param_globals::tend);
179  }
180 
181  set_dir(INPUT);
182 
183  this->initialize_time += timing(t2, t1);
184 }
185 
186 double IonicsOnFace::setup_MIIF(limpet::node_count_t nnodes, int nreg, IMPregion_EMI* impreg, int* mask,
187  const char *start_fn, int numadjust, IMPVariableAdjustment *adjust,
188  double time_step, bool close)
189 {
190  double tstart = 0;
191 
192  miif->N_IIF = nreg;
193  miif->numNode = nnodes;
194  miif->iontypes = {};
195  miif->numplugs = (int*)calloc( miif->N_IIF, sizeof(int));
196  miif->plugtypes = std::vector<limpet::IonTypeList>(miif->N_IIF);
197  miif->targets = std::vector<limpet::Target>(miif->N_IIF, limpet::Target::AUTO);
198 
199  log_msg(logger,0,ECHO, "\nSetting up ionic models on EMI Face and plugins\n" \
200  "-----------------------------------\n\n" \
201  "Assigning IMPS to tagged regions:" );
202 
203  for (int i=0;i<miif->N_IIF;i++) {
204  auto pT = limpet::get_ion_type(std::string(impreg[i].im));
205  if (pT != NULL)
206  {
207  miif->iontypes.push_back(*pT);
208  log_msg(logger, 0, ECHO|NONL, "\tIonic model: %s to tag region(s)", impreg[i].im);
209 
210  if(impreg[i].num_IDs > 0) {
211  for(int j = 0; j < impreg[i].num_IDs; j++)
212  log_msg(logger,0,ECHO|NONL, " [%s],", impreg[i].ID[j]);
213  log_msg(logger,0,ECHO,"\b.");
214  }
215  else {
216  log_msg(logger,0,ECHO, " [0] (implicitely)");
217  }
218  }
219  else {
220  log_msg(NULL,5,ECHO, "Illegal IM specified: %s\n", impreg[i].im );
221  log_msg(NULL,5,ECHO, "Run bench --list-imps for a list of all available models.\n" );
222  EXIT(1);
223  }
224  if (limpet::get_plug_flag( impreg[i].plugins, &miif->numplugs[i], miif->plugtypes[i]))
225  {
226  if(impreg[i].plugins[0] != '\0') {
227  log_msg(logger,0, ECHO|NONL, "\tPlug-in(s) : %s to tag region(s)", impreg[i].plugins);
228 
229  for(int j = 0; j < impreg[i].num_IDs; j++)
230  log_msg(logger,0,ECHO|NONL, " [%d],", impreg[i].ID[j]);
231  log_msg(logger,0,ECHO,"\b.");
232  }
233  }
234  else {
235  log_msg(NULL,5,ECHO,"Illegal plugin specified: %s\n", impreg[i].plugins);
236  log_msg(NULL,5,ECHO, "Run bench --list-imps for a list of all available plugins.\n" );
237  EXIT(1);
238  }
239  }
240 
241  miif->IIFmask = (limpet::IIF_Mask_t*)calloc(miif->numNode, sizeof(limpet::IIF_Mask_t));
242 
243  // The mask is element-wise on the EMI unique-face mesh: one ionic/gap-junction
244  // region id per local unique-face element.
245  if (mask) {
246  for (limpet::node_index_t i=0; i<miif->numNode; i++)
247  miif->IIFmask[i] = (limpet::IIF_Mask_t) mask[i];
248  }
249 
250  miif->initialize_MIIF();
251 
252  for (int i=0;i<miif->N_IIF;i++) {
253  // the IMP tuning does not handle spaces well, thus we remove them here
254  remove_char(impreg[i].im_param, strlen(impreg[i].im_param), ' ');
255  miif->IIF[i]->tune(impreg[i].im_param, impreg[i].plugins, impreg[i].plug_param);
256  }
257 
258  set_dir(INPUT);
259  miif->initialize_currents(time_step, param_globals::ode_fac);
260 
261  // overriding initial values goes here
262  // read in single cell state vector and spread it out over the entire region
263  set_dir(INPUT);
264  for (int i=0;i<miif->N_IIF;i++) {
265  if (impreg[i].im_sv_init && strlen(impreg[i].im_sv_init) > 0)
266  if (read_sv(miif, i, impreg[i].im_sv_init)) {
267  log_msg(NULL, 5, ECHO|FLUSH, "State vector initialization failed for %s.\n", impreg[i].name);
268  EXIT(-1);
269  }
270  }
271 
272  if( !start_fn || strlen(start_fn)>0 )
273  tstart = (double) miif->restore_state(start_fn, ion_domain, close);
274 
275  for (int i=0; i<numadjust; i++)
276  {
277  set_dir(INPUT);
278 
279  SF::vector<SF_int> indices;
280  SF::vector<double> values;
281  bool restrict_to_algebraic = true;
282 
283  sf_mesh & imesh = get_mesh(ion_domain);
284  std::map<std::string,std::string> metadata;
285  read_metadata(adjust[i].file, metadata, SF_COMM);
286 
287  SF::SF_nbr nbr = SF::NBR_REF;
288  if(metadata.count("grid") && metadata["grid"].compare("intra") == 0) {
289  nbr = SF::NBR_SUBMESH;
290  }
291  read_indices_with_data(indices, values, adjust[i].file, imesh, nbr, restrict_to_algebraic, 1, SF_COMM);
292 
293  int rank = get_rank();
294 
295  for(size_t gi = 0; gi < indices.size(); gi++)
296  indices[gi] = SF::local_nodal_to_local_petsc<mesh_int_t, mesh_real_t>(imesh, rank, indices[gi]);
297 
298  // debug, output parameters on global intracellular vector
299  if(adjust[i].dump) {
300  sf_vec* adjPars;
301  SF::init_vector(&adjPars, imesh, 1, sf_vec::algebraic);
302  adjPars->set(indices, values, false, true);
303 
304  set_dir(OUTPUT);
305  char fname[2085];
306  snprintf(fname, sizeof fname, "adj_%s_perm.dat", adjust[i].variable);
307  adjPars->write_ascii(fname, false);
308 
309  // get the scattering to the canonical permutation
310  SF::scattering* sc = get_permutation(ion_domain, PETSC_TO_CANONICAL, 1);
311  if(sc == NULL) {
312  log_msg(0,3,0, "%s warning: PETSC_TO_CANONICAL permutation needed registering!", __func__);
313  sc = register_permutation(ion_domain, PETSC_TO_CANONICAL, 1);
314  }
315 
316  (*sc)(*adjPars, true);
317  snprintf(fname, sizeof fname, "adj_%s_canonical.dat", adjust[i].variable);
318  adjPars->write_ascii(fname, false);
319  }
320 
321  int nc = miif->adjust_MIIF_variables(adjust[i].variable, indices, values);
322  log_msg(logger, 0, 0, "Adjusted %d values for %s", nc, adjust[i].variable);
323  }
324 
325  return tstart;
326 }
327 
338 bool check_tags_in_elems_onFace(std::vector<std::string> & tags_data, SF::vector<RegionSpecs_EMI> & regspec,
339  const char* gridname, const char* reglist)
340 {
341  bool AllTagsExist = true;
343 
344  tagset.insert(tags_data.begin(), tags_data.end());
345 
346  // cycle through all user-specified regions
347  for (size_t reg=0; reg<regspec.size(); reg++)
348  // cycle through all tags which belong to the region
349  for (int k=0; k<regspec[reg].nsubregs; k++) {
350  // check whether this tag exists in element list
351  int n = 0;
352  size_t colon_pos = regspec[reg].subregtags[k].find(':');
353  int tag1 = std::stoi(regspec[reg].subregtags[k].substr(0, colon_pos));
354  int tag2 = std::stoi(regspec[reg].subregtags[k].substr(colon_pos + 1));
355 
356  // considered the other combinations of tags
357  std::string inverse_pair;
358  inverse_pair = std::to_string(tag2) + ":" + std::to_string(tag1);
359  if(tagset.count(regspec[reg].subregtags[k]) || tagset.count(inverse_pair)) {
360  n++;
361  }
362 
363  // globalize n
364  int N = get_global(n, MPI_SUM);
365 
366  if (N==0) {
367  log_msg(NULL, 3, ECHO,
368  "on face Region tag %s in %s[%d] not found in element list for %s grid.\n",
369  regspec[reg].subregtags[k].c_str(), reglist, reg, gridname);
370  AllTagsExist = false;
371  }
372  }
373 
374  if (!AllTagsExist) {
375  log_msg(NULL, 4, ECHO,"Assigned wrong pair of tags on the face of EMI surface mesh!\n"
376  "Check region ID specs in input files!\n");
377  }
378 
379  return AllTagsExist;
380 }
381 
382 inline bool pair_is_gapjunction(const std::pair<mesh_int_t, mesh_int_t>& tags,
383  const hashmap::unordered_set<int>& intra_tags)
384 {
385  return intra_tags.find(tags.first) != intra_tags.end() &&
386  intra_tags.find(tags.second) != intra_tags.end();
387 }
388 
389 void region_mask_onFace(mesh_t meshspec,
390  std::vector<std::string> & tags_data,
392  std::pair<SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>,
393  SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>>> & line_face,
395  std::pair<SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>,
396  SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>>> & tri_face,
398  std::pair<SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>,
399  SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>>> & quad_face,
400  hashmap::unordered_map<std::pair<mesh_int_t,mesh_int_t>, mesh_int_t> & map_vertex_tag_to_dof,
401  hashmap::unordered_map<mesh_int_t, std::pair<mesh_int_t, mesh_int_t>> & map_elem_uniqueFace_to_tags,
402  const hashmap::unordered_set<int> & intra_tags,
403  SF::vector<RegionSpecs_EMI> & regspec,
404  SF::vector<int> & regionIDs, bool mask_elem, const char* reglist)
405 {
406  if(regspec.size() == 1) return;
407 
408  sf_mesh & mesh = get_mesh(meshspec);
410 
411  // Initialize the list with the default membrane region id, 0.
412  size_t rIDsize = mask_elem ? mesh.l_numelem : mesh.l_numpts;
413 
414  size_t nelem = mesh.l_numelem;
415 
416  // Check whether all specified face-tag pairs exist in the unique-face list.
417  check_tags_in_elems_onFace(tags_data, regspec, mesh.name.c_str(), reglist);
418 
419  regionIDs.assign(rIDsize, 0);
420 
421  int* rid = regionIDs.data();
423 
424  // Generate a map from face-tag pair strings to region IDs. This also lets us
425  // detect duplicate assignments and simplifies the regionIDs fill loop.
426  for (size_t reg=0; reg < regspec.size(); reg++) {
427  int err = 0;
428  for (int k=0; k < regspec[reg].nsubregs; k++) {
429  std::string curtag = regspec[reg].subregtags[k];
430  if(tag_to_reg.count(curtag)) err++;
431 
432  if(get_global(err, MPI_SUM))
433  log_msg(0,4,0, "%s warning: Tag idx %s is assigned to multiple regions!\n"
434  "Its final assignment will be to the highest assigned region ID!",
435  __func__, curtag.c_str());
436 
437  tag_to_reg[curtag] = reg;
438  }
439  }
440 
441  std::vector<int> mx_tag( rIDsize, -1 );
442 
443  // Cycle through unique-face elements and assign default or user-defined regions.
444  for(size_t eidx=0; eidx<nelem; eidx++)
445  {
446  std::vector<int> elem_nodes;
447  mesh_int_t tag = mesh.tag[eidx];
448  std::string result_pair_orginal;
449  std::string result_pair_reverse;
450  std::pair<mesh_int_t,mesh_int_t> value = map_elem_uniqueFace_to_tags[eidx];
451  result_pair_orginal = std::to_string(value.first) + ":" + std::to_string(value.second);
452  result_pair_reverse = std::to_string(value.second) + ":" + std::to_string(value.first);
453  rid[eidx] = pair_is_gapjunction(value, intra_tags) ? 1 : 0;
454 
455  if (tag_to_reg.count(result_pair_orginal)) {
456  rid[eidx] = tag_to_reg[result_pair_orginal];
457  } else if(tag_to_reg.count(result_pair_reverse)){
458  rid[eidx] = tag_to_reg[result_pair_reverse];
459  }
460  if(tag!=value.first){
461  log_msg(NULL, 5, ECHO, "error in tags on surface mesh with the unique faces!!!.\\n");
462  exit(1);
463  }
464  }
465 }
466 
469 double IonicsOnFace::timer_val(const int timer_id)
470 {
471  double val = std::nan("NaN");
472  return val;
473 }
474 
477 std::string IonicsOnFace::timer_unit(const int timer_id)
478 {
479  std::string s_unit;
480  return s_unit;
481 }
482 
483 void compute_IIF_OnFace(limpet::IonIfBase& pIF, limpet::GlobalData_t** impdata, limpet::node_index_t n)
484 {
485  if (impdata[limpet::Iion] != NULL)
486  impdata[limpet::Iion][n] = 0;
487 
488  pIF.for_each([&](limpet::IonIfBase& imp) {
489  update_ts(&imp.get_tstp());
490  imp.compute(n, n + 1, impdata);
491  });
492 }
493 
506 void* find_SV_in_IMP_onFace(limpet::MULTI_IF* miif, const int idx, const char *IMP, const char *SV,
507  int* offset, int* sz, int* plugin_idx)
508 {
509  *plugin_idx = -1;
510 
511  if(strcmp(IMP, miif->iontypes[idx].get().get_name().c_str()) == 0) {
512  return (void*) miif->iontypes[idx].get().get_sv_offset(SV, offset, sz);
513  }
514  else {
515  for( int k=0; k<miif->numplugs[idx]; k++ )
516  if(strcmp(IMP, miif->plugtypes[idx][k].get().get_name().c_str()) == 0) {
517  *plugin_idx = k;
518  return (void*) miif->plugtypes[idx][k].get().get_sv_offset(SV, offset, sz);
519  }
520  }
521 
522  return NULL;
523 }
524 
525 
536 void alloc_gvec_data_onFace(const int nGVcs, const int nRegs, const int nEmiRegs,
537  GVecs *prmGVecs, gvec_data_OnFace &glob_vecs)
538 {
539  glob_vecs.nRegs = nRegs;
540 
541  if (nGVcs) {
542  glob_vecs.vecs.resize(nGVcs);
543 
544  for (size_t i = 0; i < glob_vecs.vecs.size(); i++) {
545  sv_data_onFace &gvec = glob_vecs.vecs[i];
546 
547  gvec.name = dupstr(prmGVecs[i].name);
548  gvec.units = dupstr(prmGVecs[i].units);
549  gvec.bogus = prmGVecs[i].bogus;
550 
551  gvec.imps = (char**) calloc(nRegs, sizeof(char *));
552  gvec.svNames = (char**) calloc(nRegs, sizeof(char *));
553  gvec.svSizes = (int*) calloc(nRegs, sizeof(int));
554  gvec.svOff = (int*) calloc(nRegs, sizeof(int));
555  gvec.plugin_idx = (int*) calloc(nRegs, sizeof(int));
556  gvec.getsv = (void**) calloc(nRegs, sizeof(limpet::SVgetfcn));
557 
558  for (int j = 0; j < nRegs; j++) {
559  gvec.plugin_idx[j] = -1;
560  if (strlen(prmGVecs[i].imp)) gvec.imps[j] = dupstr(prmGVecs[i].imp);
561 #ifdef WITH_PURK
562  else if (j < nEmiRegs)
563  gvec.imps[j] = dupstr(param_globals::imp_region_emi[j].im);
564  else
565  gvec.imps[j] = dupstr(param_globals::PurkIon[j - nEmiRegs].im);
566 #else
567  else if (j < nEmiRegs)
568  gvec.imps[j] = dupstr(param_globals::imp_region_emi[j].im);
569 #endif
570  gvec.svNames[j] = dupstr(prmGVecs[i].ID[j]);
571  }
572  }
573  }
574 }
575 
588 void init_sv_gvec_onFace(gvec_data_OnFace& GVs, limpet::MULTI_IF* miif, sf_vec & tmpl,
589  igb_output_manager & output_manager,
590  const SF::vector<mesh_int_t>* gvec_output_idx)
591 {
592  GVs.inclPS = false;
593  // int num_purk_regions = GVs->inclPS ? purk->ion.N_IIF : 0;
594  int num_purk_regions = 0;
595  int nEmiRegs = effective_num_imp_regions_emi();
596  int nRegs = nEmiRegs + num_purk_regions;
597 
598  alloc_gvec_data_onFace(param_globals::num_gvecs, nRegs, nEmiRegs, param_globals::gvec, GVs);
599 
600 #ifdef WITH_PURK
601  if (GVs->inclPS) sample_PS_ionSVs(purk);
602 #endif
603 
604  for (unsigned int i = 0; i < GVs.vecs.size(); i++) {
605  sv_data_onFace & gv = GVs.vecs[i];
606  int noSV = 0;
607 
608  for (int j = 0; j < miif->N_IIF; j++) {
609  gv.getsv[j] = find_SV_in_IMP_onFace(miif, j, gv.imps[j], gv.svNames[j],
610  gv.svOff + j, gv.svSizes + j, gv.plugin_idx + j);
611 
612  if (gv.getsv[j] == NULL) {
613  log_msg(NULL, 3, ECHO, "\tWarning: SV(%s) not found in region %d\n", gv.svNames[j], j);
614  noSV++;
615  }
616  }
617 
618  SF::init_vector(&gv.ordered, &tmpl);
619  // one value per unique membrane face, same layout and restriction as vm
620  output_manager.register_output(gv.ordered, emi_surface_unique_face_msh, 1,
621  gv.name, gv.units, gvec_output_idx, true);
622 
623 #ifdef WITH_PURK
624  // same procedure for Purkinje
625  if (GVs->inclPS) {
626  IF_PURK_PROC(purk) {
627  MULTI_IF* pmiif = &purk->ion;
628  for (int j = miif->N_IIF; j < nRegs; j++) {
629  gv.getsv[j] = find_SV_in_IMP_onFace(pmiif, j - miif->N_IIF, gv.imps[j],
630  gv.svNames[j], gv.svOff + j, gv.svSizes + j, gv.plugin_idx + j);
631 
632  if (gv.getsv[j] == NULL) {
633  LOG_MSG(NULL, 3, ECHO, "\tWarning: state variable \"%s\" not found in region %d\n", gv.svNames[j], j);
634  noSV++;
635  }
636  }
637  RVector_dup(purk->vm_pt, &gv.orderedPS_PS);
638  MYO_COMM(purk);
639  }
640  RVector_dup(purk->vm_pt_over, &gv.orderedPS);
641  initialize_grid_output(grid, NULL, tmo, intra_elec_msh, GRID_WRITE, 0., 1., gv.units, gv.orderedPS,
642  1, gv.GVcName, -purk->npt, param_globals::output_level);
643  }
644 #endif
645 
646  if (noSV == nRegs) {
647  log_msg(NULL, 5, ECHO, "\tError: no state variables found for global vector %d\n", i);
648  log_msg(NULL, 5, ECHO, "Run bench --imp=YourModel --imp-info to get a list of all parameters.\\n");
649  exit(1);
650  }
651  }
652 }
653 
663 void assemble_sv_gvec_onFace(gvec_data_OnFace & gvecs, limpet::MULTI_IF *miif)
664 {
665  for(size_t i=0; i<gvecs.vecs.size(); i++ ) {
666  sv_data_onFace & gv = gvecs.vecs[i];
667 
668  // Set to the default value.
669  gv.ordered->set(gv.bogus);
670  SF_int start, stop;
671  gv.ordered->get_ownership_range(start, stop);
672 
673  for( int n = 0; n<miif->N_IIF; n++ ) {
674  if( !gv.getsv[n] ) continue;
675 
676  SF::vector<SF_real> data (miif->N_Nodes[n]);
677  SF::vector<SF_int> indices(miif->N_Nodes[n]);
678 
679  limpet::IonIfBase* base_im = miif->IIF[n];
680  int plugin_idx = gv.plugin_idx[n];
681  limpet::IonIfBase* imp = plugin_idx > -1 ? base_im->plugins()[plugin_idx] : base_im;
682 
683  for( limpet::node_index_t j=0; j<miif->N_Nodes[n]; j++ ) {
684  indices[j] = miif->NodeLists[n][j] + start;
685  data[j] = ((limpet::SVgetfcn)(gv.getsv[n]))( *imp, j, gv.svOff[n]);
686  }
687 
688  bool add = false;
689  gv.ordered->set(indices, data, add);
690  }
691 
692 #ifdef WITH_PURK
693  // include purkinje in sv dump, if exists
694  if(gvecs->inclPS) {
695  RVector_set( gv.orderedPS, gv.bogus ); // set this to the default value
696 
697  if(IS_PURK_PROC(purk))
698  {
699  MULTI_IF *pmiif = &purk->ion;
700  Real *data = new Real[purk->gvec_cab.nitems];
701 
702  int ci=0;
703  for( int n = 0; n<pmiif->N_IIF; n++ ) {
704  int gvidx = n+miif->N_IIF;
705  if( !gv.getsv[gvidx] ) continue;
706  for( int j=0; j<purk->gvec_ion[n].nitems; j++ )
707  data[ci++] = ((SVgetfcn)(gv.getsv[gvidx]))( pmiif->IIF+n, ((int*)(purk->gvec_ion[n].data))[j],
708  gv.svOff[gvidx]);
709  }
710  RVector_setvals(gv.orderedPS, purk->gvec_cab.nitems, (int*)purk->gvec_cab.data, data, true);
711  delete [] data;
712  }
713  RVector_sync( gv.orderedPS );
714  }
715 #endif
716  }
717 }
718 
719 
720 } // namespace opencarp
721 #endif
double Real
Definition: DataTypes.h:13
opencarp::local_index_t mesh_int_t
Definition: SF_container.h:31
#define SF_COMM
the default SlimFem MPI communicator
Definition: SF_globals.h:25
opencarp::global_index_t SF_int
Global algebraic index type.
Definition: SF_globals.h:29
#define EXIT(A)
finalize the backend, if it runs, and exit
Definition: backend.h:23
#define FLUSH
Definition: basics.h:305
#define ECHO
Definition: basics.h:302
#define NONL
Definition: basics.h:306
Comfort class. Provides getter functions to access the mesh member variables more comfortably.
Definition: SF_fem_utils.h:689
Redistributes vector data from indexing "a" to indexing "b".
A vector storing arbitrary data.
Definition: SF_vector.h:28
size_t size() const
The current size of the vector.
Definition: SF_vector.h:89
void assign(InputIterator s, InputIterator e)
Assign a memory range.
Definition: SF_vector.h:146
T * data()
Pointer to the vector's start.
Definition: SF_vector.h:76
hm_int count(const K &key) const
Check if key exists.
Definition: hashmap.hpp:612
Custom unordered_set implementation.
Definition: hashmap.hpp:739
iterator find(const K &key)
Definition: hashmap.hpp:1081
hm_int count(const K &key) const
Definition: hashmap.hpp:1067
void insert(InputIterator first, InputIterator last)
Definition: hashmap.hpp:1037
Represents the ionic model and plug-in (IMP) data structure.
Definition: ION_IF.h:168
std::vector< IonIfBase * > & plugins()
Returns a vector containing the plugins of this IMP.
Definition: ION_IF.cc:174
void compute(node_index_t start, node_index_t end, GlobalData_t **data)
Perform ionic model computation for 1 time step.
Definition: ION_IF.cc:258
ts & get_tstp()
Gets the time stepper.
Definition: ION_IF.cc:198
void for_each(const std::function< void(IonIfBase &)> &consumer)
Executes the consumer functions on this IMP and each of its plugins.
Definition: ION_IF.cc:526
std::vector< IonIfBase * > IIF
array of IIF's
Definition: MULTI_ION_IF.h:198
int * numplugs
number of plugins for each region
Definition: MULTI_ION_IF.h:205
std::vector< IonTypeList > plugtypes
plugins types for each region
Definition: MULTI_ION_IF.h:211
IonTypeList iontypes
type for each region
Definition: MULTI_ION_IF.h:208
int N_IIF
how many different IIF's
Definition: MULTI_ION_IF.h:207
node_count_t * N_Nodes
#nodes for each IMP
Definition: MULTI_ION_IF.h:196
node_index_t ** NodeLists
local partitioned node lists for each IMP stored
Definition: MULTI_ION_IF.h:197
void setup(double inp_dt, double inp_start, double inp_end)
Initialize the timer_manager.
Definition: timer_utils.cc:21
void reset_timers()
Reset time in timer_manager and then reset registered timers.
Definition: timer_utils.h:101
LIMPET ionics and gap-junction models on the EMI unique-face interface mesh.
void init_vector(SF::abstract_vector< T, S > **vec)
Definition: SF_init.h:127
SF_nbr
Enumeration encoding the different supported numberings.
Definition: SF_container.h:185
@ NBR_PETSC
PETSc numbering of nodes.
Definition: SF_container.h:188
@ NBR_REF
The nodal numbering of the reference mesh (the one stored on HD).
Definition: SF_container.h:186
@ NBR_SUBMESH
Submesh nodal numbering: The globally ascending sorted reference indices are reindexed.
Definition: SF_container.h:187
int get_plug_flag(char *plgstr, int *out_num_plugins, IonTypeList &out_plugins)
@ AUTO
Definition: target.h:31
IonType * get_ion_type(const std::string &name)
SF_real GlobalData_t
Definition: limpet_types.h:12
GlobalData_t(* SVgetfcn)(IonIfBase &, node_index_t, int)
Definition: ion_type.h:33
void update_ts(ts *ptstp)
Definition: ION_IF.cc:575
opencarp::local_index_t node_count_t
Definition: limpet_types.h:14
int read_sv(MULTI_IF *, int, const char *)
char IIF_Mask_t
Definition: ion_type.h:35
opencarp::local_index_t node_index_t
Definition: limpet_types.h:13
std::map< int, std::string > units
Definition: stimulate.cc:26
timer_manager * tm_manager
a manager for the various physics timers
Definition: main.cc:40
@ iotm_console
Definition: timer_utils.h:29
sf_mesh & get_mesh(const mesh_t gt)
Get a mesh by specifying the gridID.
Definition: sf_interface.cc:18
SF::scattering * get_permutation(const int mesh_id, const int perm_id, const int dpn)
Get the PETSC to canonical permutation scattering for a given mesh and number of dpn.
SF::meshdata< mesh_int_t, mesh_real_t > sf_mesh
Definition: sf_interface.h:33
int set_dir(IO_t dest)
Definition: sim_utils.cc:896
void read_metadata(const std::string filename, std::map< std::string, std::string > &metadata, MPI_Comm comm)
Read metadata from the header.
Definition: fem_utils.cc:57
T get_global(T in, MPI_Op OP, MPI_Comm comm=SF_COMM)
Do a global reduction on a variable.
Definition: basics.h:219
SF::scattering * register_permutation(const int mesh_id, const int perm_id, const int dpn)
Register a permutation between two orderings for a mesh.
void register_data(sf_vec *dat, datavec_t d)
Register a data vector in the global registry.
Definition: sim_utils.cc:1371
@ OUTPUT
Definition: sim_utils.h:39
char * dupstr(const char *old_str)
Definition: basics.cc:27
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
Definition: basics.cc:55
mesh_t
The enum identifying the different meshes we might want to load.
Definition: sf_interface.h:44
@ emi_surface_unique_face_msh
Definition: sf_interface.h:53
@ intra_elec_msh
Definition: sf_interface.h:45
void get_time(double &tm)
Definition: basics.h:430
SF::abstract_vector< SF_int, SF_real > sf_vec
Definition: sf_interface.h:35
int get_rank(MPI_Comm comm=SF_COMM)
Definition: basics.h:270
void remove_char(char *buff, const int buffsize, const char c)
Definition: basics.h:350
void read_indices_with_data(SF::vector< T > &idx, SF::vector< S > &dat, const std::string filename, const hashmap::unordered_map< mesh_int_t, mesh_int_t > &dd_map, const int dpn, MPI_Comm comm)
like read_indices, but with associated data for each index
Definition: fem_utils.h:254
V timing(V &t2, const V &t1)
Definition: basics.h:442
#define PETSC_TO_CANONICAL
Permute algebraic data from PETSC to canonical ordering.
Definition: sf_interface.h:64