Skip to content

Commit f2529e1

Browse files
Facilitate the making of the incompressible convergence plot (#147)
The main addition is pyro/incompressible/tests/convergence_error.py which automatically generates the file which contains the errors at each resolution. Perhaps some cleanup/syntax check would be good.
1 parent a6ad357 commit f2529e1

6 files changed

Lines changed: 111 additions & 37 deletions

File tree

docs/source/incompressible_basics.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,18 @@ shown below are run as:
6262
pyro_sim.py incompressible converge inputs.converge.128 vis.dovis=0
6363

6464
The error is measured by comparing with the analytic solution using
65-
the routine ``incomp_converge_error.py`` in ``analysis/``.
65+
the routine ``incomp_converge_error.py`` in ``analysis/``. To generate
66+
the plot below, run
67+
68+
.. prompt:: bash
69+
70+
python incompressible/tests/convergence_errors.py convergence_errors.txt
71+
72+
or ``convergence_errors_no_limiter.txt`` after running with that option. Then:
73+
74+
.. prompt:: bash
75+
76+
python incompressible/tests/convergence_plot.py
6677

6778
.. image:: incomp_converge.png
6879
:align: center

pyro/analysis/incomp_converge_error.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,42 @@
1616
"""
1717

1818

19-
if len(sys.argv) != 2:
20-
print(usage)
21-
sys.exit(2)
19+
def get_errors(file):
2220

21+
sim = io.read(file)
22+
myd = sim.cc_data
23+
myg = myd.grid
2324

24-
try:
25-
file1 = sys.argv[1]
26-
except IndexError:
27-
print(usage)
28-
sys.exit(2)
25+
# numerical solution
26+
u = myd.get_var("x-velocity")
27+
v = myd.get_var("y-velocity")
2928

30-
sim = io.read(file1)
31-
myd = sim.cc_data
32-
myg = myd.grid
29+
t = myd.t
3330

34-
# numerical solution
35-
u = myd.get_var("x-velocity")
36-
v = myd.get_var("y-velocity")
31+
# analytic solution
32+
u_exact = myg.scratch_array()
33+
u_exact[:, :] = 1.0 - 2.0*np.cos(2.0*math.pi*(myg.x2d-t))*np.sin(2.0*math.pi*(myg.y2d-t))
3734

38-
t = myd.t
35+
v_exact = myg.scratch_array()
36+
v_exact[:, :] = 1.0 + 2.0*np.sin(2.0*math.pi*(myg.x2d-t))*np.cos(2.0*math.pi*(myg.y2d-t))
3937

40-
# analytic solution
41-
u_exact = myg.scratch_array()
42-
u_exact[:, :] = 1.0 - 2.0*np.cos(2.0*math.pi*(myg.x2d-t))*np.sin(2.0*math.pi*(myg.y2d-t))
38+
# error
39+
udiff = u_exact - u
40+
vdiff = v_exact - v
4341

44-
v_exact = myg.scratch_array()
45-
v_exact[:, :] = 1.0 + 2.0*np.sin(2.0*math.pi*(myg.x2d-t))*np.cos(2.0*math.pi*(myg.y2d-t))
42+
return udiff.norm(), vdiff.norm()
4643

47-
# error
48-
udiff = u_exact - u
49-
vdiff = v_exact - v
5044

51-
print("errors: ", udiff.norm(), vdiff.norm())
45+
if __name__ == "__main__":
46+
if len(sys.argv) != 2:
47+
print(usage)
48+
sys.exit(2)
49+
50+
try:
51+
file = sys.argv[1]
52+
except IndexError:
53+
print(usage)
54+
sys.exit(2)
55+
56+
errors = get_errors(file)
57+
print("errors: ", errors[0], errors[1])
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This should be run from /pyro (where the output files are located)
2+
import glob
3+
import sys
4+
5+
from pyro.analysis.incomp_converge_error import get_errors
6+
7+
8+
def create_file(filename="convergence_errors.txt"):
9+
10+
# Parse all converge*.h5 outputs
11+
# store the last file at each resolution
12+
fnames = glob.glob("converge*.h5")
13+
res = set([f.split("_")[1] for f in fnames])
14+
res = list(res)
15+
res.sort(key=int)
16+
17+
simfiles = []
18+
for r in res:
19+
fnames = glob.glob(f"converge_{r}_*.h5")
20+
last = max([int(f.split("_")[-1].split(".")[0]) for f in fnames])
21+
simfiles.append(f"converge_{r}_{last:04d}.h5")
22+
23+
# Create the file
24+
with open("incompressible/tests/" + filename, "w") as f:
25+
f.write("# convergence of incompressible converge problem\n")
26+
f.write("# (convergence measured with analysis/incomp_converge_error.py)\n")
27+
f.write("#\n")
28+
for r, file in zip(res, simfiles):
29+
errors = get_errors(file)
30+
f.write(f"{r.rjust(3)} {errors[0]:.14f} {errors[1]:.14f}\n")
31+
32+
33+
if __name__ == "__main__":
34+
if len(sys.argv) == 2:
35+
create_file(sys.argv[1])
36+
else:
37+
create_file()
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# convergence of incompressible converge problem
22
# (convergence measured with analysis/incomp_converge_error.py)
33
#
4-
32 0.0220739241002 0.0220739241002
4+
32 0.02207392410018 0.02207392410018
55
64 0.00682161894998 0.00682161894998
6-
128 0.00214105266633 0.00214105266633
7-
8-
9-
6+
128 0.00214081341364 0.00214081341364
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# convergence of incompressible converge problem
2+
# (convergence measured with analysis/incomp_converge_error.py)
3+
#
4+
32 0.01491254251261 0.01491254251261
5+
64 0.00216262704768 0.00216262704768
6+
128 0.00036281268740 0.00036281268740

pyro/incompressible/tests/convergence_plot.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
1+
import os
2+
13
import matplotlib.pyplot as plt
24
import numpy as np
35

46

57
def plot_convergence():
68

7-
data = np.loadtxt("incomp_converge.txt")
8-
9-
nx = data[:, 0]
10-
errlim = data[:, 1]
11-
129
ax = plt.subplot(111)
1310
ax.set_xscale('log')
1411
ax.set_yscale('log')
1512

16-
plt.scatter(nx, errlim, marker="x", color="r", label="limiting")
17-
plt.plot(nx, errlim[0]*(nx[0]/nx)**2, "--", color="k")
13+
files = ["incompressible/tests/convergence_errors.txt",
14+
"incompressible/tests/convergence_errors_no_limiter.txt"]
15+
16+
if not os.path.exists(files[0]):
17+
print("Could not find convergence_errors.txt")
18+
else:
19+
data = np.loadtxt(files[0])
20+
nx = data[:, 0]
21+
errlim = data[:, 1]
22+
23+
plt.scatter(nx, errlim, marker="x", color="r", label="limiting")
24+
plt.plot(nx, errlim[0]*(nx[0]/nx)**2, "--", color="k")
25+
26+
if not os.path.exists(files[1]):
27+
print("Could not find convergence_errors_no_limiter.txt")
28+
else:
29+
data = np.loadtxt(files[1])
30+
nx = data[:, 0]
31+
errlim = data[:, 1]
32+
33+
plt.scatter(nx, errlim, marker="x", color="b", label="limiting")
34+
plt.plot(nx, errlim[0]*(nx[0]/nx)**2, "--", color="k")
1835

1936
plt.xlabel("number of zones")
2037
plt.ylabel("L2 norm of abs error")

0 commit comments

Comments
 (0)