forked from jviquerat/lbm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuff.py
executable file
·78 lines (63 loc) · 2.21 KB
/
buff.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Generic imports
import os
import math
import numpy as np
###############################################
### Class Buff
### A buffer class
class Buff:
### Create object
def __init__(self,
name,
dt,
obs_cv_ct,
obs_cv_nb,
output_dir):
# Fill structure
self.name = name
self.buff = np.zeros([2])
self.avg1_buff = np.zeros([2])
self.avg2_buff = np.zeros([2])
self.avg3_buff = np.zeros([2])
self.it = 0
self.dt = dt
self.output_dir = output_dir
self.freq = 1.0
self.obs = 0.0
self.obs_cv_ct = obs_cv_ct
self.obs_cv_nb = obs_cv_nb
self.obs_cv_cnt = 0
self.obs_cv = False
### Add a value to the buffer
def add(self, value):
self.buff = np.append(self.buff, value)
self.it += 1
### Full average buffer
def f_avg(self):
return np.sum(self.buff)/len(self.buff)
### Partial average buffer
def p_avg(self, buff, i ,j):
return np.sum(buff[i:j])/(float(j-i+1))
### Compute average of moving average
def mv_avg(self):
f_avg = self.f_avg()
it_s = math.floor(3*self.it/4)
it_e = self.it
self.obs = self.p_avg(self.buff, it_s, it_e)
self.avg1_buff = np.append(self.avg1_buff, self.obs)
self.obs = self.p_avg(self.avg1_buff, it_s, it_e)
self.avg2_buff = np.append(self.avg2_buff, self.obs)
self.obs = self.p_avg(self.avg2_buff, it_s, it_e)
self.avg3_buff = np.append(self.avg3_buff, self.obs)
self.obs = self.p_avg(self.avg3_buff, it_s, it_e)
growth = 0.0
if (self.it > 5):
growth = (self.avg3_buff[-1] -
self.avg3_buff[-5])/(4.0*self.dt)
if (abs(growth) < self.obs_cv_ct):
self.obs_cv_cnt += 1
else:
self.obs_cv_cnt = 0
if (self.obs_cv_cnt > self.obs_cv_nb):
self.obs_cv = True
return self.obs, growth