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