File: clocks.h

package info (click to toggle)
brian 2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,872 kB
  • sloc: python: 51,820; cpp: 2,033; makefile: 108; sh: 72
file content (51 lines) | stat: -rw-r--r-- 1,106 bytes parent folder | download | duplicates (3)
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