openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
ionics.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 #include "ionics.h"
13 
14 #include "SF_init.h"
15 
16 namespace opencarp {
17 
18 void initialize_sv_dumps(limpet::MULTI_IF *pmiif, IMPregion* reg, int id, double t, double dump_dt);
19 
21 {
22  double t1, t2;
23  get_time(t1);
24 
26 
27  double comp_time = timing(t2, t1);
28  this->compute_time += comp_time;
29 
30  comp_stats.calls++;
31  comp_stats.tot_time += comp_time;
32 
35 }
36 
38 {
39  miif->free_MIIF();
40 }
41 
43 {}
44 
46 {
47  double t1, t2;
48  get_time(t1);
49 
50  set_dir(OUTPUT);
51 
52  // initialize generic logger for ODE timings per time_dt
53  comp_stats.init_logger("ODE_stats.dat");
54 
55  double tstart = 0.;
56  sf_mesh & mesh = get_mesh(ion_domain);
57  limpet::node_count_t loc_size = mesh.pl.num_algebraic_idx();
58 
59  // create ionic current vector and register it
60  sf_vec *IIon;
61  sf_vec *Vmv;
62  SF::init_vector(&IIon, mesh, 1, sf_vec::algebraic);
63  SF::init_vector(&Vmv, mesh, 1, sf_vec::algebraic);
64  register_data(IIon, iion_vec);
65  register_data(Vmv, vm_vec);
66 
67  // setup miif
68  miif = new limpet::MULTI_IF();
69  // hand down the physics log file, so that what MULTI_IF reports while setting up and
70  // restoring state is recorded rather than only echoed to the console
71  miif->logger = logger;
72  // store IIF_IDs and Plugins in arrays
73  miif->name = "myocardium";
74  miif->gdata[limpet::Vm] = Vmv;
75  miif->gdata[limpet::Iion] = IIon;
76 
77  int rank = get_rank();
78 
79  SF::vector<RegionSpecs> rs(param_globals::num_imp_regions);
80  for (size_t i = 0; i < rs.size(); i++ ) {
81  rs[i].nsubregs = param_globals::imp_region[i].num_IDs;
82  rs[i].subregtags = param_globals::imp_region[i].ID;
83  for (int j=0;j<rs[i].nsubregs;j++) {
84  if(rs[i].subregtags[j]==-1 && rank==0)
85  log_msg(NULL,3,ECHO, "Warning: not all %u IDs provided for imp_region[%u]!\n", rs[i].nsubregs, i);
86  }
87  }
88 
89  SF::vector<int> reg_mask;
90  region_mask(ion_domain, rs, reg_mask, false, "imp_region");
91 
92  int purkfLen = 1;
93  tstart = setup_MIIF(loc_size, param_globals::num_imp_regions, param_globals::imp_region,
94  reg_mask.data(), param_globals::start_statef, param_globals::num_adjustments,
95  param_globals::adjustment, param_globals::dt, purkfLen > 0);
96 
97  miif->extUpdateVm = !param_globals::operator_splitting;
98 
99  // if we start at a non-zero time (i.e. we have restored a state), we notify the
100  // timer manager
101  if(tstart > 0.) {
102  log_msg(logger, 0, 0, "Changing simulation start time to %.2lf", tstart);
103  user_globals::tm_manager->setup(param_globals::dt, tstart, param_globals::tend);
105  }
106 
107  set_dir(INPUT);
108 
109  this->initialize_time += timing(t2, t1);
110 }
111 
112 double Ionics::setup_MIIF(limpet::node_count_t nnodes, int nreg, IMPregion* impreg, int* mask,
113  const char *start_fn, int numadjust, IMPVariableAdjustment *adjust,
114  double time_step, bool close)
115 {
116  double tstart = 0;
117 
118  miif->N_IIF = nreg;
119  miif->numNode = nnodes;
120  miif->iontypes = {};
121  miif->numplugs = (int*)calloc( miif->N_IIF, sizeof(int));
122  miif->plugtypes = std::vector<limpet::IonTypeList>(miif->N_IIF);
123  miif->targets = std::vector<limpet::Target>(miif->N_IIF, limpet::Target::AUTO);
124 
125  log_msg(logger,0,ECHO, "\nSetting up ionic models and plugins\n" \
126  "-----------------------------------\n\n" \
127  "Assigning IMPS to tagged regions:" );
128 
129  for (int i=0;i<miif->N_IIF;i++) {
130  auto pT = limpet::get_ion_type(std::string(impreg[i].im));
131  if (pT != NULL)
132  {
133  miif->iontypes.push_back(*pT);
134  log_msg(logger, 0, ECHO|NONL, "\tIonic model: %s to tag region(s)", impreg[i].im);
135 
136  if(impreg[i].num_IDs > 0) {
137  for(int j = 0; j < impreg[i].num_IDs; j++)
138  log_msg(logger,0,ECHO|NONL, " [%d],", impreg[i].ID[j]);
139  log_msg(logger,0,ECHO,"\b.");
140  }
141  else {
142  log_msg(logger,0,ECHO, " [0] (implicitely)");
143  }
144  }
145  else {
146  log_msg(NULL,5,ECHO, "Illegal IM specified: %s\n", impreg[i].im );
147  log_msg(NULL,5,ECHO, "Run bench --list-imps for a list of all available models.\n" );
148  EXIT(1);
149  }
150  if (limpet::get_plug_flag( impreg[i].plugins, &miif->numplugs[i], miif->plugtypes[i]))
151  {
152  if(impreg[i].plugins[0] != '\0') {
153  log_msg(logger,0, ECHO|NONL, "\tPlug-in(s) : %s to tag region(s)", impreg[i].plugins);
154 
155  for(int j = 0; j < impreg[i].num_IDs; j++)
156  log_msg(logger,0,ECHO|NONL, " [%d],", impreg[i].ID[j]);
157  log_msg(logger,0,ECHO,"\b.");
158  }
159  }
160  else {
161  log_msg(NULL,5,ECHO,"Illegal plugin specified: %s\n", impreg[i].plugins);
162  log_msg(NULL,5,ECHO, "Run bench --list-imps for a list of all available plugins.\n" );
163  EXIT(1);
164  }
165  }
166 
168 
169  // The mask is a nodal vector with the region IDs of each node.
170  // It is already reduced during the region_mask call to guarantee unique values
171  // for overlapping interface nodes
172  if (mask) {
173  for (limpet::node_index_t i=0; i<miif->numNode; i++)
174  miif->IIFmask[i] = (limpet::IIF_Mask_t) mask[i];
175  }
176 
178 
179  for(int i=0; i<miif->N_IIF; i++)
180  if(!miif->IIF[i]->cgeom().SVratio)
181  miif->IIF[i]->cgeom().SVratio = param_globals::imp_region[i].cellSurfVolRatio;
182 
183  for (int i=0;i<miif->N_IIF;i++) {
184  // the IMP tuning does not handle spaces well, thus we remove them here
185  remove_char(impreg[i].im_param, strlen(impreg[i].im_param), ' ');
186  miif->IIF[i]->tune(impreg[i].im_param, impreg[i].plugins, impreg[i].plug_param);
187  }
188 
189  set_dir(INPUT);
190  miif->initialize_currents(time_step, param_globals::ode_fac);
191 
192  // overriding initial values goes here
193  // read in single cell state vector and spread it out over the entire region
194  set_dir(INPUT);
195  for (int i=0;i<miif->N_IIF;i++) {
196  if (impreg[i].im_sv_init && strlen(impreg[i].im_sv_init) > 0)
197  if (read_sv(miif, i, impreg[i].im_sv_init)) {
198  log_msg(NULL, 5, ECHO|FLUSH, "State vector initialization failed for %s.\n", impreg[i].name);
199  EXIT(-1);
200  }
201  }
202 
203  if( !start_fn || strlen(start_fn)>0 )
204  tstart = (double) miif->restore_state(start_fn, ion_domain, close);
205 
206  for (int i=0; i<numadjust; i++)
207  {
208  set_dir(INPUT);
209 
210  SF::vector<SF_int> indices;
211  SF::vector<SF_real> values;
212  bool restrict_to_algebraic = true;
213 
214  sf_mesh & imesh = get_mesh(ion_domain);
215  std::map<std::string,std::string> metadata;
216  read_metadata(adjust[i].file, metadata, PETSC_COMM_WORLD);
217 
218  SF::SF_nbr nbr = SF::NBR_REF;
219  if(metadata.count("grid") && metadata["grid"].compare("intra") == 0) {
220  nbr = SF::NBR_SUBMESH;
221  }
222  read_indices_with_data(indices, values, adjust[i].file, imesh, nbr, restrict_to_algebraic, 1, PETSC_COMM_WORLD);
223 
224  int rank = get_rank();
225 
226  for(size_t gi = 0; gi < indices.size(); gi++)
227  indices[gi] = SF::local_nodal_to_local_petsc<mesh_int_t,mesh_real_t>(imesh, rank, indices[gi]);
228 
229  // debug, output parameters on global intracellular vector
230  if(adjust[i].dump) {
231  sf_vec* adjPars;
232  SF::init_vector(&adjPars, imesh, 1, sf_vec::algebraic);
233  adjPars->set(indices, values, false, true);
234 
235  set_dir(OUTPUT);
236  char fname[2085];
237  snprintf(fname, sizeof fname, "adj_%s_perm.dat", adjust[i].variable);
238  adjPars->write_ascii(fname, false);
239 
240  // get the scattering to the canonical permutation
242  if(sc == NULL) {
243  log_msg(0,3,0, "%s warning: PETSC_TO_CANONICAL permutation needed registering!", __func__);
245  }
246 
247  (*sc)(*adjPars, true);
248  snprintf(fname, sizeof fname, "adj_%s_canonical.dat", adjust[i].variable);
249  adjPars->write_ascii(fname, false);
250  }
251 
252  int nc = miif->adjust_MIIF_variables(adjust[i].variable, indices, values);
253  log_msg(logger, 0, 0, "Adjusted %d values for %s", nc, adjust[i].variable);
254  }
255 
256  set_dir(OUTPUT);
257 
258  for (int i=0;i<miif->N_IIF;i++)
259  initialize_sv_dumps(miif, impreg+i, i, tstart, param_globals::spacedt);
260 
261  return tstart;
262 }
263 
273 void initialize_sv_dumps(limpet::MULTI_IF *pmiif, IMPregion* reg, int id, double t, double dump_dt)
274 {
275  char svs[1024], plgs[1024], plgsvs[1024], fname[1024];
276 
277  strcpy(svs, reg->im_sv_dumps ? reg->im_sv_dumps : "");
278  strcpy(plgs, reg->plugins ? reg->plugins : "");
279  strcpy(plgsvs, reg->plug_sv_dumps ? reg->plug_sv_dumps : "");
280 
281  if( !(strlen(svs)+strlen(plgsvs) ) )
282  return;
283 
284  /* The string passed to the "reg_name" argument (#4) of the sv_dump_add
285  * function is supposed to be "region name". It's only purpose is to
286  * provide the base name for the SV dump file, eg: Purkinje.Ca_i.bin.
287  * Thus, we pass: [vofile].[reg name], Otherwise, dumping SVs in
288  * batched runs would be extremely tedious.
289  */
290  strcpy(fname, param_globals::vofile); // [vofile].igb
291 
292  if( !reg->name ) {
293  log_msg(NULL, 5, ECHO, "%s: a region name must be specified\n", __func__ );
294  exit(0);
295  }
296 
297  // We want to convert vofile.igb to vofile.regname
298  size_t fname_len = strlen(fname);
299  char* ext_start = fname + fname_len;
300  if(fname_len >= 4 && strcmp(ext_start - 4, ".igb") == 0) ext_start -= 3;
301  strcpy(ext_start, reg->name);
302 
303  pmiif->sv_dump_add_by_name_list(id, reg->im, fname, svs, plgs, plgsvs, t, dump_dt);
304 }
305 
317  const char* gridname, const char* reglist)
318 {
319  bool AllTagsExist = true;
320 
322  tagset.insert(tags.begin(), tags.end());
323 
324  // cycle through all user-specified regions
325  for (size_t reg=0; reg<regspec.size(); reg++)
326  // cycle through all tags which belong to the region
327  for (int k=0; k<regspec[reg].nsubregs; k++) {
328  // check whether this tag exists in element list
329  int n = 0;
330  if(tagset.count(regspec[reg].subregtags[k])) n++;
331  // globalize n
332  int N = get_global(n, MPI_SUM);
333  if (N==0) {
334  if(strcmp(reglist, "gregion_vol"))
335  log_msg(NULL, 3, ECHO,
336  "%s[%d] references tag %d, but no element in the %s grid carries this tag — region will have no elements.\n",
337  reglist, reg, regspec[reg].subregtags[k], gridname);
338  AllTagsExist = false;
339  }
340  }
341 
342  if (!AllTagsExist) {
343  log_msg(NULL, 4, ECHO,"One or more configured regions are empty. Check that region tag IDs match the tags in your mesh or tagfile.\n");
344  }
345 
346  return AllTagsExist;
347 }
348 
350  const char* gridname, const char* reglist,
351  bool warn_on_default_tags)
352 {
353  if(!warn_on_default_tags) return;
354 
355  // build the set of tags covered by configured regions (1..N-1; region 0 is the implicit default)
357  for (size_t reg = 1; reg < regspec.size(); reg++)
358  for (int k = 0; k < regspec[reg].nsubregs; k++)
359  configured.insert(regspec[reg].subregtags[k]);
360 
361  SF::vector<mesh_int_t> unmatched;
362  long int n_defaulted = 0;
363  for (const mesh_int_t & t : tags) {
364  if (!configured.count(t)) {
365  unmatched.push_back(t);
366  n_defaulted++;
367  }
368  }
369 
370  n_defaulted = get_global(n_defaulted, MPI_SUM);
371  if (!n_defaulted) return;
372 
373  binary_sort(unmatched);
374  unique_resize(unmatched);
375  make_global(unmatched, PETSC_COMM_WORLD);
376  binary_sort(unmatched);
377  unique_resize(unmatched);
378 
379  std::string taglist;
380  for (size_t i = 0; i < unmatched.size(); i++) {
381  if (i) taglist += ", ";
382  taglist += std::to_string(unmatched[i]);
383  }
384 
385  log_msg(NULL, 3, ECHO,
386  "%s: %ld element(s) in %s grid carry tags {%s} not assigned to any "
387  "%s region; these elements default to region 0.\n",
388  __func__, n_defaulted, gridname, taglist.c_str(), reglist);
389 }
390 
391 void region_mask(mesh_t meshspec, SF::vector<RegionSpecs> & regspec,
392  SF::vector<int> & regionIDs, bool mask_elem, const char* reglist,
393  bool warn_on_default_tags)
394 {
395  if(regspec.size() == 1) return;
396 
397  sf_mesh & mesh = get_mesh(meshspec);
399 
400  // initialize the list with the default regionID, 0
401  size_t rIDsize = mask_elem ? mesh.l_numelem : mesh.l_numpts;
402 
403  size_t nelem = mesh.l_numelem;
404  const SF::vector<mesh_int_t> & tags = mesh.tag;
405 
406  // check whether all specified tags exist in the element list
407  check_tags_in_elems(tags, regspec, mesh.name.c_str(), reglist);
408  check_unassigned_tags(tags, regspec, mesh.name.c_str(), reglist, warn_on_default_tags);
409 
410  regionIDs.assign(rIDsize, 0);
412 
413  // we generate a map from tags to region IDs. This has many benefits, mainly we can check
414  // whether a tag is assigned to multiple regions and simplify our regionIDs filling loop
415  for (size_t reg=1; reg < regspec.size(); reg++) {
416  int err = 0;
417  for (int k=0; k < regspec[reg].nsubregs; k++) {
418  int curtag = regspec[reg].subregtags[k];
419  if(tag_to_reg.count(curtag)) err++;
420 
421  if(get_global(err, MPI_SUM))
422  log_msg(0,4,0, "%s warning: Tag idx %d is assigned to multiple regions!\n"
423  "Its final assignment will be to the highest assigned region ID!",
424  __func__, curtag);
425 
426  tag_to_reg[curtag] = reg;
427  }
428  }
429 
430  SF::vector<int> mx_tag(regionIDs.size(), -1);
431 
432  // cycle through the element list
433  for(size_t i=0; i<nelem; i++) {
434  const mesh_int_t & cur_tag = tags[i];
435  // check if current element tag has a custom region ID
436  if (tag_to_reg.count(cur_tag))
437  {
438  int reg = tag_to_reg[cur_tag];
439 
440  if (mask_elem) regionIDs[i] = reg;
441  else {
442  eview.set_elem(i);
443 
444  for (int j=0; j < eview.num_nodes(); j++) {
445  mesh_int_t n = eview.node(j);
446  if (cur_tag > mx_tag[n]) mx_tag[n] = cur_tag;
447  }
448  }
449  }
450  }
451 
452  if(mask_elem) return;
453 
454  // The tie between the tags meeting at a node must be broken on the tags themselves.
455  // Reducing the region IDs instead would let the highest region win at nodes shared
456  // between ranks while the highest tag wins everywhere else, making the assignment
457  // depend on the partitioning whenever the tags are not listed in ascending order.
458  mesh.pl.reduce(mx_tag, "max");
459 
460  for(size_t i=0; i < regionIDs.size(); i++)
461  if(mx_tag[i] >= 0) regionIDs[i] = tag_to_reg[mx_tag[i]];
462 
463  const SF::vector<mesh_int_t> & alg_nod = mesh.pl.algebraic_nodes();
465 
466  SF::vector<int> alg_reg(alg_nod.size());
467  SF::vector<int> gids (alg_nod.size());
468 
469  for(size_t i=0; i<alg_nod.size(); i++) {
470  alg_reg[i] = regionIDs[alg_nod[i]];
471  gids[i] = nbr[alg_nod[i]];
472  }
473 
474  regionIDs = alg_reg;
475 
476  if(param_globals::dump_imp_region) {
477  set_dir(OUTPUT);
478  write_data_ascii(PETSC_COMM_WORLD, gids, regionIDs, std::string(reglist)+".dat");
479  }
480 }
481 
484 double Ionics::timer_val(const int timer_id)
485 {
486  double val = std::nan("NaN");
487  return val;
488 }
489 
492 std::string Ionics::timer_unit(const int timer_id)
493 {
494  std::string s_unit;
495  return s_unit;
496 }
497 
499 {
500  if (impdata[limpet::Iion] != NULL)
501  impdata[limpet::Iion][n] = 0;
502 
503  pIF.for_each([&](limpet::IonIfBase& imp) {
504  update_ts(&imp.get_tstp());
505  imp.compute(n, n + 1, impdata);
506  });
507 }
508 
520 void* find_SV_in_IMP(limpet::MULTI_IF* miif, const int idx, const char *IMP, const char *SV,
521  int* offset, int* sz, int* plugin_idx)
522 {
523  *plugin_idx = -1;
524 
525  if(strcmp(IMP, miif->iontypes[idx].get().get_name().c_str()) == 0) {
526  return (void*) miif->iontypes[idx].get().get_sv_offset(SV, offset, sz);
527  }
528  else {
529  for(int k=0; k<miif->numplugs[idx]; k++)
530  if(strcmp(IMP, miif->plugtypes[idx][k].get().get_name().c_str()) == 0) {
531  *plugin_idx = k;
532  return (void*) miif->plugtypes[idx][k].get().get_sv_offset(SV, offset, sz);
533  }
534  }
535 
536  return NULL;
537 }
538 
539 
549 void alloc_gvec_data(const int nGVcs, const int nRegs,
550  GVecs *prmGVecs, gvec_data &glob_vecs)
551 {
552  glob_vecs.nRegs = nRegs;
553 
554  if (nGVcs) {
555  glob_vecs.vecs.resize(nGVcs);
556  glob_vecs.plugin_idx.resize(nGVcs);
557 
558  for (size_t i = 0; i < glob_vecs.vecs.size(); i++) {
559  sv_data &gvec = glob_vecs.vecs[i];
560 
561  gvec.name = dupstr(prmGVecs[i].name);
562  gvec.units = dupstr(prmGVecs[i].units);
563  gvec.bogus = prmGVecs[i].bogus;
564 
565  gvec.imps = (char**) calloc(nRegs, sizeof(char *));
566  gvec.svNames = (char**) calloc(nRegs, sizeof(char *));
567  gvec.svSizes = (int*) calloc(nRegs, sizeof(int));
568  gvec.svOff = (int*) calloc(nRegs, sizeof(int));
569  gvec.getsv = (void**) calloc(nRegs, sizeof(limpet::SVgetfcn));
570 
571  for (int j = 0; j < nRegs; j++) {
572  if (strlen(prmGVecs[i].imp)) gvec.imps[j] = dupstr(prmGVecs[i].imp);
573 #ifdef WITH_PURK
574  else if (j < param_globals::num_imp_regions)
575  gvec.imps[j] = dupstr(param_globals::imp_region[j].im);
576  else
577  gvec.imps[j] = dupstr(param_globals::PurkIon[j - param_globals::num_imp_regions].im);
578 #else
579  else if (j < param_globals::num_imp_regions)
580  gvec.imps[j] = dupstr(param_globals::imp_region[j].im);
581 #endif
582  gvec.svNames[j] = dupstr(prmGVecs[i].ID[j]);
583  }
584  }
585  }
586 }
587 
601  igb_output_manager & output_manager)
602 {
603  GVs.inclPS = false;
604  // int num_purk_regions = GVs->inclPS ? purk->ion.N_IIF : 0;
605  int num_purk_regions = 0;
606  int nRegs = param_globals::num_imp_regions + num_purk_regions;
607 
608  alloc_gvec_data(param_globals::num_gvecs, nRegs, param_globals::gvec, GVs);
609 
610 #ifdef WITH_PURK
611  if (GVs->inclPS) sample_PS_ionSVs(purk);
612 #endif
613 
614  for (unsigned int i = 0; i < GVs.vecs.size(); i++) {
615  sv_data & gv = GVs.vecs[i];
616  int noSV = 0;
617 
618  for (int j = 0; j < miif->N_IIF; j++) {
619  gv.getsv[j] = find_SV_in_IMP(miif, j, gv.imps[j], gv.svNames[j], gv.svOff + j, gv.svSizes + j, &GVs.plugin_idx[i]);
620 
621  if (gv.getsv[j] == NULL) {
622  log_msg(NULL, 3, ECHO, "\tWarning: SV(%s) not found in region %d\n", gv.svNames[j], j);
623  noSV++;
624  }
625  }
626 
627  SF::init_vector(&gv.ordered, &tmpl);
628  output_manager.register_output(gv.ordered, intra_elec_msh, 1, gv.name, gv.units);
629 
630 #ifdef WITH_PURK
631  // same procedure for Purkinje
632  if (GVs->inclPS) {
633  IF_PURK_PROC(purk) {
634  MULTI_IF* pmiif = &purk->ion;
635  for (int j = miif->N_IIF; j < nRegs; j++) {
636  gv.getsv[j] = find_SV_in_IMP(pmiif, j - miif->N_IIF, gv.imps[j],
637  gv.svNames[j], gv.svOff + j, gv.svSizes + j);
638 
639  if (gv.getsv[j] == NULL) {
640  LOG_MSG(NULL, 3, ECHO, "\tWarning: state variable \"%s\" not found in region %d\n", gv.svNames[j], j);
641  noSV++;
642  }
643  }
644  RVector_dup(purk->vm_pt, &gv.orderedPS_PS);
645  MYO_COMM(purk);
646  }
647  RVector_dup(purk->vm_pt_over, &gv.orderedPS);
648  initialize_grid_output(grid, NULL, tmo, intra_elec_msh, GRID_WRITE, 0., 1., gv.units, gv.orderedPS,
649  1, gv.GVcName, -purk->npt, param_globals::output_level);
650  }
651 #endif
652 
653  if (noSV == nRegs) {
654  log_msg(NULL, 5, ECHO, "\tError: no state variables found for global vector %d\n", i);
655  log_msg(NULL, 5, ECHO, "Run bench --imp=YourModel --imp-info to get a list of all parameters.\\n");
656  exit(1);
657  }
658  }
659 }
660 
672 {
673  for(size_t i=0; i<gvecs.vecs.size(); i++ ) {
674  sv_data & gv = gvecs.vecs[i];
675  int plugin_idx = gvecs.plugin_idx[i];
676 
677  // set to the defalt value
678  gv.ordered->set(gv.bogus);
679  SF_int start, stop;
680  gv.ordered->get_ownership_range(start, stop);
681 
682  for( int n = 0; n<miif->N_IIF; n++ ) {
683  if( !gv.getsv[n] ) continue;
684 
685  SF::vector<SF_real> data (miif->N_Nodes[n]);
686  SF::vector<SF_int> indices(miif->N_Nodes[n]);
687 
688  limpet::IonIfBase* base_im = miif->IIF[n];
689  limpet::IonIfBase* imp = plugin_idx > -1 ? base_im->plugins()[plugin_idx] : base_im;
690 
691  for( limpet::node_index_t j=0; j<miif->N_Nodes[n]; j++ ) {
692  indices[j] = miif->NodeLists[n][j] + start;
693  data[j] = ((limpet::SVgetfcn)(gv.getsv[n]))( *miif->IIF[n], j, gv.svOff[n]);
694  }
695 
696  bool add = false;
697  gv.ordered->set(indices, data, add);
698  }
699  }
700 }
701 
702 
703 } // namespace opencarp
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
virtual void get_ownership_range(T &start, T &stop) const =0
virtual void set(const vector< T > &idx, const vector< S > &vals, const bool additive=false, const bool local=false)=0
Comfort class. Provides getter functions to access the mesh member variables more comfortably.
Definition: SF_fem_utils.h:689
const T & node(short nidx) const
Access the connectivity information.
Definition: SF_fem_utils.h:778
void set_elem(size_t eidx)
Set the view to a new element.
Definition: SF_fem_utils.h:716
T num_nodes() const
Getter function for the number of nodes.
Definition: SF_fem_utils.h:746
overlapping_layout< T > pl
nodal parallel layout
Definition: SF_container.h:414
size_t l_numelem
local number of elements
Definition: SF_container.h:384
std::string name
the mesh name
Definition: SF_container.h:392
size_t l_numpts
local number of points
Definition: SF_container.h:386
vector< T > & get_numbering(SF_nbr nbr_type)
Get the vector defining a certain numbering.
Definition: SF_container.h:449
vector< T > tag
element tag
Definition: SF_container.h:402
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 resize(size_t n)
Resize a vector.
Definition: SF_vector.h:194
const T * end() const
Pointer to the vector's end.
Definition: SF_vector.h:113
void assign(InputIterator s, InputIterator e)
Assign a memory range.
Definition: SF_vector.h:146
const T * begin() const
Pointer to the vector's start.
Definition: SF_vector.h:101
T * data()
Pointer to the vector's start.
Definition: SF_vector.h:76
T & push_back(T val)
Definition: SF_vector.h:268
hm_int count(const K &key) const
Check if key exists.
Definition: hashmap.hpp:612
Custom unordered_set implementation.
Definition: hashmap.hpp:739
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
bool extUpdateVm
flag indicating update function for Vm
Definition: MULTI_ION_IF.h:204
std::vector< IonIfBase * > IIF
array of IIF's
Definition: MULTI_ION_IF.h:198
opencarp::sf_vec * gdata[NUM_IMP_DATA_TYPES]
data used by all IMPs
Definition: MULTI_ION_IF.h:212
void sv_dump_add_by_name_list(int, char *, char *, char *, char *, char *, double, double)
node_count_t numNode
local number of nodes
Definition: MULTI_ION_IF.h:206
int * numplugs
number of plugins for each region
Definition: MULTI_ION_IF.h:205
std::vector< Target > targets
target for each region
Definition: MULTI_ION_IF.h:209
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
void initialize_currents(double, int)
float restore_state(const char *, opencarp::mesh_t gid, bool)
int N_IIF
how many different IIF's
Definition: MULTI_ION_IF.h:207
void compute_ionic_current(bool flag_send=1, bool flag_receive=1)
GPU kernel to emulate the add_scaled call made to adjust the Vm values when the update to Vm is not m...
node_count_t * N_Nodes
#nodes for each IMP
Definition: MULTI_ION_IF.h:196
opencarp::FILE_SPEC logger
Definition: MULTI_ION_IF.h:213
node_index_t ** NodeLists
local partitioned node lists for each IMP stored
Definition: MULTI_ION_IF.h:197
IIF_Mask_t * IIFmask
region for each node
Definition: MULTI_ION_IF.h:210
int adjust_MIIF_variables(const char *variable, const SF::vector< SF_int > &indices, const SF::vector< SF_real > &values)
std::string name
name for MIIF region
Definition: MULTI_ION_IF.h:194
FILE_SPEC logger
The logger of the physic, each physic should have one.
Definition: physics_types.h:49
const char * name
The name of the physic, each physic should have one.
Definition: physics_types.h:47
void output_step()
Definition: ionics.cc:42
mesh_t ion_domain
Definition: ionics.h:53
generic_timing_stats comp_stats
Definition: ionics.h:55
limpet::MULTI_IF * miif
Definition: ionics.h:52
void compute_step()
Definition: ionics.cc:20
void initialize()
Definition: ionics.cc:45
void destroy()
Definition: ionics.cc:37
std::string timer_unit(const int timer_id)
figure out units of a signal linked to a given timer
Definition: ionics.cc:492
double timer_val(const int timer_id)
figure out current value of a signal linked to a given timer
Definition: ionics.cc:484
void register_output(sf_vec *inp_data, const mesh_t inp_meshid, const int dpn, const char *name, const char *units, const SF::vector< mesh_int_t > *idx=NULL, bool elem_data=false)
Register a data vector for output.
Definition: sim_utils.cc:2850
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
Electrical ionics functions and LIMPET wrappers.
void write_data_ascii(const MPI_Comm comm, const vector< T > &idx, const vector< S > &data, std::string file, short dpn=1)
void make_global(const vector< T > &vec, vector< T > &out, MPI_Comm comm)
make a parallel vector global
Definition: SF_network.h:210
void unique_resize(vector< T > &_P)
Definition: SF_sort.h:338
void init_vector(SF::abstract_vector< T, S > **vec)
Definition: SF_init.h:110
void binary_sort(vector< T > &_V)
Definition: SF_sort.h:274
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
void compute_IIF(limpet::IonIfBase &pIF, limpet::GlobalData_t **impdata, limpet::node_index_t n)
Definition: ionics.cc:498
void * find_SV_in_IMP(limpet::MULTI_IF *miif, const int idx, const char *IMP, const char *SV, int *offset, int *sz, int *plugin_idx)
Definition: ionics.cc:520
@ 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.
void region_mask(mesh_t meshspec, SF::vector< RegionSpecs > &regspec, SF::vector< int > &regionIDs, bool mask_elem, const char *reglist, bool warn_on_default_tags)
classify elements/points as belonging to a region
Definition: ionics.cc:391
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
void check_unassigned_tags(const SF::vector< mesh_int_t > &tags, SF::vector< RegionSpecs > &regspec, const char *gridname, const char *reglist, bool warn_on_default_tags)
Definition: ionics.cc:349
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
void initialize_sv_dumps(limpet::MULTI_IF *pmiif, IMPregion *reg, int id, double t, double dump_dt)
Definition: ionics.cc:273
void init_sv_gvec(gvec_data &GVs, limpet::MULTI_IF *miif, sf_vec &tmpl, igb_output_manager &output_manager)
Definition: ionics.cc:600
void assemble_sv_gvec(gvec_data &gvecs, limpet::MULTI_IF *miif)
Definition: ionics.cc:671
char * dupstr(const char *old_str)
Definition: basics.cc:29
bool check_tags_in_elems(const SF::vector< mesh_int_t > &tags, SF::vector< RegionSpecs > &regspec, const char *gridname, const char *reglist)
Check whether the tags in the region spec struct matches with an array of tags.
Definition: ionics.cc:316
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 alloc_gvec_data(const int nGVcs, const int nRegs, GVecs *prmGVecs, gvec_data &glob_vecs)
Definition: ionics.cc:549
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
void log_stats(double tm, bool cflg)
Definition: timers.cc:96
void init_logger(const char *filename)
Definition: timers.cc:80
int calls
# calls for this interval, this is incremented externally
Definition: timers.h:73
double tot_time
total time, this is incremented externally
Definition: timers.h:75
SF::vector< int > plugin_idx
if we use a plugin, its index in the plugins list of the IMP will be stored here, else -1.
Definition: ionics.h:135
SF::vector< sv_data > vecs
store sv dump indices for global vectors
Definition: ionics.h:134
unsigned int nRegs
number of imp regions
Definition: ionics.h:132
bool inclPS
include PS if exists
Definition: ionics.h:133
float bogus
value indicating sv not in region
Definition: ionics.h:128
void ** getsv
functions to retrieve sv
Definition: ionics.h:125
char * name
Name of global composite sv vector.
Definition: ionics.h:118
int * svOff
sv size in bytes
Definition: ionics.h:123
char ** svNames
sv names of components forming global vector
Definition: ionics.h:120
int * svSizes
sv size in bytes
Definition: ionics.h:122
char ** imps
Name of imp to which sv belongs.
Definition: ionics.h:119
char * units
units of sv
Definition: ionics.h:127
sf_vec * ordered
vector in which to place ordered data
Definition: ionics.h:126