forked from pressel/pycles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulation3d.pyx
305 lines (253 loc) · 14.2 KB
/
Simulation3d.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import time
import numpy as np
cimport numpy as np
from Initialization import InitializationFactory, AuxillaryVariables
from Thermodynamics import ThermodynamicsFactory
from Microphysics import MicrophysicsFactory
from AuxiliaryStatistics import AuxiliaryStatistics
from ConditionalStatistics import ConditionalStatistics
from Thermodynamics cimport LatentHeat
cimport ParallelMPI
cimport Grid
cimport PrognosticVariables
cimport DiagnosticVariables
cimport ScalarAdvection
cimport MomentumAdvection
cimport SGS
cimport ScalarDiffusion
cimport MomentumDiffusion
cimport ReferenceState
cimport PressureSolver
cimport TimeStepping
cimport Kinematics
cimport Damping
cimport NetCDFIO
cimport VisualizationOutput
cimport Surface
cimport Forcing
cimport Radiation
cimport Radiation_RRTM
cimport Restart
class Simulation3d:
def __init__(self, namelist):
return
def initialize(self, namelist):
self.Pa = ParallelMPI.ParallelMPI(namelist)
self.Gr = Grid.Grid(namelist, self.Pa)
self.PV = PrognosticVariables.PrognosticVariables(self.Gr)
self.Ke = Kinematics.Kinematics()
self.DV = DiagnosticVariables.DiagnosticVariables()
self.Pr = PressureSolver.PressureSolver()
self.LH = LatentHeat(namelist, self.Pa)
self.Micro = MicrophysicsFactory(namelist, self.LH, self.Pa)
self.SA = ScalarAdvection.ScalarAdvection(namelist, self.LH, self.Pa)
self.MA = MomentumAdvection.MomentumAdvection(namelist, self.Pa)
self.SGS = SGS.SGS(namelist)
self.SD = ScalarDiffusion.ScalarDiffusion(namelist, self.LH, self.DV, self.Pa)
self.MD = MomentumDiffusion.MomentumDiffusion(self.DV, self.Pa)
self.Th = ThermodynamicsFactory(namelist, self.Micro, self.LH, self.Pa)
self.Ref = ReferenceState.ReferenceState(self.Gr)
self.Sur = Surface.Surface(namelist, self.LH, self.Pa)
self.Fo = Forcing.Forcing(namelist, self.Pa)
self.Ra = Radiation.Radiation(namelist, self.Pa)
self.Ra2 = Radiation_RRTM.Radiation_RRTM(namelist, self.Pa)
self.StatsIO = NetCDFIO.NetCDFIO_Stats()
self.FieldsIO = NetCDFIO.NetCDFIO_Fields()
self.CondStatsIO = NetCDFIO.NetCDFIO_CondStats()
self.Restart = Restart.Restart(namelist, self.Pa)
self.VO = VisualizationOutput.VisualizationOutput(namelist, self.Pa)
self.Damping = Damping.Damping(namelist, self.Pa)
self.TS = TimeStepping.TimeStepping()
# Add new prognostic variables
self.PV.add_variable('u', 'm/s', "sym", "velocity", self.Pa)
self.PV.set_velocity_direction('u', 0, self.Pa)
self.PV.add_variable('v', 'm/s', "sym", "velocity", self.Pa)
self.PV.set_velocity_direction('v', 1, self.Pa)
self.PV.add_variable('w', 'm/s', "asym", "velocity", self.Pa)
self.PV.set_velocity_direction('w', 2, self.Pa)
AuxillaryVariables(namelist, self.PV, self.DV, self.Pa)
self.StatsIO.initialize(namelist, self.Gr, self.Pa)
self.FieldsIO.initialize(namelist, self.Pa)
self.CondStatsIO.initialize(namelist, self.Gr, self.Pa)
self.Aux = AuxiliaryStatistics(namelist)
self.CondStats = ConditionalStatistics(namelist)
self.Restart.initialize()
self.VO.initialize()
self.Th.initialize(self.Gr, self.PV, self.DV, self.StatsIO, self.Pa)
self.Micro.initialize(self.Gr, self.PV, self.DV, self.StatsIO, self.Pa)
self.SGS.initialize(self.Gr,self.PV,self.StatsIO, self.Pa)
self.PV.initialize(self.Gr, self.StatsIO, self.Pa)
self.Ke.initialize(self.Gr, self.StatsIO, self.Pa)
self.SA.initialize(self.Gr,self.PV, self.StatsIO, self.Pa)
self.MA.initialize(self.Gr,self.PV, self.StatsIO, self.Pa)
self.SD.initialize(self.Gr,self.PV,self.DV,self.StatsIO,self.Pa)
self.MD.initialize(self.Gr,self.PV,self.DV,self.StatsIO, self.Pa)
self.TS.initialize(namelist,self.PV,self.Pa)
if self.Restart.is_restart_run:
self.Pa.root_print('This run is being restarted!')
try:
self.Restart.read(self.Pa)
except:
self.Pa.root_print('Could not read restart file')
self.Pa.kill()
self.TS.t = self.Restart.restart_data['TS']['t']
self.TS.dt = self.Restart.restart_data['TS']['dt']
self.Ref.init_from_restart(self.Gr, self.Restart)
self.PV.init_from_restart(self.Gr, self.Restart)
self.StatsIO.last_output_time = self.Restart.restart_data['last_stats_output']
self.CondStatsIO.last_output_time = self.Restart.restart_data['last_condstats_output']
self.FieldsIO.last_output_time = self.Restart.restart_data['last_fields_output']
self.Restart.last_restart_time = self.Restart.restart_data['last_restart_time']
self.VO.last_vis_time = self.Restart.restart_data['last_vis_time']
self.Restart.free_memory()
else:
self.Pa.root_print('This is not a restart run!')
SetInitialConditions = InitializationFactory(namelist)
SetInitialConditions(self.Gr, self.PV, self.Ref, self.Th, self.StatsIO, self.Pa)
del SetInitialConditions
self.Sur.initialize(self.Gr, self.Ref, self.DV, self.StatsIO, self.Pa)
self.Fo.initialize(self.Gr, self.StatsIO, self.Pa)
self.Ra.initialize(self.Gr, self.StatsIO,self.Pa)
self.Ra2.initialize(self.Gr, self.StatsIO,self.Pa)
self.Pr.initialize(namelist, self.Gr, self.Ref, self.DV, self.Pa)
self.DV.initialize(self.Gr, self.StatsIO, self.Pa)
self.Damping.initialize(self.Gr)
self.Aux.initialize(namelist, self.Gr, self.PV, self.DV, self.StatsIO, self.Pa)
self.CondStats.initialize(namelist, self.Gr, self.PV, self.DV, self.CondStatsIO, self.Pa)
return
def run(self):
cdef PrognosticVariables.PrognosticVariables PV_ = self.PV
cdef DiagnosticVariables.DiagnosticVariables DV_ = self.DV
PV_.Update_all_bcs(self.Gr, self.Pa)
cdef LatentHeat LH_ = self.LH
cdef Grid.Grid GR_ = self.Gr
cdef ParallelMPI.ParallelMPI PA_ = self.Pa
cdef int rk_step
# DO First Output
self.Th.update(self.Gr, self.Ref, PV_, DV_)
#Do IO if not a restarted run
if not self.Restart.is_restart_run:
self.force_io()
while (self.TS.t < self.TS.t_max):
time1 = time.time()
for self.TS.rk_step in xrange(self.TS.n_rk_steps):
self.Ke.update(self.Gr,PV_)
self.Th.update(self.Gr,self.Ref,PV_,DV_)
self.Micro.update(self.Gr, self.Ref, PV_, DV_, self.TS, self.Pa )
self.SA.update(self.Gr,self.Ref,PV_, DV_, self.Pa)
self.MA.update(self.Gr,self.Ref,PV_,self.Pa)
self.Sur.update(self.Gr,self.Ref,self.PV, self.DV,self.Pa,self.TS)
self.SGS.update(self.Gr,self.DV,self.PV, self.Ke,self.Pa)
self.Damping.update(self.Gr,self.PV,self.Pa)
self.SD.update(self.Gr,self.Ref,self.PV,self.DV)
self.MD.update(self.Gr,self.Ref,self.PV,self.DV,self.Ke)
self.Fo.update(self.Gr, self.Ref, self.PV, self.DV, self.Pa)
self.Ra.update(self.Gr, self.Ref, self.PV, self.DV, self.Pa)
self.TS.update(self.Gr, self.PV, self.Pa)
PV_.Update_all_bcs(self.Gr, self.Pa)
self.Pr.update(self.Gr, self.Ref, self.DV, self.PV, self.Pa)
self.TS.adjust_timestep(self.Gr, self.PV, self.DV,self.Pa)
self.io()
#PV_.debug(self.Gr,self.Ref,self.StatsIO,self.Pa)
time2 = time.time()
self.Pa.root_print('T = ' + str(self.TS.t) + ' dt = ' + str(self.TS.dt) +
' cfl_max = ' + str(self.TS.cfl_max) + ' walltime = ' + str(time2 - time1))
self.Restart.cleanup()
return
def io(self):
cdef:
double fields_dt = 0.0
double stats_dt = 0.0
double condstats_dt = 0.0
double restart_dt = 0.0
double vis_dt = 0.0
double min_dt = 0.0
if self.TS.t > 0 and self.TS.rk_step == self.TS.n_rk_steps - 1:
# Adjust time step for output if necessary
fields_dt = self.FieldsIO.last_output_time + self.FieldsIO.frequency - self.TS.t
stats_dt = self.StatsIO.last_output_time + self.StatsIO.frequency - self.TS.t
condstats_dt = self.CondStatsIO.last_output_time + self.CondStatsIO.frequency - self.TS.t
restart_dt = self.Restart.last_restart_time + self.Restart.frequency - self.TS.t
vis_dt = self.VO.last_vis_time + self.VO.frequency - self.TS.t
dts = np.array([fields_dt, stats_dt, condstats_dt, restart_dt, vis_dt,
self.TS.dt, self.TS.dt_max, self.VO.frequency, self.Restart.frequency,
self.StatsIO.frequency, self.CondStatsIO.frequency, self.FieldsIO.frequency])
self.TS.dt = np.amin(dts[dts > 0.0])
# If time to ouptut fields do output
if self.FieldsIO.last_output_time + self.FieldsIO.frequency == self.TS.t:
self.Pa.root_print('Doing 3D FieldIO')
self.FieldsIO.last_output_time = self.TS.t
self.FieldsIO.update(self.Gr, self.PV, self.DV, self.TS, self.Pa)
self.FieldsIO.dump_prognostic_variables(self.Gr, self.PV)
self.FieldsIO.dump_diagnostic_variables(self.Gr, self.DV, self.Pa)
self.Pa.root_print('Finished Doing 3D FieldIO')
# If time to ouput stats do output
if self.StatsIO.last_output_time + self.StatsIO.frequency == self.TS.t:
self.Pa.root_print('Doing StatsIO')
self.StatsIO.last_output_time = self.TS.t
self.StatsIO.open_files(self.Pa)
self.StatsIO.write_simulation_time(self.TS.t, self.Pa)
self.Micro.stats_io(self.Gr, self.Ref, self.PV, self.DV, self.StatsIO, self.Pa) # do Micro.stats_io prior to DV.stats_io to get sedimentation velocity only in output
self.PV.stats_io(self.Gr, self.Ref, self.StatsIO, self.Pa)
self.DV.stats_io(self.Gr, self.StatsIO, self.Pa)
self.Fo.stats_io(self.Gr, self.Ref, self.PV, self.DV, self.StatsIO, self.Pa)
self.Th.stats_io(self.Gr, self.Ref, self.PV, self.DV, self.StatsIO, self.Pa)
self.Sur.stats_io(self.Gr, self.StatsIO, self.Pa)
self.SGS.stats_io(self.Gr,self.DV,self.PV,self.Ke,self.StatsIO,self.Pa)
self.SA.stats_io(self.Gr, self.PV, self.StatsIO, self.Pa)
self.MA.stats_io(self.Gr, self.PV, self.StatsIO, self.Pa)
self.SD.stats_io(self.Gr, self.Ref,self.PV, self.DV, self.StatsIO, self.Pa)
self.MD.stats_io(self.Gr, self.PV, self.DV, self.Ke, self.StatsIO, self.Pa)
self.Ke.stats_io(self.Gr,self.Ref,self.PV,self.StatsIO,self.Pa)
self.Aux.stats_io(self.Gr, self.Ref, self.PV, self.DV, self.MA, self.MD, self.StatsIO, self.Pa)
self.StatsIO.close_files(self.Pa)
self.Pa.root_print('Finished Doing StatsIO')
# If time to ouput stats do output
if self.CondStatsIO.last_output_time + self.CondStatsIO.frequency == self.TS.t:
self.Pa.root_print('Doing CondStatsIO')
self.CondStatsIO.last_output_time = self.TS.t
self.CondStatsIO.write_condstat_time(self.TS.t, self.Pa)
self.CondStats.stats_io(self.Gr, self.Ref, self.PV, self.DV, self.CondStatsIO, self.Pa)
self.Pa.root_print('Finished Doing CondStatsIO')
if self.VO.last_vis_time + self.VO.frequency == self.TS.t:
self.VO.last_vis_time = self.TS.t
self.VO.write(self.Gr, self.Ref, self.PV, self.DV, self.Pa)
if self.Restart.last_restart_time + self.Restart.frequency == self.TS.t:
self.Pa.root_print('Dumping Restart Files!')
self.Restart.last_restart_time = self.TS.t
self.Restart.restart_data['last_stats_output'] = self.StatsIO.last_output_time
self.Restart.restart_data['last_fields_output'] = self.FieldsIO.last_output_time
self.Restart.restart_data['last_condstats_output'] = self.CondStatsIO.last_output_time
self.Restart.restart_data['last_vis_time'] = self.VO.last_vis_time
self.Gr.restart(self.Restart)
self.Ref.restart(self.Gr, self.Restart)
self.PV.restart(self.Gr, self.Restart)
self.TS.restart(self.Restart)
self.Restart.write(self.Pa)
self.Pa.root_print('Finished Dumping Restart Files!')
return
def force_io(self):
# output stats here
self.Pa.root_print('Doing 3D FiledIO')
self.FieldsIO.update(self.Gr, self.PV, self.DV, self.TS, self.Pa)
self.FieldsIO.dump_prognostic_variables(self.Gr, self.PV)
self.FieldsIO.dump_diagnostic_variables(self.Gr, self.DV, self.Pa)
self.Pa.root_print('Finished Doing 3D FieldIO')
self.StatsIO.open_files(self.Pa)
self.StatsIO.write_simulation_time(self.TS.t, self.Pa)
self.PV.stats_io(self.Gr, self.Ref, self.StatsIO, self.Pa)
self.DV.stats_io(self.Gr, self.StatsIO, self.Pa)
self.Fo.stats_io(
self.Gr, self.Ref, self.PV, self.DV, self.StatsIO, self.Pa)
self.Th.stats_io(self.Gr, self.Ref, self.PV, self.DV, self.StatsIO, self.Pa)
self.Micro.stats_io(self.Gr, self.Ref, self.PV, self.DV, self.StatsIO, self.Pa)
self.Sur.stats_io(self.Gr, self.StatsIO, self.Pa)
self.SGS.stats_io(self.Gr,self.DV,self.PV,self.Ke ,self.StatsIO, self.Pa)
self.SA.stats_io(self.Gr, self.PV, self.StatsIO, self.Pa)
self.MA.stats_io(self.Gr, self.PV, self.StatsIO, self.Pa)
self.SD.stats_io(self.Gr, self.Ref,self.PV, self.DV, self.StatsIO, self.Pa)
self.MD.stats_io(self.Gr, self.PV, self.DV, self.Ke, self.StatsIO, self.Pa)
self.Ke.stats_io(self.Gr, self.Ref, self.PV, self.StatsIO, self.Pa)
self.Aux.stats_io(self.Gr, self.Ref, self.PV, self.DV, self.MA, self.MD, self.StatsIO, self.Pa)
self.StatsIO.close_files(self.Pa)
return