-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaussian_bump.py
executable file
·343 lines (261 loc) · 10.9 KB
/
gaussian_bump.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python3
# encoding: utf-8
# SPDX-License-Identifier: MIT
# Copyright (c) 2021 ETH Zurich, Luc Grosheintz-Laval
import numpy as np
from matplotlib import rcParams
# matplotlib.rc('text', usetex = True)
rcParams.update({ 'font.family': 'sans-serif',
'font.size': 15,
'xtick.labelsize': 15,
'ytick.labelsize': 15,
'figure.autolayout': True,
'axes.formatter.limits': (-1, 3)})
import matplotlib.pyplot as plt
import pickle
from morinth.equilibrium import IsothermalEquilibrium, IsentropicEquilibrium
from morinth.euler_experiment import EulerExperiment
from morinth.boundary_conditions import Outflow, HydrostaticOutflow
from morinth.visualize import EquilibriumGraphs, DensityGraph, ConvergencePlot, DumpToDisk
from morinth.visualize import CombineIO, EulerGraphs, Markers
from morinth.weno import OptimalWENO, EquilibriumStencil
from morinth.source_terms import BalancedSourceTerm, UnbalancedSourceTerm
from morinth.math_tools import gaussian, l1_error, l1rel_error, linf_error, convergence_rate
from morinth.time_keeper import FixedSteps, PlotNever, PlotLast
from morinth.quadrature import GaussLegendre
from morinth.coding_tools import with_default
from morinth.latex_tables import LatexConvergenceTable
from morinth.euler import LinearGravity, PointMassGravity
from morinth.gaussian_bump import GaussianBumpIC, ExtremeGaussianBumpIC
class EquilibriumExperiment(EulerExperiment):
@property
def gravity(self):
return 1.0
@property
def n_cells(self):
return self._n_cells
@n_cells.setter
def n_cells(self, rhs):
self._n_cells = rhs
@property
def order(self):
return 5
@property
def weno(self):
mode = self.well_balancing
if mode == "wb_o2" or mode == "wb_o4":
return OptimalWENO(EquilibriumStencil(self.grid, self.equilibrium, self.model))
elif mode == "naive":
return OptimalWENO()
else:
raise Exception("Wrong `well_balancing`.")
@property
def thermodynamic_equilibrium(self):
label = self._thermodynamic_equilibrium
if label == "isentropic":
return IsentropicEquilibrium
elif label == "isothermal":
return IsothermalEquilibrium
else:
raise Exception("Unknown equilibrium.")
@thermodynamic_equilibrium.setter
def thermodynamic_equilibrium(self, rhs):
self._thermodynamic_equilibrium = rhs
@property
def boundary_condition(self):
equilibrium = self.thermodynamic_equilibrium(self.grid, self.model)
return HydrostaticOutflow(self.grid, equilibrium)
@property
def source(self):
mode = self.well_balancing
if mode == "wb_o2" or mode == "wb_o4":
source_order = 4.0 if mode == "wb_o4" else 2.0
return BalancedSourceTerm(self.grid, self.model,
self.equilibrium, source_order)
elif mode == "naive":
return UnbalancedSourceTerm(self.grid, self.model)
else:
raise Exception("Wrong `well_balancing`.")
@property
def well_balancing(self):
return self._well_balancing
@well_balancing.setter
def well_balancing(self, rhs):
self._well_balancing = rhs
@property
def equilibrium(self):
mode = self.well_balancing
if mode == "wb_o2" or mode == "wb_o4":
return self.thermodynamic_equilibrium(self.grid, self.model)
elif mode == "naive":
return None
else:
assert Exception("Wrong `well_balancing`.")
@property
def visualize(self):
back_ground = self.initial_condition.back_ground(self.grid)
graphs = EquilibriumGraphs(self.grid,
"img/" + self.output_filename,
self.model,
back_ground)
raw_data = DumpToDisk(self.grid, "data/" + self.output_filename, back_ground)
return CombineIO(graphs, raw_data)
@property
def output_filename(self):
pattern = self.base_filename + "-{:s}_{:s}_res{:05d}"
return pattern.format(self._thermodynamic_equilibrium,
self.well_balancing,
self.n_cells)
class GaussianBump(EquilibriumExperiment):
@property
def final_time(self):
return 8.0
@property
def specific_gas_constant(self):
return 0.01
@property
def domain(self):
return np.array([0, 500])
@property
def initial_condition(self):
equilibrium = self.thermodynamic_equilibrium(self.grid, self.model)
ic = ExtremeGaussianBumpIC(self.model, equilibrium)
ic.p_amplitude, ic.rho_amplitude = 1e-6, 1e-6
return ic
@property
def gravity(self):
return PointMassGravity(gravitational_constant=800.0,
mass=3000.0,
radius=1000.0)
@property
def base_filename(self):
return "extreme_gaussian_bump"
@property
def steps_per_frame(self):
return 5
class GaussianBumpConvergence(GaussianBump):
@property
def plotting_steps(self):
# return super().plotting_steps
return PlotLast()
class GaussianBumpReference(GaussianBumpConvergence):
@property
def well_balancing(self):
return "naive"
@property
def n_cells(self):
return 2**14 + 6
@property
def output_filename(self):
pattern = "extreme_gaussian_bump-{:s}"
return pattern.format(self._thermodynamic_equilibrium)
def compute_reference_solution(Experiment, thermodynamic_equilibrium):
experiment = Experiment()
experiment.thermodynamic_equilibrium = thermodynamic_equilibrium
grid = experiment.grid
u0 = experiment.initial_condition.back_ground(grid)
u_ref = experiment()
filename_base = "data/" + experiment.output_filename
np.save(filename_base + "_background.npy", u0)
np.save(filename_base + "_reference.npy", u_ref)
with open(filename_base + "_grid.pkl", 'wb') as f:
pickle.dump(grid, f)
def load_reference_solution(Experiment, thermodynamic_equilibrium):
experiment = Experiment()
experiment.thermodynamic_equilibrium = thermodynamic_equilibrium
filename_base = "data/" + experiment.output_filename
u0_ref = np.load(filename_base + "_background.npy")
u_ref = np.load(filename_base + "_reference.npy")
with open(filename_base + "_grid.pkl", 'rb') as f:
grid = pickle.load(f)
return u0_ref, u_ref, grid
def down_sample(u_fine, grid_fine, grid_coarse):
"""Compute cell-averages of `u_fine` on the coarse grid."""
if grid_fine.n_dims == 2:
raise Exception("Needs to be implemented.")
ngf = grid_fine.n_ghost
ngc = grid_coarse.n_ghost
ncf = grid_fine.n_cells[0] - 2*ngf
ncc = grid_coarse.n_cells[0] - 2*ngc
r = ncf // ncc
assert r*ncc == ncf
shape = (u_fine.shape[0], -1, r)
u_coarse = np.mean(u_fine[:,ngf:-ngf].reshape(shape), axis=-1)
return u_coarse
class EquilibriumConvergenceRates:
def __call__(self, Experiment, ExperimentReference, thermal_equilibrium):
all_errors, all_rates = [], []
all_labels = self.all_labels
if self.is_reference_solution_required:
compute_reference_solution(ExperimentReference, thermal_equilibrium)
for well_balancing in ["naive", "wb_o2", "wb_o4"]:
# for well_balancing in ["naive", "wb_o4"]:
# for well_balancing in ["wb_o2"]:
# for well_balancing in ["wb_o4"]:
error, rate, resolutions = self.compute_convergence(Experiment,
ExperimentReference,
thermal_equilibrium,
well_balancing)
all_errors += error
all_rates += rate
experiment = ExperimentReference()
experiment.thermodynamic_equilibrium = thermal_equilibrium
filename_base = "".join(["img/code-validation/",
experiment.base_filename,
"-{:s}".format(experiment._thermodynamic_equilibrium)])
latex_table = LatexConvergenceTable(all_errors,
all_rates,
resolutions-6,
all_labels)
latex_table.write(filename_base + ".tex")
plot = ConvergencePlot(self.trend_lines)
plot(all_errors, resolutions-6, all_labels)
plot.save(filename_base)
def compute_convergence(self, Experiment,
ExperimentReference,
thermal_equilibrium,
well_balancing):
u0_ref, u_ref, grid_ref = load_reference_solution(ExperimentReference,
thermal_equilibrium)
du_ref = u_ref - u0_ref
# plt.clf()
# marker = iter(Markers())
# self.plot_delta(grid_ref, u_ref, du_ref, None, next(marker))
resolutions = self.resolutions
err = np.empty((4, resolutions.size))
for l, res in enumerate(resolutions):
experiment = Experiment()
experiment.thermodynamic_equilibrium = thermal_equilibrium
experiment.well_balancing = well_balancing
experiment.n_cells = res
grid = experiment.grid
n_ghost = grid.n_ghost
u0 = experiment.initial_condition.back_ground(grid)
u = experiment()
du = u - u0
du_ref_c = down_sample(du_ref, grid_ref, grid)
# self.plot_delta(grid, u, du, du_ref_c, next(marker))
err[:,l] = l1rel_error(du[:,n_ghost:-n_ghost], du_ref_c, ref=u_ref)
# plt.show()
error_vars = self.error_vars
rates = [convergence_rate(err[k,...], resolutions-6) for k in error_vars]
errors = [err[k,...] for k in error_vars]
return errors, rates, resolutions
def plot_delta(self, grid, u, du, du_ref_c, marker):
n_ghost = grid.n_ghost
x = grid.cell_centers[:,0]
plt.plot(x, du[0,...], marker = marker)
class GaussianBumpConvergenceRates(EquilibriumConvergenceRates):
def __init__(self):
super().__init__()
self.all_labels = ["$\\rho_{(0)}$", "$E_{(0)}$",
"$\\rho_{(1)}$", "$E_{(1)}$",
"$\\rho_{(2)}$", "$E_{(2)}$"]
self.error_vars = [0, 3]
self.resolutions = 2**np.arange(4, 11) + 6
self.is_reference_solution_required = True
self.trend_lines = [5]
if __name__ == "__main__":
sim = GaussianBumpConvergenceRates()
# sim(GaussianBumpConvergence, GaussianBumpReference, "isothermal")
sim(GaussianBumpConvergence, GaussianBumpReference, "isentropic")