openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
ionicsOnFace.cc
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // openCARP is an open cardiac electrophysiology simulator.
3 //
4 // Copyright (C) 2020 openCARP project
5 //
6 // This program is licensed under the openCARP Academic Public License (APL)
7 // v1.0: You can use and redistribute it and/or modify it in non-commercial
8 // academic environments under the terms of APL as published by the openCARP
9 // project v1.0, or (at your option) any later version. Commercial use requires
10 // a commercial license (info@opencarp.org).
11 //
12 // This program is distributed without any warranty; see the openCARP APL for
13 // more details.
14 //
15 // You should have received a copy of the openCARP APL along with this program
16 // and can find it online: http://www.opencarp.org/license
17 // ----------------------------------------------------------------------------
18 
27 #if WITH_EMI_MODEL
28 #include "ionicsOnFace.h"
29 
30 #include "SF_init.h"
31 
32 namespace opencarp {
33 
34 void initialize_sv_dumps_onFace(limpet::MULTI_IF *pmiif, IMPregion_EMI* reg, int id, double t, double dump_dt);
35 
36 namespace {
37 
38 bool imp_region_emi_is_assigned(const IMPregion_EMI& region, int idx)
39 {
40  if (!(region.im && strlen(region.im) > 0)) return false;
41  if (idx < 2) return true;
42  return region.num_IDs > 0;
43 }
44 
45 int effective_num_imp_regions_emi()
46 {
47  const int configured_nreg = param_globals::num_imp_regions;
48  int nreg = configured_nreg;
49  while (nreg > 2 && !imp_region_emi_is_assigned(param_globals::imp_region_emi[nreg - 1], nreg - 1)) {
50  --nreg;
51  }
52 
53  if (nreg < configured_nreg) {
54  static bool warned_once = false;
55  int rank = 0;
56  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
57  if (!warned_once && rank == 0) {
58  log_msg(NULL, 1, ECHO,
59  "Warning: num_imp_regions=%d but only %d imp_region_emi entries are assigned; ignoring trailing unassigned regions.\n",
60  configured_nreg, nreg);
61  warned_once = true;
62  }
63  }
64 
65  return nreg;
66 }
67 
68 } // namespace
69 
70 void IonicsOnFace::compute_step()
71 {
72  double t1, t2;
73  get_time(t1);
74 
75  miif->compute_ionic_current();
76 
77  double comp_time = timing(t2, t1);
78  this->compute_time += comp_time;
79 
80  comp_stats.calls++;
81  comp_stats.tot_time += comp_time;
82 
84  comp_stats.log_stats(user_globals::tm_manager->time, false);
85 }
86 
87 void IonicsOnFace::destroy()
88 {
89  miif->free_MIIF();
90 }
91 
92 void IonicsOnFace::output_step()
93 {}
94 
95 void IonicsOnFace::initialize()
96 {
97  double t1, t2;
98  get_time(t1);
99 
100  set_dir(OUTPUT);
101 
102  // initialize generic logger for ODE timings per time_dt
103  comp_stats.init_logger("ODE_stats.dat");
104 
105  double tstart = 0.;
106  sf_mesh & mesh = get_mesh(ion_domain);
107  int loc_size = mesh.l_numelem;
108 
109  // create ionic current vector and register it
110  sf_vec *IIon;
111  sf_vec *Vmv;
112  SF::init_vector(&IIon, mesh, 1, sf_vec::elemwise);
113  SF::init_vector(&Vmv, mesh, 1, sf_vec::elemwise);
116 
117  // setup miif
118  miif = new limpet::MULTI_IF();
119  // store IIF_IDs and Plugins in arrays
120  miif->name = "myocardium";
121  miif->gdata[limpet::Vm] = Vmv;
122  miif->gdata[limpet::Iion] = IIon;
123 
124  const int num_imp_regions = effective_num_imp_regions_emi();
125  SF::vector<RegionSpecs_EMI> rs(num_imp_regions);
126 
127  if (rs.size() <2) {
128  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");
129  log_msg(NULL, 5, ECHO, "set a default ionic model and gapjuntion model in parameter file\\n");
130  exit(1);
131  }
132 
133  // Region 0 and 1 are implicit defaults:
134  // - region 0: default membrane faces
135  // - region 1: default gap-junction faces
136  rs[0].nsubregs = 0;
137  rs[0].subregtags = nullptr;
138  rs[1].nsubregs = 0;
139  rs[1].subregtags = nullptr;
140 
141  // imp_region_emi[2+] assign models to specific face tag pairs like "t1:t2"
142  for (size_t i = 2; i < rs.size(); i++ ) {
143  rs[i].nsubregs = param_globals::imp_region_emi[i].num_IDs;
144  rs[i].subregtags = new std::string[rs[i].nsubregs]; // Allocate memory for the string array
145  for (int j = 0; j < rs[i].nsubregs;j++) {
146  std::string t1t2 = param_globals::imp_region_emi[i].ID[j];
147  size_t colon_pos = t1t2.find(':');
148  if (colon_pos == std::string::npos) {
149  std::cerr << "Error: ':' the face tags are not defined properly in input file, it should be tag1:tag2 as a string" << std::endl;
150  exit(1);
151  }
152 
153  rs[i].subregtags[j] = param_globals::imp_region_emi[i].ID[j]; // Convert char* to std::string
154  }
155  }
156 
157  SF::vector<int> reg_mask;
158  region_mask_onFace(ion_domain, tags_data, line_face, tri_face, quad_face,
159  map_vertex_tag_to_dof, map_elem_uniqueFace_to_tags, intra_tags,
160  rs, reg_mask, true, "imp_region_emi");
161 
162  for (size_t i = 0; i < rs.size(); i++ ) {
163  delete[] rs[i].subregtags;
164  rs[i].subregtags = nullptr;
165  }
166 
167  int purkfLen = 1;
168  tstart = setup_MIIF(loc_size, num_imp_regions, param_globals::imp_region_emi,
169  reg_mask.data(), param_globals::start_statef, param_globals::num_adjustments,
170  param_globals::adjustment, param_globals::dt, purkfLen > 0);
171 
172  miif->extUpdateVm = !param_globals::operator_splitting;
173 
174  // if we start at a non-zero time (i.e. we have restored a state), we notify the
175  // timer manager
176  if(tstart > 0.) {
177  log_msg(logger, 0, 0, "Changing simulation start time to %.2lf", tstart);
178  user_globals::tm_manager->setup(param_globals::dt, tstart, param_globals::tend);
180  }
181 
182  set_dir(INPUT);
183 
184  this->initialize_time += timing(t2, t1);
185 }
186 
187 double IonicsOnFace::setup_MIIF(int nnodes, int nreg, IMPregion_EMI* impreg, int* mask,
188  const char *start_fn, int numadjust, IMPVariableAdjustment *adjust,
189  double time_step, bool close)
190 {
191  double tstart = 0;
192 
193  miif->N_IIF = nreg;
194  miif->numNode = nnodes;
195  miif->iontypes = {};
196  miif->numplugs = (int*)calloc( miif->N_IIF, sizeof(int));
197  miif->plugtypes = std::vector<limpet::IonTypeList>(miif->N_IIF);
198  miif->targets = std::vector<limpet::Target>(miif->N_IIF, limpet::Target::AUTO);
199 
200  log_msg(logger,0,ECHO, "\nSetting up ionic models on EMI Face and plugins\n" \
201  "-----------------------------------\n\n" \
202  "Assigning IMPS to tagged regions:" );
203 
204  for (int i=0;i<miif->N_IIF;i++) {
205  auto pT = limpet::get_ion_type(std::string(impreg[i].im));
206  if (pT != NULL)
207  {
208  miif->iontypes.push_back(*pT);
209  log_msg(logger, 0, ECHO|NONL, "\tIonic model: %s to tag region(s)", impreg[i].im);
210 
211  if(impreg[i].num_IDs > 0) {
212  for(int j = 0; j < impreg[i].num_IDs; j++)
213  log_msg(logger,0,ECHO|NONL, " [%s],", impreg[i].ID[j]);
214  log_msg(logger,0,ECHO,"\b.");
215  }
216  else {
217  log_msg(logger,0,ECHO, " [0] (implicitely)");
218  }
219  }
220  else {
221  log_msg(NULL,5,ECHO, "Illegal IM specified: %s\n", impreg[i].im );
222  log_msg(NULL,5,ECHO, "Run bench --list-imps for a list of all available models.\n" );
223  EXIT(1);
224  }
225  if (limpet::get_plug_flag( impreg[i].plugins, &miif->numplugs[i], miif->plugtypes[i]))
226  {
227  if(impreg[i].plugins[0] != '\0') {
228  log_msg(logger,0, ECHO|NONL, "\tPlug-in(s) : %s to tag region(s)", impreg[i].plugins);
229 
230  for(int j = 0; j < impreg[i].num_IDs; j++)
231  log_msg(logger,0,ECHO|NONL, " [%d],", impreg[i].ID[j]);
232  log_msg(logger,0,ECHO,"\b.");
233  }
234  }
235  else {
236  log_msg(NULL,5,ECHO,"Illegal plugin specified: %s\n", impreg[i].plugins);
237  log_msg(NULL,5,ECHO, "Run bench --list-imps for a list of all available plugins.\n" );
238  EXIT(1);
239  }
240  }
241 
242  miif->IIFmask = (limpet::IIF_Mask_t*)calloc(miif->numNode, sizeof(limpet::IIF_Mask_t));
243 
244  // The mask is a nodal vector with the region IDs of each node.
245  // It is already reduced during the region_mask call to guarantee unique values
246  // for overlapping interface nodes
247  if (mask) {
248  for (int i=0; i<miif->numNode; i++)
249  miif->IIFmask[i] = (limpet::IIF_Mask_t) mask[i];
250  }
251 
252  miif->initialize_MIIF();
253 
254  for (int i=0;i<miif->N_IIF;i++) {
255  // the IMP tuning does not handle spaces well, thus we remove them here
256  remove_char(impreg[i].im_param, strlen(impreg[i].im_param), ' ');
257  miif->IIF[i]->tune(impreg[i].im_param, impreg[i].plugins, impreg[i].plug_param);
258  }
259 
260  set_dir(INPUT);
261  miif->initialize_currents(time_step, param_globals::ode_fac);
262 
263  // overriding initial values goes here
264  // read in single cell state vector and spread it out over the entire region
265  set_dir(INPUT);
266  for (int i=0;i<miif->N_IIF;i++) {
267  if (impreg[i].im_sv_init && strlen(impreg[i].im_sv_init) > 0)
268  if (read_sv(miif, i, impreg[i].im_sv_init)) {
269  log_msg(NULL, 5, ECHO|FLUSH, "State vector initialization failed for %s.\n", impreg[i].name);
270  EXIT(-1);
271  }
272  }
273 
274  if( !start_fn || strlen(start_fn)>0 )
275  tstart = (double) miif->restore_state(start_fn, ion_domain, close);
276 
277  for (int i=0; i<numadjust; i++)
278  {
279  set_dir(INPUT);
280 
281  SF::vector<int> indices;
282  SF::vector<double> values;
283  bool restrict_to_algebraic = true;
284 
285  sf_mesh & imesh = get_mesh(ion_domain);
286  std::map<std::string,std::string> metadata;
287  read_metadata(adjust[i].file, metadata, PETSC_COMM_WORLD);
288 
289  SF::SF_nbr nbr = SF::NBR_REF;
290  if(metadata.count("grid") && metadata["grid"].compare("intra") == 0) {
291  nbr = SF::NBR_SUBMESH;
292  }
293  read_indices_with_data(indices, values, adjust[i].file, imesh, nbr, restrict_to_algebraic, 1, PETSC_COMM_WORLD);
294 
295  int rank = get_rank();
296 
297  for(size_t gi = 0; gi < indices.size(); gi++)
298  indices[gi] = SF::local_nodal_to_local_petsc(imesh, rank, indices[gi]);
299 
300  // debug, output parameters on global intracellular vector
301  if(adjust[i].dump) {
302  sf_vec* adjPars;
303  SF::init_vector(&adjPars, imesh, 1, sf_vec::algebraic);
304  adjPars->set(indices, values);
305 
306  set_dir(OUTPUT);
307  char fname[2085];
308  snprintf(fname, sizeof fname, "adj_%s_perm.dat", adjust[i].variable);
309  adjPars->write_ascii(fname, false);
310 
311  // get the scattering to the canonical permutation
312  SF::scattering* sc = get_permutation(ion_domain, PETSC_TO_CANONICAL, 1);
313  if(sc == NULL) {
314  log_msg(0,3,0, "%s warning: PETSC_TO_CANONICAL permutation needed registering!", __func__);
315  sc = register_permutation(ion_domain, PETSC_TO_CANONICAL, 1);
316  }
317 
318  (*sc)(*adjPars, true);
319  snprintf(fname, sizeof fname, "adj_%s_canonical.dat", adjust[i].variable);
320  adjPars->write_ascii(fname, false);
321  }
322 
323  int nc = miif->adjust_MIIF_variables(adjust[i].variable, indices, values);
324  log_msg(logger, 0, 0, "Adjusted %d values for %s", nc, adjust[i].variable);
325  }
326 
327  set_dir(OUTPUT);
328 
329  for (int i=0;i<miif->N_IIF;i++)
330  initialize_sv_dumps_onFace(miif, impreg+i, i, tstart, param_globals::spacedt);
331 
332  return tstart;
333 }
334 
344 void initialize_sv_dumps_onFace(limpet::MULTI_IF *pmiif, IMPregion_EMI* reg, int id, double t, double dump_dt)
345 {
346  char svs[1024], plgs[1024], plgsvs[1024], fname[1024];
347 
348  strcpy(svs, reg->im_sv_dumps ? reg->im_sv_dumps : "");
349  strcpy(plgs, reg->plugins ? reg->plugins : "");
350  strcpy(plgsvs, reg->plug_sv_dumps ? reg->plug_sv_dumps : "");
351 
352  if( !(strlen(svs)+strlen(plgsvs) ) )
353  return;
354 
355  /* The string passed to the "reg_name" argument (#4) of the sv_dump_add
356  * function is supposed to be "region name". It's only purpose is to
357  * provide the base name for the SV dump file, eg: Purkinje.Ca_i.bin.
358  * Thus, we pass: [vofile].[reg name], Otherwise, dumping SVs in
359  * batched runs would be extremely tedious.
360  */
361  strcpy(fname, param_globals::vofile); // [vofile].igb.gz
362 
363  if( !reg->name ) {
364  log_msg(NULL, 5, ECHO, "%s: a region name must be specified\n", __func__ );
365  exit(0);
366  }
367 
368  // We want to convert vofile.igb or vofile.igb.gz to vofile.regname
369  char* ext_start = NULL;
370  ext_start = strstr(fname, "igb.gz");
371  if(ext_start == NULL) ext_start = strstr(fname, "igb");
372  if(ext_start == NULL) ext_start = fname + strlen(fname);
373  strcpy(ext_start, reg->name);
374 
375  pmiif->sv_dump_add_by_name_list(id, reg->im, fname, svs, plgs, plgsvs, t, dump_dt);
376 }
377 
388 bool check_tags_in_elems_onFace(std::vector<std::string> & tags_data, SF::vector<RegionSpecs_EMI> & regspec,
389  const char* gridname, const char* reglist)
390 {
391  bool AllTagsExist = true;
393 
394  tagset.insert(tags_data.begin(), tags_data.end());
395 
396  // cycle through all user-specified regions
397  for (size_t reg=0; reg<regspec.size(); reg++)
398  // cycle through all tags which belong to the region
399  for (int k=0; k<regspec[reg].nsubregs; k++) {
400  // check whether this tag exists in element list
401  int n = 0;
402  size_t colon_pos = regspec[reg].subregtags[k].find(':');
403  int tag1 = std::stoi(regspec[reg].subregtags[k].substr(0, colon_pos));
404  int tag2 = std::stoi(regspec[reg].subregtags[k].substr(colon_pos + 1));
405 
406  // considered the other combinations of tags
407  std::string inverse_pair;
408  inverse_pair = std::to_string(tag2) + ":" + std::to_string(tag1);
409  if(tagset.count(regspec[reg].subregtags[k]) || tagset.count(inverse_pair)) {
410  n++;
411  }
412 
413  // globalize n
414  int N = get_global(n, MPI_SUM);
415 
416  if (N==0) {
417  log_msg(NULL, 3, ECHO,
418  "on face Region tag %s in %s[%d] not found in element list for %s grid.\n",
419  regspec[reg].subregtags[k].c_str(), reglist, reg, gridname);
420  AllTagsExist = false;
421  }
422  }
423 
424  if (!AllTagsExist) {
425  log_msg(NULL, 4, ECHO,"Assigned wrong pair of tags on the face of EMI surface mesh!\n"
426  "Check region ID specs in input files!\n");
427  }
428 
429  return AllTagsExist;
430 }
431 
432 inline bool pair_is_gapjunction(const std::pair<mesh_int_t, mesh_int_t>& tags,
433  const hashmap::unordered_set<int>& intra_tags)
434 {
435  return intra_tags.find(tags.first) != intra_tags.end() &&
436  intra_tags.find(tags.second) != intra_tags.end();
437 }
438 
439 void region_mask_onFace(mesh_t meshspec,
440  std::vector<std::string> & tags_data,
442  std::pair<SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>,
443  SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>>> & line_face,
445  std::pair<SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>,
446  SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>>> & tri_face,
448  std::pair<SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>,
449  SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>>> & quad_face,
450  hashmap::unordered_map<std::pair<mesh_int_t,mesh_int_t>, mesh_int_t> & map_vertex_tag_to_dof,
451  hashmap::unordered_map<mesh_int_t, std::pair<mesh_int_t, mesh_int_t>> & map_elem_uniqueFace_to_tags,
452  const hashmap::unordered_set<int> & intra_tags,
453  SF::vector<RegionSpecs_EMI> & regspec,
454  SF::vector<int> & regionIDs, bool mask_elem, const char* reglist)
455 {
456  if(regspec.size() == 1) return;
457 
458  sf_mesh & mesh = get_mesh(meshspec);
459  SF::element_view<mesh_int_t,mesh_real_t> eview(mesh, SF::NBR_PETSC); // error is here!!!
460 
461  // initialize the list with the default regionID, 0
462  size_t rIDsize = mask_elem ? mesh.l_numelem : mesh.l_numpts;
463 
464  size_t nelem = mesh.l_numelem;
465 
466  // check whether all specified tags exist in the element list
467  check_tags_in_elems_onFace(tags_data, regspec, mesh.name.c_str(), reglist);
468 
469  regionIDs.assign(rIDsize, 0);
470 
471  int* rid = regionIDs.data();
473 
474  // we generate a map from tags to region IDs. This has many benefits, mainly we can check
475  // whether a tag is assigned to multiple regions and simplify our regionIDs filling loop
476  for (size_t reg=0; reg < regspec.size(); reg++) {
477  int err = 0;
478  for (int k=0; k < regspec[reg].nsubregs; k++) {
479  std::string curtag = regspec[reg].subregtags[k];
480  if(tag_to_reg.count(curtag)) err++;
481 
482  if(get_global(err, MPI_SUM))
483  log_msg(0,4,0, "%s warning: Tag idx %s is assigned to multiple regions!\n"
484  "Its final assignment will be to the highest assigned region ID!",
485  __func__, curtag.c_str());
486 
487  tag_to_reg[curtag] = reg;
488  }
489  }
490 
491  std::vector<int> mx_tag( rIDsize, -1 );
492 
493  // cycle through the element list
494  for(size_t eidx=0; eidx<nelem; eidx++)
495  {
496  std::vector<int> elem_nodes;
497  mesh_int_t tag = mesh.tag[eidx];
498  std::string result_pair_orginal;
499  std::string result_pair_reverse;
500  std::pair<mesh_int_t,mesh_int_t> value = map_elem_uniqueFace_to_tags[eidx];
501  result_pair_orginal = std::to_string(value.first) + ":" + std::to_string(value.second);
502  result_pair_reverse = std::to_string(value.second) + ":" + std::to_string(value.first);
503  rid[eidx] = pair_is_gapjunction(value, intra_tags) ? 1 : 0;
504 
505  if (tag_to_reg.count(result_pair_orginal)) {
506  rid[eidx] = tag_to_reg[result_pair_orginal];
507  } else if(tag_to_reg.count(result_pair_reverse)){
508  rid[eidx] = tag_to_reg[result_pair_reverse];
509  }
510  if(tag!=value.first){
511  log_msg(NULL, 5, ECHO, "error in tags on surface mesh with the unique faces!!!.\\n");
512  exit(1);
513  }
514  }
515 }
516 
519 double IonicsOnFace::timer_val(const int timer_id)
520 {
521  double val = std::nan("NaN");
522  return val;
523 }
524 
527 std::string IonicsOnFace::timer_unit(const int timer_id)
528 {
529  std::string s_unit;
530  return s_unit;
531 }
532 
533 void compute_IIF_OnFace(limpet::IonIfBase& pIF, limpet::GlobalData_t** impdata, int n)
534 {
535  if (impdata[limpet::Iion] != NULL)
536  impdata[limpet::Iion][n] = 0;
537 
538  pIF.for_each([&](limpet::IonIfBase& imp) {
539  update_ts(&imp.get_tstp());
540  imp.compute(n, n + 1, impdata);
541  });
542 }
543 
555 void* find_SV_in_IMP_onFace(limpet::MULTI_IF* miif, const int idx, const char *IMP, const char *SV,
556  int* offset, int* sz)
557 {
558  if(strcmp(IMP, miif->iontypes[idx].get().get_name().c_str()) == 0) {
559  return (void*) miif->iontypes[idx].get().get_sv_offset(SV, offset, sz);
560  }
561  else {
562  for( int k=0; k<miif->numplugs[idx]; k++ )
563  if(strcmp(IMP, miif->plugtypes[idx][k].get().get_name().c_str()) == 0) {
564  return (void*) miif->plugtypes[idx][k].get().get_sv_offset(SV, offset, sz);
565  }
566  }
567 
568  return NULL;
569 }
570 
571 
581 void alloc_gvec_data_onFace(const int nGVcs, const int nRegs, const int nEmiRegs,
582  GVecs *prmGVecs, gvec_data_OnFace &glob_vecs)
583 {
584  glob_vecs.nRegs = nRegs;
585 
586  if (nGVcs) {
587  glob_vecs.vecs.resize(nGVcs);
588 
589  for (size_t i = 0; i < glob_vecs.vecs.size(); i++) {
590  sv_data_onFace &gvec = glob_vecs.vecs[i];
591 
592  gvec.name = dupstr(prmGVecs[i].name);
593  gvec.units = dupstr(prmGVecs[i].units);
594  gvec.bogus = prmGVecs[i].bogus;
595 
596  gvec.imps = (char**) calloc(nRegs, sizeof(char *));
597  gvec.svNames = (char**) calloc(nRegs, sizeof(char *));
598  gvec.svSizes = (int*) calloc(nRegs, sizeof(int));
599  gvec.svOff = (int*) calloc(nRegs, sizeof(int));
600  gvec.getsv = (void**) calloc(nRegs, sizeof(limpet::SVgetfcn));
601 
602  for (int j = 0; j < nRegs; j++) {
603  if (strlen(prmGVecs[i].imp)) gvec.imps[j] = dupstr(prmGVecs[i].imp);
604 #ifdef WITH_PURK
605  else if (j < nEmiRegs)
606  gvec.imps[j] = dupstr(param_globals::imp_region_emi[j].im);
607  else
608  gvec.imps[j] = dupstr(param_globals::PurkIon[j - nEmiRegs].im);
609 #else
610  else if (j < nEmiRegs)
611  gvec.imps[j] = dupstr(param_globals::imp_region_emi[j].im);
612 #endif
613  gvec.svNames[j] = dupstr(prmGVecs[i].ID[j]);
614  }
615  }
616  }
617 }
618 
631 void init_sv_gvec_onFace(gvec_data_OnFace& GVs, limpet::MULTI_IF* miif, sf_vec & tmpl,
632  igb_output_manager & output_manager)
633 {
634  GVs.inclPS = false;
635  // int num_purk_regions = GVs->inclPS ? purk->ion.N_IIF : 0;
636  int num_purk_regions = 0;
637  int nEmiRegs = effective_num_imp_regions_emi();
638  int nRegs = nEmiRegs + num_purk_regions;
639 
640  alloc_gvec_data_onFace(param_globals::num_gvecs, nRegs, nEmiRegs, param_globals::gvec, GVs);
641 
642 #ifdef WITH_PURK
643  if (GVs->inclPS) sample_PS_ionSVs(purk);
644 #endif
645 
646  for (unsigned int i = 0; i < GVs.vecs.size(); i++) {
647  sv_data_onFace & gv = GVs.vecs[i];
648  int noSV = 0;
649 
650  for (int j = 0; j < miif->N_IIF; j++) {
651  gv.getsv[j] = find_SV_in_IMP_onFace(miif, j, gv.imps[j], gv.svNames[j],
652  gv.svOff + j, gv.svSizes + j);
653 
654  if (gv.getsv[j] == NULL) {
655  log_msg(NULL, 3, ECHO, "\tWarning: SV(%s) not found in region %d\n", gv.svNames[j], j);
656  noSV++;
657  }
658  }
659 
660  SF::init_vector(&gv.ordered, &tmpl);
661  output_manager.register_output(gv.ordered, intra_elec_msh, 1, gv.name, gv.units);
662 
663 #ifdef WITH_PURK
664  // same procedure for Purkinje
665  if (GVs->inclPS) {
666  IF_PURK_PROC(purk) {
667  MULTI_IF* pmiif = &purk->ion;
668  for (int j = miif->N_IIF; j < nRegs; j++) {
669  gv.getsv[j] = find_SV_in_IMP_onFace(pmiif, j - miif->N_IIF, gv.imps[j],
670  gv.svNames[j], gv.svOff + j, gv.svSizes + j);
671 
672  if (gv.getsv[j] == NULL) {
673  LOG_MSG(NULL, 3, ECHO, "\tWarning: state variable \"%s\" not found in region %d\n", gv.svNames[j], j);
674  noSV++;
675  }
676  }
677  RVector_dup(purk->vm_pt, &gv.orderedPS_PS);
678  MYO_COMM(purk);
679  }
680  RVector_dup(purk->vm_pt_over, &gv.orderedPS);
681  initialize_grid_output(grid, NULL, tmo, intra_elec_msh, GRID_WRITE, 0., 1., gv.units, gv.orderedPS,
682  1, gv.GVcName, -purk->npt, param_globals::output_level);
683  }
684 #endif
685 
686  if (noSV == nRegs) {
687  log_msg(NULL, 5, ECHO, "\tError: no state variables found for global vector %d\n", i);
688  log_msg(NULL, 5, ECHO, "Run bench --imp=YourModel --imp-info to get a list of all parameters.\\n");
689  exit(1);
690  }
691  }
692 }
693 
704 void assemble_sv_gvec_onFace(gvec_data_OnFace & gvecs, limpet::MULTI_IF *miif)
705 {
706  for(size_t i=0; i<gvecs.vecs.size(); i++ ) {
707  sv_data_onFace & gv = gvecs.vecs[i];
708 
709  // set to the defalt value
710  gv.ordered->set(gv.bogus);
711  SF_int start, stop;
712  gv.ordered->get_ownership_range(start, stop);
713 
714  for( int n = 0; n<miif->N_IIF; n++ ) {
715  if( !gv.getsv[n] ) continue;
716 
717  SF::vector<SF_real> data (miif->N_Nodes[n]);
718  SF::vector<SF_int> indices(miif->N_Nodes[n]);
719 
720  for( int j=0; j<miif->N_Nodes[n]; j++ ) {
721  indices[j] = miif->NodeLists[n][j] + start;
722  data[j] = ((limpet::SVgetfcn)(gv.getsv[n]))( *miif->IIF[n], j, gv.svOff[n]);
723  }
724 
725  bool add = false;
726  gv.ordered->set(indices, data, add);
727  }
728 
729 #ifdef WITH_PURK
730  // include purkinje in sv dump, if exists
731  if(gvecs->inclPS) {
732  RVector_set( gv.orderedPS, gv.bogus ); // set this to the default value
733 
734  if(IS_PURK_PROC(purk))
735  {
736  MULTI_IF *pmiif = &purk->ion;
737  Real *data = new Real[purk->gvec_cab.nitems];
738 
739  int ci=0;
740  for( int n = 0; n<pmiif->N_IIF; n++ ) {
741  int gvidx = n+miif->N_IIF;
742  if( !gv.getsv[gvidx] ) continue;
743  for( int j=0; j<purk->gvec_ion[n].nitems; j++ )
744  data[ci++] = ((SVgetfcn)(gv.getsv[gvidx]))( pmiif->IIF+n, ((int*)(purk->gvec_ion[n].data))[j],
745  gv.svOff[gvidx]);
746  }
747  RVector_setvals(gv.orderedPS, purk->gvec_cab.nitems, (int*)purk->gvec_cab.data, data, true);
748  delete [] data;
749  }
750  RVector_sync( gv.orderedPS );
751  }
752 #endif
753  }
754 }
755 
756 
757 } // namespace opencarp
758 #endif
double Real
Definition: DataTypes.h:14
int mesh_int_t
Definition: SF_container.h:46
std::int32_t SF_int
Use the general std::int32_t as int type.
Definition: SF_globals.h:37
#define FLUSH
Definition: basics.h:327
#define ECHO
Definition: basics.h:324
#define NONL
Definition: basics.h:328
Comfort class. Provides getter functions to access the mesh member variables more comfortably.
Definition: SF_fem_utils.h:704
Container for a PETSc VecScatter.
A vector storing arbitrary data.
Definition: SF_vector.h:43
size_t size() const
The current size of the vector.
Definition: SF_vector.h:104
void assign(InputIterator s, InputIterator e)
Assign a memory range.
Definition: SF_vector.h:161
T * data()
Pointer to the vector's start.
Definition: SF_vector.h:91
hm_int count(const K &key) const
Check if key exists.
Definition: hashmap.hpp:623
Custom unordered_set implementation.
Definition: hashmap.hpp:750
iterator find(const K &key)
Definition: hashmap.hpp:1092
hm_int count(const K &key) const
Definition: hashmap.hpp:1078
void insert(InputIterator first, InputIterator last)
Definition: hashmap.hpp:1048
Represents the ionic model and plug-in (IMP) data structure.
Definition: ION_IF.h:139
ts & get_tstp()
Gets the time stepper.
Definition: ION_IF.cc:208
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:417
void compute(int start, int end, GlobalData_t **data)
Perform ionic model computation for 1 time step.
Definition: ION_IF.cc:270
std::vector< IonIfBase * > IIF
array of IIF's
Definition: MULTI_ION_IF.h:202
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:209
std::vector< IonTypeList > plugtypes
plugins types for each region
Definition: MULTI_ION_IF.h:215
IonTypeList iontypes
type for each region
Definition: MULTI_ION_IF.h:212
int N_IIF
how many different IIF's
Definition: MULTI_ION_IF.h:211
int * N_Nodes
#nodes for each IMP
Definition: MULTI_ION_IF.h:200
int ** NodeLists
local partitioned node lists for each IMP stored
Definition: MULTI_ION_IF.h:201
void setup(double inp_dt, double inp_start, double inp_end)
Initialize the timer_manager.
Definition: timer_utils.cc:36
void reset_timers()
Reset time in timer_manager and then reset registered timers.
Definition: timer_utils.h:115
Electrical ionics functions (gap junction + ionic current) on the face of interface mesh for EMI mesh...
T local_nodal_to_local_petsc(const meshdata< T, S > &mesh, int rank, T local_nodal)
void init_vector(SF::abstract_vector< T, S > **vec)
Definition: SF_init.h:99
SF_nbr
Enumeration encoding the different supported numberings.
Definition: SF_container.h:200
@ NBR_PETSC
PETSc numbering of nodes.
Definition: SF_container.h:203
@ NBR_REF
The nodal numbering of the reference mesh (the one stored on HD).
Definition: SF_container.h:201
@ NBR_SUBMESH
Submesh nodal numbering: The globally ascending sorted reference indices are reindexed.
Definition: SF_container.h:202
int get_plug_flag(char *plgstr, int *out_num_plugins, IonTypeList &out_plugins)
GlobalData_t(* SVgetfcn)(IonIfBase &, int, int)
Definition: ion_type.h:48
@ AUTO
Definition: target.h:46
IonType * get_ion_type(const std::string &name)
SF_real GlobalData_t
Definition: limpet_types.h:27
void update_ts(ts *ptstp)
Definition: ION_IF.cc:466
int read_sv(MULTI_IF *, int, const char *)
char IIF_Mask_t
Definition: ion_type.h:50
std::map< int, std::string > units
Definition: stimulate.cc:41
timer_manager * tm_manager
a manager for the various physics timers
Definition: main.cc:58
@ iotm_console
Definition: timer_utils.h:44
sf_mesh & get_mesh(const mesh_t gt)
Get a mesh by specifying the gridID.
Definition: sf_interface.cc:33
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:48
int set_dir(IO_t dest)
Definition: sim_utils.cc:494
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:72
int get_rank(MPI_Comm comm=PETSC_COMM_WORLD)
Definition: basics.h:292
T get_global(T in, MPI_Op OP, MPI_Comm comm=PETSC_COMM_WORLD)
Do a global reduction on a variable.
Definition: basics.h:233
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:927
@ OUTPUT
Definition: sim_utils.h:55
char * dupstr(const char *old_str)
Definition: basics.cc:44
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
Definition: basics.cc:72
mesh_t
The enum identifying the different meshes we might want to load.
Definition: sf_interface.h:59
@ intra_elec_msh
Definition: sf_interface.h:60
void get_time(double &tm)
Definition: basics.h:452
SF::abstract_vector< SF_int, SF_real > sf_vec
Definition: sf_interface.h:50
void remove_char(char *buff, const int buffsize, const char c)
Definition: basics.h:372
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:269
V timing(V &t2, const V &t1)
Definition: basics.h:464
#define PETSC_TO_CANONICAL
Permute algebraic data from PETSC to canonical ordering.
Definition: sf_interface.h:79