forked from pressel/pycles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeStepping.pyx
251 lines (202 loc) · 9.36 KB
/
TimeStepping.pyx
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!python
#cython: boundscheck=False
#cython: wraparound=False
#cython: initializedcheck=False
#cython: cdivision=True
cimport ParallelMPI as ParallelMPI
cimport PrognosticVariables as PrognosticVariables
cimport Grid as Grid
cimport mpi4py.mpi_c as mpi
import numpy as np
cimport numpy as np
import cython
from libc.math cimport fmin, fmax, fabs
cdef class TimeStepping:
def __init__(self):
return
cpdef initialize(self,namelist,PrognosticVariables.PrognosticVariables PV, ParallelMPI.ParallelMPI Pa):
#Get the time stepping potions from the name list
try:
self.ts_type = namelist['time_stepping']['ts_type']
except:
Pa.root_print('ts_type not given in namelist')
Pa.root_print('Killing simulation now')
Pa.kill()
try:
self.dt = namelist['time_stepping']['dt_initial']
except:
Pa.root_print('dt_initial (initial time step) not given in namelist so taking defualt value dt_initail = 1.0')
self.dt = 1.0
try:
self.dt_max = namelist['time_stepping']['dt_max']
except:
Pa.root_print('dt_max (maximum permissible time step) not given in namelist so taking default value dt_max =10.0')
self.dt_max = 10.0
try:
self.t = namelist['time_stepping']['t']
except:
Pa.root_print('t (initial time) not given in namelist so taking default value t = 0')
self.t = 0.0
try:
self.cfl_limit = namelist['time_stepping']['cfl_limit']
except:
Pa.root_print('cfl_limit (maximum permissible cfl number) not given in namelist so taking default value cfl_max=0.7')
self.cfl_limit = 0.7
try:
self.t_max = namelist['time_stepping']['t_max']
except:
Pa.root_print('t_max (time at end of simulation) not given in name list! Killing Simulation Now')
Pa.kill()
#Now initialize the correct time stepping routine
if self.ts_type == 2:
self.initialize_second(PV)
elif self.ts_type == 3:
self.initialize_third(PV)
elif self.ts_type == 4:
self.initialize_fourth(PV)
else:
Pa.root_print('Invalid ts_type: ' + str(self.ts_type))
Pa.root_print('Killing simulation now')
Pa.kill()
return
cpdef update(self, Grid.Grid Gr, PrognosticVariables.PrognosticVariables PV, ParallelMPI.ParallelMPI Pa):
if self.ts_type == 2:
self.update_second(Gr,PV)
elif self.ts_type == 3:
self.update_third(Gr,PV)
elif self.ts_type == 4:
self.update_fourth(Gr,PV)
else:
Pa.root_print('Time stepping option not found ts_type = ' + str(self.ts_type))
Pa.root_print('Killing Simulation Now!')
Pa.kill()
return
cpdef adjust_timestep(self,Grid.Grid Gr, PrognosticVariables.PrognosticVariables PV, ParallelMPI.ParallelMPI Pa):
#Compute the CFL number and diffusive stability criterion
if self.rk_step == self.n_rk_steps - 1:
self.compute_cfl_max(Gr, PV, Pa)
self.dt = self.cfl_time_step()
#Diffusive limiting not yet implemented
if self.t + self.dt > self.t_max:
self.dt = self.t_max - self.t
return
cpdef update_second(self, Grid.Grid Gr, PrognosticVariables.PrognosticVariables PV):
cdef:
Py_ssize_t i
with nogil:
if self.rk_step == 0:
for i in xrange(Gr.dims.npg*PV.nv):
self.value_copies[0,i] = PV.values[i]
PV.values[i] += PV.tendencies[i]*self.dt
PV.tendencies[i] = 0.0
else:
for i in xrange(Gr.dims.npg*PV.nv):
PV.values[i] = 0.5 * (self.value_copies[0,i] + PV.values[i] + PV.tendencies[i] * self.dt)
PV.tendencies[i] = 0.0
self.t += self.dt
return
cpdef update_third(self, Grid.Grid Gr, PrognosticVariables.PrognosticVariables PV):
cdef:
Py_ssize_t i
with nogil:
if self.rk_step == 0:
for i in xrange(Gr.dims.npg*PV.nv):
self.value_copies[0,i] = PV.values[i]
PV.values[i] += PV.tendencies[i]*self.dt
PV.tendencies[i] = 0.0
elif self.rk_step == 1:
for i in xrange(Gr.dims.npg*PV.nv):
PV.values[i] = 0.75 * self.value_copies[0,i] + 0.25*(PV.values[i] + PV.tendencies[i]*self.dt)
PV.tendencies[i] = 0.0
else:
for i in xrange(Gr.dims.npg*PV.nv):
PV.values[i] = (1.0/3.0) * self.value_copies[0,i] + (2.0/3.0)*(PV.values[i] + PV.tendencies[i]*self.dt)
PV.tendencies[i] = 0.0
self.t += self.dt
return
cpdef update_fourth(self, Grid.Grid Gr, PrognosticVariables.PrognosticVariables PV):
cdef:
Py_ssize_t i
with nogil:
if self.rk_step == 0:
for i in xrange(Gr.dims.npg*PV.nv):
self.value_copies[0,i] = PV.values[i]
PV.values[i] += 0.391752226571890 * PV.tendencies[i]*self.dt
PV.tendencies[i] = 0.0
elif self.rk_step == 1:
for i in xrange(Gr.dims.npg*PV.nv):
PV.values[i] = (0.444370493651235*self.value_copies[0,i] + 0.555629506348765*PV.values[i]
+ 0.368410593050371*PV.tendencies[i]*self.dt )
PV.tendencies[i] = 0.0
elif self.rk_step == 2:
for i in xrange(Gr.dims.npg*PV.nv):
self.value_copies[1,i] = PV.values[i]
PV.values[i] = (0.620101851488403*self.value_copies[0,i] + 0.379898148511597*PV.values[i]
+ 0.251891774271694*PV.tendencies[i]*self.dt)
PV.tendencies[i] = 0.0
elif self.rk_step == 3:
for i in xrange(Gr.dims.npg*PV.nv):
self.value_copies[2,i] = PV.values[i]
self.tendency_copies[0,i] = PV.tendencies[i]
PV.values[i] = (0.178079954393132*self.value_copies[0,i] + 0.821920045606868*PV.values[i]
+0.544974750228521*PV.tendencies[i]*self.dt)
PV.tendencies[i] = 0.0
else:
for i in xrange(Gr.dims.npg*PV.nv):
PV.values[i] = (0.517231671970585*self.value_copies[1,i]
+ 0.096059710526147*self.value_copies[2,i] +0.063692468666290*self.tendency_copies[0,i]*self.dt
+ 0.386708617503269*PV.values[i] + 0.226007483236906*PV.tendencies[i]*self.dt)
PV.tendencies[i] = 0.0
self.t += self.dt
return
cdef void initialize_second(self,PrognosticVariables.PrognosticVariables PV):
self.rk_step = 0
self.n_rk_steps = 2
#Initialize storage
self.value_copies = np.zeros((1,PV.values.shape[0]),dtype=np.double,order='c')
self.tendency_copies = None
return
cdef void initialize_third(self,PrognosticVariables.PrognosticVariables PV):
self.rk_step = 0
self.n_rk_steps = 3
#Initialize storage
self.value_copies = np.zeros((1,PV.values.shape[0]),dtype=np.double,order='c')
self.tendency_copies = None
return
cdef void initialize_fourth(self,PrognosticVariables.PrognosticVariables PV):
self.rk_step = 0
self.n_rk_steps = 5
#Initialize storage
self.value_copies = np.zeros((3,PV.values.shape[0]),dtype=np.double,order='c')
self.tendency_copies = np.zeros((1,PV.values.shape[0]),dtype=np.double,order='c')
return
cdef void compute_cfl_max(self,Grid.Grid Gr, PrognosticVariables.PrognosticVariables PV, ParallelMPI.ParallelMPI Pa):
cdef:
double cfl_max_local = -9999.0
double [3] dxi = Gr.dims.dxi
long u_shift = PV.get_varshift(Gr,'u')
long v_shift = PV.get_varshift(Gr,'v')
long w_shift = PV.get_varshift(Gr,'w')
long imin = Gr.dims.gw
long jmin = Gr.dims.gw
long kmin = Gr.dims.gw
long imax = Gr.dims.nlg[0] - Gr.dims.gw
long jmax = Gr.dims.nlg[1] - Gr.dims.gw
long kmax = Gr.dims.nlg[2] - Gr.dims.gw
long istride = Gr.dims.nlg[1] * Gr.dims.nlg[2]
long jstride = Gr.dims.nlg[2]
long i,j,k, ijk, ishift, jshift
with nogil:
for i in xrange(imin,imax):
ishift = i * istride
for j in xrange(jmin,jmax):
jshift = j * jstride
for k in xrange(kmin,kmax):
ijk = ishift + jshift + k
cfl_max_local = fmax(cfl_max_local, self.dt * (fabs(PV.values[u_shift + ijk])*dxi[0] + fabs(PV.values[v_shift+ijk])*dxi[1] + fabs(PV.values[w_shift+ijk])*dxi[2]))
mpi.MPI_Allreduce(&cfl_max_local,&self.cfl_max,1,
mpi.MPI_DOUBLE,mpi.MPI_MAX,Pa.comm_world)
self.cfl_max += 1e-11
return
cdef inline double cfl_time_step(self):
return fmin(self.dt_max,self.cfl_limit/(self.cfl_max/self.dt))