Skip to content

Commit 7f796b5

Browse files
authored
Merge pull request #35 from harpolea/swe
add a shallow water solver
2 parents 1ce68fb + e98cbe2 commit 7f796b5

44 files changed

Lines changed: 3644 additions & 45 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ finite-volume framework. The code is mainly written in python and is
1111
designed with simplicity in mind. The algorithms are written to
1212
encourage experimentation and allow for self-learning of these code
1313
methods.
14-
14+
1515
The latest version of pyro is always available at:
1616

1717
https://github.com/zingale/pyro2
@@ -154,6 +154,8 @@ pyro provides the following solvers (all in 2-d):
154154
variable-coefficient Poisson equation (which inherits from the
155155
constant-coefficient solver).
156156

157+
- `swe`: a solver for the shallow water equations.
158+
157159

158160
## Working with data
159161

@@ -175,6 +177,12 @@ with their data.
175177

176178
- `analysis/`
177179

180+
* `dam_compare.py`: this takes an output file from the
181+
shallow water dam break problem and plots a slice through the domain
182+
together with the analytic solution (calculated in the script).
183+
184+
usage: `./dam_compare.py file`
185+
178186
* `gauss_diffusion_compare.py`: this is for the diffusion solver's
179187
Gaussian diffusion problem. It takes a sequence of output
180188
files as arguments, computes the angle-average, and the plots

analysis/dam_compare.py

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
#!/usr/bin/env python3
2+
3+
from __future__ import print_function
4+
5+
import numpy as np
6+
from scipy.optimize import brentq
7+
import sys
8+
import os
9+
import matplotlib.pyplot as plt
10+
from util import msg, runparams, io
11+
12+
usage = """
13+
compare the output for a dam problem with the exact solution contained
14+
in dam-exact.out.
15+
16+
usage: ./dam_compare.py file
17+
"""
18+
19+
20+
def abort(string):
21+
print(string)
22+
sys.exit(2)
23+
24+
25+
if not len(sys.argv) == 2:
26+
print(usage)
27+
sys.exit(2)
28+
29+
try:
30+
file1 = sys.argv[1]
31+
except IndexError:
32+
print(usage)
33+
sys.exit(2)
34+
35+
sim = io.read(file1)
36+
myd = sim.cc_data
37+
myg = myd.grid
38+
39+
# time of file
40+
t = myd.t
41+
if myg.nx > myg.ny:
42+
# x-problem
43+
xmin = myg.xmin
44+
xmax = myg.xmax
45+
param_file = "inputs.dam.x"
46+
else:
47+
# y-problem
48+
xmin = myg.ymin
49+
xmax = myg.ymax
50+
param_file = "inputs.dam.y"
51+
52+
53+
height = myd.get_var("height")
54+
xmom = myd.get_var("x-momentum")
55+
ymom = myd.get_var("y-momentum")
56+
57+
# get the 1-d profile from the simulation data -- assume that whichever
58+
# coordinate is the longer one is the direction of the problem
59+
60+
# parameter defaults
61+
rp = runparams.RuntimeParameters()
62+
rp.load_params("../_defaults")
63+
rp.load_params("../swe/_defaults")
64+
rp.load_params("../swe/problems/_dam.defaults")
65+
66+
# now read in the inputs file
67+
if not os.path.isfile(param_file):
68+
# check if the param file lives in the solver's problems directory
69+
param_file = "../swe/problems/" + param_file
70+
if not os.path.isfile(param_file):
71+
msg.fail("ERROR: inputs file does not exist")
72+
73+
rp.load_params(param_file, no_new=1)
74+
75+
if myg.nx > myg.ny:
76+
# x-problem
77+
x = myg.x[myg.ilo:myg.ihi+1]
78+
jj = myg.ny//2
79+
80+
h = height[myg.ilo:myg.ihi+1, jj]
81+
82+
u = xmom[myg.ilo:myg.ihi+1, jj]/h
83+
ut = ymom[myg.ilo:myg.ihi+1, jj]/h
84+
85+
else:
86+
87+
# y-problem
88+
x = myg.y[myg.jlo:myg.jhi+1]
89+
ii = myg.nx//2
90+
91+
h = height[ii, myg.jlo:myg.jhi+1]
92+
93+
u = ymom[ii, myg.jlo:myg.jhi+1]/h
94+
ut = xmom[ii, myg.jlo:myg.jhi+1]/h
95+
96+
print(myg)
97+
98+
x_exact = x
99+
h_exact = np.zeros_like(x)
100+
u_exact = np.zeros_like(x)
101+
102+
# find h0, h1
103+
h1 = rp.get_param("dam.h_left")
104+
h0 = rp.get_param("dam.h_right")
105+
106+
107+
def find_h2(h2):
108+
return (h2/h1)**3 - 9*(h2/h1)**2*(h0/h1) + \
109+
16*(h2/h1)**1.5*(h0/h1) - (h2/h1)*(h0/h1)*(h0/h1+8) + \
110+
(h0/h1)**3
111+
112+
113+
h2 = brentq(find_h2, min(h0, h1), max(h0, h1))
114+
115+
# calculate sound speeds
116+
g = rp.get_param("swe.grav")
117+
118+
c0 = np.sqrt(g*h0)
119+
c1 = np.sqrt(g*h1)
120+
c2 = np.sqrt(g*h2)
121+
122+
u2 = 2 * (c1 - c2)
123+
124+
# shock speed
125+
xi = c0 * np.sqrt(1/8 * ((2*(c2/c0)**2 + 1)**2 - 1))
126+
127+
xctr = 0.5*(xmin + xmax)
128+
129+
# h0
130+
idx = x >= xctr + xi*t
131+
h_exact[idx] = h0
132+
u_exact[idx] = 0
133+
134+
# h1
135+
idx = x <= xctr - c1*t
136+
h_exact[idx] = h1
137+
u_exact[idx] = 0
138+
139+
# h2
140+
idx = ((x >= xctr + (u2-c2)*t) & (x < xctr + xi*t))
141+
h_exact[idx] = h2
142+
u_exact[idx] = u2
143+
144+
# h3
145+
idx = ((x >= xctr - c1*t) & (x < xctr + (u2-c2)*t))
146+
c3 = 1/3 * (2*c1 - (x-xctr)/t)
147+
h_exact[idx] = c3[idx]**2 / g
148+
u_exact[idx] = 2 * (c1-c3[idx])
149+
150+
# plot
151+
fig, axes = plt.subplots(nrows=2, ncols=1, num=1)
152+
153+
plt.rc("font", size=10)
154+
155+
ax = axes.flat[0]
156+
157+
ax.plot(x_exact, h_exact, label='Exact')
158+
ax.scatter(x, h, marker="x", s=7, color="r", label='Pyro')
159+
160+
ax.set_ylabel(r"$h$")
161+
ax.set_xlim(0, 1.0)
162+
ax.set_ylim(0, 1.1)
163+
164+
ax = axes.flat[1]
165+
166+
ax.plot(x_exact, u_exact)
167+
ax.scatter(x, u, marker="x", s=7, color="r")
168+
169+
ax.set_ylabel(r"$u$")
170+
ax.set_xlim(0, 1.0)
171+
172+
if (myg.nx > myg.ny):
173+
ax.set_xlabel(r"x")
174+
else:
175+
ax.set_xlabel(r"y")
176+
177+
lgd = axes.flat[0].legend()
178+
179+
plt.subplots_adjust(hspace=0.25)
180+
181+
fig.set_size_inches(8.0, 8.0)
182+
183+
plt.savefig("dam_compare.png", bbox_inches="tight")

docs/source/analysis.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ with their data.
2727
the finer data and then computes the norm of the difference. This
2828
is used to test the convergence of solvers.
2929

30+
* ``dam_compare.py``: this takes an output file from the
31+
shallow water dam break problem and plots a slice through the domain
32+
together with the analytic solution (calculated in the script).
33+
34+
usage: ``./dam_compare.py file``
35+
3036
* ``gauss_diffusion_compare.py``: this is for the diffusion solver's
3137
Gaussian diffusion problem. It takes a sequence of output files as
3238
arguments, computes the angle-average, and the plots the resulting
@@ -65,6 +71,3 @@ with their data.
6571
the analytic solution (read in from ``sod-exact.out``).
6672

6773
usage: ``./sod_compare.py file``
68-
69-
70-

docs/source/design.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ The overall structure is:
8585
* ``problems/``: The problem setups for when the multigrid solver is used in a stand-alone fashion.
8686
* ``tests/``: Reference multigrid solver solutions (from when the multigrid solver is used stand-alone) for regression testing.
8787

88+
* ``swe/``: The shallow water solver.
89+
90+
* ``problems/``: The problem setups for the shallow water solver.
91+
* ``tests/``: Reference shallow water output for regression testing.
92+
8893
* ``util/``: Various service modules used by the pyro routines,
8994
including runtime parameters, I/O, profiling, and pretty output
9095
modes.

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pyro: a python hydro code
3737
diffusion_basics
3838
incompressible_basics
3939
lowmach_basics
40+
swe_basics
4041

4142
.. toctree::
4243
:maxdepth: 1

docs/source/modules.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ pyro2
2323
plot
2424
pyro
2525
simulation_null
26+
swe
2627
test
2728
util

docs/source/swe.problems.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
swe\.problems package
2+
==============================
3+
4+
.. automodule:: swe.problems
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:
8+
9+
Submodules
10+
----------
11+
12+
swe\.problems\.acoustic\_pulse module
13+
----------------------------------------------
14+
15+
.. automodule:: swe.problems.acoustic_pulse
16+
:members:
17+
:undoc-members:
18+
:show-inheritance:
19+
20+
swe\.problems\.advect module
21+
-------------------------------------
22+
23+
.. automodule:: swe.problems.advect
24+
:members:
25+
:undoc-members:
26+
:show-inheritance:
27+
28+
swe\.problems\.dam module
29+
----------------------------------
30+
31+
.. automodule:: swe.problems.dam
32+
:members:
33+
:undoc-members:
34+
:show-inheritance:
35+
36+
swe\.problems\.kh module
37+
---------------------------------
38+
39+
.. automodule:: swe.problems.kh
40+
:members:
41+
:undoc-members:
42+
:show-inheritance:
43+
44+
swe\.problems\.quad module
45+
-----------------------------------
46+
47+
.. automodule:: swe.problems.quad
48+
:members:
49+
:undoc-members:
50+
:show-inheritance:
51+
52+
swe\.problems\.test module
53+
-----------------------------------
54+
55+
.. automodule:: swe.problems.test
56+
:members:
57+
:undoc-members:
58+
:show-inheritance:

docs/source/swe.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
swe package
2+
====================
3+
4+
.. automodule:: swe
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:
8+
9+
Subpackages
10+
-----------
11+
12+
.. toctree::
13+
14+
swe.problems
15+
16+
Submodules
17+
----------
18+
19+
swe\.derives module
20+
----------------------------
21+
22+
.. automodule:: swe.derives
23+
:members:
24+
:undoc-members:
25+
:show-inheritance:
26+
27+
swe\.simulation module
28+
-------------------------------
29+
30+
.. automodule:: swe.simulation
31+
:members:
32+
:undoc-members:
33+
:show-inheritance:
34+
35+
swe\.unsplit\_fluxes module
36+
------------------------------------
37+
38+
.. automodule:: swe.unsplit_fluxes
39+
:members:
40+
:undoc-members:
41+
:show-inheritance:

0 commit comments

Comments
 (0)