Debian Codegraph @

brian2/devices/cpp_standalone/brianlib/clocks.h

raw
1 #ifndef _BRIAN_CLOCKS_H
2 #define _BRIAN_CLOCKS_H
3 #include<stdlib.h>
4 #include<iostream>
5 #include<algorithm>
6 #include<brianlib/stdint_compat.h>
7 #include<math.h>
8
9 namespace {
10 inline int64_t fround(double x)
11 {
12 return (int64_t)(x+0.5);
13 };
14 };
15
16 class BaseClock
17 {
18 public:
19 int64_t *timestep;
20 double *t;
21 virtual void tick() = 0;
22 virtual void set_interval(double start, double end) = 0;
23 inline bool running() { return timestep[0]<i_end; };
24 protected:
25 int64_t i_end;
26 };
27
28 class Clock : public BaseClock
29 {
30 public:
31 double epsilon;
32 double *dt;
33 Clock(double _epsilon=1e-14) : epsilon(_epsilon) { i_end = 0;};
34 inline void tick()
35 {
36 timestep[0] += 1;
37 t[0] = timestep[0] * dt[0];
38 }
39 void set_interval(double start, double end)
40 {
41 int64_t i_start = fround(start/dt[0]);
42 double t_start = i_start*dt[0];
43 if(t_start==start || fabs(t_start-start)<=epsilon*fabs(t_start))
44 {
45 timestep[0] = i_start;
46 } else
47 {
48 timestep[0] = (int64_t)ceil(start/dt[0]);
49 }
50 i_end = fround(end/dt[0]);
51 double t_end = i_end*dt[0];
52 if(!(t_end==end || fabs(t_end-end)<=epsilon*fabs(t_end)))
53 {
54 i_end = (int64_t)ceil(end/dt[0]);
55 }
56 }
57 };
58
59 class EventClock : public BaseClock
60 {
61 public:
62 double *times;
63 size_t n_times;
64
65 EventClock() { i_end = 0;};
66 inline void tick()
67 {
68 timestep[0] += 1;
69 t[0] = times[timestep[0]];
70 }
71 void set_interval(double start, double end)
72 {
73 timestep[0] = std::lower_bound(times, times + n_times, start) - times;
74 t[0] = times[timestep[0]];
75 i_end = std::lower_bound(times, times + n_times, end) - times;
76 }
77 };
78
79 #endif