openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
stimulate.h
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 #ifndef _STIMULATE_H
28 #define _STIMULATE_H
29 
30 #include "signals.h"
31 #include "physics_types.h"
32 #include "fem_utils.h"
33 
34 namespace opencarp {
35 
36 // the following must be kept in jive with stim.crct.type in openCARP.prm
37 #define Transmembrane_I 0
38 #define Extracellular_I 1
39 #define Extracellular_V 2
40 #define Extracellular_Ground 3
41 #define Intracellular_I 4
42 #define Extracellular_V_OL 5
43 #define Illumination 6
44 // #define Transmembrane_I_Grad 7
45 // #define LAT_Triggered 8
46 #define Vm_clamp 9
47 #define Intracellular_V 10
48 #define Intracellular_V_OL 11
49 #define Ignore_Stim 12
50 
51 #define IsExtraV(A) ((A.stimtype==Extracellular_V) || (A.stimtype==Extracellular_V_OL))
52 #define SELECTED_STIM(A,B) ((A==B) || ((A==Extracellular_V_OL) && (B==Extracellular_V)))
53 
54 // flags for ignoring stimuli as function of simulation mode
55 #define IGNORE_NONE 0
56 #define NO_EXTRA_GND 1
57 #define NO_EXTRA_V 2
58 #define NO_EXTRA_I 4
59 
60 #define STM_IGNORE_BIDOMAIN (IGNORE_NONE) // bidomain
61 #define STM_IGNORE_MONODOMAIN (NO_EXTRA_GND | NO_EXTRA_V | NO_EXTRA_I) // monodomain
62 #define STM_IGNORE_PSEUDO_BIDM (NO_EXTRA_V | NO_EXTRA_I) // pseudo-bidomain
63 #define STM_IGNORE_FLOAT_GND (NO_EXTRA_GND | NO_EXTRA_V) // bidomain w/floating ground
64 
65 // define electrode specification method
66 #define VOL_BASED_ELEC_DEF 0
67 #define FILE_BASED_ELEC_DEF 1
68 
69 // define boundary condition specification method
70 #define VOL_BASED_BC_DEF 0
71 #define FILE_BASED_BC_DEF 1
72 
75 const std::string wfLabels [] = {"squarePulse", "truncExpPulse", "sinePulse", "arbPulse"};
76 
77 //enum stim_t {I_transmembrane = 0, I_extra};
78 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};
79 enum stim_domain_t {intra_stim = 1, purk_stim = 2, all_stim = 3}; // matches Stimulus.domain from openCARP.prm
80 
81 // stimulus info conveniene functions
82 void init_stim_info(void);
83 bool is_potential(stim_t type);
84 bool is_current(stim_t type);
85 bool is_dbc(stim_t type);
86 bool is_extra(stim_t type);
87 
91 {
92  public:
93  double strength = 0.0;
94  double duration = 0.0;
96 
98 
99  inline void assign(double _strength, double _duration, double _dt, waveform_t _wform)
100  {
101  strength = _strength;
102  duration = _duration;
103  wform = _wform;
104  wave = sig::time_trace(_duration, _dt);
106  }
107 
113  void setup(int id);
114 };
115 
117 {
118  public:
119  double start = 0.0;
120  int npls = 0;
121  double pcl = 0.0;
122  int timer_id = -1;
123  int xtrg_id = -1;
124 
131  void setup(int idx, std::string name);
132 };
133 
135 {
136  public:
138  std::string unit;
139  double scale;
141 
142  void setup(int idx, mesh_t intra_mesh, mesh_t extra_mesh);
143 };
144 
146 {
147  public:
148 
150 
154  std::string input_filename;
155  bool dump_vtx;
156 
157  void setup(int idx, mesh_t intra_mesh, mesh_t extra_mesh);
158 };
159 
160 
161 class stimulus
162 {
163  public:
164  int idx = -1;
165  std::string name = "unnamed";
166 
171 
172  // in some cases one wants to construct a stimulus that is not associated to the
173  // electric meshes. One example is the EMI solver.
176 
182  void setup(int idx);
183  void translate(int id);
184 
192  bool value(double & v) const;
193 
197  void dump_vtx_file(int idx);
198 
200  bool is_active() const;
201 };
202 
205 {
206  public:
207 
208  struct dbc_data {
210  sf_vec* cntr = nullptr;
211 
213  if(nod) delete nod;
214  if(cntr) delete cntr;
215  }
216  };
217 
223  std::map<int, dbc_data*> active_dbc;
224 
226  mat(im), stimuli(is)
227  {
228  recompute_dbcs();
229  }
230 
232  {
233  clear_active_dbc();
234  }
235 
237  bool dbc_update();
239  void recompute_dbcs();
240 
241  void enforce_dbc_lhs();
242  void enforce_dbc_rhs(sf_vec & rhs);
243 
244 
245  private:
246  inline void clear_active_dbc()
247  {
248  for(auto & d : active_dbc)
249  delete d.second;
250 
251  active_dbc.clear();
252  }
253 };
254 
261 void sample_wave_form(stim_pulse& sp,int idx);
262 
263 } // namespace opencarp
264 
265 #endif
manager for dirichlet boundary conditions
Definition: stimulate.h:205
std::map< int, dbc_data * > active_dbc
the DBCs that are currently active
Definition: stimulate.h:223
sf_mat & mat
the matrix we link the dbc_manager to
Definition: stimulate.h:219
dbc_manager(sf_mat &im, const SF::vector< stimulus > &is)
Definition: stimulate.h:225
void enforce_dbc_rhs(sf_vec &rhs)
Definition: stimulate.cc:675
void recompute_dbcs()
recompute the dbc data.
Definition: stimulate.cc:596
const SF::vector< stimulus > & stimuli
the stimuli we link the dbc_manager to
Definition: stimulate.h:221
bool dbc_update()
check if dbcs have updated
Definition: stimulate.cc:635
Time tracing class.
Definition: signals.h:142
void set_labels(std::string _label)
Definition: signals.h:234
SF::vector< mesh_int_t > vertices
Definition: stimulate.h:152
void setup(int idx, mesh_t intra_mesh, mesh_t extra_mesh)
Definition: stimulate.cc:500
std::string input_filename
Definition: stimulate.h:154
SF::vector< SF_real > scaling
Definition: stimulate.h:153
double scale
internal unit conversion scaling
Definition: stimulate.h:139
bool total_current
whether we apply total current scaling
Definition: stimulate.h:140
stim_t type
type of stimulus
Definition: stimulate.h:137
std::string unit
physical units of stimulus
Definition: stimulate.h:138
void setup(int idx, mesh_t intra_mesh, mesh_t extra_mesh)
assign stimulus physics parameters
Definition: stimulate.cc:304
int timer_id
timer for stimulus
Definition: stimulate.h:122
int npls
number of stimulus pulses
Definition: stimulate.h:120
double pcl
pacing cycle length
Definition: stimulate.h:121
int xtrg_id
external trigger ID, not used for now
Definition: stimulate.h:123
double start
start time of protocol
Definition: stimulate.h:119
void setup(int idx, std::string name)
Setup from a param stimulus index.
Definition: stimulate.cc:276
define the wave form of a stimulation pulse
Definition: stimulate.h:91
sig::time_trace wave
wave form of stimulus pulse
Definition: stimulate.h:97
void assign(double _strength, double _duration, double _dt, waveform_t _wform)
Definition: stimulate.h:99
void setup(int id)
Setup from a param stimulus index.
Definition: stimulate.cc:202
double duration
duration of stimulus
Definition: stimulate.h:94
waveform_t wform
wave form of stimulus
Definition: stimulate.h:95
double strength
strength of stimulus
Definition: stimulate.h:93
stim_protocol ptcl
applied stimulation protocol used
Definition: stimulate.h:168
int idx
index in global input stimulus array
Definition: stimulate.h:164
stim_electrode electrode
electrode geometry
Definition: stimulate.h:170
mesh_t associated_intra_mesh
Definition: stimulate.h:174
stim_pulse pulse
stimulus wave form
Definition: stimulate.h:167
mesh_t associated_extra_mesh
Definition: stimulate.h:175
void translate(int id)
convert legacy definitions to new format
Definition: stimulate.cc:105
bool is_active() const
Return whether stim is active.
Definition: stimulate.cc:190
void setup(int idx)
Setup from a param stimulus index.
Definition: stimulate.cc:166
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:169
bool value(double &v) const
Get the current value if the stimulus is active.
Definition: stimulate.cc:437
std::string name
label stimulus
Definition: stimulate.h:165
FEM utilities functions.
void sample_wave_form(stim_pulse &sp, int idx)
sample a signal given in analytic form
Definition: stimulate.cc:337
bool is_dbc(stim_t type)
whether stimulus is a dirichlet type. implies boundary conditions on matrix
Definition: stimulate.cc:77
const std::string wfLabels[]
Definition: stimulate.h:75
@ sinePulse
Definition: stimulate.h:74
@ constPulse
Definition: stimulate.h:74
@ squarePulse
Definition: stimulate.h:74
@ truncExpPulse
Definition: stimulate.h:74
@ arbPulse
Definition: stimulate.h:74
@ unsetPulse
Definition: stimulate.h:74
@ Phi_in
Definition: stimulate.h:78
@ Ignore_stim
Definition: stimulate.h:78
@ I_tm_grad
Definition: stimulate.h:78
@ GND_ex
Definition: stimulate.h:78
@ Phi_ex
Definition: stimulate.h:78
@ Phi_ex_ol
Definition: stimulate.h:78
@ Vm_clmp
Definition: stimulate.h:78
@ Phi_in_ol
Definition: stimulate.h:78
bool is_potential(stim_t type)
uses current for stimulation
Definition: stimulate.cc:66
void init_stim_info(void)
uses potential for stimulation
Definition: stimulate.cc:49
bool is_extra(stim_t type)
whether stimulus is on extra grid (or on intra)
Definition: stimulate.cc:82
bool is_current(stim_t type)
uses current as stimulation
Definition: stimulate.cc:72
stim_domain_t
Definition: stimulate.h:79
@ purk_stim
Definition: stimulate.h:79
@ intra_stim
Definition: stimulate.h:79
@ all_stim
Definition: stimulate.h:79
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
Basic physics types.
Manage time signals used for stimulation and other time-dependent boundary conditions.
SF::vector< SF_int > * nod
Definition: stimulate.h:209