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 #define LeadField_I 13
51 
52 #define IsExtraV(A) ((A.stimtype==Extracellular_V) || (A.stimtype==Extracellular_V_OL))
53 #define SELECTED_STIM(A,B) ((A==B) || ((A==Extracellular_V_OL) && (B==Extracellular_V)))
54 
55 // flags for ignoring stimuli as function of simulation mode
56 #define IGNORE_NONE 0
57 #define NO_EXTRA_GND 1
58 #define NO_EXTRA_V 2
59 #define NO_EXTRA_I 4
60 
61 #define STM_IGNORE_BIDOMAIN (IGNORE_NONE) // bidomain
62 #define STM_IGNORE_MONODOMAIN (NO_EXTRA_GND | NO_EXTRA_V | NO_EXTRA_I) // monodomain
63 #define STM_IGNORE_PSEUDO_BIDM (NO_EXTRA_V | NO_EXTRA_I) // pseudo-bidomain
64 #define STM_IGNORE_FLOAT_GND (NO_EXTRA_GND | NO_EXTRA_V) // bidomain w/floating ground
65 
66 // define electrode specification method
67 #define VOL_BASED_ELEC_DEF 0
68 #define FILE_BASED_ELEC_DEF 1
69 
70 // define boundary condition specification method
71 #define VOL_BASED_BC_DEF 0
72 #define FILE_BASED_BC_DEF 1
73 
76 const std::string wfLabels [] = {"squarePulse", "truncExpPulse", "sinePulse", "arbPulse"};
77 
78 //enum stim_t {I_transmembrane = 0, I_extra};
79 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};
80 enum stim_domain_t {intra_stim = 1, purk_stim = 2, all_stim = 3}; // matches Stimulus.domain from openCARP.prm
81 
82 // stimulus info conveniene functions
83 void init_stim_info(void);
84 bool is_potential(stim_t type);
85 bool is_current(stim_t type);
86 bool is_dbc(stim_t type);
87 bool is_extra(stim_t type);
88 
92 {
93  public:
94  double strength = 0.0;
95  double duration = 0.0;
97 
99 
100  inline void assign(double _strength, double _duration, double _dt, waveform_t _wform)
101  {
102  strength = _strength;
103  duration = _duration;
104  wform = _wform;
105  wave = sig::time_trace(_duration, _dt);
107  }
108 
114  void setup(int id);
115 };
116 
118 {
119  public:
120  double start = 0.0;
121  int npls = 0;
122  double pcl = 0.0;
123  int timer_id = -1;
124  int xtrg_id = -1;
125 
132  void setup(int idx, std::string name);
133 };
134 
136 {
137  public:
139  std::string unit;
140  double scale;
142 
143  void setup(int idx, mesh_t intra_mesh, mesh_t extra_mesh);
144 };
145 
147 {
148  public:
149 
151 
155  std::string input_filename;
156  bool dump_vtx;
157 
158  void setup(int idx, mesh_t intra_mesh, mesh_t extra_mesh);
159 };
160 
161 
162 class stimulus
163 {
164  public:
165  int idx = -1;
166  std::string name = "unnamed";
167 
172 
173  // in some cases one wants to construct a stimulus that is not associated to the
174  // electric meshes. One example is the EMI solver.
177 
183  void setup(int idx);
184  void translate(int id);
185 
193  bool value(double & v) const;
194 
198  void dump_vtx_file(int idx);
199 
201  bool is_active() const;
202 };
203 
206 {
207  public:
208 
209  struct dbc_data {
211  sf_vec* cntr = nullptr;
212 
214  if(nod) delete nod;
215  if(cntr) delete cntr;
216  }
217  };
218 
224  std::map<int, dbc_data*> active_dbc;
225 
227  mat(im), stimuli(is)
228  {
229  recompute_dbcs();
230  }
231 
233  {
234  clear_active_dbc();
235  }
236 
238  bool dbc_update();
240  void recompute_dbcs();
241 
242  void enforce_dbc_lhs();
243  void enforce_dbc_rhs(sf_vec & rhs);
244 
245 
246  private:
247  inline void clear_active_dbc()
248  {
249  for(auto & d : active_dbc)
250  delete d.second;
251 
252  active_dbc.clear();
253  }
254 };
255 
262 void sample_wave_form(stim_pulse& sp,int idx);
263 
264 } // namespace opencarp
265 
266 #endif
manager for dirichlet boundary conditions
Definition: stimulate.h:206
std::map< int, dbc_data * > active_dbc
the DBCs that are currently active
Definition: stimulate.h:224
sf_mat & mat
the matrix we link the dbc_manager to
Definition: stimulate.h:220
dbc_manager(sf_mat &im, const SF::vector< stimulus > &is)
Definition: stimulate.h:226
void enforce_dbc_rhs(sf_vec &rhs)
Definition: stimulate.cc:691
void recompute_dbcs()
recompute the dbc data.
Definition: stimulate.cc:612
const SF::vector< stimulus > & stimuli
the stimuli we link the dbc_manager to
Definition: stimulate.h:222
bool dbc_update()
check if dbcs have updated
Definition: stimulate.cc:651
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:153
void setup(int idx, mesh_t intra_mesh, mesh_t extra_mesh)
Definition: stimulate.cc:515
std::string input_filename
Definition: stimulate.h:155
SF::vector< SF_real > scaling
Definition: stimulate.h:154
double scale
internal unit conversion scaling
Definition: stimulate.h:140
bool total_current
whether we apply total current scaling
Definition: stimulate.h:141
stim_t type
type of stimulus
Definition: stimulate.h:138
std::string unit
physical units of stimulus
Definition: stimulate.h:139
void setup(int idx, mesh_t intra_mesh, mesh_t extra_mesh)
assign stimulus physics parameters
Definition: stimulate.cc:315
int timer_id
timer for stimulus
Definition: stimulate.h:123
int npls
number of stimulus pulses
Definition: stimulate.h:121
double pcl
pacing cycle length
Definition: stimulate.h:122
int xtrg_id
external trigger ID, not used for now
Definition: stimulate.h:124
double start
start time of protocol
Definition: stimulate.h:120
void setup(int idx, std::string name)
Setup from a param stimulus index.
Definition: stimulate.cc:287
define the wave form of a stimulation pulse
Definition: stimulate.h:92
sig::time_trace wave
wave form of stimulus pulse
Definition: stimulate.h:98
void assign(double _strength, double _duration, double _dt, waveform_t _wform)
Definition: stimulate.h:100
void setup(int id)
Setup from a param stimulus index.
Definition: stimulate.cc:212
double duration
duration of stimulus
Definition: stimulate.h:95
waveform_t wform
wave form of stimulus
Definition: stimulate.h:96
double strength
strength of stimulus
Definition: stimulate.h:94
stim_protocol ptcl
applied stimulation protocol used
Definition: stimulate.h:169
int idx
index in global input stimulus array
Definition: stimulate.h:165
stim_electrode electrode
electrode geometry
Definition: stimulate.h:171
mesh_t associated_intra_mesh
Definition: stimulate.h:175
stim_pulse pulse
stimulus wave form
Definition: stimulate.h:168
mesh_t associated_extra_mesh
Definition: stimulate.h:176
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
bool value(double &v) const
Get the current value if the stimulus is active.
Definition: stimulate.cc:449
std::string name
label stimulus
Definition: stimulate.h:166
FEM utilities functions.
void sample_wave_form(stim_pulse &sp, int idx)
sample a signal given in analytic form
Definition: stimulate.cc:349
bool is_dbc(stim_t type)
whether stimulus is a dirichlet type. implies boundary conditions on matrix
Definition: stimulate.cc:78
const std::string wfLabels[]
Definition: stimulate.h:76
@ sinePulse
Definition: stimulate.h:75
@ constPulse
Definition: stimulate.h:75
@ squarePulse
Definition: stimulate.h:75
@ truncExpPulse
Definition: stimulate.h:75
@ arbPulse
Definition: stimulate.h:75
@ unsetPulse
Definition: stimulate.h:75
@ Phi_in
Definition: stimulate.h:79
@ Ignore_stim
Definition: stimulate.h:79
@ I_tm_grad
Definition: stimulate.h:79
@ GND_ex
Definition: stimulate.h:79
@ Phi_ex
Definition: stimulate.h:79
@ Phi_ex_ol
Definition: stimulate.h:79
@ Vm_clmp
Definition: stimulate.h:79
@ Phi_in_ol
Definition: stimulate.h:79
bool is_potential(stim_t type)
uses current for stimulation
Definition: stimulate.cc:67
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:83
bool is_current(stim_t type)
uses current as stimulation
Definition: stimulate.cc:73
stim_domain_t
Definition: stimulate.h:80
@ purk_stim
Definition: stimulate.h:80
@ intra_stim
Definition: stimulate.h:80
@ all_stim
Definition: stimulate.h:80
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:210