openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
timer_utils.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 TIMER_H
28 #define TIMER_H
29 
30 #include <cstdio>
31 #include <cstdlib>
32 #include <cstdbool>
33 #include <cmath>
34 
35 #include <string>
36 #include <vector>
37 
38 
39 namespace opencarp {
40 
45 
50 
51 struct base_timer {
53  const char* name;
54  bool triggered;
56  int numIOs;
57  double trigger_dur;
59  int active;
60  std::string pool;
61 
62  virtual void update() = 0;
63  virtual void reset() = 0;
64 
65  inline void assign_pool(const char* poolname)
66  {
67  pool = poolname;
68  }
69 };
70 
71 
74 public:
75 
76  double time;
77  long d_time;
78  double time_step;
79  double start;
80  long d_start;
81  double end;
82  long d_end;
83 
84  std::vector<base_timer*> timers;
85 
91  timer_manager(double inp_dt, double inp_start, double inp_end)
92  {
93  setup(inp_dt, inp_start, inp_end);
94  timers.assign(iotm_num_timers, nullptr);
95  }
96 
103  inline void update_timers()
104  {
105  d_time++;
106  time = d_time * time_step;
107 
108  for (base_timer* t : timers)
109  if(t != nullptr) t->update();
110  }
111 
115  inline void reset_timers() {
116  time = start;
117  d_time = d_start;
118 
119  for (base_timer* t : timers)
120  if(t != nullptr) t->reset();
121  }
122 
136  int add_eq_timer(double istart, double iend, int ntrig, double iintv, double idur,
137  const char *iname, const char *poolname = nullptr);
138 
139  int add_neq_timer(const std::vector<double> & itrig, double idur, const char *iname,
140  const char *poolname = nullptr);
141 
142  inline int
143  add_singlestep_timer(double tg, double idur, const char *iname,
144  const char *poolname = nullptr)
145  {
146  return add_eq_timer(tg, tg, 0, 0, idur, iname, poolname);
147  }
148 
149  void initialize_eq_timer(double istart, double iend, int ntrig, double iintv,
150  double idur, int ID, const char *iname, const char* poolname = nullptr);
151 
152  void initialize_neq_timer(const std::vector<double> & itrig, double idur, int ID,
153  const char *iname, const char* poolname = nullptr);
154 
155  inline void
156  initialize_singlestep_timer(double tg, double idur, int ID, const char *iname,
157  const char *poolname = nullptr)
158  {
159  initialize_eq_timer(tg, tg, 0, 0, idur, ID, iname, poolname);
160  }
161 
162  inline size_t num_timers() const {return timers.size();}
163  inline double timer_duration(int ID) const {return ID > -1 ? timers[ID]->trigger_dur : 0.0;}
164  inline bool elapsed() const {return d_time > d_end;}
165 
166  inline bool trigger(int ID) const
167  {
168  return ID > -1 && timers[ID] != nullptr && timers[ID]->triggered;
169  }
170 
171  inline bool triggered_now(int ID) const
172  {
173  return ID > -1 &&
174  timers[ID] != nullptr &&
175  ((timers[ID]->d_trigger_dur) - (timers[ID]->active)) == 1;
176  }
177 
178  inline bool trigger_end(int ID) const
179  {
180  return ID > -1 &&
181  timers[ID] != nullptr &&
182  timers[ID]->triggered &&
183  !timers[ID]->active;
184  }
185 
186  inline int trigger_elapse(int ID) const
187  {
188  if(ID > -1 && timers[ID] != nullptr)
189  return timers[ID]->d_trigger_dur - timers[ID]->active - 1;
190  else
191  return 0;
192  }
193 
195  inline int active_ticks(int ID) const
196  {
197  if(ID > -1 && timers[ID] != nullptr)
198  return timers[ID]->triggered ? timers[ID]->d_trigger_dur - timers[ID]->active : 0;
199  else
200  return 0;
201  }
202 
212  void setup(double inp_dt, double inp_start, double inp_end);
213 };
214 
215 
217 {
219 
220  double start;
221  long d_start;
222  double end;
223  long d_end;
224  double intv;
225  long d_intv;
226  double nxt;
227  int d_nxt;
228 
229  void initialize(const timer_manager* t, double istart, double iend, int ntrig, double iintv,
230  double idur, const char *iname);
231  void update();
232  void reset();
233 };
234 
236 {
238 
239  std::vector<double> trig;
240  std::vector<long> d_trig;
241 
242  void initialize(const timer_manager* t, const std::vector<double> & itrig,
243  const double idur, const char *iname);
244  void update();
245  void reset();
246 };
247 
248 } // namespace opencarp
249 
250 #endif
251 
int trigger_elapse(int ID) const
Definition: timer_utils.h:186
double timer_duration(int ID) const
Definition: timer_utils.h:163
virtual void reset()=0
std::string pool
label of pool (IO || TS || STIM || ...) to which timer belongs
Definition: timer_utils.h:60
void assign_pool(const char *poolname)
Definition: timer_utils.h:65
void reset_timers()
Reset time in timer_manager and then reset registered timers.
Definition: timer_utils.h:115
bool triggered
flag indicating trigger at current time step
Definition: timer_utils.h:54
const timer_manager * mng
Definition: timer_utils.h:237
bool trigger_end(int ID) const
Definition: timer_utils.h:178
const timer_manager * mng
Definition: timer_utils.h:218
double time_step
global reference time step
Definition: timer_utils.h:78
std::vector< long > d_trig
the discrete times associated to trig
Definition: timer_utils.h:240
bool trigger(int ID) const
Definition: timer_utils.h:166
t_timer type
type of timer, continuous equidistant, or non-equidistant
Definition: timer_utils.h:52
double end
final time
Definition: timer_utils.h:81
long d_start
discrete start in multiples of dt
Definition: timer_utils.h:221
size_t num_timers() const
Definition: timer_utils.h:162
double intv
io interval in ms
Definition: timer_utils.h:224
int trigger_count
count number of triggered IO events
Definition: timer_utils.h:55
double trigger_dur
triggered event duration (e.g.,stim duration)
Definition: timer_utils.h:57
int active
count down to end of a trigger with duration
Definition: timer_utils.h:59
void initialize_singlestep_timer(double tg, double idur, int ID, const char *iname, const char *poolname=nullptr)
Definition: timer_utils.h:156
double time
current time
Definition: timer_utils.h:76
int d_nxt
next output index
Definition: timer_utils.h:227
long d_time
current time instance index
Definition: timer_utils.h:77
long d_trigger_dur
discrete duration
Definition: timer_utils.h:58
double end
Time when we stop I/O in mode _EQUDIST.
Definition: timer_utils.h:222
long d_start
initial index in multiples of dt
Definition: timer_utils.h:80
double nxt
next output time in ms
Definition: timer_utils.h:226
const char * name
timer name
Definition: timer_utils.h:53
io_time
The timers we always need are indexed in this enum.
Definition: timer_utils.h:44
timer_manager(double inp_dt, double inp_start, double inp_end)
Definition: timer_utils.h:91
long d_end
discrete stop index in multiples of dt
Definition: timer_utils.h:223
t_timer
We distinguish between equidistant and non-equidistant timers with an enum.
Definition: timer_utils.h:49
std::vector< base_timer * > timers
vector containing individual timers
Definition: timer_utils.h:84
std::vector< double > trig
the times that the timer will trigger
Definition: timer_utils.h:239
bool triggered_now(int ID) const
Definition: timer_utils.h:171
double start
Time when we start I/O in mode _EQUDIST.
Definition: timer_utils.h:220
int numIOs
total number of triggers during simulation
Definition: timer_utils.h:56
int active_ticks(int ID) const
return number of ticks elapsed after trigger
Definition: timer_utils.h:195
double start
initial time (nonzero when restarting)
Definition: timer_utils.h:79
long d_intv
discrete io interval in multiples of dt
Definition: timer_utils.h:225
centralize time managment and output triggering
Definition: timer_utils.h:73
long d_end
final index in multiples of dt
Definition: timer_utils.h:82
virtual void update()=0
int add_singlestep_timer(double tg, double idur, const char *iname, const char *poolname=nullptr)
Definition: timer_utils.h:143