openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
electrics_eikonal.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 "electrics_eikonal.h"
28 #include "electric_integrators.h"
29 #include <cstdlib>
30 #include "SF_globals.h"
31 #include "basics.h"
32 #include "petsc_utils.h"
33 #include "timers.h"
34 #include "stimulate.h"
35 
36 #include "SF_init.h"
37 
38 namespace opencarp
39 {
40 
42 {
43  if (get_size() > 1) {
44  log_msg(NULL, 5, 0, "DREAM/Eikonal physics currently do not support MPI parallelization. Use openMP instead. Aborting!");
45  EXIT(EXIT_FAILURE);
46  }
47 
48  double t1, t2;
49  get_time(t1);
50 
51  set_dir(OUTPUT);
52 
53  // open logger
54  logger = f_open("eikonal.log", param_globals::experiment != 4 ? "w" : "r");
55 
56  // setup mappings between extra and intra grids, algebraic and nodal,
57  // and between PETSc and canonical orderings
58  setup_mappings();
59 
60  eik_tech = static_cast<Eikonal::eikonal_t>(param_globals::dream.solve);
61 
62  // the ionic physics is currently triggered from inside the Electrics to have tighter
63  // control over it. The standalone eikonal solver does not require it.
64  switch (eik_tech) {
65  case EIKONAL: break;
66  default:
67  ion.logger = logger;
68  ion.initialize();
69  }
70 
71  // set up Intracellular tissue
72  set_elec_tissue_properties(mtype, intra_grid, logger);
73  region_mask(intra_elec_msh, mtype[intra_grid].regions, mtype[intra_grid].regionIDs, true, "gregion_i");
74 
75  // add electrics timer for time stepping, add to time stepper tool (TS)
76  double global_time = user_globals::tm_manager->time;
77  timer_idx = user_globals::tm_manager->add_eq_timer(global_time, param_globals::tend, 0,
78  param_globals::dt, 0, "elec::ref_dt", "TS");
79 
80  // electrics stimuli setup
81  setup_stimuli();
82 
83  // set up the linear equation systems. this needs to happen after the stimuli have been
84  // set up, since we need boundary condition info
85  setup_solvers();
86 
87  // the next setup steps require the solvers to be set up, since they use the matrices
88  // generated by those
89 
90  // initialize the LATs detector
91  switch (eik_tech) {
92  case EIKONAL: break; // not available for pure eikonal solve
93  default:
95  }
96 
97  // prepare the electrics output. we skip it if we do post-processing
98  if (param_globals::experiment != EXP_POSTPROCESS)
99  setup_output();
100 
101  this->initialize_time += timing(t2, t1);
102  log_msg(NULL, 0, 0, "All done in %f sec.", float(t2 - t1));
103 }
104 
105 void Eikonal::set_elec_tissue_properties(MaterialType* mtype, Eikonal::grid_t g, FILE_SPEC logger)
106 {
107  MaterialType* m = mtype + g;
108 
109  // initialize random conductivity fluctuation structure with PrM values
110  m->regions.resize(param_globals::num_gregions);
111 
112  const char* grid_name = g == Eikonal::intra_grid ? "intracellular" : "extracellular";
113  log_msg(logger, 0, 0, "Setting up %s tissue poperties for %d regions ..", grid_name,
114  param_globals::num_gregions);
115 
116  char buf[64];
117  RegionSpecs* reg = m->regions.data();
118 
119  for (size_t i = 0; i < m->regions.size(); i++, reg++) {
120  if (!strcmp(param_globals::gregion[i].name, "")) {
121  snprintf(buf, sizeof buf, ", gregion_%d", int(i));
122  param_globals::gregion[i].name = dupstr(buf);
123  }
124 
125  reg->regname = strdup(param_globals::gregion[i].name);
126  reg->regID = i;
127  reg->nsubregs = param_globals::gregion[i].num_IDs;
128  if (!reg->nsubregs)
129  reg->subregtags = NULL;
130  else {
131  reg->subregtags = new int[reg->nsubregs];
132 
133  for (int j = 0; j < reg->nsubregs; j++)
134  reg->subregtags[j] = param_globals::gregion[i].ID[j];
135  }
136 
137  // describe material in given region
138  elecMaterial* emat = new elecMaterial();
139  emat->material_type = ElecMat;
140 
141  emat->InVal[0] = param_globals::gregion[i].g_il;
142  emat->InVal[1] = param_globals::gregion[i].g_it;
143  emat->InVal[2] = param_globals::gregion[i].g_in;
144 
145  emat->ExVal[0] = param_globals::gregion[i].g_el;
146  emat->ExVal[1] = param_globals::gregion[i].g_et;
147  emat->ExVal[2] = param_globals::gregion[i].g_en;
148 
149  emat->BathVal[0] = param_globals::gregion[i].g_bath;
150  emat->BathVal[1] = param_globals::gregion[i].g_bath;
151  emat->BathVal[2] = param_globals::gregion[i].g_bath;
152 
153  // convert units from S/m -> mS/um
154  for (int j = 0; j < 3; j++) {
155  emat->InVal[j] *= 1e-3 * param_globals::gregion[i].g_mult;
156  emat->ExVal[j] *= 1e-3 * param_globals::gregion[i].g_mult;
157  emat->BathVal[j] *= 1e-3 * param_globals::gregion[i].g_mult;
158  }
159  reg->material = emat;
160  }
161 
162  {
164  const char* file = g == Eikonal::intra_grid ? param_globals::gi_scale_vec : param_globals::ge_scale_vec;
165  if (strlen(file))
166  read_el_scale_vec(file, mt, m->el_scale, m->el_scale_dpn);
167  }
168 }
169 
170 void Eikonal::setup_mappings()
171 {
172  bool intra_exits = mesh_is_registered(intra_elec_msh), extra_exists = mesh_is_registered(extra_elec_msh);
173  assert(intra_exits);
174  const int dpn = 1;
175 
176  // It may be that another physic (e.g. ionic models) has already computed the intracellular mappings,
177  // thus we first test their existence
178  if (get_scattering(intra_elec_msh, ALG_TO_NODAL, dpn) == NULL) {
179  log_msg(logger, 0, 0, "%s: Setting up intracellular algebraic-to-nodal scattering.", __func__);
181  }
183  log_msg(logger, 0, 0, "%s: Setting up intracellular PETSc to canonical permutation.", __func__);
185  }
186 }
187 
189 {
190  double t1, t2;
191  get_time(t1);
192 
193  switch (eik_tech) {
194  case EIKONAL: solve_EIKONAL(); break;
195  case DREAM: solve_DREAM(); break;
196  default: solve_RE(); break;
197  }
198 
200  // output lin solver stats
202  }
203  this->compute_time += timing(t2, t1);
204 
205  // since the traces have their own timing, we check for trace dumps in the compute step loop
208 }
209 
211 {
212  double t1, t2;
213  get_time(t1);
214 
215  switch (eik_tech) {
216  case EIKONAL: break;
217  default:
219  output_manager_time.write_data(); // does not exist in EIKONAL
220  }
221 
222  if (do_output_eikonal) {
224  do_output_eikonal = false;
225  }
226 
227  double curtime = timing(t2, t1);
228  this->output_time += curtime;
229 
230  IO_stats.calls++;
231  IO_stats.tot_time += curtime;
232 
235 }
236 
241 {
242  // close logger
243  f_close(logger);
244 
245  switch (eik_tech) {
246  case EIKONAL:
248  break;
249  default:
250  // output LAT data
254  ion.destroy();
255  }
256 }
257 
258 void Eikonal::setup_stimuli()
259 {
260  // initialize basic stim info data (used units, supported types, etc)
261  init_stim_info();
262 
263  stimuli.resize(param_globals::num_stim);
264  for (int i = 0; i < param_globals::num_stim; i++) {
265  // construct new stimulus
266  stimulus& s = stimuli[i];
267 
268  if (param_globals::stim[i].crct.type != 0 && param_globals::stim[i].crct.type != 9) {
269  // In the Eikonal class, only the intracellular domain is registered.
270  // Therefore, only I_tm and Vm_clmp are compatible.
271  log_msg(NULL, 5, 0, "%s error: stimulus of type %i is incompatible with the eikonal model! Use I_tm or Vm_clmp instead. Aborting!", __func__, s.phys.type);
272  EXIT(EXIT_FAILURE);
273  }
274 
276  s.translate(i);
277 
278  s.setup(i);
279 
280  if (s.electrode.dump_vtx)
281  s.dump_vtx_file(i);
282 
283  log_msg(NULL, 2, 0, "Only geometry, start time, npls, and bcl of stim[%i] are used", i);
284 
285  if (param_globals::stim[i].pulse.dumpTrace && get_rank() == 0) {
286  set_dir(OUTPUT);
287  s.pulse.wave.write_trace(s.name + ".trc");
288  }
289  }
290 
292 }
293 
294 void Eikonal::stimulate_intracellular()
295 {
296  parabolic_solver& ps = parab_solver;
297 
298  // iterate over stimuli
299  for (stimulus& s : stimuli) {
300  if (s.is_active()) {
301  // for active stimuli, deal with the stimuli-type specific stimulus application
302  switch (s.phys.type) {
303  case I_tm: {
304  if (param_globals::operator_splitting) {
305  apply_stim_to_vector(s, *ps.Vmv, true);
306  } else {
307  SF_real Cm = 1.0;
308  timer_manager& tm = *user_globals::tm_manager;
309  SF_real sc = tm.time_step / Cm;
310 
311  ps.Irhs->set(0.0);
312  apply_stim_to_vector(s, *ps.Irhs, true);
313 
314  *ps.tmp_i1 = *ps.IIon;
315  *ps.tmp_i1 -= *ps.Irhs;
316  *ps.tmp_i1 *= sc; // tmp_i1 = sc * (IIon - Irhs)
317 
318  // add ionic, transmembrane and intracellular currents to rhs
319  if (param_globals::parab_solve != parabolic_solver::EXPLICIT)
320  ps.mass_i->mult(*ps.tmp_i1, *ps.Irhs);
321  else
322  *ps.Irhs = *ps.tmp_i1;
323  }
324  break;
325  }
326 
327  case Illum: {
328  sf_vec* illum_vec = ion.miif->gdata[limpet::illum];
329 
330  if (illum_vec == NULL) {
331  log_msg(0, 5, 0, "Cannot apply illumination stim: global vector not present!");
332  EXIT(EXIT_FAILURE);
333  } else {
334  apply_stim_to_vector(s, *illum_vec, false);
335  }
336 
337  break;
338  }
339 
340  default: break;
341  }
342  }
343  }
344 }
345 
346 void Eikonal::clamp_Vm()
347 {
348  for (stimulus& s : stimuli) {
349  if (s.phys.type == Vm_clmp && s.is_active())
351  }
352 }
353 
354 void Eikonal::setup_output()
355 {
356  int rank = get_rank();
357  SF::vector<mesh_int_t>* restr_i = NULL;
358  SF::vector<mesh_int_t>* restr_e = NULL;
359  set_dir(OUTPUT);
360 
361  setup_dataout(param_globals::dataout_i, param_globals::dataout_i_vtx, intra_elec_msh,
362  restr_i, param_globals::num_io_nodes > 0);
363 
364  if (param_globals::dataout_i) {
365  switch (eik_tech) {
366  case EIKONAL:
367  output_manager_cycle.register_output(eik_solver.AT, intra_elec_msh, 1, param_globals::dream.output.atfile, "ms", restr_i);
368  break;
369  case DREAM:
370  output_manager_time.register_output(parab_solver.Vmv, intra_elec_msh, 1, param_globals::vofile, "mV", restr_i);
371  output_manager_cycle.register_output(eik_solver.AT, intra_elec_msh, 1, param_globals::dream.output.atfile, "ms", restr_i);
372  output_manager_cycle.register_output(eik_solver.RT, intra_elec_msh, 1, param_globals::dream.output.rtfile, "ms", restr_i);
373  if (strcmp(param_globals::dream.output.idifffile, "") != 0) {
374  output_manager_time.register_output(eik_solver.Idiff, intra_elec_msh, 1, param_globals::dream.output.idifffile, "muA/cm2", restr_i);
375  }
376  break;
377  default:
378  output_manager_time.register_output(parab_solver.Vmv, intra_elec_msh, 1, param_globals::vofile, "mV", restr_i);
379  output_manager_cycle.register_output(eik_solver.AT, intra_elec_msh, 1, param_globals::dream.output.atfile, "ms", restr_i);
380  if (strcmp(param_globals::dream.output.idifffile, "") != 0) {
381  output_manager_time.register_output(eik_solver.Idiff, intra_elec_msh, 1, param_globals::dream.output.idifffile, "muA/cm2", restr_i);
382  }
383  }
384  }
385 
387 
388  if (param_globals::num_trace) {
389  sf_mesh& imesh = get_mesh(intra_elec_msh);
390  open_trace(ion.miif, param_globals::num_trace, param_globals::trace_node, NULL, &imesh);
391  }
392 
393  // initialize generic logger for IO timings per time_dt
394  IO_stats.init_logger("IO_stats.dat");
395 }
396 
397 void Eikonal::dump_matrices()
398 {
399  std::string bsname = param_globals::dump_basename;
400  std::string fn;
401 
402  set_dir(OUTPUT);
403 
404  // dump monodomain matrices
405  if (param_globals::parab_solve == 1) {
406  // using Crank-Nicolson
407  fn = bsname + "_Ki_CN.bin";
408  parab_solver.lhs_parab->write(fn.c_str());
409  }
410  fn = bsname + "_Ki.bin";
411  parab_solver.rhs_parab->write(fn.c_str());
412 
413  fn = bsname + "_Mi.bin";
414  parab_solver.mass_i->write(fn.c_str());
415 }
416 
419 double Eikonal::timer_val(const int timer_id)
420 {
421  // determine
422  int sidx = stimidx_from_timeridx(stimuli, timer_id);
423  double val = 0.0;
424  if (sidx != -1) {
425  stimuli[sidx].value(val);
426  } else
427  val = std::nan("NaN");
428 
429  return val;
430 }
431 
434 std::string Eikonal::timer_unit(const int timer_id)
435 {
436  int sidx = stimidx_from_timeridx(stimuli, timer_id);
437  std::string s_unit;
438 
439  if (sidx != -1)
440  // found a timer-linked stimulus
441  s_unit = stimuli[sidx].pulse.wave.f_unit;
442 
443  return s_unit;
444 }
445 
446 void Eikonal::setup_solvers()
447 {
448  set_dir(OUTPUT);
449 
450  switch (eik_tech) {
451  case EIKONAL:
452  eik_solver.init();
453  break;
454  default:
455  parab_solver.init();
457  eik_solver.init();
459  if (param_globals::dump2MatLab) dump_matrices();
460  }
461 }
462 
463 void Eikonal::checkpointing()
464 {
465  const timer_manager& tm = *user_globals::tm_manager;
466 
467  // regular user selected state save
468  if (tm.trigger(iotm_chkpt_list)) {
469  char save_fnm[1024];
470  const char* tsav_ext = get_tsav_ext(tm.time);
471 
472  snprintf(save_fnm, sizeof save_fnm, "%s.%s.roe", param_globals::write_statef, tsav_ext);
473 
474  ion.miif->dump_state(save_fnm, tm.time, intra_elec_msh, false, GIT_COMMIT_COUNT);
475  eik_solver.save_eikonal_state(tsav_ext);
476  }
477 
478  // checkpointing based on interval
479  if (tm.trigger(iotm_chkpt_intv)) {
480  char save_fnm[1024];
481  snprintf(save_fnm, sizeof save_fnm, "checkpoint.%.1f.roe", tm.time);
482  ion.miif->dump_state(save_fnm, tm.time, intra_elec_msh, false, GIT_COMMIT_COUNT);
483  }
484 }
485 
486 void Eikonal::solve_EIKONAL()
487 {
488  if (user_globals::tm_manager->time == 0) {
489  eik_solver.FIM();
492  do_output_eikonal = true;
493  }
494 }
495 
496 void Eikonal::solve_RE()
497 {
498  if (user_globals::tm_manager->time == 0) {
499  eik_solver.FIM();
502  do_output_eikonal = true;
503  }
504  solve_RD();
505 }
506 
507 void Eikonal::solve_DREAM()
508 {
510  solve_RD();
511  } else {
512  if (user_globals::tm_manager->time != 0) {
514  }
515 
516  eik_solver.cycFIM();
518 
521  do_output_eikonal = true;
522  }
523 }
524 
525 void Eikonal::solve_RD()
526 {
527  // if requested, we checkpoint the current state
528  checkpointing();
529 
530  // activation checking
531  const double time = user_globals::tm_manager->time,
532  time_step = user_globals::tm_manager->time_step;
533 
534  lat.check_acts(time);
535  lat.check_quiescence(time, time_step);
536 
537  clamp_Vm();
538 
539  *parab_solver.old_vm = *parab_solver.Vmv; // needed in step D of DREAM
540 
541  // compute ionics update
542  ion.compute_step();
543 
544  // Compute the I diff current
545  *eik_solver.Idiff *= 0;
548 
549  if (eik_tech == REp) {
551  }
552 
553  switch (eik_tech) {
555  default: break;
556  }
557 
558  clamp_Vm();
559 }
560 
562 {
563  if (param_globals::output_level > 1) log_msg(0, 0, 0, "\n *** Initializing Eikonal Solver ***\n");
564  stats.init_logger("eik_stats.dat");
565 
566  if (param_globals::dream.output.debugNode >= 0) {
567  nodeData.idX = param_globals::dream.output.debugNode;
568  char buf[256];
569  snprintf(buf, sizeof buf, "node_%lld.dat", static_cast<long long>(nodeData.idX));
570  nodeData.init_logger(buf);
571  }
572 
573  // currently only used for output
574  const sf_mesh& mesh = get_mesh(intra_elec_msh);
575  twoFib = mesh.she.size() > 0;
579 
580  num_pts = mesh.l_numpts;
581 
582  e2n_con = mesh.con; // Connectivity vector with nodes that belong to each element
583  // The 4 nodes from index 4j to 4j+3 belong to the same element for 0<=j<num elements
584  elem_start = mesh.dsp; // For the i_th element elem_start[j] stores its starting position in the "connect" vector
585 
589 
590  List.assign(num_pts, 0); // List of active nodes
591  T_A.assign(num_pts, inf); // Activation times
592  D_I.assign(num_pts, 0); // Diastolic Intervals
593  T_R.assign(num_pts, -400); // Recovery times
594  TA_old.assign(num_pts, inf); // Activation Time is the previous cycle
595  num_changes.assign(num_pts, 0); // Number of Times entered in the List per current LAT
596  nReadded2List.assign(num_pts, 0); // Number of Times entered in the List per current LAT
597  stim_status.assign(num_pts, 0); // Status of nodes
598 
599  switch (mesh.type[0]) {
600  case 0:
601  MESH_SIZE = 4;
602  break;
603 
604  case 6:
605  MESH_SIZE = 3;
606  break;
607 
608  case 7:
609  MESH_SIZE = 2;
610  break;
611 
612  default:
613  log_msg(0, 5, 0, "Error: Type of element is not compatible with this version of the eikonal model. Use tetrahedra or triangles");
614  EXIT(EXIT_FAILURE);
615  break;
616  }
617 
618  if (strlen(param_globals::start_statef) > 0) load_state_file();
619 
620  create_node_to_node_graph();
621 
622  translate_stim_to_eikonal();
623 
624  precompute_squared_anisotropy_metric();
625 }
626 
628 {
629  double t1, t2;
630  get_time(t1);
631 
632  const sf_mesh& mesh = get_mesh(intra_elec_msh);
633 
634  diff_cur.resize(mesh.l_numpts);
635  rho_cvrest.resize(mesh.l_numpts);
639 
640  // --- Precompute resolved region IDs for each point ---
641  // Use the same region delegation as the ionics class
642  SF::vector<int> regionIDs;
643  SF::vector<RegionSpecs> rs(param_globals::num_imp_regions);
644  for (size_t i = 0; i < rs.size(); i++) {
645  rs[i].nsubregs = param_globals::imp_region[i].num_IDs;
646  rs[i].subregtags = param_globals::imp_region[i].ID;
647  for (int j = 0; j < rs[i].nsubregs; j++) {
648  if (rs[i].subregtags[j] == -1 && get_rank() == 0)
649  log_msg(NULL, 3, ECHO, "Warning: not all %u IDs provided for imp_region[%u]!\n", rs[i].nsubregs, i);
650  }
651  }
652  if (rs.size() == 1) {
653  regionIDs.assign(mesh.l_numpts, 0);
654  } else {
655  region_mask(intra_elec_msh, rs, regionIDs, false, "imp_regions");
656  }
657 
658  // --- Parallel initialization, one write per vertex ---
659  #pragma omp parallel for schedule(dynamic)
660  for (int v = 0; v < mesh.l_numpts; v++) {
661  int reg = regionIDs[v];
662 
663  const auto& region_diff = param_globals::imp_region[reg].dream.Idiff;
664  const auto& region_rest = param_globals::imp_region[reg].dream.CVrest;
665 
666  auto model = static_cast<eikonal_solver::Idiff_t>(region_diff.model);
667  diff_cur[v].model = model;
668 
669  if (model == GAUSS) {
670  diff_cur[v].alpha_1 = region_diff.alpha_i[0];
671  diff_cur[v].alpha_2 = region_diff.alpha_i[1];
672  diff_cur[v].alpha_3 = region_diff.alpha_i[2];
673  diff_cur[v].beta_1 = region_diff.beta_i[0];
674  diff_cur[v].beta_2 = region_diff.beta_i[1];
675  diff_cur[v].beta_3 = region_diff.beta_i[2];
676  diff_cur[v].gamma_1 = region_diff.gamma_i[0];
677  diff_cur[v].gamma_2 = region_diff.gamma_i[1];
678  diff_cur[v].gamma_3 = region_diff.gamma_i[2];
679  } else {
680  diff_cur[v].A_F = region_diff.A_F;
681  diff_cur[v].tau_F = region_diff.tau_F;
682  diff_cur[v].V_th = region_diff.V_th;
683  }
684 
685  rho_cvrest[v] = region_rest.rho;
686  kappa_cvrest[v] = region_rest.kappa;
687  theta_cvrest[v] = region_rest.theta;
688  denom_cvrest[v] = log(region_rest.rho) / region_rest.psi;
689  }
690  if (param_globals::output_level) log_msg(NULL, 0, 0, "Diffusion current and CV restitution initialized in %f sec.", timing(t2, t1));
691 }
692 
693 void eikonal_solver::translate_stim_to_eikonal()
694 {
695  double t1, t2;
696  get_time(t1);
697  Index_currStim = 0;
698 
699  // Collect all pulses as (start_time, stimulus_index)
700  std::vector<std::pair<SF_real, int>> stim_events;
701  stim_events.reserve(param_globals::num_stim * 8); // rough guess
702 
703  for (int stim_idx = 0; stim_idx < stimuliRef->size(); ++stim_idx) {
704  const stimulus& s = (*stimuliRef)[stim_idx];
705  for (int idx_pls = 0; idx_pls < s.ptcl.npls; ++idx_pls) {
706  SF_real start_time = s.ptcl.start + s.ptcl.pcl * idx_pls;
707  stim_events.emplace_back(start_time, stim_idx);
708  }
709  }
710 
711  // Sort events by start_time
712  std::sort(stim_events.begin(), stim_events.end(),
713  [](auto& a, auto& b) { return a.first < b.first; });
714 
715  // Reserve enough space for output
716  size_t total_nodes = 0;
717  for (auto& ev : stim_events)
718  total_nodes += (*stimuliRef)[ev.second].electrode.vertices.size();
719  StimulusPoints.resize(total_nodes + 1, -1);
720  StimulusTimes.resize(total_nodes + 1, -1);
721 
722  // Fill outputs
723  size_t count = 0;
724  for (auto& ev : stim_events) {
725  const stimulus& s = (*stimuliRef)[ev.second];
726  for (mesh_int_t v : s.electrode.vertices) {
727  StimulusPoints[count] = v;
728  StimulusTimes[count] = ev.first;
729  ++count;
730  }
731  }
732 
733  if (param_globals::output_level)
734  log_msg(NULL, 0, 0, "Translating stimuli for eikonal model done in %f sec.", timing(t2, t1));
735 }
736 
737 void eikonal_solver::create_node_to_node_graph()
738 {
739  double t1, t2;
740  get_time(t1);
741 
742  n2n_dsp.resize(num_pts + 1, 0);
743  const sf_mesh& mesh = get_mesh(intra_elec_msh);
744 
745  // Thread-local storage for neighbors
746  std::vector<std::vector<mesh_int_t>> thread_neighbors(num_pts);
747 
748  // Preallocate thread-local marker arrays for O(n) duplicate removal
749  #pragma omp parallel
750  {
751  std::vector<char> mark(num_pts, 0); // one per thread
752 
753  #pragma omp for schedule(dynamic)
754  for (int point_idx = 0; point_idx < num_pts; point_idx++) {
755  int numNBElem = n2e_dsp[point_idx + 1] - n2e_dsp[point_idx];
756  std::vector<mesh_int_t> neighbors;
757  neighbors.reserve(numNBElem * MESH_SIZE); // rough estimate
758 
759  // Loop over all elements containing this node
760  for (int eedsp = 0; eedsp < numNBElem; eedsp++) {
761  int currElem = n2e_con[n2e_dsp[point_idx] + eedsp];
762 
763  // Loop over all nodes of the current element
764  for (int nndsp = 0; nndsp < MESH_SIZE; nndsp++) {
765  int nb = e2n_con[elem_start[currElem] + nndsp];
766 
767  if (nb == point_idx) continue; // skip self
768  if (!mark[nb]) {
769  mark[nb] = 1;
770  neighbors.push_back(nb);
771  }
772  }
773  }
774 
775  // Reset marks for the next iteration
776  for (int nb : neighbors) mark[nb] = 0;
777 
778  // Move neighbor list into the final container
779  thread_neighbors[point_idx] = std::move(neighbors);
780  }
781  }
782 
783  // Build prefix sums (serial, cheap compared to above)
784  for (int i = 0; i < num_pts; i++) {
785  n2n_dsp[i + 1] = n2n_dsp[i] + thread_neighbors[i].size();
786  }
787 
788  // Allocate once, then fill in parallel
789  n2n_connect.resize(n2n_dsp[num_pts]);
790 
791  #pragma omp parallel for schedule(static)
792  for (int i = 0; i < num_pts; i++) {
793  std::copy(thread_neighbors[i].begin(),
794  thread_neighbors[i].end(),
795  n2n_connect.begin() + n2n_dsp[i]);
796  }
797 
798  if (param_globals::output_level) {
799  log_msg(NULL, 0, 0, "Node-to-node graph done in %f sec.", timing(t2, t1));
800  }
801 }
802 
803 void eikonal_solver::precompute_squared_anisotropy_metric()
804 {
805  double t1, t2;
806  get_time(t1);
807 
808  const sf_mesh& mesh = get_mesh(intra_elec_msh);
809 
810  S.resize(mesh.l_numelem); // store anisotropy matrices (dimensionless)
811  CV_L.resize(mesh.l_numelem); // store CV_L per element for later use
812 
813  // temporary variables
814  SF::dmat<double> I(3, 3);
815  I.assign(0.0);
816  I.diag(1.0);
817 
818  #pragma omp parallel
819  {
820  SF::dmat<double> Saniso(3, 3);
821  SF::dmat<double> Ff(3, 3);
822  SF::dmat<double> diff(3, 3);
823  SF::Point f, s, n;
824 
825  #pragma omp for schedule(static)
826  for (int eidx = 0; eidx < mesh.l_numelem; eidx++) {
827  double vl = 0.0, vt = 0.0, vn = 0.0;
828 
829  // Find region match (break early when found)
830  for (int g = 0; g < param_globals::num_gregions; g++) {
831  const auto& reg = param_globals::gregion[g];
832  const auto& dream = reg.dream;
833  for (int j = 0; j < reg.num_IDs; j++) {
834  if (mesh.tag[eidx] == reg.ID[j]) {
835  vl = dream.vel_l;
836  vt = dream.vel_t;
837  vn = dream.vel_n;
838  goto region_found; // break out of both loops
839  }
840  }
841  }
842  region_found:;
843 
844  // Store CV_L directly (for later rescaling)
845  CV_L[eidx] = vl;
846 
847  // Compute anisotropy ratios relative to CV_L
848  double AR_T2 = (vl / vt) * (vl / vt);
849  double AR_N2 = (vl / vn) * (vl / vn);
850 
851  f.x = mesh.fib[3 * eidx + 0];
852  f.y = mesh.fib[3 * eidx + 1];
853  f.z = mesh.fib[3 * eidx + 2];
854 
855  if (twoFib) {
856  s.x = mesh.she[3 * eidx + 0];
857  s.y = mesh.she[3 * eidx + 1];
858  s.z = mesh.she[3 * eidx + 2];
859  n = cross(f, s);
860 
861  SF::outer_prod(f, f, 1.0, Saniso[0], false);
862  SF::outer_prod(s, s, AR_T2, Saniso[0], true);
863  SF::outer_prod(n, n, AR_N2, Saniso[0], true);
864  } else {
865  SF::outer_prod(f, f, 1.0, Ff[0], false);
866  diff = I - Ff;
867  diff *= AR_T2;
868 
869  Saniso = Ff + diff;
870  }
871 
872  S[eidx] = Saniso;
873  }
874  }
875 
876  if (param_globals::output_level)
877  log_msg(NULL, 0, 0, "Anisotropy tensors precomputed in %f sec.", timing(t2, t1));
878 }
879 
881 {
882  double t0, t1;
883  get_time(t0);
884 
885  // --- 0) Reset all activation times to infinity
886  std::fill(T_A.begin(), T_A.end(), inf);
887 
888  // --- 1) Build initial active list = neighbors of stimuli
889  std::vector<mesh_int_t> activeList;
890  activeList.reserve(num_pts / 10); // heuristic reserve to avoid frequent reallocs
891  std::vector<char> in_active(num_pts, 0);
892 
893  // Mark stimuli
894  for (size_t si = 0; si < StimulusTimes.size() - 1; ++si) { // skip last entry, since its a -1 placeholder currently still used in DREAM
895  mesh_int_t s = StimulusPoints[si];
896  T_A[s] = StimulusTimes[si];
897  }
898  // Seed neighbors; separate loop to avoid adding stim nodes into the list if they are a neighbor
899  for (size_t si = 0; si < StimulusTimes.size() - 1; ++si) { // skip last entry, since its a -1 placeholder currently still used in DREAM
900  mesh_int_t s = StimulusPoints[si];
901  for (int off = n2n_dsp[s]; off < n2n_dsp[s + 1]; ++off) {
902  mesh_int_t nb = n2n_connect[off];
903  if (T_A[nb] == inf) {
904  add_to_active(activeList, in_active, nb);
905  }
906  }
907  }
908 
909  // --- 2) Iterative solve of activeList
910  int niter = 0;
911  while (!activeList.empty() && niter <= param_globals::dream.fim.max_iter) {
912  const std::vector<mesh_int_t> activeVec = std::move(activeList);
913  activeList.clear();
914 
915  #pragma omp parallel
916  {
917  std::vector<mesh_int_t> local_active;
918  local_active.reserve(64); // small local buffer
919 
920  #pragma omp for schedule(dynamic)
921  for (size_t i = 0; i < activeVec.size(); ++i) {
922  mesh_int_t id = activeVec[i];
923  remove_from_active(in_active, id);
924 
925  SF_real p = T_A[id];
926  SF_real q = update(id);
927 
928  #pragma omp atomic write
929  T_A[id] = q;
930 
931  if (std::fabs(p - q) < param_globals::dream.fim.tol) {
932  for (int off = n2n_dsp[id]; off < n2n_dsp[id + 1]; ++off) {
933  mesh_int_t nb = n2n_connect[off];
934  p = T_A[nb];
935  q = update(nb);
936  if (p > q) {
937  #pragma omp atomic write
938  T_A[nb] = q;
939  local_active.push_back(nb);
940  }
941  }
942  } else {
943  local_active.push_back(id); // non-converged -> add back
944  }
945  }
946 
947  // Merge local results
948  #pragma omp critical
949  {
950  for (mesh_int_t nb : local_active) {
951  add_to_active(activeList, in_active, nb);
952  }
953  }
954  }
955  ++niter;
956  }
957 
958  // collect iteration stats
959  stats.update_iter(niter);
960  auto [minIt, maxIt] = std::minmax_element(T_A.begin(), T_A.end());
961  actMIN = *minIt;
962  actMAX = *maxIt;
963 
964  // --- 3) Copy into AT for output
965  double* atc = AT->ptr();
966  const SF_real* t_a = T_A.data();
967  #pragma omp parallel for simd
968  for (mesh_int_t i = 0; i < num_pts; ++i)
969  atc[i] = (t_a[i] == inf ? -1.0 : t_a[i]);
970  AT->release_ptr(atc);
971 
972  // --- 4) Timing & list‐size stats
973  auto dur = timing(t1, t0);
974  stats.slvtime_A += dur;
975  stats.minAT = actMIN;
976  stats.maxAT = actMAX;
977  stats.activeList = activeList.size();
978  stats.bc_status = true;
980 }
981 
983 {
984  int niter = 0;
985  double t1, t0;
986  get_time(t0);
987 
988  if (sum(List) == 0) {
989  compute_bc();
990  }
991 
992  double time2stop_eikonal = param_globals::dream.tau_inc;
993  float maxadvance = user_globals::tm_manager->time + param_globals::dream.tau_s + param_globals::dream.tau_inc + param_globals::dream.tau_max;
994 
995  do {
996  if (StimulusTimes[Index_currStim] <= maxadvance) {
997  compute_bc();
998  }
999 
1000  for (mesh_int_t indX = 0; indX < List.size(); indX++) {
1001  SF_real p = T_A[indX];
1002  SF_real q;
1003 
1004  if (List[indX] == 0) continue;
1005 
1006  q = compute_coherence(indX);
1007 
1008  T_A[indX] = q;
1009  num_changes[indX] = num_changes[indX] + 1;
1010 
1011  if (q > maxadvance) continue;
1012 
1013  if (abs(p - q) < param_globals::dream.fim.tol || (num_changes[indX] > param_globals::dream.fim.max_iter) || (q == inf || p == inf)) {
1014  for (int ii = n2n_dsp[indX]; ii < n2n_dsp[indX + 1]; ii++) {
1015  mesh_int_t indXNB = n2n_connect[ii];
1016 
1017  if (List[indXNB] == 1) {
1018  continue;
1019  }
1020 
1021  SF_real pNB = T_A[indXNB];
1022  SF_real qNB;
1023 
1024  qNB = compute_coherence(indXNB);
1025 
1026  bool node_is_valid = add_node_neighbor_to_list(T_R[indXNB], pNB, qNB) && qNB != inf && qNB > user_globals::tm_manager->time;
1027  // This second condition is there to be able to add a node to the list when a new valid activation time
1028  // is found but a reentry would be blocked by the L2 parameter. Normally this condition does not make or brake the
1029  // simulation but would leave individual nodes inactivated, which is not ideal.
1030  bool ignore_L2_if_valid = node_is_valid && qNB > pNB && nReadded2List[indXNB] >= param_globals::dream.fim.max_addpt;
1031 
1032  if (node_is_valid && (nReadded2List[indXNB] < param_globals::dream.fim.max_addpt) || ignore_L2_if_valid) {
1033  if (qNB > pNB) {
1034  nReadded2List[indXNB] = 0;
1035  }
1036  T_A[indXNB] = qNB;
1037  nReadded2List[indXNB]++;
1038  num_changes[indXNB] = 0;
1039  List[indXNB] = 1;
1040  if (param_globals::dream.output.debugNode == indXNB) {
1042  nodeData.idXNB = indX;
1043  nodeData.nbn_T_A = q;
1044  }
1045  }
1046  }
1047 
1048  List[indX] = 0;
1049  if (param_globals::dream.output.debugNode == indX) {
1051  }
1052  }
1053  }
1054 
1055  SF_real actMIN_old = actMIN;
1056  SF_real progress_time;
1057 
1058  update_Ta_in_active_list();
1059 
1060  if (actMIN > actMIN_old) {
1061  progress_time = actMIN - actMIN_old;
1062  } else {
1063  progress_time = 0;
1064  }
1065 
1066  time2stop_eikonal -= progress_time;
1067  niter++;
1068 
1069  } while (time2stop_eikonal > 0 && sum(List) > 0);
1070 
1071  if (sum(List) == 0) {
1073  }
1074 
1075  // copy for igb output
1076  double* atc = AT->ptr();
1077  double* rpt = RT->ptr();
1078  for (mesh_int_t i = 0; i < List.size(); i++) {
1079  if (T_A[i] == inf) {
1080  // for better visualization in meshalyzer
1081  atc[i] = -1;
1082  } else {
1083  atc[i] = T_A[i];
1084  }
1085  rpt[i] = T_R[i];
1086  }
1087  AT->release_ptr(atc);
1088  RT->release_ptr(rpt);
1089 
1090  // treat solver statistics
1091  auto dur = timing(t1, t0);
1092  stats.slvtime_A += dur;
1093  stats.update_iter(niter);
1094  stats.minAT = actMIN;
1095  stats.maxAT = actMAX;
1096  stats.activeList = sum(List);
1098 
1099  // treat node stats
1100  if (param_globals::dream.output.debugNode >= 0) {
1104  }
1105 
1106 } // close iterate list
1107 
1108 SF_real eikonal_solver::update(mesh_int_t& indX, SF_real CVrest_factor, bool isDREAM)
1109 {
1110  switch (MESH_SIZE) {
1111  case 4: return update_impl<4>(indX, CVrest_factor, isDREAM);
1112  case 3: return update_impl<3>(indX, CVrest_factor, isDREAM);
1113  case 2: return update_impl<2>(indX, CVrest_factor, isDREAM);
1114  default:
1115  return T_A[indX]; // fallback
1116  }
1117 }
1118 
1119 template <int N>
1120 SF_real eikonal_solver::update_impl(mesh_int_t& indX, SF_real CVrest_factor, bool CheckValidity)
1121 {
1122  double min = inf;
1123  const double time = user_globals::tm_manager->time;
1124  const sf_mesh& mesh = get_mesh(intra_elec_msh);
1125 
1126  // element-wise update
1127  for (int e_i = n2e_dsp[indX]; e_i < n2e_dsp[indX + 1]; e_i++) { // gather all elements the node belongs to
1128  int Elem_i = n2e_con[e_i];
1129  mesh_int_t indEle = elem_start[Elem_i];
1130  std::array<SF::Point, N> base; // points
1131  std::array<double, N> values; // activation times
1132  std::array<int, N> nodeIDs;
1133 
1134  std::size_t k = 0;
1135  for (std::size_t j = 0; j < N; j++) { // loop over nodes of element e_i
1136  int n_i = e2n_con[indEle + j];
1137  if (n_i != indX) {
1138  base[k].x = mesh.xyz[3 * n_i + 0]; base[k].y = mesh.xyz[3 * n_i + 1]; base[k].z = mesh.xyz[3 * n_i + 2];
1139  values[k] = T_A[n_i];
1140  nodeIDs[k] = n_i;
1141  k++;
1142  } else { // last slot is reserved for the current vertex we are solving for
1143  base[N - 1].x = mesh.xyz[3 * indX + 0]; base[N - 1].y = mesh.xyz[3 * indX + 1]; base[N - 1].z = mesh.xyz[3 * indX + 2];
1144  values[N - 1] = T_A[indX];
1145  nodeIDs[N - 1] = indX;
1146  }
1147  }
1148  // scale slowness metric by CV
1149  double cv = CV_L[Elem_i] * CVrest_factor;
1150  if (cv == 0.0) continue; // avoid 1/(cv*cv)
1151  SF::dmat<double> D = 1.0 / (cv * cv) * S[Elem_i];
1152 
1153  // 1) solve full N-simplex (eikonal run OR if valid for DREAM)
1154  if (!CheckValidity || !is_not_valid_update<N>(nodeIDs, time)) {
1155  LocalSolver<N> solver(D, base, values);
1156  SF_real tmp = solver.solve();
1157  if (min > tmp && tmp > T_R[indX] && compute_H(indX, tmp) > 0.0) {
1158  min = tmp;
1159  }
1160  continue;
1161  }
1162 
1163  // If the full N-simplex was not valid, we have to do additional checks for the DREAM,
1164  // since a subsimplex could be valid if e.g. only one node of a tet/triangle is invalid
1165  // 2) triangles that include the target (only done if MESH_SIZE is 4)
1166  if constexpr (N - 1 == 3) {
1167  // neighbor indices are 0..(N-2); choose pairs (i,j) and add target (N-1)
1168  for (int i = 0; i < (N - 1); ++i) {
1169  for (int j = i + 1; j < (N - 1); ++j) {
1170  // build nodeIDs/points/values for face {i, j, target}
1171  std::array<int, 3> tri_ids{nodeIDs[i], nodeIDs[j], nodeIDs[N - 1]};
1172  if (is_not_valid_update<3>(tri_ids, time)) continue;
1173 
1174  std::array<SF::Point, 3> tri_pts{base[i], base[j], base[N - 1]};
1175  std::array<double, 3> tri_vals{values[i], values[j], values[N - 1]};
1176 
1177  LocalSolver<3> solver(D, tri_pts, tri_vals);
1178  // min = std::min(min, solver.solve());
1179  SF_real tmp = solver.solve();
1180  if (min > tmp && tmp > T_R[indX] && compute_H(indX, tmp) > 0.0) {
1181  min = tmp;
1182  }
1183  }
1184  }
1185  }
1186 
1187  // 3) edges that include the target
1188  for (int i = 0; i < N - 1; i++) {
1189  std::array<int, 2> edge_ids{nodeIDs[i], nodeIDs[N - 1]};
1190  if (is_not_valid_update<2>(edge_ids, time)) continue;
1191 
1192  std::array<SF::Point, 2> edge_pts{base[i], base[N - 1]};
1193  std::array<double, 2> edge_vals{values[i], values[N - 1]};
1194 
1195  LocalSolver<2> solver(D, edge_pts, edge_vals);
1196  SF_real tmp = solver.solve();
1197  if (min > tmp && tmp > T_R[indX] && compute_H(indX, tmp) > 0.0) {
1198  min = tmp;
1199  }
1200  }
1201  }
1202 
1203  return min;
1204 }
1205 
1206 template <int N>
1207 bool eikonal_solver::is_not_valid_update(const std::array<int, N>& nodeIDs, double time)
1208 {
1209  const int target = nodeIDs[N - 1];
1210  const bool failedStim = (stim_status[target] == 2);
1211 
1212  for (int i = 0; i < N - 1; i++) {
1213  const int nb = nodeIDs[i];
1214 
1215  if (T_A[nb] < time) return true;
1216  if (T_A[nb] < T_R[nb]) return true;
1217  if (failedStim && stim_status[nb] == 1) return true;
1218  }
1219 
1220  return false;
1221 }
1222 
1223 SF_real eikonal_solver::compute_H(mesh_int_t& indX, SF_real& tmpTA)
1224 {
1225  // Apply a refractory delay if stimulus previously failed
1226  const double delay = (stim_status[indX] == 2) ? 5.0 : 0.0;
1227 
1228  // Early exit conditions
1229  if (tmpTA == inf ||
1230  (T_R[indX] + delay) == -400.0 ||
1231  std::fabs(tmpTA - TA_old[indX]) < param_globals::dream.fim.tol ||
1232  T_R[indX] > tmpTA) {
1233  return 1.0;
1234  }
1235 
1236  // Compute diastolic interval
1237  const double DI = tmpTA - (T_R[indX] + delay);
1238  D_I[indX] = DI;
1239 
1240  // CV restitution factor
1241  const double exponent = -(DI + kappa_cvrest[indX]) * denom_cvrest[indX];
1242  const double Factor_DI = 1.0 - rho_cvrest[indX] * std::exp(exponent);
1243 
1244  if (Factor_DI <= 0.0) {
1245  return 0.0;
1246  }
1247 
1248  // Apply restitution curve behavior
1249  return (DI <= theta_cvrest[indX]) ? -Factor_DI : Factor_DI;
1250 }
1251 
1252 SF_real eikonal_solver::compute_coherence(mesh_int_t& indX)
1253 {
1254  SF_real scaling = 1.0;
1255  SF_real p = T_A[indX]; // starting guess (previous arrival time)
1256  SF_real q = update(indX, scaling, true); // candidate new arrival time
1257 
1258  for (int niter = 0; niter < param_globals::dream.fim.max_coh; ++niter) {
1259  scaling = compute_H(indX, p); // restitution-based scaling for CV (can be negative mid-iteration)
1260  q = update(indX, std::fabs(scaling), true); // new candidate arrival time; use fabs() to allow intermediate negative scaling
1261 
1262  if (std::fabs(p - q) < param_globals::dream.fim.tol)
1263  break; // converged
1264 
1265  p = q; // continue iterating
1266  }
1267 
1268  // Only accept q if final state is physiologically valid
1269  return (compute_H(indX, q) < 0.0) ? T_A[indX] : q;
1270 }
1271 
1272 void eikonal_solver::compute_bc()
1273 {
1274  bool EmpList = sum(List) == 0;
1275 
1276  if (EmpList) {
1277  for (size_t j = Index_currStim; j < StimulusTimes.size(); j++) {
1278  if ((StimulusPoints[j] == -1) || (StimulusTimes[j] > StimulusTimes[Index_currStim])) {
1279  Index_currStim = j;
1280  break;
1281  }
1282 
1283  mesh_int_t indNode = StimulusPoints[j];
1284  SF_real TimeSt = StimulusTimes[j];
1285 
1286  bool cond2add = add_node_neighbor_to_list(T_R[indNode], T_A[indNode], TimeSt);
1287  if (cond2add && compute_H(indNode, TimeSt) > 0) {
1288  T_A[indNode] = TimeSt;
1289  stim_status[indNode] = 1;
1290  stats.bc_status = true;
1291 
1292  if (param_globals::dream.output.debugNode == indNode) {
1293  nodeData.T_A = TimeSt;
1295  }
1296 
1297  for (int ii = n2n_dsp[indNode]; ii < n2n_dsp[indNode + 1]; ii++) {
1298  mesh_int_t indXNB = n2n_connect[ii];
1299  if (List[indXNB] == 0) {
1300  List[indXNB] = 1;
1301  if (param_globals::dream.output.debugNode == indXNB) {
1303  nodeData.idXNB = indNode;
1304  nodeData.nbn_T_A = TimeSt;
1305  }
1306  }
1307  }
1308  }
1309 
1310  if (cond2add && compute_H(indNode, TimeSt) <= 0) {
1311  stim_status[indNode] = 2;
1312  }
1313  }
1314 
1315  // After NB of initial points are assigned remove initial points from the list if they were added in the previous loop.
1316  for (size_t j = 0; j < StimulusPoints.size(); j++) {
1317  if ((StimulusPoints[j] == -1) && (StimulusTimes[j] > StimulusTimes[Index_currStim])) continue;
1318  if (List[StimulusPoints[j]] == 1) {
1319  List[StimulusPoints[j]] = 0;
1320  if (param_globals::dream.output.debugNode == StimulusPoints[j]) nodeData.update_status(node_stats::out, node_stats::stim);
1321  }
1322  }
1323 
1325  } else {
1326  for (size_t i = Index_currStim; i < StimulusTimes.size(); i++) {
1327  mesh_int_t indNode = StimulusPoints[i];
1328  SF_real TimeSt = StimulusTimes[i];
1329 
1330  if (indNode == -1) continue;
1331 
1332  if (TimeSt <= actMAX) {
1333  bool cond2add = add_node_neighbor_to_list(T_R[indNode], T_A[indNode], TimeSt);
1334  if (cond2add && compute_H(indNode, TimeSt) > 0) {
1335  T_A[indNode] = TimeSt;
1336  stim_status[indNode] = 1;
1337  stats.bc_status = true;
1338 
1339  if (param_globals::dream.output.debugNode == indNode) {
1340  nodeData.T_A = TimeSt;
1342  }
1343 
1344  for (int ii = n2n_dsp[indNode]; ii < n2n_dsp[indNode + 1]; ii++) {
1345  mesh_int_t indXNB = n2n_connect[ii];
1346  if (List[indXNB] == 0) {
1347  List[indXNB] = 1;
1348  if (param_globals::dream.output.debugNode == indXNB) {
1350  nodeData.idXNB = indNode;
1351  nodeData.nbn_T_A = TimeSt;
1352  }
1353  }
1354  }
1355 
1356  for (size_t j = 0; j < StimulusPoints.size(); j++) {
1357  if ((StimulusPoints[j] == -1) && (StimulusTimes[j] > StimulusTimes[Index_currStim])) continue;
1358  if (List[StimulusPoints[j]] == 1) {
1359  List[StimulusPoints[j]] = 0;
1360  if (param_globals::dream.output.debugNode == StimulusPoints[j]) nodeData.update_status(node_stats::out, node_stats::stim);
1361  }
1362  }
1363  }
1364  if (cond2add && compute_H(indNode, TimeSt) <= 0) {
1365  stim_status[indNode] = 2;
1366  }
1367 
1368  } else {
1369  Index_currStim = i;
1370 
1371  break;
1372  }
1373  }
1374  }
1375 
1376 } // end compute stimulus
1377 
1379 {
1380  bool act_is_in_safety_window, empty_list_and_illegal_stimulus, empty_stimulus, stimulus_is_in_safety_window;
1381 
1382  act_is_in_safety_window = (actMIN > param_globals::dream.tau_s) && (actMIN - time > param_globals::dream.tau_s);
1383  empty_stimulus = (StimulusPoints[Index_currStim] == -1);
1384  stimulus_is_in_safety_window = (StimulusTimes[Index_currStim] - time > param_globals::dream.tau_s);
1385  empty_list_and_illegal_stimulus = (sum(List) == 0) && (empty_stimulus || stimulus_is_in_safety_window);
1386 
1387  return act_is_in_safety_window || empty_list_and_illegal_stimulus;
1388 }
1389 
1390 void eikonal_solver::update_Ta_in_active_list()
1391 {
1392  bool First_in_List = 1;
1393 
1394  for (size_t j = 0; j < List.size(); j++) {
1395  if (T_A[j] < 0) continue;
1396  if (List[j] == 1 && First_in_List) {
1397  actMIN = T_A[j];
1398  actMAX = T_A[j];
1399  First_in_List = 0;
1400  continue;
1401  }
1402  if (List[j] == 1) {
1403  if (T_A[j] < actMIN) actMIN = T_A[j];
1404  if (T_A[j] > actMAX) actMAX = T_A[j];
1405  }
1406  }
1407 }
1408 
1410 {
1411  for (size_t j = 0; j < List.size(); j++) {
1412  if (T_A[j] < user_globals::tm_manager->time) {
1413  TA_old[j] = T_A[j];
1414  stim_status[j] = 0;
1415  nReadded2List[j] = 0;
1416 
1417  if (List[j] == 1) List[j] = 0;
1418  }
1419  }
1420 }
1421 
1422 bool eikonal_solver::add_node_neighbor_to_list(SF_real& RT, SF_real& oldTA, SF_real& newTA)
1423 {
1424  // Condition (A): RT < newTA < oldTA
1425  // Condition (B): oldTA < RT < newTA
1426  return ((RT < newTA) && (newTA < oldTA)) ||
1427  ((oldTA < RT) && (RT < newTA));
1428 }
1429 
1431 {
1432  SF_real* old_ptr = Vmv_old.ptr();
1433  SF_real* new_ptr = Vmv.ptr();
1434 
1435  const double thresh = param_globals::dream.repol_time_thresh;
1436 
1437  for (int ind_nodes = 0; ind_nodes < num_pts; ind_nodes++) {
1438  if (old_ptr[ind_nodes] >= thresh && new_ptr[ind_nodes] < thresh) {
1439  T_R[ind_nodes] = time;
1440  }
1441  }
1442 
1443  Vmv_old.release_ptr(old_ptr);
1444  Vmv.release_ptr(new_ptr);
1445 }
1446 
1448 {
1449  double t1, t0;
1450  get_time(t0);
1451 
1452  #ifdef _OPENMP
1453  int max_threads = omp_get_max_threads();
1454  omp_set_num_threads(1); // with the current setup of this function, it is better to run it in serial.
1455  #endif
1456 
1457  limpet::MULTI_IF* miif = ion.miif;
1458 
1459  const double time = user_globals::tm_manager->time,
1461 
1462  for (int i = 0; i < miif->N_IIF; i++) {
1463  if (!miif->N_Nodes[i]) continue;
1464  limpet::IonIfBase* pIF = ion.miif->IIF[i];
1465  limpet::IonIfBase* IIF_old = pIF->get_type().make_ion_if(pIF->get_target(),
1466  pIF->get_num_node(),
1467  ion.miif->plugtypes[i]);
1468  IIF_old->copy_SVs_from(*pIF, false);
1469 
1470  int current = 0;
1471  do {
1472  int ind_gb = (miif->NodeLists[i][current]);
1473  // Global index of current node;
1474  // save states at the moment
1475  double Vm_old = miif->ldata[i][limpet::Vm][current];
1476  double Iion_old = miif->ldata[i][limpet::Iion][current];
1477  double prev_R = T_R[ind_gb];
1478 
1479  bool cond_2upd = T_R[ind_gb] <= T_A[ind_gb] && T_A[ind_gb] != inf && Vm_old > param_globals::dream.repol_time_thresh;
1480 
1481  if (cond_2upd) {
1482  double elapsed_time = 0;
1483  double prev_Vm = miif->ldata[i][limpet::Vm][current];
1484 
1485  do {
1486  elapsed_time += dt;
1487  pIF->compute(current, current + 1, miif->ldata[i]);
1488  miif->ldata[i][limpet::Vm][current] -= miif->ldata[i][limpet::Iion][current] * param_globals::dt;
1489  if (miif->ldata[i][limpet::Vm][current] < param_globals::dream.repol_time_thresh) {
1490  T_R[ind_gb] = time + elapsed_time;
1491  break;
1492  }
1493  } while (elapsed_time < 600);
1494 
1495  // Restore old states
1496  miif->ldata[i][limpet::Vm][current] = Vm_old;
1497  miif->ldata[i][limpet::Iion][current] = Iion_old;
1498  }
1499  current++;
1500  } while (current < miif->N_Nodes[i]);
1501  pIF->copy_SVs_from(*IIF_old, false);
1502  }
1503 
1504  #ifdef _OPENMP
1505  omp_set_num_threads(max_threads); // restore max threads for parabolic solver
1506  #endif
1507 
1508  // treat solver statistics
1509  auto dur = timing(t1, t0);
1510  stats.slvtime_D += dur;
1511 }
1512 
1514 {
1515  double t0, t1;
1516  get_time(t0);
1517 
1518  SF_real* c = Idiff->ptr();
1519  SF_real* v = vm.ptr();
1520 
1521  const sf_mesh& mesh = get_mesh(intra_elec_msh);
1522  const SF::vector<mesh_int_t>& alg_nod = mesh.pl.algebraic_nodes();
1523  int rank = get_rank();
1524 
1525  for (size_t j = 0; j < alg_nod.size(); j++) {
1526  mesh_int_t loc_nodal_idx = alg_nod[j];
1527  mesh_int_t loc_petsc_idx = local_nodal_to_local_petsc(mesh, rank, loc_nodal_idx);
1528 
1529  double TA = T_A[loc_nodal_idx];
1530  if (!(time >= TA && (TA + 5) >= time && List[loc_nodal_idx] == 0))
1531  continue;
1532 
1533  double dT = time - TA;
1534  auto& node = diff_cur[loc_nodal_idx];
1535 
1536  switch (node.model) {
1537  case GAUSS: {
1538  double term1 = (dT - node.beta_1) / node.gamma_1;
1539  double term2 = (dT - node.beta_2) / node.gamma_2;
1540  double term3 = (dT - node.beta_3) / node.gamma_3;
1541  c[loc_petsc_idx] = node.alpha_1 * exp(-term1 * term1) + node.alpha_2 * exp(-term2 * term2) + node.alpha_3 * exp(-term3 * term3);
1542  break;
1543  }
1544  default: {
1545  double e_on = (dT >= 0.0) ? 1.0 : 0.0;
1546  double e_off = (v[loc_petsc_idx] < node.V_th) ? 1.0 : 0.0;
1547  c[loc_petsc_idx] = node.A_F / node.tau_F * exp(dT / node.tau_F) * e_on * e_off;
1548  break;
1549  }
1550  }
1551  }
1552 
1553  Idiff->release_ptr(c);
1554  vm.release_ptr(v);
1555 
1556  stats.slvtime_B += timing(t1, t0);
1557 }
1558 
1559 void eikonal_solver::save_eikonal_state(const char* tsav_ext)
1560 {
1561  if (get_rank() == 0) {
1562  FILE* file_writestate;
1563  char buffer_writestate[1024];
1564  snprintf(buffer_writestate, sizeof buffer_writestate, "%s.%s.roe.dat", param_globals::write_statef, tsav_ext);
1565  file_writestate = fopen(buffer_writestate, "w");
1566  for (size_t jjj = 0; jjj < List.size(); jjj++) {
1567  fprintf(file_writestate, "%lld %lld %lld %f %f %f %f \n",
1568  static_cast<long long>(List[jjj]),
1569  static_cast<long long>(num_changes[jjj]),
1570  static_cast<long long>(nReadded2List[jjj]),
1571  T_A[jjj], T_R[jjj], TA_old[jjj], D_I[jjj]);
1572  }
1573 
1574  fclose(file_writestate);
1575  }
1576 }
1577 
1578 void eikonal_solver::load_state_file()
1579 {
1580  set_dir(INPUT);
1581  FILE* file_startstate;
1582  char buffer_startstate[strlen(param_globals::start_statef) + 10];
1583 
1584  snprintf(buffer_startstate, sizeof buffer_startstate, "%s.dat", param_globals::start_statef);
1585  file_startstate = fopen(buffer_startstate, "r");
1586 
1587  if (file_startstate == NULL) {
1588  log_msg(NULL, 5, 0, "Not able to open state file: %s", buffer_startstate);
1589  } else if (param_globals::output_level) {
1590  log_msg(NULL, 0, 0, "Open state file for eikonal model: %s", buffer_startstate);
1591  }
1592 
1593  int ListVal, numChangesVal, numChanges2Val;
1594  float T_AVal, T_RVal, TA_oldVal, D_IVal, PCLVal;
1595  int index = 0;
1596 
1597  while (fscanf(file_startstate, "%d %d %d %f %f %f %f", &ListVal, &numChangesVal, &numChanges2Val, &T_AVal, &T_RVal, &TA_oldVal, &D_IVal) == 7) {
1598  List[index] = ListVal;
1599  num_changes[index] = numChangesVal;
1600  nReadded2List[index] = numChanges2Val;
1601  T_A[index] = T_AVal;
1602  T_R[index] = T_RVal;
1603  TA_old[index] = TA_oldVal;
1604  D_I[index] = D_IVal;
1605  ++index;
1606  }
1607  if (param_globals::output_level) log_msg(NULL, 0, 0, "Number of nodes in active list: %i", sum(List));
1608 
1609  fclose(file_startstate);
1610 }
1611 
1613 {
1614  logger = f_open(filename, "w");
1615 
1616  const char* h1 = " ------ ---------- ---------- ------- ------- | List logic ----- -------- | Neighbor node ---- |";
1617  const char* h2 = " cycle AT old AT RT DI | Status Entry Exit | ID AT |";
1618 
1619  if (logger == NULL)
1620  log_msg(NULL, 3, 0, "%s error: Could not open file %s in %s. Turning off logging.\n",
1621  __func__, filename);
1622  else {
1623  log_msg(logger, 0, 0, "%s", h1);
1624  log_msg(logger, 0, 0, "%s", h2);
1625  }
1626 }
1627 
1628 void node_stats::log_stats(double time, bool cflg)
1629 {
1630  if (!this->logger) return;
1631 
1632  char abuf[256];
1633  char bbuf[256];
1634  char cbuf[256];
1635 
1636  // create nicer output in logger for inf, -inf, nan
1637  std::ostringstream oss_TA, oss_TA_, oss_TR, oss_DI, oss_nbnTA;
1638  oss_TA << this->T_A;
1639  oss_TA_ << this->T_A_;
1640  oss_TR << this->T_R;
1641  oss_DI << this->D_I;
1642  oss_nbnTA << this->nbn_T_A;
1643 
1644  if (this->idXNB == std::numeric_limits<Int>::min()) {
1645  snprintf(cbuf, sizeof cbuf, "%7s %10s", "-", "-");
1646  } else {
1647  snprintf(cbuf, sizeof cbuf, "%7lld %10s", static_cast<long long>(this->idXNB), oss_nbnTA.str().c_str());
1648  }
1649 
1650  snprintf(abuf, sizeof abuf, "%6lld %10s %10s %7s %7s",
1651  static_cast<long long>(this->cycle),
1652  oss_TA.str().c_str(), oss_TA_.str().c_str(), oss_TR.str().c_str(), oss_DI.str().c_str());
1653  snprintf(bbuf, sizeof bbuf, "%7s %8s %8s", this->status, this->reasonIn, this->reasonOut);
1654 
1655  unsigned char flag = cflg ? ECHO : 0;
1656  log_msg(this->logger, 0, flag | FLUSH | NONL, "%9.3f %s | %s | %s |\n", time, abuf, bbuf, cbuf);
1657 
1658  this->reasonIn = "-";
1659  this->reasonOut = "-";
1660  this->T_A_ = this->T_A;
1661  this->T_A = std::numeric_limits<double>::quiet_NaN();
1662  this->T_R = std::numeric_limits<double>::quiet_NaN();
1663  this->D_I = std::numeric_limits<double>::quiet_NaN();
1665  this->nbn_T_A = std::numeric_limits<double>::quiet_NaN();
1666  this->cycle++;
1667 }
1668 
1670 {
1671  // directly convert enums to strings for logging
1672  const char* stat_str;
1673  const char* reas_str;
1674  switch (s) {
1675  case node_stats::in: stat_str = "in"; break;
1676  case node_stats::out: stat_str = "out"; break;
1677  }
1678 
1679  switch (r) {
1680  case node_stats::none: reas_str = "-"; break;
1681  case node_stats::nbn: reas_str = "nbn"; break;
1682  case node_stats::conv: reas_str = "conv"; break;
1683  case node_stats::stim: reas_str = "stim"; break;
1684  }
1685 
1686  this->status = stat_str;
1687  if (s == in) {
1688  this->reasonIn = reas_str;
1689  } else {
1690  this->reasonOut = reas_str;
1691  }
1692 }
1693 
1694 template<int MESH_SIZE>
1696  const SF::Point& x1 = points[0];
1697  const SF::Point& x2 = points[1];
1698 
1699  const double& u1 = values[0];
1700 
1701  if constexpr (MESH_SIZE == 2) {
1702  return tsitsiklis_update_line({x1,x2}, D, u1);
1703 
1704  } else if constexpr (MESH_SIZE == 3) {
1705  const SF::Point& x3 = points[2];
1706  const double& u2 = values[1];
1707  return tsitsiklis_update_triangle({x1,x2,x3}, D, {u1,u2});
1708 
1709  } else if constexpr (MESH_SIZE == 4) {
1710  const SF::Point& x3 = points[2];
1711  const SF::Point& x4 = points[3];
1712  const double& u2 = values[1];
1713  const double& u3 = values[2];
1714 
1715  double u_tet = tsitsiklis_update_tetra({x1,x2,x3,x4}, D, {u1,u2,u3});
1716  if (isnan(u_tet)) {
1717  u_tet = std::numeric_limits<double>::infinity();
1718  }
1719  // face calculations (contains edge update as fallback)
1720  double u_face1 = tsitsiklis_update_triangle({x1, x2, x4}, D, {u1, u2});
1721  double u_face2 = tsitsiklis_update_triangle({x1, x3, x4}, D, {u1, u3});
1722  double u_face3 = tsitsiklis_update_triangle({x2, x3, x4}, D, {u2, u3});
1723 
1724  double u_tri = std::min({u_face1, u_face2, u_face3});
1725  return std::min(u_tet, u_tri);
1726  }
1727 }
1728 
1729 template <int MESH_SIZE>
1730 double LocalSolver<MESH_SIZE>::tsitsiklis_update_line(const std::array<SF::Point, 2>& base,
1731  const SF::dmat<double>& D,
1732  const double& value)
1733 {
1734  // Compute the difference vector: a1 = x2 - x1
1735  SF::Point a1 = base[1] - base[0];
1736  // Return updated value at x2: u1 + norm
1737  return value + std::sqrt(SF::inner_prod(a1, D * a1));
1738 }
1739 
1740 template <int MESH_SIZE>
1741 double LocalSolver<MESH_SIZE>::tsitsiklis_update_triangle(const std::array<SF::Point, 3>& base,
1742  const SF::dmat<double>& D,
1743  const std::array<double, 2>& values)
1744 {
1745  const SF::Point& x1 = base[0];
1746  const SF::Point& x2 = base[1];
1747  const SF::Point& x3 = base[2];
1748  const double& u1 = values[0];
1749  const double& u2 = values[1];
1750  double result = std::numeric_limits<double>::infinity();
1751 
1752  SF::Point z1 = x1 - x2;
1753  SF::Point z2 = x2 - x3;
1754  double k = u1 - u2;
1755 
1756  // Squared Mahalanobis norms
1757  const SF::Point Dz1 = D * z1;
1758  const SF::Point Dz2 = D * z2;
1759  double p11 = SF::inner_prod(z1, Dz1);
1760  double p12 = SF::inner_prod(z1, Dz2);
1761  double p22 = SF::inner_prod(z2, Dz2);
1762 
1763  double denominator = p11 - k * k;
1764  double sqrt_val = (p11 * p22 - p12 * p12) / denominator;
1765 
1766  if (denominator > 1e-12) { // avoid dividing by zero or near zero -> k very small, likely due to collapsed triangle/coinciding points
1767  const double sqrt_val = (p11 * p22 - p12 * p12) / denominator;
1768  if (sqrt_val >= 0.0) { // only real solutions are considered
1769  const double rhs = k * std::sqrt(sqrt_val);
1770  double alpha1 = -(p12 + rhs) / p11;
1771  double alpha2 = -(p12 - rhs) / p11;
1772 
1773  alpha1 = std::clamp(alpha1, 0.0, 1.0);
1774  alpha2 = std::clamp(alpha2, 0.0, 1.0);
1775 
1776  for (double alpha : {alpha1, alpha2}) {
1777  SF::Point x_interp = x1 * alpha + x2 * (1.0 - alpha);
1778  SF::Point dist = x3 - x_interp;
1779  double norm_D = std::sqrt(SF::inner_prod(dist, D * dist));
1780  double u3 = alpha * u1 + (1.0 - alpha) * u2 + norm_D;
1781  result = std::min(result, u3);
1782  }
1783  }
1784  }
1785 
1786  // Fallback: point-based update if square root was invalid or denominator is too small
1787  double u_edge1 = tsitsiklis_update_line({x1,x3}, D, u1);
1788  double u_edge2 = tsitsiklis_update_line({x2,x3}, D, u2);
1789 
1790  return std::min({result, u_edge1, u_edge2});
1791 }
1792 
1793 template <int MESH_SIZE>
1794 double LocalSolver<MESH_SIZE>::tsitsiklis_update_tetra(const std::array<SF::Point, 4>& base,
1795  const SF::dmat<double>& D,
1796  const std::array<double, 3>& values)
1797 {
1798  const SF::Point& x1 = base[0];
1799  const SF::Point& x2 = base[1];
1800  const SF::Point& x3 = base[2];
1801  const SF::Point& x4 = base[3];
1802 
1803  const double& u1 = values[0];
1804  const double& u2 = values[1];
1805  const double& u3 = values[2];
1806 
1807  // edge vectors
1808  const SF::Point y1 = x3 - x1;
1809  const SF::Point y2 = x3 - x2;
1810  const SF::Point y3 = x4 - x3;
1811 
1812  const double k1 = u1 - u3;
1813  const double k2 = u2 - u3;
1814 
1815  // Matrix products (squared norms and dot products under metric D)
1816  const SF::Point Dy1 = D * y1;
1817  const SF::Point Dy2 = D * y2;
1818  const SF::Point Dy3 = D * y3;
1819  const double r11 = SF::inner_prod(y1, Dy1);
1820  const double r12 = SF::inner_prod(y1, Dy2);
1821  const double r13 = SF::inner_prod(y1, Dy3);
1822  const double r21 = r12;
1823  const double r22 = SF::inner_prod(y2, Dy2);
1824  const double r23 = SF::inner_prod(y2, Dy3);
1825  const double r31 = r13;
1826  const double r32 = r23;
1827 
1828  const double A1 = k2 * r11 - k1 * r12;
1829  const double A2 = k2 * r21 - k1 * r22;
1830  const double B = k2 * r31 - k1 * r32;
1831  const double k = k1 - (A1 / A2) * k2;
1832  const SF::Point z1 = y1 - (A1 / A2) * y2;
1833  const SF::Point z2 = y3 - (B / A2) * y2;
1834 
1835  // compute quadratic equation
1836  const SF::Point Dz1 = D * z1;
1837  const SF::Point Dz2 = D * z2;
1838  const double p11 = SF::inner_prod(z1, Dz1);
1839  const double p12 = SF::inner_prod(z1, Dz2);
1840  const double p22 = SF::inner_prod(z2, Dz2);
1841  const double denominator = p11 - k*k;
1842  const double sqrt_val = (p11 * p22 - (p12 * p12)) / denominator;
1843  const double rhs = k * std::sqrt(sqrt_val);
1844 
1845  double alpha1 = -(p12 + rhs) / p11;
1846  double alpha2 = -(B + alpha1 * A1) / A2;
1847 
1848  // handle degenerate cases
1849  const double EPS = 1e-16;
1850  if ((std::abs(A1) < EPS) && (std::abs(A2) < EPS)) {
1851  alpha1 = (r12 * r23 - r13 * r22) / (r11 * r22 - (r12 * r12));
1852  alpha2 = (r12 * r13 - r11 * r23) / (r11 * r22 - (r12 * r12));
1853  } else if ((std::abs(A1) < EPS) && (std::abs(A2) > EPS)) {
1854  alpha1 = 0;
1855  alpha2 = -B / A2;
1856  } else if ((std::abs(A1) > EPS) && (std::abs(A2) < EPS)) {
1857  alpha1 = -B / A1;
1858  alpha2 = 0;
1859  }
1860 
1861  double alpha3 = 1 - alpha1 - alpha2;
1862  const SF::Point dist = x4 - (alpha1 * x1 + alpha2 * x2 + alpha3 * x3);
1863  // barycentric coordinate should be inside the tetrahedron
1864  if (alpha1 < -EPS || alpha2 < -EPS || alpha3 < -EPS ||
1865  alpha1 > 1.0+EPS || alpha2 > 1.0+EPS || alpha3 > 1.0+EPS) {
1866  return std::numeric_limits<double>::infinity();
1867  } else {
1868  return alpha1 * u1 + alpha2 * u2 + alpha3 * u3 + std::sqrt(SF::inner_prod(dist, D * dist));
1869  }
1870 }
1871 
1872 } // namespace opencarp
void output(vector< int > nodes, IGBheader *h, char *ofname, enum_format of, int t0, int t1, int stride, bool explode, float scale)
Definition: IGBextract.cc:210
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
Basic utility structs and functions, mostly IO related.
#define FLUSH
Definition: basics.h:319
#define ECHO
Definition: basics.h:316
#define NONL
Definition: basics.h:320
virtual void write(const char *filename) const =0
virtual S * ptr()=0
virtual void release_ptr(S *&p)=0
virtual void add_scaled(const abstract_vector< T, S > &vec, S k)=0
overlapping_layout< T > pl
nodal parallel layout
Definition: SF_container.h:429
vector< T > dsp
connectivity starting index of each element
Definition: SF_container.h:416
vector< S > she
sheet direction
Definition: SF_container.h:421
vector< elem_t > type
element type
Definition: SF_container.h:418
vector< T > con
Definition: SF_container.h:412
size_t l_numpts
local number of points
Definition: SF_container.h:401
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
void reserve(size_t n)
Definition: SF_vector.h:241
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
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
virtual void copy_SVs_from(IonIfBase &other, bool alloc)=0
Copies the state variables of an IMP.
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
Target get_target() const
Definition: ION_IF.h:339
node_count_t get_num_node() const
Gets the number of nodes handled by this IMP.
Definition: ION_IF.cc:148
virtual IonIfBase * make_ion_if(Target target, node_count_t num_node, const std::vector< std::reference_wrapper< IonType >> &plugins) const =0
Generate an IonIf object from this type.
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
std::vector< IonTypeList > plugtypes
plugins types for each region
Definition: MULTI_ION_IF.h:215
void dump_state(char *, float, opencarp::mesh_t gid, bool, unsigned int)
GlobalData_t *** ldata
data local to each IMP
Definition: MULTI_ION_IF.h:205
int N_IIF
how many different IIF's
Definition: MULTI_ION_IF.h:211
node_count_t * N_Nodes
#nodes for each IMP
Definition: MULTI_ION_IF.h:200
node_index_t ** NodeLists
local partitioned node lists for each IMP stored
Definition: MULTI_ION_IF.h:201
int timer_idx
the timer index received from the timer manager
Definition: physics_types.h:66
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
std::string timer_unit(const int timer_id)
figure out units of a signal linked to a given timer
SF::vector< stimulus > stimuli
the electrical stimuli
parabolic_solver parab_solver
Solver for the parabolic bidomain equation.
MaterialType mtype[2]
the material types of intra_grid and extra_grid grids.
void destroy()
Currently we only need to close the file logger.
double timer_val(const int timer_id)
figure out current value of a signal linked to a given timer
sf_vec * phie_dummy
no elliptic solver needed, but we need a dummy for phie to use parabolic solver
eikonal_solver eik_solver
Solver for the eikonal equation.
LAT_detector lat
the activation time detector
gvec_data gvec
datastruct holding global IMP state variable output
generic_timing_stats IO_stats
grid_t
An electrics grid identifier to distinguish between intra and extra grids.
igb_output_manager output_manager_cycle
void initialize()
Initialize the Eikonal class.
igb_output_manager output_manager_time
class handling the igb output
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
int check_quiescence(double tm, double dt)
check for quiescence
Definition: electrics.cc:1715
void output_initial_activations()
output one nodal vector of initial activation time
Definition: electrics.cc:1824
void init(sf_vec &vm, sf_vec &phie, int offset, enum physic_t=elec_phys)
initializes all datastructs after electric solver setup
Definition: electrics.cc:1535
int check_acts(double tm)
check activations at sim time tm
Definition: electrics.cc:1660
void FIM()
Standard fast iterative method to solve eikonal equation with active list approach.
void update_repolarization_times_from_rd(sf_vec &Vmv, sf_vec &Vmv_old, double time)
Updates node repolarization times based on transmembrane voltage crossing.
SF::vector< SF_real > T_R
void init_imp_region_properties()
Initializes diffusion current models and CV restitution parameters per mesh node.
SF::vector< mesh_int_t > n2e_dsp
SF::vector< SF_real > T_A
SF::vector< SF_real > TA_old
SF::vector< diffusion_current > diff_cur
void init()
Initialize vectors and variables in the eikonal_solver class.
SF::vector< mesh_int_t > e2n_con
bool determine_model_to_run(double &time)
Determine the next model to run in the alternation between RD and Eikonal.
SF::vector< mesh_int_t > stim_status
SF::vector< mesh_int_t > elem_start
SF::vector< mesh_int_t > StimulusPoints
SF::vector< SF_real > rho_cvrest
std::vector< double > CV_L
void save_eikonal_state(const char *tsav_ext)
Save the current state of variables related to the Eikonal simulation to a file to initialize a futur...
SF::vector< SF_real > denom_cvrest
void compute_diffusion_current(const double &time, sf_vec &vm)
Computes the stimulus-driven diffusion current at mesh nodes.
std::vector< mesh_int_t > n2n_connect
void set_stimuli(SF::vector< stimulus > &stimuli)
Simple setter for stimulus vector.
SF::vector< mesh_int_t > e2n_cnt
SF::vector< mesh_int_t > n2e_con
void clean_list()
Clean the list of nodes by resetting their status and tracking changes based on the time step of the ...
SF::vector< SF_real > D_I
SF::vector< mesh_int_t > nReadded2List
SF::vector< SF_real > theta_cvrest
SF::vector< mesh_int_t > num_changes
std::vector< mesh_int_t > n2n_dsp
SF::vector< SF_real > StimulusTimes
SF::vector< SF_real > kappa_cvrest
SF::vector< mesh_int_t > n2e_cnt
void cycFIM()
Implementation of the cyclical fast iterative method used in step A of the DREAM model.
void update_repolarization_times(const Ionics &ion)
Estimates initial repolarization times (T_R) in Step D of DREAM.
eikonal_solver_stats stats
std::vector< SF::dmat< double > > S
void write_data()
write registered data to disk
Definition: sim_utils.cc:1584
void close_files_and_cleanup()
close file descriptors
Definition: sim_utils.cc:1631
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:1551
sf_mat * rhs_parab
rhs matrix to solve parabolic
Definition: electrics.h:119
lin_solver_stats stats
Definition: electrics.h:129
void rebuild_matrices(MaterialType *mtype, limpet::MULTI_IF &miif, FILE_SPEC logger)
Definition: electrics.cc:1186
void solve(sf_vec &phie_i)
Definition: electrics.cc:1300
sf_mat * mass_i
lumped for parabolic problem
Definition: electrics.h:118
sf_mat * lhs_parab
lhs matrix (CN) to solve parabolic
Definition: electrics.h:120
sf_vec * Vmv
global Vm vector
Definition: electrics.h:104
sf_vec * old_vm
older Vm needed for 2nd order dT
Definition: electrics.h:110
int write_trace()
write traces to file
Definition: signals.h:701
stim_t type
type of stimulus
Definition: stimulate.h:138
int npls
number of stimulus pulses
Definition: stimulate.h:121
double pcl
pacing cycle length
Definition: stimulate.h:122
double start
start time of protocol
Definition: stimulate.h:120
sig::time_trace wave
wave form of stimulus pulse
Definition: stimulate.h:98
stim_protocol ptcl
applied stimulation protocol used
Definition: stimulate.h:169
stim_electrode electrode
electrode geometry
Definition: stimulate.h:171
stim_pulse pulse
stimulus wave form
Definition: stimulate.h:168
void translate(int id)
convert legacy definitions to new format
Definition: stimulate.cc:107
bool is_active() const
Return whether stim is active.
Definition: stimulate.cc:200
void setup(int idx)
Setup from a param stimulus index.
Definition: stimulate.cc:168
void dump_vtx_file(int idx)
Export the vertices to vtx file.
Definition: stimulate.cc:472
stim_physics phys
physics of stimulus
Definition: stimulate.h:170
std::string name
label stimulus
Definition: stimulate.h:166
double time_step
global reference time step
Definition: timer_utils.h:78
int add_eq_timer(double istart, double iend, int ntrig, double iintv, double idur, const char *iname, const char *poolname=nullptr)
Add a equidistant step timer to the array of timers.
Definition: timer_utils.cc:78
double time
current time
Definition: timer_utils.h:76
Diffusion Reaction Eikonal Alternant Model (DREAM) based on the electrics physics class.
void transpose_connectivity(const vector< T > &a_cnt, const vector< T > &a_con, vector< T > &b_cnt, vector< T > &b_con)
Transpose CRS matrix graph A into B.
double inner_prod(const Point &a, const Point &b)
Definition: SF_container.h:90
T sum(const vector< T > &vec)
Compute sum of a vector's entries.
Definition: SF_vector.h:340
void count(const vector< T > &data, vector< S > &cnt)
Count number of occurrences of indices.
Definition: SF_vector.h:332
void outer_prod(const Point &a, const Point &b, const double s, double *buff, const bool add=false)
Definition: SF_container.h:95
T local_nodal_to_local_petsc(const meshdata< T, S > &mesh, int rank, T local_nodal)
void init_vector(SF::abstract_vector< T, S > **vec)
Definition: SF_init.h:107
V clamp(const V val, const W start, const W end)
Clamp a value into an interval [start, end].
Definition: kdpart.hpp:132
void cnt_from_dsp(const std::vector< T > &dsp, std::vector< T > &cnt)
Compute counts from displacements.
Definition: kdpart.hpp:149
void dsp_from_cnt(const std::vector< T > &cnt, std::vector< T > &dsp)
Compute displacements from counts.
Definition: kdpart.hpp:140
constexpr T min(T a, T b)
Definition: ion_type.h:33
void dump_trace(MULTI_IF *MIIF, limpet::Real time)
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.
timer_manager * tm_manager
a manager for the various physics timers
Definition: main.cc:58
bool using_legacy_stimuli
flag storing whether legacy stimuli are used
Definition: main.cc:64
int stimidx_from_timeridx(const SF::vector< stimulus > &stimuli, const int timer_id)
determine link between timer and stimulus
Definition: electrics.cc:782
@ iotm_chkpt_list
Definition: timer_utils.h:44
@ iotm_console
Definition: timer_utils.h:44
@ iotm_trace
Definition: timer_utils.h:44
@ iotm_chkpt_intv
Definition: timer_utils.h:44
SF::scattering * get_scattering(const int from, const int to, const SF::SF_nbr nbr, const int dpn)
Get a scattering from the global scatter registry.
void read_el_scale_vec(const char *file, mesh_t mt, SF::vector< double > &el_scale, int &el_scale_dpn)
sf_mesh & get_mesh(const mesh_t gt)
Get a mesh by specifying the gridID.
Definition: sf_interface.cc:33
SF::scattering * register_scattering(const int from, const int to, const SF::SF_nbr nbr, const int dpn)
Register a scattering between to grids, or between algebraic and nodal representation of data on the ...
Definition: sf_interface.cc:69
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:404
SF::meshdata< mesh_int_t, mesh_real_t > sf_mesh
Definition: sf_interface.h:48
void apply_stim_to_vector(const stimulus &s, sf_vec &vec, bool add)
Definition: electrics.cc:453
int set_dir(IO_t dest)
Definition: sim_utils.cc:503
vec3< V > cross(const vec3< V > &a, const vec3< V > &b)
Definition: vect.h:144
int get_rank(MPI_Comm comm=PETSC_COMM_WORLD)
Definition: basics.h:284
V dist(const vec3< V > &p1, const vec3< V > &p2)
Definition: vect.h:114
@ Vm_clmp
Definition: stimulate.h:79
void init_stim_info(void)
uses potential for stimulation
Definition: stimulate.cc:49
FILE_SPEC f_open(const char *fname, const char *mode)
Open a FILE_SPEC.
Definition: basics.cc:138
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 setup_dataout(const int dataout, std::string dataout_vtx, mesh_t grid, SF::vector< mesh_int_t > *&restr, bool async)
Definition: electrics.cc:613
@ OUTPUT
Definition: sim_utils.h:55
void init_sv_gvec(gvec_data &GVs, limpet::MULTI_IF *miif, sf_vec &tmpl, igb_output_manager &output_manager)
Definition: ionics.cc:610
void assemble_sv_gvec(gvec_data &gvecs, limpet::MULTI_IF *miif)
Definition: ionics.cc:681
char * dupstr(const char *old_str)
Definition: basics.cc:44
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
Definition: basics.cc:72
mesh_t
The enum identifying the different meshes we might want to load.
Definition: sf_interface.h:59
@ extra_elec_msh
Definition: sf_interface.h:61
@ intra_elec_msh
Definition: sf_interface.h:60
void get_time(double &tm)
Definition: basics.h:444
bool mesh_is_registered(const mesh_t gt)
check wheter a SF mesh is set
Definition: sf_interface.cc:63
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
const char * get_tsav_ext(double time)
Definition: electrics.cc:868
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
@ ElecMat
Definition: fem_types.h:39
#define PETSC_TO_CANONICAL
Permute algebraic data from PETSC to canonical ordering.
Definition: sf_interface.h:79
#define ALG_TO_NODAL
Scatter algebraic to nodal.
Definition: sf_interface.h:77
#define EXP_POSTPROCESS
Definition: sim_utils.h:173
Electrical stimulation functions.
Point and vector struct.
Definition: SF_container.h:65
double y
Definition: SF_container.h:67
double z
Definition: SF_container.h:68
double x
Definition: SF_container.h:66
description of materal properties in a mesh
Definition: fem_types.h:121
SF::vector< RegionSpecs > regions
array with region params
Definition: fem_types.h:126
SF::vector< double > el_scale
optionally provided per-element params scale
Definition: fem_types.h:127
int el_scale_dpn
0=disabled, 1=isotropic scalar, 3=anisotropic (sl, st, sn) per element
Definition: fem_types.h:128
region based variations of arbitrary material parameters
Definition: fem_types.h:93
physMaterial * material
material parameter description
Definition: fem_types.h:98
int nsubregs
#subregions forming this region
Definition: fem_types.h:96
int * subregtags
FEM tags forming this region.
Definition: fem_types.h:97
char * regname
name of region
Definition: fem_types.h:94
int regID
region ID
Definition: fem_types.h:95
double slvtime_A
total time in Step A
Definition: timers.h:45
void log_stats(double time, bool cflg)
Definition: timers.cc:130
double minAT
minimum activation time in current solve
Definition: timers.h:40
double maxAT
maximum activation time in current solve
Definition: timers.h:42
void init_logger(const char *filename)
Definition: timers.cc:114
int activeList
number of nodes currently in list
Definition: timers.h:38
void update_iter(const int curiter)
Definition: timers.cc:162
double slvtime_B
total time in Step B
Definition: timers.h:47
double slvtime_D
total time in Step D
Definition: timers.h:49
bool bc_status
boundary conditions were applied?
Definition: timers.h:53
void update_cli(double time, bool cflg)
Definition: timers.cc:168
File descriptor struct.
Definition: basics.h:135
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
void log_stats(double tm, bool cflg)
Definition: timers.cc:27
const char * reasonOut
reason for list entry
SF_real T_R
repolarization time
void init_logger(const char *filename)
void log_stats(double tm, bool cflg)
SF_real D_I
diastolic interval
mesh_int_t idXNB
neighboring node index responsible for list entry
const char * reasonIn
reason for list entry
SF_real T_A_
previous activation time
SF_real T_A
current activation time
SF_real nbn_T_A
activation time of neighboring node
mesh_int_t cycle
DREAM cycle.
mesh_int_t idX
node index
void update_status(enum status s, enum reason r)