-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_class.py
137 lines (118 loc) · 3.5 KB
/
model_class.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
################################################################################
# Author : Kriti Bhargava
# Created on : Tue Jan 15 13:02:03 EST 2019
# Last Modified on : Thu Sep 6 12:24:51 EDT 2018
################################################################################
"""
# An implementation of Lorenz system driven with chaotic forcing as in
# Trpevski, I., Basnarkov, L., Smilkov, D., & Kocarev, L. (2013).
# "Empirical correction techniques: Analysis and applications to chaotically driven
# low-order atmospheric models."
# Nonlinear Processes in Geophysics
# https://doi.org/10.5194/npg-20-199-2013
#
# dx1/dt = sigma * (y1-x1) +epsilon*z2
# dy1/dt = x1 * ( rho - z1 ) - y1
# dz1/dt = x1 * y1 - beta * z1 +delta*(x2-eta)
# dx2/dt = sigma * (y2-x2)
# dy2/dt = x2 * ( rho - z2 ) - y2
# dz2/dt = x2 * y2 - beta * z2
# The model can be integrated using the double approx. method described in L63
# or Runge-Kutta 4 (RK4).
"""
################################################################################
import numpy as np
from params import *
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import time
class L_model:
def __init__(self,x0,model_type,verbose=True):
self.x = np.array(x0)
self.dt = dt
self.t = 0
self.int = True
self.verbose = verbose
self.model_type = model_type
if model_type == "true":
self.dxdt = self._dxdt_true
else:
self.dxdt = self._dxdt_forced
def advance(self,tmax=1,tw=1):
"""
Integrate the model state by tmax timesteps
Saving every tw timstep
"""
x_return=np.empty((len(self.x),tmax+1))
t_return = np.empty(tmax+1)
for i in range(tmax):
x_return[:,i] = self.x
t_return[i] =self.t
self.rk4()
if self.verbose:
print self.t
print self.x
x_return[:,tmax] = self.x
t_return[tmax] =self.t
return x_return, t_return
def set_x(self,x0):
self.x = x0
def set_t(self,t):
self.t = t
def rk4(self):
"""
Runge-Kutta 4-stage integration scheme
"""
x_old = self.x
k1 = self.dxdt()
self.x = x_old +k1*self.dt/2.0
k2 = self.dxdt()
self.x = x_old+k2*self.dt/2.0
k3 = self.dxdt()
self.x = x_old+k3*self.dt
k4 = self.dxdt()
x_new = x_old + (self.dt/6)*(k1+2.0*k2+2.0*k3+k4)
self.x = x_new
self.t+=self.dt
def _dxdt_true(self):
"""
Calculating tendency
"""
if self.verbose and self.int:
self.verbose =False
print "true model"
time.sleep(2)
k = np.empty_like(self.x)
x1,y1,z1,x2,y2,z2 =self.x.T
k[0] = sigma * (y1-x1) +eps*z2
k[1] = x1 * ( rho - z1 ) - y1
k[2] = x1 * y1 - beta * z1 +delta*(x2-eta)
k[3] = sigma * (y2-x2)
k[4] = x2 * ( rho - z2 ) - y2
k[5] = x2 * y2 - beta * z2
return k
def _dxdt_forced(self):
"""
Calculating tendency
"""
alpha,gamma = get_forcing(self.model_type)
if self.verbose and self.int:
self.verbose =False
print "forced model"
print "alpha =", alpha
print "gamma =", gamma
k = np.empty_like(self.x)
x1,y1,z1 =self.x.T
k[0] = sigma * (y1-x1) + alpha
k[1] = x1 * ( rho - z1 ) - y1
k[2] = x1 * y1 - beta * z1 +gamma
return k
if __name__ == '__main__':
#x0 = [0.0,1.0,0.0,0.0,1.0,0.0]
x0 = np.load("IC_true_attr.npy")[0:3]
print x0
timesteps = 1000000
model = L_model(x0,"set_forcing",True)
x,t = model.advance(tmax=timesteps)
np.save("test_run",x)