-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimesteppers.py
54 lines (43 loc) · 1.38 KB
/
timesteppers.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
"""
Timestepping schemes to serve as attributes of a `Model` instance.
"""
def _AB1_step(model, dt):
"""One step of the first-order Adams-Bashforth scheme, aka forward Euler.
"""
model.zk = model.zk + model.rhs * dt
model.rhs_m1 = model.rhs.copy()
def _AB2_step(model, dt):
"""One step of the second-order Adams-Bashforth scheme.
"""
model.zk = model.zk + (3 * model.rhs - model.rhs_m1) / 2. * dt
model.rhs_m2 = model.rhs_m1.copy()
model.rhs_m1 = model.rhs.copy()
def _AB3_step(model, dt):
"""One step of the third-order Adams-Bashforth scheme.
"""
model.zk = model.zk + (
23 * model.rhs - 16 * model.rhs_m1 + 5 * model.rhs_m2) / 12. * dt
model.rhs_m2 = model.rhs_m1.copy()
model.rhs_m1 = model.rhs.copy()
class AB3():
"""Third-order Adams-Bashforth timestepper with initialising steps.
"""
def __init__(self, model, dt, T):
self.model = model
self.dt = dt
self.T = T
self.t = 0
self.tn = 0
self.Tn = int(self.T / self.dt)
def step(self):
if self.tn == 0:
_AB1_step(self.model, self.dt)
elif self.tn == 1:
_AB2_step(self.model, self.dt)
else:
_AB3_step(self.model, self.dt)
self.tn += 1
self.t += self.dt
def extend(self, new_T):
self.T = new_T
self.Tn = int(self.T / self.dt)