1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#ifndef _BRIAN_CLOCKS_H
#define _BRIAN_CLOCKS_H
#include<stdlib.h>
#include<iostream>
#include<brianlib/stdint_compat.h>
#include<math.h>
namespace {
inline int64_t fround(double x)
{
return (int64_t)(x+0.5);
};
};
class Clock
{
public:
double epsilon;
double *dt;
int64_t *timestep;
double *t;
Clock(double _epsilon=1e-14) : epsilon(_epsilon) { i_end = 0;};
inline void tick()
{
timestep[0] += 1;
t[0] = timestep[0] * dt[0];
}
inline bool running() { return timestep[0]<i_end; };
void set_interval(double start, double end)
{
int64_t i_start = fround(start/dt[0]);
double t_start = i_start*dt[0];
if(t_start==start || fabs(t_start-start)<=epsilon*fabs(t_start))
{
timestep[0] = i_start;
} else
{
timestep[0] = (int64_t)ceil(start/dt[0]);
}
i_end = fround(end/dt[0]);
double t_end = i_end*dt[0];
if(!(t_end==end || fabs(t_end-end)<=epsilon*fabs(t_end)))
{
i_end = (int64_t)ceil(end/dt[0]);
}
}
private:
int64_t i_end;
};
#endif
|