31 #include "petsc_utils.h" 41 std::map<int,std::string>
units;
44 namespace user_globals {
105 Stim & s = param_globals::stim[idx];
106 Stimulus & curstim = param_globals::stimulus[idx];
108 s.ptcl.bcl = curstim.bcl;
109 s.ptcl.npls = curstim.npls;
110 s.ptcl.start = curstim.start;
111 s.ptcl.duration = curstim.duration;
115 std::string pulse_name;
116 pulse_name =
"pulse_" + std::to_string(idx);
117 s.pulse.name = (
char*) calloc(pulse_name.length()+1,
sizeof(char));
118 strcpy(s.pulse.name, pulse_name.c_str());
122 s.pulse.file = strdup(curstim.pulse_file);
123 s.pulse.bias = curstim.bias;
124 s.pulse.tau_plateau = curstim.tau_plateau;
125 s.pulse.tau_edge = curstim.tau_edge;
126 s.pulse.s2 = curstim.s2;
128 s.pulse.tilt_ampl = curstim.s2;
129 s.pulse.tilt_time = curstim.d1;
130 s.pulse.strength = curstim.strength;
133 s.elec.vtx_fcn = curstim.vtx_fcn;
136 s.crct.type = curstim.stimtype;
137 s.crct.balance = curstim.balance;
138 s.crct.total_current = curstim.total_current;
141 s.elec.geomID = curstim.geometry;
143 s.elec.vtx_file = strdup(curstim.vtx_file);
144 s.elec.dump_vtx_file = curstim.dump_vtx_file;
147 s.elec.geom_type = 2;
148 s.elec.p0[0] = curstim.x0 - (curstim.ctr_def?curstim.xd/2.:0.);
149 s.elec.p0[1] = curstim.y0 - (curstim.ctr_def?curstim.yd/2.:0.);
150 s.elec.p0[2] = curstim.z0 - (curstim.ctr_def?curstim.zd/2.:0.);
151 s.elec.p1[0] = s.elec.p0[0] + curstim.xd;
152 s.elec.p1[1] = s.elec.p0[1] + curstim.yd;
153 s.elec.p1[2] = s.elec.p0[2] + curstim.zd;
156 s.name = strdup(curstim.name);
166 const Stim & cur_stim = param_globals::stim[idx];
169 if(cur_stim.name != std::string(
""))
170 name = cur_stim.name;
172 name =
"Stimulus_" + std::to_string(idx);
178 ptcl.setup(idx,name);
184 electrode.setup(idx);
190 if(this->phys.type ==
V_ex)
201 const Stim & cur_stim = param_globals::stim[idx];
202 const Pulse & pulse = cur_stim.pulse;
208 switch(cur_stim.crct.type) {
214 if(pulse.file && strlen(pulse.file)) wvt =
arbPulse;
219 assign(pulse.strength, cur_stim.ptcl.duration, param_globals::dt, wvt);
231 _err = file_wave.
read_trace(pulse.file, unitize);
238 "Failed reading pulse for stimulus[%d] from file %s.\n", idx, pulse.file);
247 if(wave.duration() != duration)
248 duration = wave.duration();
259 std::string tUnit =
"ms";
260 wave.setUnits(tUnit,sUnit);
266 std::string tstim = str_list;
267 std::vector<std::string> stims;
270 stlist.resize(stims.size());
271 size_t widx = 0, err = 0;
273 for(
size_t i=0; i<stims.size(); i++) {
274 std::string & stim = stims[i];
278 double last = widx == 0 ? 0.0 : stlist[widx-1];
284 size_t nread = sscanf(stim.c_str(),
"%lf", &cur);
286 stlist[widx++] = cur + last;
289 size_t nread = sscanf(stim.c_str(),
"%lf", &cur);
291 stlist[widx++] = cur;
298 if(widx != stims.size())
299 log_msg(0, 4, 0,
"%s warning: Some values of %s could not be converted in stim times!",
308 const Stim & cur_stim = param_globals::stim[idx];
309 const Protocol & ptcl = cur_stim.ptcl;
311 if(strlen(ptcl.stimlist) == 0) {
312 start = ptcl.start, npls = ptcl.npls, pcl = ptcl.bcl;
316 param_globals::tend, npls, pcl, ptcl.duration, name.c_str());
318 std::vector<double> stims;
322 for(
double t : stims)
325 log_msg(0,0,0,
"stim %d: .bcl and .npls values will be ignored.", idx);
336 const Stim & cur_stim = param_globals::stim[idx];
337 type =
stim_t(cur_stim.crct.type);
340 total_current = cur_stim.crct.total_current;
353 default: scale = 1.0;
break;
367 const Stimulus & curstim = param_globals::stimulus[idx];
375 truncExp.
tau_edge = curstim.tau_edge;
376 truncExp.
tau_plat = curstim.tau_plateau;
380 if(curstim.d1 == 1.) {
448 if(this->phys.type ==
V_ex || this->phys.type ==
GND_ex) {
457 v = pulse.strength * phys.scale * pulse.wave.f[i];
464 const Stim & curstim = param_globals::stim[idx];
468 switch(curstim.crct.type) {
483 log_msg(0,5,0,
"stim_electrode::setup error: Can't determine domain from stim type! Aborting!", __func__);
491 bool vertex_file_given = strlen(curstim.elec.vtx_file) > 0;
492 bool tag_index_given = curstim.elec.geomID > -1;
496 if(vertex_file_given && tag_index_given)
497 log_msg(0,3,0,
"%s warning: More than one stimulus electrode definintions set in electrode %d", __func__, idx);
499 if(vertex_file_given) {
500 definition = def_t::file_based;
501 input_filename = curstim.elec.vtx_file;
503 log_msg(logger, 0, 0,
"Stimulus %d: Selecting vertices from file %s", idx, input_filename.c_str());
510 if(curstim.elec.vtx_fcn)
515 int gnum_idx =
get_global(vertices.size(), MPI_SUM);
517 log_msg(0, 5, 0,
"Stimulus %d: Specified vertices are not in stimulus domain! Aborting!", idx);
521 else if(tag_index_given) {
522 definition = def_t::vol_based_tag;
524 int tag = curstim.elec.geomID;
525 log_msg(logger, 0, 0,
"Stimulus %d: Selecting vertices from tag %d", idx, tag);
532 definition = def_t::vol_based_shape;
533 log_msg(logger, 0, 0,
"Stimulus %d: Selecting vertices from shape.", idx);
537 shape.
p0 = curstim.elec.p0;
538 shape.
p1 = curstim.elec.p1;
539 shape.
radius = curstim.elec.radius;
546 int gsize = vertices.size();
548 log_msg(0,5,0,
"error: Empty stimulus[%d] electrode def! Aborting!", idx);
553 if(curstim.elec.dump_vtx_file) {
567 char dump_name[1024];
569 if (strlen(curstim.name)) {
570 snprintf(dump_name,
sizeof dump_name,
"%s.vtx", curstim.name);
572 snprintf(dump_name,
sizeof dump_name,
"ELECTRODE_%d.vtx", idx);
575 f =
f_open(dump_name,
"w");
578 fprintf(f->
fd,
"%zd\nextra\n", num_vtx);
585 log_msg(0, 4, 0,
"error: stimulus[%d] cannot be dumped!");
598 if(
is_dbc(s.phys.type) && s.is_active()) {
611 for(
size_t i=0; i<dbc_idx.
size(); i++)
612 (*dbc_buff->
nod)[widx++] = petsc_nbr[dbc_idx[i]];
617 if(s.electrode.scaling.size()) {
619 dbc->
set(*dbc_buff->
nod, s.electrode.scaling);
622 dbc->
set(*dbc_buff->
nod, 1.0);
625 mat.mult(*dbc, *dbc_buff->
cntr);
627 active_dbc[s.idx] = dbc_buff;
635 for(
const auto & d : active_dbc) {
636 if(stimuli[d.first].is_active() ==
false)
642 if(
is_dbc(s.phys.type) && s.is_active() && active_dbc.count(s.idx) == 0)
655 for(
auto it = active_dbc.begin(); it != active_dbc.end(); ++it) {
659 dbc->
set(dbc_nod, 0.0);
661 mat.mult_LR(*dbc, *dbc);
664 dbc->
set(dbc_nod, 1.0);
671 for(
auto it = active_dbc.begin(); it != active_dbc.end(); ++it) {
672 const int & dbc_idx = it->first;
674 double strength = 0.0;
675 bool is_active = stimuli[dbc_idx].value(strength);
684 if(stimuli[dbc_idx].electrode.scaling.size()) {
688 for(
SF_real s : stimuli[dbc_idx].electrode.scaling)
689 scaled_strength[widx++] = s * strength;
691 rhs.
set(*dbc.
nod, scaled_strength);
693 rhs.
set(*dbc.
nod, strength);
int trigger_elapse(int ID) const
sReal duration
pulse duration, default is 1 ms
std::map< physic_t, Basic_physic * > physics_reg
the physics
virtual SF::vector< sReal > & sample(time_trace &trc)
class to store shape definitions
void setup(int idx, std::string name)
Setup from a param stimulus index.
virtual void add_scaled(const abstract_vector< T, S > &vec, S k)=0
void read_indices(SF::vector< T > &idx, const std::string filename, const hashmap::unordered_map< mesh_int_t, mesh_int_t > &dd_map, MPI_Comm comm)
Read indices from a file.
MPI_Comm comm
the parallel mesh is defined on a MPI world
int read_trace(const std::string fname)
determine duration of a signal stored in file
std::set< int > voltage_stim
float tau_plat
time constant governing plateau of pulse
void split_string(const std::string &input, const char s, STRVEC &list)
Split a string holding a character-seperated list into a vector of strings.
PETSc numbering of nodes.
SF::vector< SF_int > * nod
void warn_when_passing_intra_vtx(const std::string filename)
void sort_parallel(MPI_Comm comm, const vector< T > &idx, vector< T > &out_idx)
Sort index values parallel ascending across the ranks.
overlapping_layout< T > pl
nodal parallel layout
bool is_voltage(stim_t type)
uses voltage as stimulation
void globalize(vector< T > &lvec) const
Globalize local indices.
void restrict_to_set(vector< T > &v, const hashmap::unordered_set< T > &set)
FILE_SPEC logger
The logger of the physic, each physic should have one.
timer_manager * tm_manager
a manager for the various physics timers
The nodal numbering of the reference mesh (the one stored on HD).
SF::vector< sReal > t
time axis
void indices_from_geom_shape(SF::vector< mesh_int_t > &idx, const sf_mesh &mesh, const geom_shape shape, const bool nodal)
Populate vertex data with the vertices inside a defined box shape.
bool is_current(stim_t type)
uses current as stimulation
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.
double time_step
global reference time step
void enforce_dbc_rhs(sf_vec &rhs)
mesh_t
The enum identifying the different meshes we might want to load.
bool trigger(int ID) const
void init_stim_info(void)
void init_vector(SF::abstract_vector< T, S > **vec)
void recompute_dbcs()
recompute the dbc data.
bool value(double &v) const
Get the current value if the stimulus is active.
monophasic truncated exponentials (capacitive discharge)
waveform_t wform
wave form of stimulus
void setup(int id)
Setup from a param stimulus index.
Tissue level electrics, main Electrics physics class.
void indices_from_region_tag(SF::vector< mesh_int_t > &idx, const sf_mesh &mesh, const int tag)
Populate vertex data with the vertices of a given tag region.
define the wave form of a stimulation pulse
float tau_edge
time constant for leading/trailing edges
double d1
duration of first sub-pulse in [ms] (zero with monophasic pulse)
virtual void set(const vector< T > &idx, const vector< S > &vals, const bool additive=false)=0
sf_mesh & get_mesh(const mesh_t gt)
Get a mesh by specifying the gridID.
bool is_active() const
Return whether stim is active.
biphasic truncated exponentials (capacitive discharge)
virtual SF::vector< sReal > & sample(time_trace &trc)
int decay
edge decay time (multiples of tau_edge)
virtual SF::vector< sReal > & sample(time_trace &trc)
Basic_physic * get_physics(physic_t p, bool error_if_missing)
Convinience function to get a physics.
bool dbc_update()
check if dbcs have updated
T get_global(T in, MPI_Op OP, MPI_Comm comm=PETSC_COMM_WORLD)
Do a global reduction on a variable.
void resample(time_trace &trc)
double duration
duration of stimulus
sig::time_trace wave
wave form of stimulus pulse
void setup(int idx)
Setup from a param stimulus index.
void update_cwd()
save the current working directory to curdir so that we can switch back to it if needed.
void get_stim_list(const char *str_list, std::vector< double > &stlist)
#define UM_to_CM
convert um to cm
size_t size() const
The current size of the vector.
bool is_extra(stim_t type)
whether stimulus is on extra grid (or on intra)
Electrical stimulation functions.
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
short get_mesh_dim(mesh_t id)
get (lowest) dimension of the mesh used in the experiment
void sample_wave_form(stim_pulse &sp, int idx)
sample a signal given in analytic form
std::set< int > current_stim
int add_neq_timer(const std::vector< double > &itrig, double idur, const char *iname, const char *poolname=nullptr)
void f_close(FILE_SPEC &f)
Close a FILE_SPEC.
void read_indices_with_data(SF::vector< T > &idx, SF::vector< S > &dat, const std::string filename, const hashmap::unordered_map< mesh_int_t, mesh_int_t > &dd_map, const int dpn, MPI_Comm comm)
like read_indices, but with associated data for each index
std::map< int, std::string > units
const vector< T > & algebraic_nodes() const
Getter function for the local indices forming the local algebraic node set.
double SF_real
Use the general double as real type.
void translate(int id)
convert legacy definitions to new format
void print_vector(MPI_Comm comm, const vector< T > &vec, const short dpn, FILE *fd)
SF::vector< sReal > f
store function values of trace
bool is_dbc(stim_t type)
whether stimulus is a dirichlet type. implies boundary conditions on matrix
centralize time managment and output triggering
int get_rank(MPI_Comm comm=PETSC_COMM_WORLD)
void setup(int idx)
assign stimulus physics parameters
#define UM2_to_CM2
convert um^2 to cm^2
FILE_SPEC f_open(const char *fname, const char *mode)
Open a FILE_SPEC.