openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
stimulate.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
2 // SPDX-License-Identifier: LicenseRef-APL-1.1
3 
12 #ifndef _STIMULATE_H
13 #define _STIMULATE_H
14 
15 #include "signals.h"
16 #include "physics_types.h"
17 #include "fem_utils.h"
18 
19 namespace opencarp {
20 
21 // the following must be kept in jive with stim.crct.type in openCARP.prm
22 #define Transmembrane_I 0
23 #define Extracellular_I 1
24 #define Extracellular_V 2
25 #define Extracellular_Ground 3
26 #define Intracellular_I 4
27 #define Extracellular_V_OL 5
28 #define Illumination 6
29 // #define Transmembrane_I_Grad 7
30 // #define LAT_Triggered 8
31 #define Vm_clamp 9
32 #define Intracellular_V 10
33 #define Intracellular_V_OL 11
34 #define Ignore_Stim 12
35 #define LeadField_I 13
36 
37 #define IsExtraV(A) ((A.stimtype==Extracellular_V) || (A.stimtype==Extracellular_V_OL))
38 #define SELECTED_STIM(A,B) ((A==B) || ((A==Extracellular_V_OL) && (B==Extracellular_V)))
39 
40 // flags for ignoring stimuli as function of simulation mode
41 #define IGNORE_NONE 0
42 #define NO_EXTRA_GND 1
43 #define NO_EXTRA_V 2
44 #define NO_EXTRA_I 4
45 
46 #define STM_IGNORE_BIDOMAIN (IGNORE_NONE) // bidomain
47 #define STM_IGNORE_MONODOMAIN (NO_EXTRA_GND | NO_EXTRA_V | NO_EXTRA_I) // monodomain
48 #define STM_IGNORE_PSEUDO_BIDM (NO_EXTRA_V | NO_EXTRA_I) // pseudo-bidomain
49 #define STM_IGNORE_FLOAT_GND (NO_EXTRA_GND | NO_EXTRA_V) // bidomain w/floating ground
50 
51 // define electrode specification method
52 #define VOL_BASED_ELEC_DEF 0
53 #define FILE_BASED_ELEC_DEF 1
54 
55 // define boundary condition specification method
56 #define VOL_BASED_BC_DEF 0
57 #define FILE_BASED_BC_DEF 1
58 
61 const std::string wfLabels [] = {"squarePulse", "truncExpPulse", "sinePulse", "arbPulse", "constPulse", "unset"};
62 
63 //enum stim_t {I_transmembrane = 0, I_extra};
64 enum stim_t {I_tm=0, I_ex=1, Phi_ex=2, GND_ex=3, I_in=4, Phi_ex_ol=5, Illum=6, I_tm_grad=7, I_lat=8, Vm_clmp=9, Phi_in=10, Phi_in_ol=11, Ignore_stim=12, LF_I=13};
65 enum stim_domain_t {intra_stim = 1, purk_stim = 2, all_stim = 3}; // matches Stimulus.domain from openCARP.prm
66 
67 // stimulus info conveniene functions
68 void init_stim_info(void);
69 bool is_potential(stim_t type);
70 bool is_current(stim_t type);
71 bool is_dbc(stim_t type);
72 bool is_extra(stim_t type);
73 
77 {
78  public:
79  double strength = 0.0;
80  double duration = 0.0;
82 
84 
85  inline void assign(double _strength, double _duration, double _dt, waveform_t _wform)
86  {
87  strength = _strength;
88  duration = _duration;
89  wform = _wform;
90  wave = sig::time_trace(_duration, _dt);
92  }
93 
99  void setup(int id);
100 };
101 
103 {
104  public:
105  double start = 0.0;
106  int npls = 0;
107  double pcl = 0.0;
108  int timer_id = -1;
109  int xtrg_id = -1;
110 
117  void setup(int idx, std::string name);
118 };
119 
121 {
122  public:
124  std::string unit;
125  double scale;
127 
128  void setup(int idx, mesh_t intra_mesh, mesh_t extra_mesh);
129 };
130 
132 {
133  public:
134 
136 
140  std::string input_filename;
141  bool dump_vtx;
142 
143  void setup(int idx, mesh_t intra_mesh, mesh_t extra_mesh);
144 };
145 
146 
147 class stimulus
148 {
149  public:
150  int idx = -1;
151  std::string name = "unnamed";
152 
157 
158  // in some cases one wants to construct a stimulus that is not associated to the
159  // electric meshes. One example is the EMI solver.
162 
168  void setup(int idx);
169  void translate(int id);
170 
178  bool value(double & v) const;
179 
183  void dump_vtx_file(int idx);
184 
186  bool is_active() const;
187 };
188 
191 {
192  public:
193 
194  struct dbc_data {
196  sf_vec* cntr = nullptr;
197 
199  if(nod) delete nod;
200  if(cntr) delete cntr;
201  }
202  };
203 
209  std::map<int, dbc_data*> active_dbc;
210 
212  mat(im), stimuli(is)
213  {
214  recompute_dbcs();
215  }
216 
218  {
219  clear_active_dbc();
220  }
221 
223  bool dbc_update();
225  void recompute_dbcs();
226 
227  void enforce_dbc_lhs();
228  void enforce_dbc_rhs(sf_vec & rhs);
229 
230 
231  private:
232  inline void clear_active_dbc()
233  {
234  for(auto & d : active_dbc)
235  delete d.second;
236 
237  active_dbc.clear();
238  }
239 };
240 
247 void sample_wave_form(stim_pulse& sp,int idx);
248 
249 } // namespace opencarp
250 
251 #endif
manager for dirichlet boundary conditions
Definition: stimulate.h:191
std::map< int, dbc_data * > active_dbc
the DBCs that are currently active
Definition: stimulate.h:209
sf_mat & mat
the matrix we link the dbc_manager to
Definition: stimulate.h:205
dbc_manager(sf_mat &im, const SF::vector< stimulus > &is)
Definition: stimulate.h:211
void enforce_dbc_rhs(sf_vec &rhs)
Definition: stimulate.cc:676
void recompute_dbcs()
recompute the dbc data.
Definition: stimulate.cc:597
const SF::vector< stimulus > & stimuli
the stimuli we link the dbc_manager to
Definition: stimulate.h:207
bool dbc_update()
check if dbcs have updated
Definition: stimulate.cc:636
Time tracing class.
Definition: signals.h:127
void set_labels(std::string _label)
Definition: signals.h:219
SF::vector< mesh_int_t > vertices
Definition: stimulate.h:138
void setup(int idx, mesh_t intra_mesh, mesh_t extra_mesh)
Definition: stimulate.cc:500
std::string input_filename
Definition: stimulate.h:140
SF::vector< SF_real > scaling
Definition: stimulate.h:139
double scale
internal unit conversion scaling
Definition: stimulate.h:125
bool total_current
whether we apply total current scaling
Definition: stimulate.h:126
stim_t type
type of stimulus
Definition: stimulate.h:123
std::string unit
physical units of stimulus
Definition: stimulate.h:124
void setup(int idx, mesh_t intra_mesh, mesh_t extra_mesh)
assign stimulus physics parameters
Definition: stimulate.cc:300
int timer_id
timer for stimulus
Definition: stimulate.h:108
int npls
number of stimulus pulses
Definition: stimulate.h:106
double pcl
pacing cycle length
Definition: stimulate.h:107
int xtrg_id
external trigger ID, not used for now
Definition: stimulate.h:109
double start
start time of protocol
Definition: stimulate.h:105
void setup(int idx, std::string name)
Setup from a param stimulus index.
Definition: stimulate.cc:272
define the wave form of a stimulation pulse
Definition: stimulate.h:77
sig::time_trace wave
wave form of stimulus pulse
Definition: stimulate.h:83
void assign(double _strength, double _duration, double _dt, waveform_t _wform)
Definition: stimulate.h:85
void setup(int id)
Setup from a param stimulus index.
Definition: stimulate.cc:197
double duration
duration of stimulus
Definition: stimulate.h:80
waveform_t wform
wave form of stimulus
Definition: stimulate.h:81
double strength
strength of stimulus
Definition: stimulate.h:79
stim_protocol ptcl
applied stimulation protocol used
Definition: stimulate.h:154
int idx
index in global input stimulus array
Definition: stimulate.h:150
stim_electrode electrode
electrode geometry
Definition: stimulate.h:156
mesh_t associated_intra_mesh
Definition: stimulate.h:160
stim_pulse pulse
stimulus wave form
Definition: stimulate.h:153
mesh_t associated_extra_mesh
Definition: stimulate.h:161
void translate(int id)
convert legacy definitions to new format
Definition: stimulate.cc:92
bool is_active() const
Return whether stim is active.
Definition: stimulate.cc:185
void setup(int idx)
Setup from a param stimulus index.
Definition: stimulate.cc:153
void dump_vtx_file(int idx)
Export the vertices to vtx file.
Definition: stimulate.cc:457
stim_physics phys
physics of stimulus
Definition: stimulate.h:155
bool value(double &v) const
Get the current value if the stimulus is active.
Definition: stimulate.cc:434
std::string name
label stimulus
Definition: stimulate.h:151
FEM utilities functions.
void sample_wave_form(stim_pulse &sp, int idx)
sample a signal given in analytic form
Definition: stimulate.cc:334
bool is_dbc(stim_t type)
whether stimulus is a dirichlet type. implies boundary conditions on matrix
Definition: stimulate.cc:63
const std::string wfLabels[]
Definition: stimulate.h:61
@ sinePulse
Definition: stimulate.h:60
@ constPulse
Definition: stimulate.h:60
@ squarePulse
Definition: stimulate.h:60
@ truncExpPulse
Definition: stimulate.h:60
@ arbPulse
Definition: stimulate.h:60
@ unsetPulse
Definition: stimulate.h:60
@ Phi_in
Definition: stimulate.h:64
@ Ignore_stim
Definition: stimulate.h:64
@ I_tm_grad
Definition: stimulate.h:64
@ GND_ex
Definition: stimulate.h:64
@ Phi_ex
Definition: stimulate.h:64
@ Phi_ex_ol
Definition: stimulate.h:64
@ Vm_clmp
Definition: stimulate.h:64
@ Phi_in_ol
Definition: stimulate.h:64
bool is_potential(stim_t type)
uses current for stimulation
Definition: stimulate.cc:52
void init_stim_info(void)
uses potential for stimulation
Definition: stimulate.cc:34
bool is_extra(stim_t type)
whether stimulus is on extra grid (or on intra)
Definition: stimulate.cc:68
bool is_current(stim_t type)
uses current as stimulation
Definition: stimulate.cc:58
stim_domain_t
Definition: stimulate.h:65
@ purk_stim
Definition: stimulate.h:65
@ intra_stim
Definition: stimulate.h:65
@ all_stim
Definition: stimulate.h:65
mesh_t
The enum identifying the different meshes we might want to load.
Definition: sf_interface.h:44
@ extra_elec_msh
Definition: sf_interface.h:46
@ intra_elec_msh
Definition: sf_interface.h:45
Basic physics types.
Manage time signals used for stimulation and other time-dependent boundary conditions.
SF::vector< SF_int > * nod
Definition: stimulate.h:195