forked from python-hydro/pyro2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation_null.py
254 lines (200 loc) · 6.8 KB
/
simulation_null.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
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
import h5py
import importlib
import mesh.boundary as bnd
import mesh.patch as patch
from util import msg, profile
def grid_setup(rp, ng=1):
nx = rp.get_param("mesh.nx")
ny = rp.get_param("mesh.ny")
try:
xmin = rp.get_param("mesh.xmin")
except KeyError:
xmin = 0.0
msg.warning("mesh.xmin not set, defaulting to 0.0")
try:
xmax = rp.get_param("mesh.xmax")
except KeyError:
xmax = 1.0
msg.warning("mesh.xmax not set, defaulting to 1.0")
try:
ymin = rp.get_param("mesh.ymin")
except KeyError:
ymin = 0.0
msg.warning("mesh.ymin not set, defaulting to 0.0")
try:
ymax = rp.get_param("mesh.ymax")
except KeyError:
ymax = 1.0
msg.warning("mesh.ynax not set, defaulting to 1.0")
my_grid = patch.Grid2d(nx, ny,
xmin=xmin, xmax=xmax,
ymin=ymin, ymax=ymax, ng=ng)
return my_grid
def bc_setup(rp):
# first figure out the BCs
try:
xlb_type = rp.get_param("mesh.xlboundary")
except KeyError:
xlb_type = "periodic"
msg.warning("mesh.xlboundary is not set, defaulting to periodic")
try:
xrb_type = rp.get_param("mesh.xrboundary")
except KeyError:
xrb_type = "periodic"
msg.warning("mesh.xrboundary is not set, defaulting to periodic")
try:
ylb_type = rp.get_param("mesh.ylboundary")
except KeyError:
ylb_type = "periodic"
msg.warning("mesh.ylboundary is not set, defaulting to periodic")
try:
yrb_type = rp.get_param("mesh.yrboundary")
except KeyError:
yrb_type = "periodic"
msg.warning("mesh.yrboundary is not set, defaulting to periodic")
bc = bnd.BC(xlb=xlb_type, xrb=xrb_type,
ylb=ylb_type, yrb=yrb_type)
# if we are reflecting, we need odd reflection in the normal
# directions for the velocity
bc_xodd = bnd.BC(xlb=xlb_type, xrb=xrb_type,
ylb=ylb_type, yrb=yrb_type,
odd_reflect_dir="x")
bc_yodd = bnd.BC(xlb=xlb_type, xrb=xrb_type,
ylb=ylb_type, yrb=yrb_type,
odd_reflect_dir="y")
return bc, bc_xodd, bc_yodd
class NullSimulation(object):
def __init__(self, solver_name, problem_name, rp, timers=None, data_class=patch.CellCenterData2d):
"""
Initialize the Simulation object
Parameters
----------
problem_name : str
The name of the problem we wish to run. This should
correspond to one of the modules in advection/problems/
rp : RuntimeParameters object
The runtime parameters for the simulation
timers : TimerCollection object, optional
The timers used for profiling this simulation
"""
self.n = 0
self.dt = -1.e33
self.old_dt = -1.e33
self.data_class = data_class
try:
self.tmax = rp.get_param("driver.tmax")
except (AttributeError, KeyError):
self.tmax = None
try:
self.max_steps = rp.get_param("driver.max_steps")
except (AttributeError, KeyError):
self.max_steps = None
self.rp = rp
self.cc_data = None
self.particles = None
self.SMALL = 1.e-12
self.solver_name = solver_name
self.problem_name = problem_name
if timers is None:
self.tc = profile.TimerCollection()
else:
self.tc = timers
try:
self.verbose = self.rp.get_param("driver.verbose")
except (AttributeError, KeyError):
self.verbose = 0
self.n_num_out = 0
# plotting
self.cm = "viridis"
def finished(self):
"""
is the simulation finished based on time or the number of steps
"""
return self.cc_data.t >= self.tmax or self.n >= self.max_steps
def do_output(self):
"""
is it time to output?
"""
dt_out = self.rp.get_param("io.dt_out")
n_out = self.rp.get_param("io.n_out")
do_io = self.rp.get_param("io.do_io")
is_time = self.cc_data.t >= (self.n_num_out + 1)*dt_out or self.n % n_out == 0
if is_time and do_io == 1:
self.n_num_out += 1
return True
else:
return False
def initialize(self):
pass
def method_compute_timestep(self):
"""
the method-specific timestep code
"""
pass
def compute_timestep(self):
"""
a generic wrapper for computing the timestep that respects the
driver parameters on timestepping
"""
init_tstep_factor = self.rp.get_param("driver.init_tstep_factor")
max_dt_change = self.rp.get_param("driver.max_dt_change")
fix_dt = self.rp.get_param("driver.fix_dt")
# get the timestep
if fix_dt > 0.0:
self.dt = fix_dt
else:
self.method_compute_timestep()
if self.n == 0:
self.dt = init_tstep_factor*self.dt
else:
self.dt = min(max_dt_change*self.dt_old, self.dt)
self.dt_old = self.dt
if self.cc_data.t + self.dt > self.tmax:
self.dt = self.tmax - self.cc_data.t
def preevolve(self):
"""
Do any necessary evolution before the main evolve loop. This
is not needed for advection
"""
pass
def evolve(self):
# increment the time
self.cc_data.t += self.dt
self.n += 1
def dovis(self):
pass
def finalize(self):
"""
Do any final clean-ups for the simulation and call the problem's
finalize() method.
"""
# there should be a cleaner way of doing this
problem = importlib.import_module("{}.problems.{}".format(self.solver_name, self.problem_name))
problem.finalize()
def write(self, filename):
"""
Output the state of the simulation to an HDF5 file for plotting
"""
if not filename.endswith(".h5"):
filename += ".h5"
with h5py.File(filename, "w") as f:
# main attributes
f.attrs["solver"] = self.solver_name
f.attrs["problem"] = self.problem_name
f.attrs["time"] = self.cc_data.t
f.attrs["nsteps"] = self.n
self.cc_data.write_data(f)
if self.particles is not None:
self.particles.write_particles(f)
self.rp.write_params(f)
self.write_extras(f)
def write_extras(self, f):
"""
write out any extra simulation-specific stuff
"""
pass
def read_extras(self, f):
"""
read in any simulation-specific data from an h5py file object f
"""
pass