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 void initialize_sv_dumps_onFace(limpet::MULTI_IF *pmiif, IMPregion_EMI* reg, int id, double t, double dump_dt);
20 
21 namespace {
22 
23 // Region 0 and 1 are default EMI face regions and are valid when an ionic
24 // model is assigned. User-defined regions 2+ must additionally provide at
25 // least one face-tag pair.
26 bool imp_region_emi_is_assigned(const IMPregion_EMI& region, int idx)
27 {
28  if (!(region.im && strlen(region.im) > 0)) return false;
29  if (idx < 2) return true;
30  return region.num_IDs > 0;
31 }
32 
33 // Ignore trailing unassigned imp_region_emi entries. This lets users keep the
34 // generated default parameter count while only assigning the regions they use.
35 int effective_num_imp_regions_emi()
36 {
37  const int configured_nreg = param_globals::num_imp_regions;
38  int nreg = configured_nreg;
39  while (nreg > 2 && !imp_region_emi_is_assigned(param_globals::imp_region_emi[nreg - 1], nreg - 1)) {
40  --nreg;
41  }
42 
43  if (nreg < configured_nreg) {
44  static bool warned_once = false;
45  int rank = 0;
46  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
47  if (!warned_once && rank == 0) {
48  log_msg(NULL, 1, ECHO,
49  "Warning: num_imp_regions=%d but only %d imp_region_emi entries are assigned; ignoring trailing unassigned regions.\n",
50  configured_nreg, nreg);
51  warned_once = true;
52  }
53  }
54 
55  return nreg;
56 }
57 
58 } // namespace
59 
60 void IonicsOnFace::compute_step()
61 {
62  double t1, t2;
63  get_time(t1);
64 
65  miif->compute_ionic_current();
66 
67  double comp_time = timing(t2, t1);
68  this->compute_time += comp_time;
69 
70  comp_stats.calls++;
71  comp_stats.tot_time += comp_time;
72 
74  comp_stats.log_stats(user_globals::tm_manager->time, false);
75 }
76 
77 void IonicsOnFace::destroy()
78 {
79  miif->free_MIIF();
80 }
81 
82 void IonicsOnFace::output_step()
83 {}
84 
85 void IonicsOnFace::initialize()
86 {
87  double t1, t2;
88  get_time(t1);
89 
90  set_dir(OUTPUT);
91 
92  // initialize generic logger for ODE timings per time_dt
93  comp_stats.init_logger("ODE_stats.dat");
94 
95  double tstart = 0.;
96  sf_mesh & mesh = get_mesh(ion_domain);
97  limpet::node_count_t loc_size = mesh.l_numelem;
98 
99  // create ionic current vector and register it
100  sf_vec *IIon;
101  sf_vec *Vmv;
102  SF::init_vector(&IIon, mesh, 1, sf_vec::elemwise);
103  SF::init_vector(&Vmv, mesh, 1, sf_vec::elemwise);
106 
107  // setup miif
108  miif = new limpet::MULTI_IF();
109  // hand down the physics log file, so that what MULTI_IF reports while setting up and
110  // restoring state is recorded rather than only echoed to the console
111  miif->logger = logger;
112  // store IIF_IDs and Plugins in arrays
113  miif->name = "myocardium";
114  miif->gdata[limpet::Vm] = Vmv;
115  miif->gdata[limpet::Iion] = IIon;
116 
117  const int num_imp_regions = effective_num_imp_regions_emi();
118  SF::vector<RegionSpecs_EMI> rs(num_imp_regions);
119 
120  if (rs.size() <2) {
121  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");
122  log_msg(NULL, 5, ECHO, "set a default ionic model and gapjuntion model in parameter file\\n");
123  exit(1);
124  }
125 
126  // Region 0 and 1 are implicit defaults:
127  // - region 0: default membrane faces
128  // - region 1: default gap-junction faces
129  rs[0].nsubregs = 0;
130  rs[0].subregtags = nullptr;
131  rs[1].nsubregs = 0;
132  rs[1].subregtags = nullptr;
133 
134  // imp_region_emi[2+] assign models to specific face tag pairs like "t1:t2"
135  for (size_t i = 2; i < rs.size(); i++ ) {
136  rs[i].nsubregs = param_globals::imp_region_emi[i].num_IDs;
137  rs[i].subregtags = new std::string[rs[i].nsubregs]; // Allocate memory for the string array
138  for (int j = 0; j < rs[i].nsubregs;j++) {
139  std::string t1t2 = param_globals::imp_region_emi[i].ID[j];
140  size_t colon_pos = t1t2.find(':');
141  if (colon_pos == std::string::npos) {
142  std::cerr << "Error: ':' the face tags are not defined properly in input file, it should be tag1:tag2 as a string" << std::endl;
143  exit(1);
144  }
145 
146  rs[i].subregtags[j] = param_globals::imp_region_emi[i].ID[j]; // Convert char* to std::string.
147  }
148  }
149 
150  SF::vector<int> reg_mask;
151  region_mask_onFace(ion_domain, tags_data, line_face, tri_face, quad_face,
152  map_vertex_tag_to_dof, map_elem_uniqueFace_to_tags, intra_tags,
153  rs, reg_mask, true, "imp_region_emi");
154 
155  for (size_t i = 0; i < rs.size(); i++ ) {
156  delete[] rs[i].subregtags;
157  rs[i].subregtags = nullptr;
158  }
159 
160  int purkfLen = 1;
161  tstart = setup_MIIF(loc_size, num_imp_regions, param_globals::imp_region_emi,
162  reg_mask.data(), param_globals::start_statef, param_globals::num_adjustments,
163  param_globals::adjustment, param_globals::dt, purkfLen > 0);
164 
165  miif->extUpdateVm = !param_globals::operator_splitting;
166 
167  // if we start at a non-zero time (i.e. we have restored a state), we notify the
168  // timer manager
169  if(tstart > 0.) {
170  log_msg(logger, 0, 0, "Changing simulation start time to %.2lf", tstart);
171  user_globals::tm_manager->setup(param_globals::dt, tstart, param_globals::tend);
173  }
174 
175  set_dir(INPUT);
176 
177  this->initialize_time += timing(t2, t1);
178 }
179 
180 double IonicsOnFace::setup_MIIF(limpet::node_count_t nnodes, int nreg, IMPregion_EMI* impreg, int* mask,
181  const char *start_fn, int numadjust, IMPVariableAdjustment *adjust,
182  double time_step, bool close)
183 {
184  double tstart = 0;
185 
186  miif->N_IIF = nreg;
187  miif->numNode = nnodes;
188  miif->iontypes = {};
189  miif->numplugs = (int*)calloc( miif->N_IIF, sizeof(int));
190  miif->plugtypes = std::vector<limpet::IonTypeList>(miif->N_IIF);
191  miif->targets = std::vector<limpet::Target>(miif->N_IIF, limpet::Target::AUTO);
192 
193  log_msg(logger,0,ECHO, "\nSetting up ionic models on EMI Face and plugins\n" \
194  "-----------------------------------\n\n" \
195  "Assigning IMPS to tagged regions:" );
196 
197  for (int i=0;i<miif->N_IIF;i++) {
198  auto pT = limpet::get_ion_type(std::string(impreg[i].im));
199  if (pT != NULL)
200  {
201  miif->iontypes.push_back(*pT);
202  log_msg(logger, 0, ECHO|NONL, "\tIonic model: %s to tag region(s)", impreg[i].im);
203 
204  if(impreg[i].num_IDs > 0) {
205  for(int j = 0; j < impreg[i].num_IDs; j++)
206  log_msg(logger,0,ECHO|NONL, " [%s],", impreg[i].ID[j]);
207  log_msg(logger,0,ECHO,"\b.");
208  }
209  else {
210  log_msg(logger,0,ECHO, " [0] (implicitely)");
211  }
212  }
213  else {
214  log_msg(NULL,5,ECHO, "Illegal IM specified: %s\n", impreg[i].im );
215  log_msg(NULL,5,ECHO, "Run bench --list-imps for a list of all available models.\n" );
216  EXIT(1);
217  }
218  if (limpet::get_plug_flag( impreg[i].plugins, &miif->numplugs[i], miif->plugtypes[i]))
219  {
220  if(impreg[i].plugins[0] != '\0') {
221  log_msg(logger,0, ECHO|NONL, "\tPlug-in(s) : %s to tag region(s)", impreg[i].plugins);
222 
223  for(int j = 0; j < impreg[i].num_IDs; j++)
224  log_msg(logger,0,ECHO|NONL, " [%d],", impreg[i].ID[j]);
225  log_msg(logger,0,ECHO,"\b.");
226  }
227  }
228  else {
229  log_msg(NULL,5,ECHO,"Illegal plugin specified: %s\n", impreg[i].plugins);
230  log_msg(NULL,5,ECHO, "Run bench --list-imps for a list of all available plugins.\n" );
231  EXIT(1);
232  }
233  }
234 
235  miif->IIFmask = (limpet::IIF_Mask_t*)calloc(miif->numNode, sizeof(limpet::IIF_Mask_t));
236 
237  // The mask is element-wise on the EMI unique-face mesh: one ionic/gap-junction
238  // region id per local unique-face element.
239  if (mask) {
240  for (limpet::node_index_t i=0; i<miif->numNode; i++)
241  miif->IIFmask[i] = (limpet::IIF_Mask_t) mask[i];
242  }
243 
244  miif->initialize_MIIF();
245 
246  for (int i=0;i<miif->N_IIF;i++) {
247  // the IMP tuning does not handle spaces well, thus we remove them here
248  remove_char(impreg[i].im_param, strlen(impreg[i].im_param), ' ');
249  miif->IIF[i]->tune(impreg[i].im_param, impreg[i].plugins, impreg[i].plug_param);
250  }
251 
252  set_dir(INPUT);
253  miif->initialize_currents(time_step, param_globals::ode_fac);
254 
255  // overriding initial values goes here
256  // read in single cell state vector and spread it out over the entire region
257  set_dir(INPUT);
258  for (int i=0;i<miif->N_IIF;i++) {
259  if (impreg[i].im_sv_init && strlen(impreg[i].im_sv_init) > 0)
260  if (read_sv(miif, i, impreg[i].im_sv_init)) {
261  log_msg(NULL, 5, ECHO|FLUSH, "State vector initialization failed for %s.\n", impreg[i].name);
262  EXIT(-1);
263  }
264  }
265 
266  if( !start_fn || strlen(start_fn)>0 )
267  tstart = (double) miif->restore_state(start_fn, ion_domain, close);
268 
269  for (int i=0; i<numadjust; i++)
270  {
271  set_dir(INPUT);
272 
273  SF::vector<SF_int> indices;
274  SF::vector<double> values;
275  bool restrict_to_algebraic = true;
276 
277  sf_mesh & imesh = get_mesh(ion_domain);
278  std::map<std::string,std::string> metadata;
279  read_metadata(adjust[i].file, metadata, PETSC_COMM_WORLD);
280 
281  SF::SF_nbr nbr = SF::NBR_REF;
282  if(metadata.count("grid") && metadata["grid"].compare("intra") == 0) {
283  nbr = SF::NBR_SUBMESH;
284  }
285  read_indices_with_data(indices, values, adjust[i].file, imesh, nbr, restrict_to_algebraic, 1, PETSC_COMM_WORLD);
286 
287  int rank = get_rank();
288 
289  for(size_t gi = 0; gi < indices.size(); gi++)
290  indices[gi] = SF::local_nodal_to_local_petsc<mesh_int_t, mesh_real_t>(imesh, rank, indices[gi]);
291 
292  // debug, output parameters on global intracellular vector
293  if(adjust[i].dump) {
294  sf_vec* adjPars;
295  SF::init_vector(&adjPars, imesh, 1, sf_vec::algebraic);
296  adjPars->set(indices, values, false, true);
297 
298  set_dir(OUTPUT);
299  char fname[2085];
300  snprintf(fname, sizeof fname, "adj_%s_perm.dat", adjust[i].variable);
301  adjPars->write_ascii(fname, false);
302 
303  // get the scattering to the canonical permutation
304  SF::scattering* sc = get_permutation(ion_domain, PETSC_TO_CANONICAL, 1);
305  if(sc == NULL) {
306  log_msg(0,3,0, "%s warning: PETSC_TO_CANONICAL permutation needed registering!", __func__);
307  sc = register_permutation(ion_domain, PETSC_TO_CANONICAL, 1);
308  }
309 
310  (*sc)(*adjPars, true);
311  snprintf(fname, sizeof fname, "adj_%s_canonical.dat", adjust[i].variable);
312  adjPars->write_ascii(fname, false);
313  }
314 
315  int nc = miif->adjust_MIIF_variables(adjust[i].variable, indices, values);
316  log_msg(logger, 0, 0, "Adjusted %d values for %s", nc, adjust[i].variable);
317  }
318 
319  set_dir(OUTPUT);
320 
321  for (int i=0;i<miif->N_IIF;i++)
322  initialize_sv_dumps_onFace(miif, impreg+i, i, tstart, param_globals::spacedt);
323 
324  return tstart;
325 }
326 
338 void initialize_sv_dumps_onFace(limpet::MULTI_IF *pmiif, IMPregion_EMI* reg, int id, double t, double dump_dt)
339 {
340  char svs[1024], plgs[1024], plgsvs[1024], fname[1024];
341 
342  strcpy(svs, reg->im_sv_dumps ? reg->im_sv_dumps : "");
343  strcpy(plgs, reg->plugins ? reg->plugins : "");
344  strcpy(plgsvs, reg->plug_sv_dumps ? reg->plug_sv_dumps : "");
345 
346  if( !(strlen(svs)+strlen(plgsvs) ) )
347  return;
348 
349  /* The string passed to the "reg_name" argument (#4) of the sv_dump_add
350  * function is supposed to be "region name". It's only purpose is to
351  * provide the base name for the SV dump file, eg: Purkinje.Ca_i.bin.
352  * Thus, we pass: [vofile].[reg name], Otherwise, dumping SVs in
353  * batched runs would be extremely tedious.
354  */
355  strcpy(fname, param_globals::vofile); // [vofile].igb
356 
357  if( !reg->name ) {
358  log_msg(NULL, 5, ECHO, "%s: a region name must be specified\n", __func__ );
359  exit(0);
360  }
361 
362  // We want to convert vofile.igb to vofile.regname
363  size_t fname_len = strlen(fname);
364  char* ext_start = fname + fname_len;
365  if(fname_len >= 4 && strcmp(ext_start - 4, ".igb") == 0) ext_start -= 3;
366  strcpy(ext_start, reg->name);
367 
368  pmiif->sv_dump_add_by_name_list(id, reg->im, fname, svs, plgs, plgsvs, t, dump_dt);
369 }
370 
381 bool check_tags_in_elems_onFace(std::vector<std::string> & tags_data, SF::vector<RegionSpecs_EMI> & regspec,
382  const char* gridname, const char* reglist)
383 {
384  bool AllTagsExist = true;
386 
387  tagset.insert(tags_data.begin(), tags_data.end());
388 
389  // cycle through all user-specified regions
390  for (size_t reg=0; reg<regspec.size(); reg++)
391  // cycle through all tags which belong to the region
392  for (int k=0; k<regspec[reg].nsubregs; k++) {
393  // check whether this tag exists in element list
394  int n = 0;
395  size_t colon_pos = regspec[reg].subregtags[k].find(':');
396  int tag1 = std::stoi(regspec[reg].subregtags[k].substr(0, colon_pos));
397  int tag2 = std::stoi(regspec[reg].subregtags[k].substr(colon_pos + 1));
398 
399  // considered the other combinations of tags
400  std::string inverse_pair;
401  inverse_pair = std::to_string(tag2) + ":" + std::to_string(tag1);
402  if(tagset.count(regspec[reg].subregtags[k]) || tagset.count(inverse_pair)) {
403  n++;
404  }
405 
406  // globalize n
407  int N = get_global(n, MPI_SUM);
408 
409  if (N==0) {
410  log_msg(NULL, 3, ECHO,
411  "on face Region tag %s in %s[%d] not found in element list for %s grid.\n",
412  regspec[reg].subregtags[k].c_str(), reglist, reg, gridname);
413  AllTagsExist = false;
414  }
415  }
416 
417  if (!AllTagsExist) {
418  log_msg(NULL, 4, ECHO,"Assigned wrong pair of tags on the face of EMI surface mesh!\n"
419  "Check region ID specs in input files!\n");
420  }
421 
422  return AllTagsExist;
423 }
424 
425 inline bool pair_is_gapjunction(const std::pair<mesh_int_t, mesh_int_t>& tags,
426  const hashmap::unordered_set<int>& intra_tags)
427 {
428  return intra_tags.find(tags.first) != intra_tags.end() &&
429  intra_tags.find(tags.second) != intra_tags.end();
430 }
431 
432 void region_mask_onFace(mesh_t meshspec,
433  std::vector<std::string> & tags_data,
435  std::pair<SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>,
436  SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>>> & line_face,
438  std::pair<SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>,
439  SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>>> & tri_face,
441  std::pair<SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>,
442  SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>>> & quad_face,
443  hashmap::unordered_map<std::pair<mesh_int_t,mesh_int_t>, mesh_int_t> & map_vertex_tag_to_dof,
444  hashmap::unordered_map<mesh_int_t, std::pair<mesh_int_t, mesh_int_t>> & map_elem_uniqueFace_to_tags,
445  const hashmap::unordered_set<int> & intra_tags,
446  SF::vector<RegionSpecs_EMI> & regspec,
447  SF::vector<int> & regionIDs, bool mask_elem, const char* reglist)
448 {
449  if(regspec.size() == 1) return;
450 
451  sf_mesh & mesh = get_mesh(meshspec);
453 
454  // Initialize the list with the default membrane region id, 0.
455  size_t rIDsize = mask_elem ? mesh.l_numelem : mesh.l_numpts;
456 
457  size_t nelem = mesh.l_numelem;
458 
459  // Check whether all specified face-tag pairs exist in the unique-face list.
460  check_tags_in_elems_onFace(tags_data, regspec, mesh.name.c_str(), reglist);
461 
462  regionIDs.assign(rIDsize, 0);
463 
464  int* rid = regionIDs.data();
466 
467  // Generate a map from face-tag pair strings to region IDs. This also lets us
468  // detect duplicate assignments and simplifies the regionIDs fill loop.
469  for (size_t reg=0; reg < regspec.size(); reg++) {
470  int err = 0;
471  for (int k=0; k < regspec[reg].nsubregs; k++) {
472  std::string curtag = regspec[reg].subregtags[k];
473  if(tag_to_reg.count(curtag)) err++;
474 
475  if(get_global(err, MPI_SUM))
476  log_msg(0,4,0, "%s warning: Tag idx %s is assigned to multiple regions!\n"
477  "Its final assignment will be to the highest assigned region ID!",
478  __func__, curtag.c_str());
479 
480  tag_to_reg[curtag] = reg;
481  }
482  }
483 
484  std::vector<int> mx_tag( rIDsize, -1 );
485 
486  // Cycle through unique-face elements and assign default or user-defined regions.
487  for(size_t eidx=0; eidx<nelem; eidx++)
488  {
489  std::vector<int> elem_nodes;
490  mesh_int_t tag = mesh.tag[eidx];
491  std::string result_pair_orginal;
492  std::string result_pair_reverse;
493  std::pair<mesh_int_t,mesh_int_t> value = map_elem_uniqueFace_to_tags[eidx];
494  result_pair_orginal = std::to_string(value.first) + ":" + std::to_string(value.second);
495  result_pair_reverse = std::to_string(value.second) + ":" + std::to_string(value.first);
496  rid[eidx] = pair_is_gapjunction(value, intra_tags) ? 1 : 0;
497 
498  if (tag_to_reg.count(result_pair_orginal)) {
499  rid[eidx] = tag_to_reg[result_pair_orginal];
500  } else if(tag_to_reg.count(result_pair_reverse)){
501  rid[eidx] = tag_to_reg[result_pair_reverse];
502  }
503  if(tag!=value.first){
504  log_msg(NULL, 5, ECHO, "error in tags on surface mesh with the unique faces!!!.\\n");
505  exit(1);
506  }
507  }
508 }
509 
512 double IonicsOnFace::timer_val(const int timer_id)
513 {
514  double val = std::nan("NaN");
515  return val;
516 }
517 
520 std::string IonicsOnFace::timer_unit(const int timer_id)
521 {
522  std::string s_unit;
523  return s_unit;
524 }
525 
526 void compute_IIF_OnFace(limpet::IonIfBase& pIF, limpet::GlobalData_t** impdata, limpet::node_index_t n)
527 {
528  if (impdata[limpet::Iion] != NULL)
529  impdata[limpet::Iion][n] = 0;
530 
531  pIF.for_each([&](limpet::IonIfBase& imp) {
532  update_ts(&imp.get_tstp());
533  imp.compute(n, n + 1, impdata);
534  });
535 }
536 
548 void* find_SV_in_IMP_onFace(limpet::MULTI_IF* miif, const int idx, const char *IMP, const char *SV,
549  int* offset, int* sz)
550 {
551  if(strcmp(IMP, miif->iontypes[idx].get().get_name().c_str()) == 0) {
552  return (void*) miif->iontypes[idx].get().get_sv_offset(SV, offset, sz);
553  }
554  else {
555  for( int k=0; k<miif->numplugs[idx]; k++ )
556  if(strcmp(IMP, miif->plugtypes[idx][k].get().get_name().c_str()) == 0) {
557  return (void*) miif->plugtypes[idx][k].get().get_sv_offset(SV, offset, sz);
558  }
559  }
560 
561  return NULL;
562 }
563 
564 
575 void alloc_gvec_data_onFace(const int nGVcs, const int nRegs, const int nEmiRegs,
576  GVecs *prmGVecs, gvec_data_OnFace &glob_vecs)
577 {
578  glob_vecs.nRegs = nRegs;
579 
580  if (nGVcs) {
581  glob_vecs.vecs.resize(nGVcs);
582 
583  for (size_t i = 0; i < glob_vecs.vecs.size(); i++) {
584  sv_data_onFace &gvec = glob_vecs.vecs[i];
585 
586  gvec.name = dupstr(prmGVecs[i].name);
587  gvec.units = dupstr(prmGVecs[i].units);
588  gvec.bogus = prmGVecs[i].bogus;
589 
590  gvec.imps = (char**) calloc(nRegs, sizeof(char *));
591  gvec.svNames = (char**) calloc(nRegs, sizeof(char *));
592  gvec.svSizes = (int*) calloc(nRegs, sizeof(int));
593  gvec.svOff = (int*) calloc(nRegs, sizeof(int));
594  gvec.getsv = (void**) calloc(nRegs, sizeof(limpet::SVgetfcn));
595 
596  for (int j = 0; j < nRegs; j++) {
597  if (strlen(prmGVecs[i].imp)) gvec.imps[j] = dupstr(prmGVecs[i].imp);
598 #ifdef WITH_PURK
599  else if (j < nEmiRegs)
600  gvec.imps[j] = dupstr(param_globals::imp_region_emi[j].im);
601  else
602  gvec.imps[j] = dupstr(param_globals::PurkIon[j - nEmiRegs].im);
603 #else
604  else if (j < nEmiRegs)
605  gvec.imps[j] = dupstr(param_globals::imp_region_emi[j].im);
606 #endif
607  gvec.svNames[j] = dupstr(prmGVecs[i].ID[j]);
608  }
609  }
610  }
611 }
612 
623 void init_sv_gvec_onFace(gvec_data_OnFace& GVs, limpet::MULTI_IF* miif, sf_vec & tmpl,
624  igb_output_manager & output_manager)
625 {
626  GVs.inclPS = false;
627  // int num_purk_regions = GVs->inclPS ? purk->ion.N_IIF : 0;
628  int num_purk_regions = 0;
629  int nEmiRegs = effective_num_imp_regions_emi();
630  int nRegs = nEmiRegs + num_purk_regions;
631 
632  alloc_gvec_data_onFace(param_globals::num_gvecs, nRegs, nEmiRegs, param_globals::gvec, GVs);
633 
634 #ifdef WITH_PURK
635  if (GVs->inclPS) sample_PS_ionSVs(purk);
636 #endif
637 
638  for (unsigned int i = 0; i < GVs.vecs.size(); i++) {
639  sv_data_onFace & gv = GVs.vecs[i];
640  int noSV = 0;
641 
642  for (int j = 0; j < miif->N_IIF; j++) {
643  gv.getsv[j] = find_SV_in_IMP_onFace(miif, j, gv.imps[j], gv.svNames[j],
644  gv.svOff + j, gv.svSizes + j);
645 
646  if (gv.getsv[j] == NULL) {
647  log_msg(NULL, 3, ECHO, "\tWarning: SV(%s) not found in region %d\n", gv.svNames[j], j);
648  noSV++;
649  }
650  }
651 
652  SF::init_vector(&gv.ordered, &tmpl);
653  output_manager.register_output(gv.ordered, intra_elec_msh, 1, gv.name, gv.units);
654 
655 #ifdef WITH_PURK
656  // same procedure for Purkinje
657  if (GVs->inclPS) {
658  IF_PURK_PROC(purk) {
659  MULTI_IF* pmiif = &purk->ion;
660  for (int j = miif->N_IIF; j < nRegs; j++) {
661  gv.getsv[j] = find_SV_in_IMP_onFace(pmiif, j - miif->N_IIF, gv.imps[j],
662  gv.svNames[j], gv.svOff + j, gv.svSizes + j);
663 
664  if (gv.getsv[j] == NULL) {
665  LOG_MSG(NULL, 3, ECHO, "\tWarning: state variable \"%s\" not found in region %d\n", gv.svNames[j], j);
666  noSV++;
667  }
668  }
669  RVector_dup(purk->vm_pt, &gv.orderedPS_PS);
670  MYO_COMM(purk);
671  }
672  RVector_dup(purk->vm_pt_over, &gv.orderedPS);
673  initialize_grid_output(grid, NULL, tmo, intra_elec_msh, GRID_WRITE, 0., 1., gv.units, gv.orderedPS,
674  1, gv.GVcName, -purk->npt, param_globals::output_level);
675  }
676 #endif
677 
678  if (noSV == nRegs) {
679  log_msg(NULL, 5, ECHO, "\tError: no state variables found for global vector %d\n", i);
680  log_msg(NULL, 5, ECHO, "Run bench --imp=YourModel --imp-info to get a list of all parameters.\\n");
681  exit(1);
682  }
683  }
684 }
685 
695 void assemble_sv_gvec_onFace(gvec_data_OnFace & gvecs, limpet::MULTI_IF *miif)
696 {
697  for(size_t i=0; i<gvecs.vecs.size(); i++ ) {
698  sv_data_onFace & gv = gvecs.vecs[i];
699 
700  // Set to the default value.
701  gv.ordered->set(gv.bogus);
702  SF_int start, stop;
703  gv.ordered->get_ownership_range(start, stop);
704 
705  for( int n = 0; n<miif->N_IIF; n++ ) {
706  if( !gv.getsv[n] ) continue;
707 
708  SF::vector<SF_real> data (miif->N_Nodes[n]);
709  SF::vector<SF_int> indices(miif->N_Nodes[n]);
710 
711  for( limpet::node_index_t j=0; j<miif->N_Nodes[n]; j++ ) {
712  indices[j] = miif->NodeLists[n][j] + start;
713  data[j] = ((limpet::SVgetfcn)(gv.getsv[n]))( *miif->IIF[n], j, gv.svOff[n]);
714  }
715 
716  bool add = false;
717  gv.ordered->set(indices, data, add);
718  }
719 
720 #ifdef WITH_PURK
721  // include purkinje in sv dump, if exists
722  if(gvecs->inclPS) {
723  RVector_set( gv.orderedPS, gv.bogus ); // set this to the default value
724 
725  if(IS_PURK_PROC(purk))
726  {
727  MULTI_IF *pmiif = &purk->ion;
728  Real *data = new Real[purk->gvec_cab.nitems];
729 
730  int ci=0;
731  for( int n = 0; n<pmiif->N_IIF; n++ ) {
732  int gvidx = n+miif->N_IIF;
733  if( !gv.getsv[gvidx] ) continue;
734  for( int j=0; j<purk->gvec_ion[n].nitems; j++ )
735  data[ci++] = ((SVgetfcn)(gv.getsv[gvidx]))( pmiif->IIF+n, ((int*)(purk->gvec_ion[n].data))[j],
736  gv.svOff[gvidx]);
737  }
738  RVector_setvals(gv.orderedPS, purk->gvec_cab.nitems, (int*)purk->gvec_cab.data, data, true);
739  delete [] data;
740  }
741  RVector_sync( gv.orderedPS );
742  }
743 #endif
744  }
745 }
746 
747 
748 } // namespace opencarp
749 #endif
double Real
Definition: DataTypes.h:13
opencarp::local_index_t mesh_int_t
Definition: SF_container.h:31
opencarp::global_index_t SF_int
Global algebraic index type.
Definition: SF_globals.h:17
#define FLUSH
Definition: basics.h:304
#define ECHO
Definition: basics.h:301
#define NONL
Definition: basics.h:305
Comfort class. Provides getter functions to access the mesh member variables more comfortably.
Definition: SF_fem_utils.h:689
Container for a PETSc VecScatter.
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
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
void sv_dump_add_by_name_list(int, char *, char *, char *, char *, char *, double, double)
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:100
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:110
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:1615
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
int get_rank(MPI_Comm comm=PETSC_COMM_WORLD)
Definition: basics.h:269
T get_global(T in, MPI_Op OP, MPI_Comm comm=PETSC_COMM_WORLD)
Do a global reduction on a variable.
Definition: basics.h:218
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:2090
@ OUTPUT
Definition: sim_utils.h:39
char * dupstr(const char *old_str)
Definition: basics.cc:29
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
Definition: basics.cc:57
mesh_t
The enum identifying the different meshes we might want to load.
Definition: sf_interface.h:44
@ intra_elec_msh
Definition: sf_interface.h:45
void get_time(double &tm)
Definition: basics.h:429
SF::abstract_vector< SF_int, SF_real > sf_vec
Definition: sf_interface.h:35
void remove_char(char *buff, const int buffsize, const char c)
Definition: basics.h:349
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:441
#define PETSC_TO_CANONICAL
Permute algebraic data from PETSC to canonical ordering.
Definition: sf_interface.h:64