forked from python-hydro/pyro2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation_null.py
More file actions
181 lines (134 loc) · 4.7 KB
/
simulation_null.py
File metadata and controls
181 lines (134 loc) · 4.7 KB
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
import importlib
import mesh.boundary as bnd
import mesh.patch as patch
from util import profile
def grid_setup(rp, ng=1):
nx = rp.get_param("mesh.nx")
ny = rp.get_param("mesh.ny")
xmin = rp.get_param("mesh.xmin")
xmax = rp.get_param("mesh.xmax")
ymin = rp.get_param("mesh.ymin")
ymax = rp.get_param("mesh.ymax")
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
xlb_type = rp.get_param("mesh.xlboundary")
xrb_type = rp.get_param("mesh.xrboundary")
ylb_type = rp.get_param("mesh.ylboundary")
yrb_type = rp.get_param("mesh.yrboundary")
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):
"""
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
try: self.tmax = rp.get_param("driver.tmax")
except:
self.tmax = None
try: self.max_steps = rp.get_param("driver.max_steps")
except:
self.max_steps = None
self.rp = rp
self.cc_data = None
self.SMALL = 1.e-12
self.solver_name = solver_name
self.problem_name = problem_name
if timers == None:
self.tc = profile.TimerCollection()
else:
self.tc = timers
try: self.verbose = self.rp.get_param("driver.verbose")
except:
self.verbose = None
self.n_num_out = 0
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
solver = importlib.import_module(self.solver_name)
problem = importlib.import_module("{}.problems.{}".format(self.solver_name, self.problem_name))
problem.finalize()