openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
MULTI_ION_IF.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 
54 // TODO remove the "get_sv_address()" and "get_sv_size()" calls by moving
55 // the functions that use them to the IonIf class
56 
57 #include "MULTI_ION_IF.h"
58 #include "limpet_types.h"
59 #include "mpi_utils.h"
60 #include <stdio.h>
61 #ifdef HAS_CUDA_MODEL
62 #include <cuda_runtime.h>
63 #endif
64 #if defined HAS_ROCM_MODEL && defined __HIP__
65 #include <hip/hip_runtime.h>
66 #endif
67 
68 #include "SF_init.h" // for SF::init_xxx()
69 #include "petsc_utils.h" // TODO for EXIT
70 
71 namespace limpet {
72 
73 using ::opencarp::base_timer;
85 using ::opencarp::Salt_list;
90 #ifdef USE_FMEM_WRAPPER
92 #endif
93 
94 /*
95  PROTOTYPES
96  */
97 void initialize_params_MIIF(MULTI_IF *pMIIF);
98 void initialize_ionic_IF(MULTI_IF *pMIIF);
99 void CreateIIFLocalNodeLsts(MULTI_IF *pMIIF);
101 void alloc_MIIF(MULTI_IF *pMIIF);
102 void initializeIMPData(MULTI_IF *pMIIF);
103 void freeIMPData(MULTI_IF *pMIIF);
104 void allocate_shared_data(MULTI_IF *);
105 node_index_t getGlobalNodalIndex(IonIfBase& pIF, node_index_t relIdx);
107 bool isIMPdata(const char *);
108 
109 static MULTI_IF *gMIIF_Error_Recovery;
110 static int current_IIF_index;
111 static float current_time = 0;
112 static float start_time = 0;
113 
114 static void prepare_error_recovery(MULTI_IF *MIIF, int IIF_index, float time) {
115  gMIIF_Error_Recovery = MIIF;
116  current_IIF_index = IIF_index;
117  current_time = start_time+time;
118 }
119 
121  return gMIIF_Error_Recovery->NodeLists[current_IIF_index][local_node];
122 }
123 
125  return current_time;
126 }
127 
128 static int g_print_bounds_exceeded = 1;
129 
131  return g_print_bounds_exceeded;
132 }
133 
135  int oldval = g_print_bounds_exceeded;
136 
137  g_print_bounds_exceeded = newval;
138  return oldval;
139 }
140 
144  CreateIIFNodeLsts_(pMIIF->N_IIF, pMIIF->IIFmask, &pMIIF->N_Nodes,
145  &pMIIF->NodeLists, pMIIF->numNode);
146 }
147 
159 void CreateIIFNodeLsts_(int N_IIF, IIF_Mask_t *IIF_Mask, node_count_t **N_Nodes,
160  node_index_t ***NodeLists, node_count_t numNode)
161 {
162  /* create node lists for each IIF
163  NodeNum: store number of nodes for each IIF
164  NodeLst: store node indices of each IIF
165  */
166  node_count_t *NodeNum = static_cast<node_count_t *>(calloc(N_IIF, sizeof(node_count_t)));
167  node_index_t **NodeLst = static_cast<node_index_t **>(calloc(N_IIF, sizeof(node_index_t *)));
168 
169  // determine number of nodes of each IIF
170  for (node_index_t i = 0; i < numNode; i++)
171  NodeNum[static_cast<int>(IIF_Mask[i])]++;
172 
173  /* now we know the number of nodes per IIF and store the indices of
174  nodes belonging to a particular IIF into a list.
175  */
176  for (int i = 0; i < N_IIF; i++) {
177  if (NodeNum[i] > 0)
178  NodeLst[i] = static_cast<node_index_t *>(malloc(sizeof(node_index_t) * NodeNum[i]));
179  else
180  NodeLst[i] = NULL;
181  }
182 
183  // just store indices relative to low
184  for (int j = 0; j < N_IIF; j++) {
185  node_count_t hcount = 0;
186  for (node_index_t i = 0; i < numNode; i++)
187  if (IIF_Mask[i] == j)
188  NodeLst[j][hcount++] = i;
189  }
190 
191  for(int j=0; j<N_IIF; j++)
192  std::sort(NodeLst[j], NodeLst[j]+NodeNum[j]);
193 
194  *N_Nodes = NodeNum;
195  *NodeLists = NodeLst;
196 
197 } // CreateIIFNodeLsts_
198 
199 
208  // get pointer to parent MIIF
209  MULTI_IF *pMIIF = (MULTI_IF *) pIF.parent(); // TODO check?!?!?!?!?!
210 
211  if (pIF.get_type().is_plugin())
212  pMIIF = (MULTI_IF *) pIF.parent()->parent();
213 
214  return pMIIF->NodeLists[pIF.miifIdx][rIdx];
215 }
216 
223 void alloc_MIIF(MULTI_IF *pMIIF) {
224  int N_IIF = pMIIF->N_IIF;
225 
226  pMIIF->contiguous = static_cast<bool *>(calloc(N_IIF, sizeof(bool)));
227 #if 0
228  pMIIF->ldata = (GlobalData_t***) build_matrix_ns<GlobalData_t>(N_IIF, NUM_IMP_DATA_TYPES, sizeof(GlobalData_t **), Target::CPU);
229 #else
230  pMIIF->ldata = allocate_on_target<GlobalData_t**>(Target::CPU, N_IIF);
231  for (std::size_t i = 0; i < pMIIF->N_IIF; i++) {
232  pMIIF->ldata[i] = allocate_on_target<GlobalData_t*>(pMIIF->iontypes[i].get().select_target(pMIIF->targets[i]), NUM_IMP_DATA_TYPES);
233  }
234 #endif
235 
236  for (int i = 0; i < pMIIF->N_IIF; i++) {
237  // Create an IonIf object on the chosen target
238  pMIIF->IIF.push_back(pMIIF->iontypes[i].get().make_ion_if(pMIIF->targets[i], pMIIF->N_Nodes[i], pMIIF->plugtypes[i]));
239  // this is questionable. A IIF does not have a parent, parent should be NULL
240  // It might make sense in some situtation to be able to refer to the MIIF
241  // structure, but pIF->parent is of type IonIf and not MULTI_IF!!!!
242  pMIIF->IIF[i]->set_parent((IonIfBase *)pMIIF);
243 
244  // set unique miifIdx for imp and plugins to enable refering back
245  // to global entities such as global node index from within imp
246  pMIIF->IIF[i]->for_each([&](IonIfBase& IF) { IF.miifIdx = i; });
247  }
248 } // alloc_MIIF
249 
255  for (auto& imp : pMIIF->IIF) {
256  imp->initialize_params();
257  }
258 }
259 
264  pMIIF->getRealData(); // needed for initializing Vm, etc.
265  for (int i = 0; i < pMIIF->IIF.size(); i++)
266  pMIIF->IIF[i]->initialize(pMIIF->dt, pMIIF->ldata[i]);
267 
268  pMIIF->releaseRealDataDuringInit();
269 }
270 
277  // we need to copy all global data which has been set, even if it is not
278  // listed as moddat. This is the case with Vm which may not be modified by
279  // LIMPET but is initialized by LIMPET
280  unsigned int *moddat = static_cast<unsigned int *>(calloc(this->N_IIF, sizeof(unsigned int)));
281 
282  for (int i = 0; i < this->N_IIF; i++) {
283  moddat[i] = this->IIF[i]->get_moddat();
284  this->IIF[i]->set_moddat(moddat[i] | this->IIF[i]->get_reqdat()); // add in the reqdat
285  }
286  this->releaseRealData();
287  for (int i = 0; i < this->N_IIF; i++) {
288  this->IIF[i]->set_moddat(moddat[i]); // remove the reqdat
289  }
290  free(moddat);
291 }
292 
298 {
300  alloc_MIIF(this);
302 
303  memset(&svd, 0, sizeof(SV_DUMP) );
304  svd.active = 0;
305  svd.intv = 1.0; // 1. ms by default
306 }
307 
308 #define FILENAME_BUF 1024
309 
311 int node_index_cmp(const void *a, const void *b) {
312  const node_index_t lhs = *static_cast<const node_index_t *>(a);
313  const node_index_t rhs = *static_cast<const node_index_t *>(b);
314  return (lhs > rhs) - (lhs < rhs);
315 }
316 
344 void open_trace(MULTI_IF *MIIF, int n_traceNodes, int *traceNodes, int *label, opencarp::sf_mesh* imesh)
345 {
346  if (!n_traceNodes) return;
347 
348  if (n_traceNodes > 1000)
349  log_msg(0, 4, 0, "%s warning: %d trace nodes may impact performance", __func__, n_traceNodes);
350 
351  MIIF->trace_info = (Trace_Info *)calloc(n_traceNodes+1, sizeof(Trace_Info));
352  MIIF->trace_info[n_traceNodes].region = -1; // end of list marker
353 
354  // bijective index mapping between set A (local petsc indexing) and set B (local nodal indexing)
356  if(imesh)
357  SF::local_petsc_to_nodal_mapping(*imesh, petsc2nod);
358 
359  for (int iTrace = 0; iTrace < n_traceNodes; iTrace++) {
360  mesh_int_t lnode = imesh ? imesh->pl.localize(traceNodes[iTrace]) : traceNodes[iTrace];
361 
362  // here we check if lnode is in set B (i.e. local nodal indexing)
363  if(imesh) {
364  // here we check if lnode is in set B (i.e. local nodal indexing)
365  if(petsc2nod.in_b(lnode) == false) continue;
366 
367  // lnode is local nodal, we map it to local petsc
368  lnode = petsc2nod.backward_map(lnode);
369  }
370 
371  for (int iRegion = 0; iRegion < MIIF->N_IIF; iRegion++)
372  {
373  node_index_t local_node = static_cast<node_index_t>(lnode);
374  auto target = static_cast<node_index_t *>(bsearch(&local_node, MIIF->NodeLists[iRegion],
375  MIIF->N_Nodes[iRegion], sizeof(node_index_t), node_index_cmp));
376 
377  if (target != NULL ) {
378  node_index_t idx = target - MIIF->NodeLists[iRegion];
379  Trace_Info *trace_info = MIIF->trace_info+iTrace;
380  trace_info->found = true;
381  trace_info->region = iRegion;
382  trace_info->node_idx = idx;
383  }
384  }
385  }
386 
387  for (int iTrace = 0; iTrace < n_traceNodes; iTrace++) {
388  if(get_global(int(MIIF->trace_info[iTrace].found), MPI_SUM) == 0) {
389  MIIF->trace_info[iTrace].ignored = true;
390  log_msg(0,4,0, "trace node %d not found", traceNodes[iTrace]);
391  continue;
392  }
393 
394  char traceName[FILENAME_BUF];
395  snprintf(traceName, sizeof traceName, "Trace_%d.dat", label ? label[iTrace] : traceNodes[iTrace]);
396  MIIF->trace_info[iTrace].file = f_open(traceName, "w");
397  }
398 } // open_trace
399 
414 void dump_trace(MULTI_IF *MIIF, limpet::Real time) {
415  if (MIIF->trace_info == NULL)
416  return;
417 
418 // struct exception_type e;
419 
420 #define MAX_TRACE_LINE_LEN 8196
421 
422  char trace_buf[MAX_TRACE_LINE_LEN] = {0};
423 
424  std::vector<IonIfBase*>& IIF = MIIF->IIF;
425  FILE *fs;
426 
427  for (int iTrace = 0; MIIF->trace_info[iTrace].region >= 0; iTrace++) {
428  Trace_Info *ctrace = MIIF->trace_info+iTrace;
429 
430  if (ctrace->ignored)
431  continue;
432 
433  if (ctrace->found) {
434  fs = fmemopen_(trace_buf, MAX_TRACE_LINE_LEN, "w");
435  fprintf(fs, "%4.10f\t", time);
436  if (IIF[ctrace->region]->get_type().has_trace()) {
437  IIF[ctrace->region]->get_type().trace(*IIF[ctrace->region], ctrace->node_idx,
438  fs, MIIF->ldata[ctrace->region]);
439  }
440  for (auto& plugin : IIF[ctrace->region]->plugins()) {
441  if (plugin->get_type().has_trace()) {
442  fprintf(fs, "\t");
443  plugin->get_type().trace(
444  *plugin, ctrace->node_idx,
445  fs, MIIF->ldata[ctrace->region]);
446  }
447  }
448  fprintf(fs, "\n");
449  fclose(fs);
450 
451  fprintf(ctrace->file->fd, "%s", trace_buf);
452  }
453  fflush(ctrace->file->fd);
454  }
455 } // dump_trace
456 
457 void close_trace(MULTI_IF *MIIF) {
458  if (!MIIF->trace_info) return;
459 
460  for (int iTrace = 0; MIIF->trace_info[iTrace].region >= 0; iTrace++)
461  f_close(MIIF->trace_info[iTrace].file);
462 
463  free(MIIF->trace_info);
464  MIIF->trace_info = NULL;
465 }
466 
473 void MULTI_IF::initialize_currents(double idt, int subDt) {
474  numSubDt = subDt;
475  dt = idt/this->numSubDt;
476 
477  allocate_shared_data(this);
478  initializeIMPData(this);
479  initialize_ionic_IF(this);
480 }
481 
488 void MULTI_IF::dump_luts_MIIF(bool zipped) {
489  // if one of the IFs in MIIF does not live in partion 0
490  // we won't get a LUT dumped.
491  if (get_rank()) return;
492 
493  std::vector<IonIfBase*>& pIF = this->IIF;
494  for (auto& IF : pIF) {
495  int ndmps = IF->dump_luts(zipped);
496  if (ndmps < IF->tables().size()) {
497  log_msg(logger, 4, 0, "LUT dump error %s: only %d out of %d LUTs dumped.\n",
498  IF->get_type().get_name().c_str(), ndmps, IF->tables().size());
499  }
500  for (auto& plugin : IF->plugins()) {
501  ndmps = plugin->dump_luts(zipped);
502  if (ndmps < plugin->tables().size()) {
503  log_msg(logger, 4, 0, "LUT dump error %s: only %d out of %d LUTs dumped.\n",
504  plugin->get_type().get_name().c_str(), ndmps, IF->tables().size());
505  }
506  }
507  }
508 }
509 
515  if (get_rank()) return;
516 
517  // close files
518  for (int i = 0; i < svd.n; i++)
519 #ifndef USE_HDF5
520  if (svd.hdls[i] != NULL)
521 #endif // ifndef USE_HDF5
522  f_close(svd.hdls[i]);
523 }
524 
535 char *get_sv(void *tab, int offset, node_count_t n, int svSize, int size, int dlo_vector_size) {
536  char *buf = static_cast<char *>(malloc(static_cast<size_t>(n)*size));
537  char *bp = buf;
538  char *p = static_cast<char *>(tab) + offset;
539 
540  for (node_index_t i = 0; i < n; i += dlo_vector_size) {
541  node_count_t dlo_array_size = min(static_cast<node_count_t>(dlo_vector_size), n - i);
542  memcpy(bp, p, size * dlo_array_size);
543  bp += size * dlo_array_size;
544  p += svSize;
545  }
546  return buf;
547 }
548 
556 size_t MULTI_IF::dump_svs(base_timer *iot) {
557  size_t nwr = 0;
558 
559  if (iot->triggered) {
560  for (int i = 0; i < svd.n; i++) {
561  nwr = 0;
562 
563  FILE* fd = svd.hdls[i] ? svd.hdls[i]->fd : NULL;
564  char *buf = get_sv(svd.svtab[i], svd.offset[i], svd.num[i], svd.svsize[i], svd.size[i], svd.dlo_vs[i]);
565  nwr += SF::root_write<char>(fd, (char*) buf,
566  static_cast<size_t>(svd.size[i]) * static_cast<size_t>(svd.num[i]),
567  PETSC_COMM_WORLD);
568  free(buf);
569  }
570  svd.nwr += static_cast<long>(nwr);
571  svd.n_dumps++;
572  }
573 
574  return nwr;
575 }
576 
582 #ifdef HAS_GPU_MODEL
583 __global__
584 void update_vm(node_index_t start, node_index_t end, double *vm, double *ion, double dt)
585 {
586  node_index_t i = blockIdx.x*blockDim.x + threadIdx.x;
587  if (i < end)
588  vm[i] = vm[i] + (ion[i] * (-dt));
589 }
590 #endif
591 
592 // * compute what has to be computed, current or otherwise
593 void MULTI_IF::compute_ionic_current(bool flag_send, bool flag_receive)
594 {
595  gdata[Iion]->set(0.0);
596  if (flag_send == 1) {
597  this->getRealData();
598  }
599 
600  for (int j = 0; j < this->numSubDt; j++)
601  {
602  for (int i = 0; i < this->N_IIF; i++)
603  {
604  IonIfBase* pIF = this->IIF[i];
605  if (!this->N_Nodes[i]) continue;
606 
607  prepare_error_recovery(this, i, pIF->get_tstp().cnt * this->dt);
608  update_ts(&pIF->get_tstp());
609 
610  node_index_t current = 0;
611  do {
612  try {
613  pIF->compute(current, this->N_Nodes[i], this->ldata[i]);
614  current = this->N_Nodes[i];
615  }
616  catch(int e) { //FIXME
617  if(e==-1) {
618  // CHECK this->NodeLists[i][e.node], e.node);
619  fprintf(stderr, "LIMPET compute fail in %s at node %jd (local %jd)! Aborting!\n",
620  this->name.c_str(), printable_int(this->NodeLists[i][j]), printable_int(j));
621  exit(1);
622  }
623  current++;
624  }
625  } while (current < this->N_Nodes[i]);
626 
627  for (auto& plugin : pIF->plugins()) {
628  current = 0;
629  update_ts(&plugin->get_tstp());
630 
631  do {
632  try {
633  plugin->compute(current, this->N_Nodes[i], this->ldata[i]);
634  current = this->N_Nodes[i];
635 
636  if (plugin == nullptr)
637  throw -1;
638  }
639  catch(int e) { //FIXME
640  if(e==-1) {
641  // CHECK this->NodeLists[i][e.node], e.node);
642  fprintf(stderr, "LIMPET plugin fail in %s at node %jd (local %jd)! Aborting!\n",
643  this->name.c_str(), printable_int(this->NodeLists[i][j]), printable_int(j));
644  exit(1);
645  }
646  current++;
647  }
648  } while (current < this->N_Nodes[i]);
649  }
650  }
651 
652  #ifdef HAS_GPU_MODEL
653  //TODO: extUpdateVm can be true outside of the bench executable ! But this should
654  // only run in bench! This should be done for each model according to its target
655  if(!extUpdateVm && is_gpu(this->targets[0])) {
656 
657  #if defined __CUDA__ || defined __HIP__
658  update_vm<<<(this->N_Nodes[0]/64)+1,64>>>(0, this->N_Nodes[0], this->ldata[0][Vm], this->ldata[0][Iion], this->dt);
659  #ifdef __CUDA__
660  cudaDeviceSynchronize();
661  #elif defined __HIP__
662  hipDeviceSynchronize();
663 #endif
664  #else
665  fprintf(stderr, "GPU/CUDA not found");
666  #endif
667  }
668  #endif
669 
670  if (flag_receive == 1) {
671  this->releaseRealData();
672  }
673 
674  // TODO: Target should be checked for each region
675  if(!extUpdateVm && !is_gpu(this->targets[0]))
676  gdata[Vm]->add_scaled(*gdata[Iion], SF_real(-dt));
677  }
678 }
679 
681  assert(!this->doppel);
682 
683  // Free data first since this function needs to access memory in the IIFs
684  freeIMPData(this);
685  for (auto& pIF : this->IIF) {
686  pIF->get_type().destroy_ion_if(pIF);
687  }
688 }
689 
697  for (int i = 0; i < pMIIF->N_IIF; i++) {
698  if (!pMIIF->N_Nodes[i])
699  continue;
700 
701  // if memory contiguous, we don't need to copy data, merely pass pointers
702  pMIIF->contiguous[i] = pMIIF->N_Nodes[i]-1 ==
703  pMIIF->NodeLists[i][pMIIF->N_Nodes[i]-1]-pMIIF->NodeLists[i][0];
704 
705  for (int j = 0; j < NUM_IMP_DATA_TYPES; j++) {
706  // check if it is used by the IMPs
707  if (!USED_DAT(pMIIF->IIF[i], imp_data_flag[j]) )
708  continue;
709 
710  // allocate mem buffer only if not contiguous or if data is on GPU
711  if (!pMIIF->contiguous[i] || is_gpu(pMIIF->IIF[i]->get_target()))
712  pMIIF->ldata[i][j] =
713  allocate_on_target<GlobalData_t>(pMIIF->IIF[i]->get_target(),
714  pMIIF->N_Nodes[i]);
715  // check if data supplied
716  if (pMIIF->gdata[j] == NULL) {
717  log_msg(pMIIF->logger, 5, LOCAL, "IMP data type %s not supplied for region %d", imp_data_names[j], i);
718  exit(1);
719  }
720  }
721  }
722 } // initializeIMPData
723 
733  // set rdata in the parent vector to point to the
734  // local data to be computed on the local processor
735  for (int j = 0; j < NUM_IMP_DATA_TYPES; j++) {
736  SF_real* rdata;
737  if (this->gdata[j] != NULL) {
738  rdata = this->gdata[j]->ptr();
739  this->procdata[j] = rdata;
740  }
741  else continue;
742 
743  // now map local data so that each ionic model can use it
744  // if noncontiguous in the parent vector, copy to contiguous local vector
745  for (int i = 0; i < this->N_IIF; i++) {
746  if (!this->N_Nodes[i] || !USED_DAT(this->IIF[i], imp_data_flag[j]) )
747  continue;
748 
749  if (this->contiguous[i] && !is_gpu(this->IIF[i]->get_target()))
750  this->ldata[i][j] = static_cast<GlobalData_t *>(rdata) + this->NodeLists[i][0];
751  else {
752  const node_index_t* ip = this->NodeLists[i];
753  for (node_index_t k = 0; k < this->N_Nodes[i]; k++)
754  this->ldata[i][j][k] = rdata[ip[k]];
755  }
756  }
757  }
758 } // getRealData
759 
769 
770  for (int i = 0; i < this->N_IIF; i++) {
771  if (!this->N_Nodes[i]) continue;
772 
773  // Every external variable used by the model must be copied on GPU
774  if (is_gpu(this->IIF[i]->get_target())) {
775  for (int j = 0; j < NUM_IMP_DATA_TYPES; j++) {
776  if (!USED_DAT(this->IIF[i], imp_data_flag[j]) )
777  continue;
778  if (this->contiguous[i])
779  memcpy(&this->procdata[j][this->NodeLists[i][0]], this->ldata[i][j], sizeof(this->ldata[i][j][0])*this->N_Nodes[i]);
780  else {
781  for (node_index_t k = 0; k < this->N_Nodes[i]; k++)
782  this->procdata[j][this->NodeLists[i][k]] = this->ldata[i][j][k];
783  }
784  }
785  }
786  else {
787  for (int j = 0; j < NUM_IMP_DATA_TYPES; j++) {
788  if (this->IIF[i]->get_moddat() & imp_data_flag[j]) {
789  if (!this->contiguous[i]) {
790  for (node_index_t k = 0; k < this->N_Nodes[i]; k++)
791  this->procdata[j][this->NodeLists[i][k]] = this->ldata[i][j][k];
792  }
793  }
794  }
795  }
796  }
797 
798  for (int j = 0; j < NUM_IMP_DATA_TYPES; j++)
799  if (this->gdata[j] != NULL)
800  this->gdata[j]->release_ptr(this->procdata[j]);
801 }
802 
803 // * free the local storage vectors
804 void freeIMPData(MULTI_IF *pMIIF) {
805  for (int j = 0; j < NUM_IMP_DATA_TYPES; j++) {
806  if (pMIIF->gdata[j] != NULL) {
807  for (int i = 0; i < pMIIF->N_IIF; i++)
808  if ((!pMIIF->contiguous[i] || is_gpu(pMIIF->IIF[i]->get_target())) && USED_DAT(pMIIF->IIF[i], imp_data_flag[j]) && pMIIF->N_Nodes[i] > 0) {
809  deallocate_on_target<GlobalData_t>(pMIIF->IIF[i]->get_target(),
810  pMIIF->ldata[i][j]);
811  }
812  delete pMIIF->gdata[j];
813  }
814  }
815  free(pMIIF->contiguous);
817 }
818 
828 int get_plug_flag(char *plgstr, int *out_num_plugins, IonTypeList& out_plugins) {
829  if (plgstr == NULL) {
830  *out_num_plugins = 0;
831  out_plugins.clear();
832  return 1;
833  }
834 
835  // maximum number of plugins is 200 apparently
836  char *plugspec = dupstr(plgstr);
837  char *saveptr;
838  char *token = tokstr_r(plugspec, ":", &saveptr);
839 
840  *out_num_plugins = 0;
841  while (token) {
842  IonType* type = get_ion_type(std::string(token));
843  if (type == NULL) {
844  free(plugspec);
845  return 0;
846  } else {
847  out_plugins.push_back(*type);
848  }
849  token = tokstr_r(NULL, ":", &saveptr);
850  }
851 
852  *out_num_plugins = out_plugins.size();
853 
854  free(plugspec);
855  return 1;
856 } // get_plug_flag
857 
876 int MULTI_IF::adjust_MIIF_variables(const char* variable,
877  const SF::vector<SF_int> & indices,
878  const SF::vector<SF_real> & values)
879 {
880  int num_changed = 0;
881 
882  // determine if we're dealing with an external variable or a state variable.
883  if (index(variable, '.') == NULL) {
884  // external variable, the easy case.
885  // which external variable?
886  int data_id = -1;
887  for (int ii = 0; ii < NUM_IMP_DATA_TYPES; ii++) {
888  if (strcmp(variable, imp_data_names[ii]) == 0) {
889  data_id = ii;
890  break;
891  }
892  }
893  if (data_id == -1) {
894  log_msg(logger, 5, FLUSH, "Error! No external variable named %s in this build of openCARP.", variable);
895  exit(EXIT_FAILURE);
896  }
897 
898  // make sure that external variable is being used.
899  if (this->gdata[data_id] == NULL) {
900  log_msg(logger, 4, 0,
901  "External variable %s is not being used this processor.\n"
902  "Is this really what you meant to do?\n"
903  "Perhaps you don't have the correct Ionic models selected.",
904  imp_data_names[data_id]);
905  }
906  else {
907  // fill in the external variable with the data from the file.
908  SF_real *raw_data = this->gdata[data_id]->ptr();
909 
910  for (size_t i = 0; i < indices.size(); i++) {
911  raw_data[indices[i]] = values[i];
912  num_changed++;
913  }
914 
915  this->gdata[data_id]->release_ptr(raw_data);
916  }
917  }
918  else {
919  // state variable.
920  // extract the IMP name and state variable name from the string.
921  char *saveptr;
922  char *my_variable = dupstr(variable);
923  char *IIF_name = tokstr_r(my_variable, ".", &saveptr);
924  char *sv_name = tokstr_r(NULL, ".", &saveptr);
925 
926  IonType* type = get_ion_type(std::string(IIF_name));
927  if (type == NULL) {
928  log_msg(logger, 5, 0, "%s error: %s is not a valid IMP name.", __func__, IIF_name);
929  exit(EXIT_FAILURE);
930  }
931 
932  int sv_offset;
933  int sv_size;
934  SVgetfcn sv_get = type->get_sv_offset(sv_name, &sv_offset, &sv_size);
935  if (sv_get == NULL) {
936  log_msg(logger, 5, 0, "%s error: %s is not a valid state variable for the %s model.",
937  __func__, sv_name, IIF_name);
938  exit(EXIT_FAILURE);
939  }
940  SVputfcn sv_put = getPutSV(sv_get);
941 
942  // Ok, go through the ionic models
943  for (int i_iif = 0; i_iif < this->N_IIF; i_iif++) {
944  IonIfBase *lIIF = NULL;
945  if (this->IIF[i_iif]->get_type() == *type) {
946  lIIF = this->IIF[i_iif];
947  }
948  else {
949  // Go through the plugins
950  for (auto& plugin : this->IIF[i_iif]->plugins()) {
951  if (plugin->get_type() == *type) {
952  lIIF = plugin;
953  break;
954  }
955  }
956  }
957 
958  if (lIIF == NULL) {
959  continue;
960  }
961 
962  for (size_t ii = 0; ii < indices.size(); ii++) {
963  GlobalData_t file_value = values[ii];
964 
965  node_index_t local_node = static_cast<node_index_t>(indices[ii]);
966  node_index_t *target = static_cast<node_index_t*>(bsearch(&local_node, this->NodeLists[i_iif],
967  this->N_Nodes[i_iif], sizeof(node_index_t), node_index_cmp));
968  if (target) {
969  // We found a point to adjust! Do the adjustment.
970  num_changed++;
971  sv_put(*lIIF, target - this->NodeLists[i_iif], sv_offset, file_value);
972  }
973  }
974 
975  }
976  free(my_variable);
977  }
978 
979  MPI_Allreduce(MPI_IN_PLACE, &num_changed, 1, MPI_INT, MPI_SUM, PETSC_COMM_WORLD);
980 
981  return num_changed;
982 } // adjust_MIIF_variables
983 
1001 int determine_write_ranges(int N, size_t *offset, size_t bufsize, int **ranges) {
1002  int nitems;
1003 
1004  if (!get_rank() ) {
1005  Salt_list r; STRUCT_ZERO(r); r.chunk = 10;
1006  int temp = 0;
1007  SLIST_APPEND(&r, temp);
1008  long loff = offset[0];
1009  for (int i = 0; i < N; i++)
1010  if (offset[i]-loff > bufsize) {
1011  SLIST_APPEND(&r, i);
1012  loff = offset[i];
1013  }
1014 
1015  nitems = r.nitems;
1016  SLIST_APPEND(&r, N);
1017  *ranges = (int *)r.data;
1018  }
1019 
1020  MPI_Bcast(&nitems, 1, MPI_INT, 0, PETSC_COMM_WORLD);
1021  if (get_rank() )
1022  *ranges = static_cast<int *>(malloc( (nitems+1)*sizeof(int) ));
1023  MPI_Bcast(*ranges, nitems+1, MPI_INT, 0, PETSC_COMM_WORLD);
1024  return nitems;
1025 } // determine_write_ranges
1026 
1065 void MULTI_IF::dump_state(char *fname, float simtime, mesh_t gid,
1066  bool append, unsigned int revision)
1067 {
1068  float t0, t1;
1069  t0 = get_time();
1070 
1071  FILE_SPEC out = NULL;
1072  int rank = get_rank();
1073  global_node_index_t miif_node_gsize = get_global(static_cast<global_node_index_t>(this->numNode), MPI_SUM);
1074  int error = 0;
1075 
1076  log_msg(logger, 0, 0, "Saving state at time %f in file: %s", simtime, fname);
1077 
1078  if (rank == 0) {
1079  out = f_open(fname, append ? "a" : "w");
1080 
1081  if(out) {
1082  fseek(out->fd, 0, SEEK_END); // NOP call to sync disk data for switch to write mode
1083  write_bin_string(out, Magic_MIIF_ID);
1084 
1085  fwrite(&MIIF_Format, sizeof(unsigned int), 1, out->fd);
1086  fwrite(&revision, sizeof(unsigned int), 1, out->fd);
1087  time_t tm = time(NULL);
1088  fwrite(&tm, sizeof(time_t), 1, out->fd);
1089  fwrite(&simtime, sizeof(float), 1, out->fd);
1090  fwrite(&miif_node_gsize, sizeof(global_node_index_t), 1, out->fd);
1091 
1092  int num_gdata = 0;
1093  for (int i = 0; i < NUM_IMP_DATA_TYPES; i++)
1094  if (this->gdata[i]) num_gdata++;
1095 
1096  fwrite(&num_gdata, sizeof(int), 1, out->fd);
1097  }
1098  else
1099  error++;
1100  }
1101 
1102 // #define CHATTY
1103 
1104  if(get_global(error, MPI_SUM))
1105  EXIT(EXIT_FAILURE);
1106 
1107  FILE* fd = rank == 0 ? out->fd : NULL;
1108  sf_vec* outVec;
1109 
1110  for (int i = 0; i < NUM_IMP_DATA_TYPES; i++) {
1111  if (this->gdata[i]) {
1112  if(rank == 0) {
1113  write_bin_string(out, imp_data_names[i]);
1114 #ifdef CHATTY
1115  log_msg(NULL, 0, 0, "\tDumping %s at %d", imp_data_names[i], ftell(fd) );
1116 #endif
1117  }
1118 
1119  // If we have a proper set up parallel context, we pass a gid != unset_msh. Then
1120  // we can query petsc-to-canonical scattering. If we are in a simpler context
1121  // like bench, we assume that no scattering is needed and just shallow-copy the vector.
1122  if(gid != unset_msh) {
1123  SF::init_vector(&outVec, this->gdata[Vm]);
1125  assert(sc != NULL);
1126 
1127  sc->forward(*this->gdata[i], *outVec);
1128  outVec->write_binary<SF_real>(fd);
1129  delete outVec;
1130  }
1131  else {
1132  this->gdata[i]->write_binary<SF_real>(fd);
1133  }
1134  }
1135  }
1136 
1137  // output IMP region info
1138  // #regions and the IM and plugins for each region
1139  // determine memory requirements for each region
1140  int* imp_mem = new int[this->N_IIF];
1141  size_t *offset = NULL;
1142  long filepos = 0.;
1143 
1144  if (rank == 0) {
1145 #ifdef CHATTY
1146  log_msg(NULL, 0, 0, "\tDumping IMP sizes at %d", ftell(fd) );
1147 #endif
1148 
1149  int max = 1;
1150  fwrite(&this->N_IIF, sizeof(int), 1, fd); // #IIF's
1151 
1152  for (int i = 0; i < this->N_IIF; i++) {
1153  IonIfBase *imp = this->IIF[i];
1154  write_bin_string(out, imp->get_type().get_name().c_str());
1155 
1156  std::size_t sv_size = imp->get_sv_size();
1157  fwrite(&sv_size, sizeof(int), 1, fd);
1158  unsigned long n_plugins = (unsigned long) imp->plugins().size();
1159  fwrite(&n_plugins, sizeof(int), 1, fd);
1160  imp_mem[i] = imp->get_sv_size();
1161 
1162  for (auto& plug : imp->plugins()) {
1163  write_bin_string(out, plug->get_type().get_name().c_str());
1164 
1165  fwrite(&sv_size, sizeof(int), 1, fd);
1166  imp_mem[i] += plug->get_sv_size();
1167  }
1168  }
1169  filepos = ftell(fd);
1170  }
1171 
1172  MPI_Bcast(imp_mem, this->N_IIF, MPI_INT, 0, PETSC_COMM_WORLD);
1173 
1174  // If we have a proper set up parallel context, we pass a gid != unset_msh. Then
1175  // we can query a mesh and its parallel numbering. If we are in a simpler context
1176  // like bench, we build the numbering ourselves. Then, we assume that no
1177  // renumbering has taken place.
1179  if(gid != unset_msh) {
1180  const sf_mesh & mesh = get_mesh(gid);
1181  const SF::vector<mesh_int_t> & canon_nbr = mesh.get_numbering(SF::NBR_SUBMESH);
1182  const SF::vector<mesh_int_t> & alg_idx = mesh.pl.algebraic_nodes();
1183  loc2canon.resize(alg_idx.size());
1184 
1185  for(size_t i=0; i<alg_idx.size(); i++) loc2canon[i] = canon_nbr[alg_idx[i]];
1186  }
1187  else {
1188  int rank = get_rank();
1190  SF::layout_from_count<global_node_index_t>(static_cast<global_node_index_t>(this->numNode),
1191  layout, PETSC_COMM_WORLD);
1192  loc2canon.resize(this->numNode);
1193 
1194  for (node_index_t i = 0; i < this->numNode; i++) loc2canon[i] = layout[rank] + i;
1195  }
1196 
1197  // write out IIF_Mask canonically ordered
1198  global_node_index_t *canord = new global_node_index_t[this->numNode];
1199  for (node_index_t i = 0; i < this->numNode; i++)
1200  canord[i] = loc2canon[i];
1201 
1202 #ifdef CHATTY
1203  log_msg(NULL, 0, 0, "\tDumping IMP masks at %d", filepos);
1204 #endif // ifdef CHATTY
1205 
1206  SF::root_write_ordered<global_node_index_t, IIF_Mask_t>(fd, canord, IIFmask, numNode, PETSC_COMM_WORLD);
1207 
1208  // for every local node, we compute the size of its ionic model
1209  // and plugins, and the associated displacments between the nodes
1210  SF::vector<global_node_index_t> impsize(numNode), impdsp;
1211  for(node_index_t i=0; i<numNode; i++)
1212  impsize[i] = imp_mem[(int)IIFmask[i]];
1213  SF::dsp_from_cnt(impsize, impdsp);
1214 
1215  // this also gives us the size of the total buffer
1216  size_t num_entr = static_cast<size_t>(SF::sum(impsize));
1217  SF::vector<char> impdata(num_entr);
1218 
1219  // now we can memcpy the ionic models from the IonIfBase arrays into our buffer
1220  for (int imp_idx = 0; imp_idx < this->N_IIF; imp_idx++)
1221  {
1222  IonIfBase* iif = this->IIF[imp_idx];
1223  // When using data layout optimization, checkpointing indices have to be
1224  // change a bit. Without DLO, this value is simply 1
1225  std::size_t vec_size = iif->get_type().dlo_vector_size();
1226  for (node_index_t imp_nod_idx = 0; imp_nod_idx < this->N_Nodes[imp_idx]; imp_nod_idx+=vec_size) {
1227  node_index_t index = imp_nod_idx / vec_size;
1228  node_index_t loc = this->NodeLists[imp_idx][imp_nod_idx];
1229  int svSize = iif->get_sv_size();
1230 
1231  char* write = impdata.data() + impdsp[loc];
1232  char* read = (char*) iif->get_sv_address() + index * svSize;
1233  memcpy(write, read, svSize);
1234 
1235  int shift = svSize;
1236  for (auto& plug : iif->plugins()) {
1237  int plugsize = plug->get_sv_size();
1238 
1239  write = impdata.data() + impdsp[loc] + shift;
1240  read = (char*) plug->get_sv_address() + index * plugsize;
1241  memcpy(write, read, plugsize);
1242  shift += plugsize;
1243  }
1244  }
1245  }
1246 
1247  // finally we can write out the IonIf data to disk
1248  SF::root_write_ordered<global_node_index_t, char>(fd, canord, impsize.data(), impdata.data(),
1249  numNode, num_entr, PETSC_COMM_WORLD);
1250  delete [] canord;
1251  delete [] imp_mem;
1252 
1253  if(fd) fclose(fd);
1254 
1255  double dump_time = timing(t1, t0);
1256  log_msg(logger, 0, 0, " in %.3f seconds.\n", dump_time);
1257 }
1258 
1281 float MULTI_IF::restore_state(const char *fname, mesh_t gid, bool close)
1282 {
1283  static FILE_SPEC in = NULL;
1284  static char *last_fn = NULL;
1285  double t0, t1;
1286 
1287  t0 = get_time();
1288  int error = 0;
1289  int my_rank = get_rank();
1290  int mpi_size = get_size();
1291 
1292  if (my_rank == 0) {
1293  if (fname) in = f_open(fname, "r");
1294 
1295  if (!in) {
1296  if (fname) log_msg(logger, 5, 0, "Error: cannot open file: %s\n", fname);
1297  else log_msg(logger, 5, 0, "Error: file stream not open yet");
1298  error++;
1299  }
1300  else {
1301  if (strcmp(read_bin_string(in), Magic_MIIF_ID) ) {
1302  log_msg(logger, 5, 0, "%s is not a recognized MIIF dump file", fname);
1303  error++;
1304  }
1305  }
1306  }
1307 
1308  if (get_global(error, MPI_SUM))
1309  EXIT(1);
1310 
1311  if (fname) {
1312  free(last_fn);
1313  last_fn = strdup(fname);
1314  }
1315  else {
1316  fname = last_fn;
1317  }
1318 
1319  unsigned int format, version;
1320  float time = 0.0f;
1321  time_t save_date;
1322  f_read_par(&format, sizeof(unsigned int), 1, in);
1323  f_read_par(&version, sizeof(unsigned int), 1, in);
1324  f_read_par(&save_date, sizeof(time_t), 1, in);
1325  f_read_par(&time, sizeof(float), 1, in);
1326  log_msg(logger, 0, 0, "Restoring time %f from %s (format v%d) generated\n\tby calling "
1327  "program r%d on %s", time, fname, format, version, ctime(&save_date) );
1328 
1329  global_node_index_t savedNum = 0;
1330  global_node_index_t glob_numNode = get_global(static_cast<global_node_index_t>(this->numNode),
1331  MPI_SUM, PETSC_COMM_WORLD);
1332 
1333  if (format >= 2) {
1334  f_read_par(&savedNum, sizeof(global_node_index_t), 1, in);
1335  } else {
1336  int savedNum32 = 0;
1337  f_read_par(&savedNum32, sizeof(int), 1, in);
1338  savedNum = savedNum32;
1339  }
1340  if (savedNum != glob_numNode) {
1341  log_msg(logger, 5, 0, "expecting %jd nodes but read %jd nodes",
1342  printable_int(glob_numNode), printable_int(savedNum));
1343  EXIT(1);
1344  }
1345 
1346  int num_gdata;
1347  f_read_par(&num_gdata, sizeof(int), 1, in);
1348  SF::scattering & petsc2canon = *get_permutation(gid, PETSC_TO_CANONICAL, 1);
1349 
1350  for (int g = 0; g < num_gdata; g++) {
1351  char *datatype = read_bin_string_par(in);
1352  int i;
1353  for (i = 0; i < NUM_IMP_DATA_TYPES; i++) {
1354  if (!strcmp(datatype, imp_data_names[i]) ) {
1355  if (this->gdata[i]) {
1356  log_msg(logger, 0, 0, "\tRestoring global data %s", imp_data_names[i], datatype);
1357 
1358  FILE* fd = my_rank == 0 ? in->fd : NULL;
1359  this->gdata[i]->read_binary<SF_real>(fd);
1360  bool fwd = false;
1361  petsc2canon(*this->gdata[i], fwd);
1362  } else {
1363  log_msg(logger, 0, 0, "\tGlobal data %s not used", datatype);
1364  }
1365  break;
1366  }
1367  }
1368  if (i == NUM_IMP_DATA_TYPES)
1369  log_msg(logger, 3, 0, "\tSaved global data %s not recognized", datatype);
1370 
1371  if ( (my_rank == 0) && ((i == NUM_IMP_DATA_TYPES) || !this->gdata[i]) )
1372  fseek(in->fd, this->gdata[i]->gsize() * sizeof(SF_real), SEEK_CUR);
1373 
1374  free(datatype);
1375  }
1376 
1377  int N_IIF;
1378  f_read_par(&N_IIF, sizeof(int), 1, in);
1379 
1380  // read in the IMPs for each region
1381  IMPinfo *imps = static_cast<IMPinfo *>(calloc(N_IIF, sizeof(IMPinfo)));
1382  int *IMPsz = static_cast<int *>(malloc(N_IIF * sizeof(int)));
1383 
1384  for (int i = 0; i < N_IIF; i++) {
1385  imps[i].name = read_bin_string_par(in);
1386  f_read_par(&imps[i].sz, sizeof(int), 1, in);
1387  f_read_par(&imps[i].nplug, sizeof(int), 1, in);
1388 
1389  imps[i].offset = 0;
1390  IMPsz[i] = imps[i].sz;
1391  imps[i].plug = static_cast<IMPinfo *>(calloc(sizeof(IMPinfo), imps[i].nplug));
1392 
1393  for (int j = 0; j < imps[i].nplug; j++) {
1394  imps[i].plug[j].offset = IMPsz[i];
1395  imps[i].plug[j].name = read_bin_string_par(in);
1396  f_read_par(&imps[i].plug[j].sz, sizeof(int), 1, in);
1397  IMPsz[i] += imps[i].plug[j].sz;
1398  }
1399  }
1400 
1401  // compare new and old regions to determine which can be copied
1402  for (int i = 0; i < N_IIF; i++) {
1403  if (i >= this->N_IIF) {
1404  log_msg(NULL, 3, 0, "Saved IMP region %d out of range", i);
1405  continue;
1406  }
1407  if (strcmp(imps[i].name, this->IIF[i]->get_type().get_name().c_str()) ) {
1408  log_msg(NULL, 3, 0, "Saved IMP region %d ionic model does not match that of IMP region %d", i, i);
1409  continue;
1410  }
1411  if (imps[i].sz != this->IIF[i]->get_sv_size()) {
1412  log_msg(NULL, 3, 0, "Saved IMP region %d size does not match current IMP size", i);
1413  continue;
1414  }
1415 
1416  // the IM is good to go
1417  imps[i].compatible = true;
1418  for (int j = 0; j < imps[i].nplug; j++)
1419  for (int k = 0; k < this->IIF[i]->plugins().size(); k++)
1420  if (!strcmp(imps[i].plug[j].name, this->IIF[i]->plugins()[k]->get_type().get_name().c_str()) &&
1421  (imps[i].plug[j].sz == this->IIF[i]->plugins()[k]->get_sv_size()) ) {
1422  log_msg(NULL, 3, 0, "Saved IMP region %d plugin %s compatible", i, imps[i].plug[j].name);
1423  imps[i].plug[j].map = k;
1424  imps[i].plug[j].compatible = true;
1425  break;
1426  }
1427 
1428  }
1429 
1430  // read in region mask
1431  const size_t global_node_count = static_cast<size_t>(glob_numNode);
1432  IIF_Mask_t *canMask = static_cast<char *>(malloc(global_node_count*sizeof(this->IIFmask[0]) ));
1433  f_read_par(canMask, sizeof(IIF_Mask_t), global_node_count, in);
1434 
1435  // determine relative IMP data offsets based on canonical ordering
1436  size_t *offset = static_cast<size_t *>(calloc(global_node_count+1, sizeof(size_t)));
1437  for (size_t i = 1; i <= global_node_count; i++)
1438  offset[i] = offset[i-1] + IMPsz[static_cast<int>(canMask[i-1])];
1439 
1440  // read in the state variable data
1441  // TODO: Aurel: Ideally rank0 would read in the data and communicate it, but right now I dont have
1442  // time to code this up. Thus, at least the ranks will read their data one by one so
1443  // we dont saturate the file system.
1444  long SVstart;
1445  if (my_rank == 0) {
1446  SVstart = ftell(in->fd);
1447  f_close(in);
1448  }
1449 
1450  MPI_Bcast(&SVstart, sizeof(long), MPI_BYTE, 0, PETSC_COMM_WORLD);
1451  global_node_index_t mismatch = 0;
1452 
1453  // we need to map from a local algebraic index to a global canonical index
1454  const sf_mesh & mesh = get_mesh(gid);
1455  const SF::vector<mesh_int_t> & canon_nbr = mesh.get_numbering(SF::NBR_SUBMESH);
1456  const SF::vector<mesh_int_t> & alg_idx = mesh.pl.algebraic_nodes();
1457  SF::vector<global_node_index_t> loc2canon(alg_idx.size());
1458 
1459  for(size_t i=0; i<alg_idx.size(); i++) loc2canon[i] = canon_nbr[alg_idx[i]];
1460 
1461  // the ranks read the file one-by-one
1462  for (int pid = 0; pid < mpi_size; pid++) {
1463  if (my_rank == pid) {
1464  if(in) delete in;
1465  in = f_open(fname, "r");
1466 
1467  for (int i = 0; i < N_IIF; i++) {
1468  fseek(in->fd, SVstart, SEEK_SET);
1469  mismatch += this->IIF[i]->restore(in, this->N_Nodes[i], this->NodeLists[i],
1470  canMask, offset, imps+i, loc2canon.data());
1471  }
1472 
1473  f_close(in);
1474  }
1475  MPI_Barrier(PETSC_COMM_WORLD);
1476  }
1477  free(canMask);
1478  free(IMPsz);
1479 
1480  MPI_Reduce(my_rank ? &mismatch : MPI_IN_PLACE, &mismatch, 1,
1481  opencarp::mpi_datatype<global_node_index_t>(), MPI_SUM, 0, PETSC_COMM_WORLD);
1482  if ( (my_rank == 0) && mismatch)
1483  log_msg(NULL, 3, 0, "Number of nonmatching nodes: %jd", printable_int(mismatch));
1484 
1485  if ( (my_rank == 0) && (!close) ) {
1486  if(in) delete in;
1487  in = f_open(fname, "r");
1488 
1489  // position at end of current MIIF data
1490  fseek(in->fd, SVstart+offset[global_node_count], SEEK_SET);
1491  }
1492 
1493  free(offset);
1494  for (int i = 0; i < N_IIF; i++)
1495  free(imps[i].plug);
1496  free(imps);
1497 
1498  double restore_time = timing(t1, t0);
1499  log_msg(logger, 0, 0, "State restored from file %s in %.3f seconds.\n", fname, restore_time);
1500 
1501  return time;
1502 }
1503 
1515 void MULTI_IF::sv_dump_add_by_name_list( int region, char *imp_name,
1516  char *reg_name, char *sv_lst, char *plg_lst,
1517  char *plg_sv_lst, double t, double dump_dt) {
1518  char file[8000], svs[1024], plgs[1024], plgsvs[1024];
1519  char *e, *l, *p, *svnames, *plgnames, *plgsvnames;
1520 
1521  strcpy(svs, sv_lst);
1522  strcpy(plgs, plg_lst);
1523  strcpy(plgsvs, plg_sv_lst);
1524  svnames = &svs[0];
1525  plgnames = &plgs[0];
1526  plgsvnames = &plgsvs[0];
1527 
1528  // override default setting for dump interval
1529  this->svd.intv = dump_dt;
1530 
1531  // override default start time for dumping (in case of a restart)
1532  this->svd.t_dump = t;
1533 
1534  // parse ionic model sv list
1535  while ( (e = get_next_list(svnames, ',') ) ) {
1536  snprintf(file, sizeof file, "%s.%s.bin", reg_name, svnames);
1537  this->sv_dump_add_by_name(region, imp_name, svnames, reg_name, file);
1538  svnames = e;
1539  }
1540 
1541  // Information is provided as follows:
1542  // region[X].plugin = "PLG_A:PLG_B"
1543  // region[X].plug_sv_dumps = "m,h,n:x,y"
1544 
1545  // parse plugin list
1546  while ( (p = get_next_list(plgnames, ':') ) ) {
1547  // get sv list corresponding to current plugin
1548  l = get_next_list(plgsvnames, ':');
1549  while ( (e = get_next_list(plgsvnames, ',') ) ) {
1550  snprintf(file, sizeof file, "%s_%s.%s.bin", reg_name, plgnames, plgsvnames);
1551  this->sv_dump_add_by_name(region, plgnames, plgsvnames, reg_name, file);
1552  plgsvnames = e;
1553  }
1554  plgnames = p;
1555  plgsvnames = l;
1556  }
1557 } // sv_dump_add_by_name_list
1558 
1574 void MULTI_IF::sv_dump_add(int region, const IonType& type, int offset, int size, int dtype,
1575  const char *filename, const char *regname) {
1576  IonIfBase *IF = this->IIF[region];
1577  int n = this->svd.n;
1578 
1579  // find the proper IMP
1580  if (IF->get_type() != type) {
1581  int i;
1582  for (i = 0; i < IF->plugins().size(); i++)
1583  if (IF->plugins()[i]->get_type() == type)
1584  break;
1585  if (i == IF->plugins().size()) {
1586  log_msg(logger, 2, 0, "Warning: IMP %s not found in Region %s\n",
1587  type.get_name().c_str(), regname);
1588  return;
1589  } else {
1590  IF = IF->plugins()[i];
1591  }
1592  }
1593 
1594  this->svd.active = 1;
1595  this->svd.n++;
1596  this->svd.hdls = static_cast<FILE_SPEC *>(realloc(this->svd.hdls, this->svd.n*sizeof(this->svd.hdls[0])));
1597  this->svd.fn = static_cast<char **>(realloc(this->svd.fn, this->svd.n*sizeof(char *)));
1598  this->svd.reg = static_cast<int *>(realloc(this->svd.reg, this->svd.n*sizeof(int)));
1599  this->svd.svnames = static_cast<char **>(realloc(this->svd.svnames, this->svd.n*sizeof(char *)));
1600  this->svd.n_dumps = 0;
1601  this->svd.nwr = 0;
1602  this->svd.offset = static_cast<int *>(realloc(this->svd.offset, this->svd.n*sizeof(int)));
1603  this->svd.size = static_cast<int *>(realloc(this->svd.size, this->svd.n*sizeof(int)));
1604  this->svd.dlo_vs = static_cast<int *>(realloc(this->svd.dlo_vs, this->svd.n*sizeof(int)));
1605  this->svd.dtype = static_cast<int *>(realloc(this->svd.dtype, this->svd.n*sizeof(int)));
1606  this->svd.svtab = static_cast<void **>(realloc(this->svd.svtab, this->svd.n*sizeof(void *)));
1607  this->svd.svsize = static_cast<size_t *>(realloc(this->svd.svsize, this->svd.n*sizeof(size_t)));
1608  this->svd.num = static_cast<node_count_t *>(realloc(this->svd.num, this->svd.n*sizeof(node_count_t)));
1609  this->svd.reg = static_cast<int *>(realloc(this->svd.reg, this->svd.n*sizeof(int)));
1610 
1611  if (!get_rank() ) {
1612 #ifdef HDF5
1613  assert(0);
1614 #else // ifdef HDF5
1615  this->svd.hdls[n] = f_open(filename, "w+");
1616 #endif // ifdef HDF5
1617  } else {
1618  this->svd.hdls[n] = NULL;
1619  }
1620 
1621  this->svd.fn[n] = dupstr(filename);
1622  this->svd.reg[n] = region;
1623  this->svd.svnames[n] = NULL; // has to be filled in in calling routine
1624  this->svd.offset[n] = offset;
1625  this->svd.size[n] = size;
1626  this->svd.dtype[n] = dtype;
1627  this->svd.svtab[n] = IF->get_sv_address();
1628  this->svd.svsize[n] = IF->get_sv_size();
1629  this->svd.num[n] = IF->get_num_node();
1630  this->svd.dlo_vs[n] = IF->get_type().dlo_vector_size();
1631 } // sv_dump_add
1632 
1643 int MULTI_IF::sv_dump_add_by_name(int region, char *impname,
1644  char *svname, char *regname, char *filename) {
1645  int offset, size, dtype, added = 0;
1646  char *svtypename = NULL;
1647  char *filename_bin;
1648 
1649  IonType* type = get_ion_type(std::string(impname));
1650  filename_bin = strcat(filename, ".bin");
1651 
1652  assert(type != NULL);
1653  if (type->get_sv_offset(svname, &offset, &size) ) {
1654  type->get_sv_type(svname, &dtype, &svtypename);
1655  this->sv_dump_add(region, *type, offset, size, dtype, filename_bin, regname);
1656  this->svd.svnames[this->svd.n-1] = dupstr(svname);
1657  added = 1;
1658  } else {
1659  log_msg(NULL, 1, 0, "No state variable added to dump list");
1660  }
1661 
1662  return added;
1663 }
1664 
1677 {
1678  assert(miif->gdata[Vm] != NULL);
1679 
1680  for (int i = 0; i < NUM_IMP_DATA_TYPES; i++) {
1681  for (int n = 0; n < miif->N_IIF; n++)
1682  if (USED_DAT(miif->IIF[n], imp_data_flag[i]) && (miif->gdata[i] == NULL)) {
1683  SF::init_vector(&miif->gdata[i], miif->gdata[Vm]);
1684  break;
1685  }
1686  }
1687 }
1688 
1697  int num_mech_data = 0;
1698 
1699  for (int i = 0; i < NUM_IMP_DATA_TYPES; i++) {
1700  if ( (Lambda_DATA_FLAG == i) || (delLambda_DATA_FLAG == i) ||
1701  (Tension_DATA_FLAG == i) || (tension_component_DATA_FLAG == i) ) {
1702  for (int n = 0; n < this->N_IIF; n++)
1703  if (USED_DAT(this->IIF[n], imp_data_flag[i]) )
1704  num_mech_data++;
1705  }
1706  }
1707  return static_cast<bool>(num_mech_data);
1708 }
1709 
1727 void MULTI_IF::transmem_stim_species(float charge, const char *species,
1728  float beta, int *node, int numnode) {
1729  float z;
1730  if (strstr(species, "Ca") != NULL) {
1731  charge *= 1000; // Ca uses micromolar instead of millimolar
1732  z = 2;
1733  } else if (strstr(species, "Na") != NULL) {
1734  z = 1;
1735  } else if (strstr(species, "Cl") != NULL) {
1736  z = -1;
1737  } else if (strstr(species, "K") != NULL) {
1738  z = 1;
1739  } else {
1740  log_msg(logger, 5, 0, "Unimplemented ion species: %s\n", species);
1741  exit(1);
1742  }
1743  int offset, sz;
1744  SVgetfcn ion_get;
1745  SVputfcn ion_put;
1746  static int warned = 0;
1747 
1748  int rank = get_rank();
1749  SF::vector<int> layout;
1750  layout_from_count(numnode, layout, PETSC_COMM_WORLD);
1751  int my_low_idx = layout[rank], my_high_idx = layout[rank+1];
1752 
1753  for (int i = 0; i < numnode; i++) {
1754  if ((node[i] < my_low_idx) || (node[i] >= my_high_idx)) continue; // not on processor
1755 
1756  for (int j = 0; j < this->N_IIF; j++) {
1757  if (this->NodeLists[j] == NULL) continue; // no stim nodes on processor
1758 
1759  if ((node[i] < this->NodeLists[j][0]) ||
1760  (node[i] > this->NodeLists[j][this->N_Nodes[j]-1])) {
1761  continue; // not in this IMP
1762  } else {
1763  ion_get = this->IIF[j]->get_type().get_sv_offset(species, &offset, &sz);
1764  if (ion_get == NULL) {
1765  if (!warned) {
1766  warned = 1;
1767  log_msg(logger, 2, 0, "Ion species not present in ionic model: %s\n",
1768  species);
1769  }
1770  return;
1771  }
1772  ion_put = getPutSV(ion_get);
1773  }
1774 
1775  // does this model provide a correct conversion factor?
1776  double delta_conc;
1777  cell_geom g = this->IIF[j]->cgeom();
1778  if (g.sl_i2c != NDEF) {
1779  delta_conc = charge*g.sl_i2c/z;
1780  } else {
1781  // conversion with generic factor
1782  delta_conc = 10*charge*beta/(FARADAY*z); // convert to millimolar/L
1783  }
1784 
1785  for (node_index_t k = 0; k < this->N_Nodes[j]; k++)
1786  if (node[i] == this->NodeLists[j][k])
1787  ion_put(*this->IIF[j], k, offset, ion_get(*this->IIF[j], k, offset)-delta_conc);
1788  }
1789  }
1790 } // transmem_stim_species
1791 
1798 void MULTI_IF::MIIF_change_dt( double Dt) {
1799  this->dt = Dt;
1800  for (int i = 0; i < this->N_IIF; i++) {
1801  this->IIF[i]->destroy_luts();
1802  this->IIF[i]->set_dt((float) Dt);
1803  this->iontypes[i].get().construct_tables(*this->IIF[i]);
1804 
1805  for (int j = 0; j < this->IIF[i]->plugins().size(); j++) {
1806  auto& plugin = this->IIF[i]->plugins()[j];
1807  plugin->destroy_luts();
1808  plugin->set_dt((float) Dt);
1809  this->plugtypes[i][j].get().construct_tables(*plugin);
1810  }
1811  }
1812 }
1813 
1814 #define MEMFREE(A) free(A)
1815 
1823  assert(m->doppel);
1824 
1825  for (int i = 0; i < m->N_IIF; i++) {
1826  m->IIF[i]->get_type().destroy_ion_if(m->IIF[i]);
1827  }
1828 
1829  for (int j = 0; j < NUM_IMP_DATA_TYPES; j++) {
1830  if (m->gdata[j])
1831  delete m->gdata[j];
1832  }
1833 
1834  m->zero_data();
1835 }
1836 
1837 #undef MEMFREE
1838 
1846 void doppel_update(MULTI_IF *orig, MULTI_IF *miif_doppel) {
1847  // removed assertion, need to be able to update bidirectionally
1848  // assert(doppel->doppel);
1849 
1850  for (int i = 0; i < NUM_IMP_DATA_TYPES; i++)
1851  if (orig->gdata[i])
1852  SF::init_vector(&miif_doppel->gdata[i], orig->gdata[i]);
1853 
1854  for (int i = 0; i < orig->N_IIF; i++) {
1855  miif_doppel->IIF[i]->copy_SVs_from(*orig->IIF[i], false);
1856 
1857  for (int j = 0; j < orig->IIF[i]->plugins().size(); j++)
1858  miif_doppel->IIF[i]->plugins()[j]->copy_SVs_from(*orig->IIF[i]->plugins()[j], false);
1859  }
1860 
1861  miif_doppel->getRealData();
1862 }
1863 
1874 void doppel_MIIF(MULTI_IF *orig, MULTI_IF *miif_doppel) {
1875  *miif_doppel = *orig;
1876  miif_doppel->doppel = true;
1877 
1878  // deal with the local portion of the global data
1879  for (int i = 0; i < NUM_IMP_DATA_TYPES; i++)
1880  if (orig->gdata[i])
1881  SF::init_vector(&miif_doppel->gdata[i], orig->gdata[i]);
1882 
1883  // copy the state variables
1884  miif_doppel->IIF = {};
1885 
1886  for (int i = 0; i < orig->N_IIF; i++) {
1887  // Make a new IonIf for this clone and copy state variables and plugins
1888  miif_doppel->IIF.push_back(orig->IIF[i]->get_type().make_ion_if(orig->IIF[i]->get_target(),
1889  orig->IIF[i]->get_num_node(), orig->plugtypes[i]));
1890  miif_doppel->IIF[i]->copy_SVs_from(*orig->IIF[i], true);
1891  miif_doppel->IIF[i]->copy_plugins_from(*orig->IIF[i]);
1892  }
1893 }
1894 
1902 bool isIMPdata(const char *sv) {
1903  return IMPdataLabel2Index(sv) != -1;
1904 }
1905 
1913 int IMPdataLabel2Index(const char *sv) {
1914  int imp_data_id = -1;
1915 
1916  for (int i = 0; i < NUM_IMP_DATA_TYPES; i++) {
1917  if (strcmp(sv, imp_data_names[i]) == 0) {
1918  imp_data_id = i;
1919  break;
1920  }
1921  }
1922  return imp_data_id;
1923 }
1924 
1925 /* copy all of the IMP data from one node to another
1926  *
1927  * \param IF ionic model
1928  * \param from local IMP number
1929  * \param to local IMP number
1930  */
1932  for (int i = 0; i < NUM_IMP_DATA_TYPES; i++)
1933  if (localdata && localdata[i])
1934  localdata[i][to] = localdata[i][from];
1935 
1936  IF.for_each([&](IonIfBase& imp) {
1937  char **sv_list;
1938  int sv_list_size = imp.get_type().get_sv_list(&sv_list);
1939 
1940  for (int i = 0; i < sv_list_size; i++) {
1941  char *sv_name = sv_list[i];
1942  int sv_offset;
1943  int sv_size;
1944  SVgetfcn sv_get = imp.get_type().get_sv_offset(sv_name, &sv_offset, &sv_size);
1945  if (sv_get == NULL) {
1946  throw std::runtime_error(std::string(__func__) + " error: " + sv_name + " is not a valid state variable for the " + imp.get_type().get_name() + " model.");
1947  }
1948  SVputfcn sv_put = getPutSV(sv_get);
1949 
1950  GlobalData_t sv_val = sv_get(imp, from, sv_offset);
1951  sv_put(imp, to, sv_offset, sv_val);
1952  }
1953  free(sv_list);
1954  });
1955 }
1956 
1957 } // namespace limpet
#define NDEF
definition of cell geometry
Definition: ION_IF.h:113
#define FILENAME_BUF
#define MAX_TRACE_LINE_LEN
Define multiple ionic models to be used in different regions.
#define FARADAY
Faraday's constant.
Definition: MULTI_ION_IF.h:147
#define USED_DAT(I, F)
Definition: MULTI_ION_IF.h:301
opencarp::local_index_t mesh_int_t
Definition: SF_container.h:46
opencarp::real_t SF_real
Global scalar type.
Definition: SF_globals.h:33
#define SLIST_APPEND(S, P)
Definition: basics.h:54
#define FLUSH
Definition: basics.h:319
#define LOCAL
Definition: basics.h:317
#define fmemopen_
Definition: basics.h:441
#define STRUCT_ZERO(S)
Definition: basics.h:53
virtual S * ptr()=0
virtual void release_ptr(S *&p)=0
size_t read_binary(FILE *fd)
virtual void add_scaled(const abstract_vector< T, S > &vec, S k)=0
size_t write_binary(FILE *fd)
Write a vector to HD in binary. File descriptor is already set up.
virtual void set(const vector< T > &idx, const vector< S > &vals, const bool additive=false, const bool local=false)=0
bool in_b(const T idx)
return whether idx is in set B
Definition: SF_container.h:366
T backward_map(T idx) const
Map one index from b to a.
Definition: SF_container.h:274
overlapping_layout< T > pl
nodal parallel layout
Definition: SF_container.h:429
Container for a PETSc VecScatter.
void forward(abstract_vector< T, S > &in, abstract_vector< T, S > &out, bool add=false)
Forward scattering.
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
T * data()
Pointer to the vector's start.
Definition: SF_vector.h:91
Represents the ionic model and plug-in (IMP) data structure.
Definition: ION_IF.h:139
const IonType & get_type() const
Gets this IMP's model type.
Definition: ION_IF.cc:144
std::vector< IonIfBase * > & plugins()
Returns a vector containing the plugins of this IMP.
Definition: ION_IF.cc:184
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:270
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
IonIfBase * parent() const
Gets the parent IMP.
Definition: ION_IF.cc:176
int miifIdx
imp index within miif
Definition: ION_IF.h:151
virtual std::size_t get_sv_size() const =0
Gets the size of the structure this IMP uses for state variables.
virtual void * get_sv_address()=0
Gets the raw address of the state variables for this IMP.
node_count_t get_num_node() const
Gets the number of nodes handled by this IMP.
Definition: ION_IF.cc:148
Abstract class representing an ionic model type.
Definition: ion_type.h:59
bool is_plugin() const
Returns whether this model is a plugin or not.
Definition: ion_type.cc:27
const std::string & get_name() const
Gets the model name.
Definition: ion_type.cc:23
virtual SVgetfcn get_sv_offset(const char *svname, int *off, int *sz) const =0
Get the offset and size of a state variable of the model, as well as an access function.
virtual int get_sv_type(const char *svname, int *type, char **type_name) const =0
Determines the type of a SV.
virtual size_t dlo_vector_size() const =0
Gets the vector size when using data layout optimization (DLO).
bool extUpdateVm
flag indicating update function for Vm
Definition: MULTI_ION_IF.h:208
std::vector< IonIfBase * > IIF
array of IIF's
Definition: MULTI_ION_IF.h:202
opencarp::sf_vec * gdata[NUM_IMP_DATA_TYPES]
data used by all IMPs
Definition: MULTI_ION_IF.h:216
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:210
size_t dump_svs(opencarp::base_timer *)
std::vector< Target > targets
target for each region
Definition: MULTI_ION_IF.h:213
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
void dump_state(char *, float, opencarp::mesh_t gid, bool, unsigned int)
SV_DUMP svd
state variable dump
Definition: MULTI_ION_IF.h:203
void sv_dump_add(int, const IonType &, int, int, int, const char *, const char *)
void transmem_stim_species(float, const char *, float, int *, int)
void initialize_currents(double, int)
GlobalData_t *** ldata
data local to each IMP
Definition: MULTI_ION_IF.h:205
float restore_state(const char *, opencarp::mesh_t gid, bool)
int N_IIF
how many different IIF's
Definition: MULTI_ION_IF.h:211
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...
int sv_dump_add_by_name(int, char *, char *, char *, char *)
Trace_Info * trace_info
Information about traces.
Definition: MULTI_ION_IF.h:207
double dt
time step (ms)
Definition: MULTI_ION_IF.h:219
node_count_t * N_Nodes
#nodes for each IMP
Definition: MULTI_ION_IF.h:200
GlobalData_t * procdata[NUM_IMP_DATA_TYPES]
data for this processor
Definition: MULTI_ION_IF.h:204
void dump_luts_MIIF(bool)
bool * contiguous
whether a region is contiguously numbered
Definition: MULTI_ION_IF.h:206
opencarp::FILE_SPEC logger
Definition: MULTI_ION_IF.h:217
int numSubDt
number of sub-dt time steps
Definition: MULTI_ION_IF.h:220
node_index_t ** NodeLists
local partitioned node lists for each IMP stored
Definition: MULTI_ION_IF.h:201
void MIIF_change_dt(double)
IIF_Mask_t * IIFmask
region for each node
Definition: MULTI_ION_IF.h:214
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:198
bool doppel
is this a shallow clone?
Definition: MULTI_ION_IF.h:199
void releaseRealDataDuringInit()
#define log_msg(F, L, O,...)
Definition: filament.h:8
void dsp_from_cnt(const vector< T > &cnt, vector< T > &dsp)
Compute displacements from counts.
Definition: SF_vector.h:310
T sum(const vector< T > &vec)
Compute sum of a vector's entries.
Definition: SF_vector.h:340
void local_petsc_to_nodal_mapping(const meshdata< T, S > &mesh, index_mapping< T > &petsc_to_nodal)
void layout_from_count(const T count, vector< T > &layout, MPI_Comm comm)
Definition: SF_network.h:201
void init_vector(SF::abstract_vector< T, S > **vec)
Definition: SF_init.h:107
@ NBR_SUBMESH
Submesh nodal numbering: The globally ascending sorted reference indices are reindexed.
Definition: SF_container.h:202
void(* SVputfcn)(IonIfBase &, node_index_t, int, GlobalData_t)
Definition: ion_type.h:49
void CreateIIFNodeLsts_(int, IIF_Mask_t *, node_count_t **, node_index_t ***, node_count_t)
void doppel_MIIF(MULTI_IF *orig, MULTI_IF *miif_doppel)
void initializeIMPData(MULTI_IF *pMIIF)
int determine_write_ranges(int N, size_t *offset, size_t bufsize, int **ranges)
int get_plug_flag(char *plgstr, int *out_num_plugins, IonTypeList &out_plugins)
SVputfcn getPutSV(SVgetfcn)
void doppel_update(MULTI_IF *orig, MULTI_IF *miif_doppel)
double Real
Definition: MULTI_ION_IF.h:151
node_index_t getGlobalNodalIndex(IonIfBase &pIF, node_index_t relIdx)
int should_print_bounds_exceeded_messages()
@ CPU
baseline CPU model generated with the original opencarp code generator
Definition: target.h:48
IonType * get_ion_type(const std::string &name)
int IMPdataLabel2Index(const char *sv)
void CreateIIFGlobalNodeLsts(MULTI_IF *pMIIF)
SF_real GlobalData_t
Definition: limpet_types.h:27
void allocate_shared_data(MULTI_IF *)
bool isIMPdata(const char *)
bool is_gpu(Target const target)
Checks if this is a GPU target.
Definition: target.cc:67
void initialize_params_MIIF(MULTI_IF *pMIIF)
constexpr T min(T a, T b)
Definition: ion_type.h:33
void alloc_MIIF(MULTI_IF *pMIIF)
void freeIMPData(MULTI_IF *pMIIF)
GlobalData_t(* SVgetfcn)(IonIfBase &, node_index_t, int)
Definition: ion_type.h:48
std::vector< std::reference_wrapper< IonType > > IonTypeList
Definition: ion_type.h:291
void dup_IMP_node_state(IonIfBase &IF, node_index_t from, node_index_t to, GlobalData_t **localdata)
constexpr T max(T a, T b)
Definition: ion_type.h:31
char * get_sv(void *tab, int offset, node_count_t n, int svSize, int size, int dlo_vector_size)
void deallocate_on_target(Target target, T *ptr)
Utility function for deallocating memory on a target. See TargetAllocator.
Definition: target.h:318
void CreateIIFLocalNodeLsts(MULTI_IF *pMIIF)
void initialize_ionic_IF(MULTI_IF *pMIIF)
void update_ts(ts *ptstp)
Definition: ION_IF.cc:466
float current_global_time()
opencarp::local_index_t node_count_t
Definition: limpet_types.h:29
void close_trace(MULTI_IF *MIIF)
int node_index_cmp(const void *a, const void *b)
void dump_trace(MULTI_IF *MIIF, limpet::Real time)
char * get_next_list(char *lst, char delimiter)
Definition: ION_IF.cc:512
void open_trace(MULTI_IF *MIIF, int n_traceNodes, int *traceNodes, int *label, opencarp::sf_mesh *imesh)
Set up ionic model traces at some global node numbers.
char IIF_Mask_t
Definition: ion_type.h:50
void free_doppel(MULTI_IF *m)
opencarp::global_index_t global_node_index_t
Definition: limpet_types.h:30
node_index_t current_global_node(node_index_t local_node)
opencarp::local_index_t node_index_t
Definition: limpet_types.h:28
char * tokstr_r(char *s1, const char *s2, char **lasts)
Definition: ION_IF.cc:88
int set_print_bounds_exceeded_messages(int newval)
std::intmax_t printable_int(T value)
Definition: mpi_utils.h:130
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
char * read_bin_string(FILE_SPEC in)
Definition: basics.cc:231
void f_read_par(void *ptr, size_t size, size_t nmemb, FILE_SPEC stream, MPI_Comm comm)
Parallel fread. Root reads, then broadcasts.
Definition: basics.cc:174
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 write_bin_string(FILE_SPEC out, const char *s)
Definition: basics.cc:222
FILE_SPEC f_open(const char *fname, const char *mode)
Open a FILE_SPEC.
Definition: basics.cc:138
char * read_bin_string_par(FILE_SPEC in)
Definition: basics.cc:242
char * dupstr(const char *old_str)
Definition: basics.cc:44
mesh_t
The enum identifying the different meshes we might want to load.
Definition: sf_interface.h:59
void get_time(double &tm)
Definition: basics.h:444
SF::abstract_vector< SF_int, SF_real > sf_vec
Definition: sf_interface.h:50
int get_size(MPI_Comm comm=PETSC_COMM_WORLD)
Definition: basics.h:298
V timing(V &t2, const V &t1)
Definition: basics.h:456
void f_close(FILE_SPEC &f)
Close a FILE_SPEC.
Definition: basics.cc:165
file_desc * FILE_SPEC
Definition: basics.h:140
#define PETSC_TO_CANONICAL
Permute algebraic data from PETSC to canonical ordering.
Definition: sf_interface.h:79
int offset
offset into node data
Definition: ION_IF.h:133
int sz
storage required
Definition: ION_IF.h:128
char * name
IMP name.
Definition: ION_IF.h:127
int map
which plugin does this IMO match
Definition: ION_IF.h:132
IMPinfo * plug
plugins
Definition: ION_IF.h:130
bool compatible
does IM match stored IM
Definition: ION_IF.h:131
int nplug
number of plugins
Definition: ION_IF.h:129
data structure to manage state variable file dumps
Definition: MULTI_ION_IF.h:160
size_t * svsize
state variable sizes
Definition: MULTI_ION_IF.h:176
char ** fn
array to store file names
Definition: MULTI_ION_IF.h:164
double intv
time interval for sv dumps
Definition: MULTI_ION_IF.h:167
int * dtype
data type
Definition: MULTI_ION_IF.h:173
int n_dumps
keep track of number of dumped time slices
Definition: MULTI_ION_IF.h:169
node_count_t * num
number of nodes
Definition: MULTI_ION_IF.h:174
long nwr
keep track of number of written tokens
Definition: MULTI_ION_IF.h:170
int * size
sizes of SV to dump
Definition: MULTI_ION_IF.h:172
void ** svtab
state variable tables
Definition: MULTI_ION_IF.h:175
int * reg
array to store region ids
Definition: MULTI_ION_IF.h:165
double t_dump
next instant for sv dump
Definition: MULTI_ION_IF.h:168
char ** svnames
array to store sv names
Definition: MULTI_ION_IF.h:166
int n
#state variables we want to dump
Definition: MULTI_ION_IF.h:162
int * offset
offsets into structure for SV
Definition: MULTI_ION_IF.h:171
opencarp::FILE_SPEC * hdls
array of file handles to sv output files
Definition: MULTI_ION_IF.h:163
data structure to manage trace dumps. Should eventually be combined with the state variable dumps,...
Definition: MULTI_ION_IF.h:184
bool ignored
globally not found
Definition: MULTI_ION_IF.h:186
node_index_t node_idx
local node number
Definition: MULTI_ION_IF.h:187
bool found
found on this node
Definition: MULTI_ION_IF.h:185
opencarp::FILE_SPEC file
Definition: MULTI_ION_IF.h:189
float sl_i2c
convert sl-currents in uA/cm^2 to mM/L without valence
Definition: ION_IF.h:119
int cnt
Definition: ION_IF.h:87