Skip to content

Commit 3f1c253

Browse files
authored
Merge pull request #62 from python-hydro/compare
compare.py tests up to a given tolerance
2 parents 51f37de + 769a64d commit 3f1c253

11 files changed

Lines changed: 67 additions & 40 deletions

compare.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,29 @@
88
from util import io
99

1010
usage = """
11-
usage: ./compare.py file1 file2
11+
usage: ./compare.py file1 file2 (rtol)
12+
13+
where rtol is an (optional) relative tolerance parameter to use when
14+
comparing the data
1215
"""
1316

1417
errors = {"gridbad": "grids don't agree",
1518
"namesbad": "variable lists don't agree",
1619
"varerr": "one or more variables don't agree"}
1720

1821

19-
def compare(data1, data2):
20-
"""given two CellCenterData2d objects, compare the data, zone-by-zone
22+
def compare(data1, data2, rtol=1.e-12):
23+
"""
24+
given two CellCenterData2d objects, compare the data, zone-by-zone
2125
and output any errors
2226
27+
Parameters
28+
----------
29+
data1, data2 : CellCenterData2d object
30+
Two data grids to compare
31+
rtol : float
32+
relative tolerance to use to compare grids
33+
2334
"""
2435

2536
# compare the grids
@@ -40,19 +51,23 @@ def compare(data1, data2):
4051
d1 = data1.get_var(name)
4152
d2 = data2.get_var(name)
4253

43-
err = np.max(np.abs(d1.v() - d2.v()))
54+
abs_err = np.max(np.abs(d1.v() - d2.v()))
4455

45-
print("{:20s} error = {:20.10g}".format(name, err))
56+
if not np.any(d2.v() == 0):
57+
rel_err = np.max(np.abs(d1.v() - d2.v()) / np.abs(d2.v()))
58+
print("{:20s} absolute error = {:10.10g}, relative error = {:10.10g}".format(name, abs_err, rel_err))
59+
else:
60+
print("{:20s} absolute error = {:10.10g}".format(name, abs_err))
4661

47-
if not err == 0:
62+
if not np.allclose(d1.v(), d2.v(), rtol=rtol):
4863
result = "varerr"
4964

5065
return result
5166

5267

5368
if __name__ == "__main__":
5469

55-
if not len(sys.argv) == 3:
70+
if not (len(sys.argv) == 3 or len(sys.argv) == 4):
5671
print(usage)
5772
sys.exit(2)
5873

@@ -62,7 +77,10 @@ def compare(data1, data2):
6277
s1 = io.read(file1)
6378
s2 = io.read(file2)
6479

65-
result = compare(s1.cc_data, s2.cc_data)
80+
if len(sys.argv) == 3:
81+
result = compare(s1.cc_data, s2.cc_data)
82+
else:
83+
result = compare(s1.cc_data, s2.cc_data, rtol=float(sys.argv[3]))
6684

6785
if result == 0:
6886
print("SUCCESS: files agree")

examples/multigrid/mg_test_general_alphabeta_only.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def f(x, y):
7171

7272

7373
def test_general_poisson_dirichlet(N, store_bench=False, comp_bench=False,
74-
make_plot=False, verbose=1):
74+
make_plot=False, verbose=1, rtol=1.e-12):
7575
"""
7676
test the general MG solver. The return value
7777
here is the error compared to the exact solution, UNLESS
@@ -186,10 +186,10 @@ def test_general_poisson_dirichlet(N, store_bench=False, comp_bench=False,
186186
msg.warning("comparing to: %s " % (compare_file))
187187
bench = io.read(compare_file)
188188

189-
result = compare.compare(my_data, bench)
189+
result = compare.compare(my_data, bench, rtol)
190190

191191
if result == 0:
192-
msg.success("results match benchmark\n")
192+
msg.success("results match benchmark to within relative tolerance of {}\n".format(rtol))
193193
else:
194194
msg.warning("ERROR: " + compare.errors[result] + "\n")
195195

examples/multigrid/mg_test_general_beta_only.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def f(x, y):
7171

7272

7373
def test_general_poisson_dirichlet(N, store_bench=False, comp_bench=False,
74-
make_plot=False, verbose=1):
74+
make_plot=False, verbose=1, rtol=1.e-12):
7575
"""
7676
test the general MG solver. The return value
7777
here is the error compared to the exact solution, UNLESS
@@ -186,10 +186,10 @@ def test_general_poisson_dirichlet(N, store_bench=False, comp_bench=False,
186186
msg.warning("comparing to: %s " % (compare_file))
187187
bench = io.read(compare_file)
188188

189-
result = compare.compare(my_data, bench)
189+
result = compare.compare(my_data, bench, rtol)
190190

191191
if result == 0:
192-
msg.success("results match benchmark\n")
192+
msg.success("results match benchmark to within relative tolerance of {}\n".format(rtol))
193193
else:
194194
msg.warning("ERROR: " + compare.errors[result] + "\n")
195195

examples/multigrid/mg_test_general_constant.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def f(x, y):
5757

5858

5959
def test_general_poisson_dirichlet(N, store_bench=False, comp_bench=False,
60-
make_plot=False, verbose=1):
60+
make_plot=False, verbose=1, rtol=1.e-12):
6161
"""
6262
test the general MG solver. The return value
6363
here is the error compared to the exact solution, UNLESS
@@ -175,7 +175,7 @@ def test_general_poisson_dirichlet(N, store_bench=False, comp_bench=False,
175175
result = compare.compare(my_data, bench)
176176

177177
if result == 0:
178-
msg.success("results match benchmark\n")
178+
msg.success("results match benchmark to within relative tolerance of {}\n".format(rtol))
179179
else:
180180
msg.warning("ERROR: " + compare.errors[result] + "\n")
181181

examples/multigrid/mg_test_general_dirichlet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def f(x, y):
7676

7777

7878
def test_general_poisson_dirichlet(N, store_bench=False, comp_bench=False,
79-
make_plot=False, verbose=1):
79+
make_plot=False, verbose=1, rtol=1.e-12):
8080
"""
8181
test the general MG solver. The return value
8282
here is the error compared to the exact solution, UNLESS
@@ -191,10 +191,10 @@ def test_general_poisson_dirichlet(N, store_bench=False, comp_bench=False,
191191
msg.warning("comparing to: %s " % (compare_file))
192192
bench = io.read(compare_file)
193193

194-
result = compare.compare(my_data, bench)
194+
result = compare.compare(my_data, bench, rtol)
195195

196196
if result == 0:
197-
msg.success("results match benchmark\n")
197+
msg.success("results match benchmark to within relative tolerance of {}\n".format(rtol))
198198
else:
199199
msg.warning("ERROR: " + compare.errors[result] + "\n")
200200

examples/multigrid/mg_test_general_inhomogeneous.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def yl_func(x):
9090

9191

9292
def test_general_poisson_inhomogeneous(N, store_bench=False, comp_bench=False,
93-
make_plot=False, verbose=1):
93+
make_plot=False, verbose=1, rtol=1.e-12):
9494
"""
9595
test the general MG solver. The return value
9696
here is the error compared to the exact solution, UNLESS
@@ -209,10 +209,10 @@ def test_general_poisson_inhomogeneous(N, store_bench=False, comp_bench=False,
209209
msg.warning("comparing to: %s " % (compare_file))
210210
bench = io.read(compare_file)
211211

212-
result = compare.compare(my_data, bench)
212+
result = compare.compare(my_data, bench, rtol)
213213

214214
if result == 0:
215-
msg.success("results match benchmark\n")
215+
msg.success("results match benchmark to within relative tolerance of {}\n".format(rtol))
216216
else:
217217
msg.warning("ERROR: " + compare.errors[result] + "\n")
218218

examples/multigrid/mg_test_simple.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def f(x, y):
3838

3939

4040
def test_poisson_dirichlet(N, store_bench=False, comp_bench=False,
41-
make_plot=False, verbose=1):
41+
make_plot=False, verbose=1, rtol=1e-12):
4242

4343
# test the multigrid solver
4444
nx = N
@@ -101,10 +101,10 @@ def test_poisson_dirichlet(N, store_bench=False, comp_bench=False,
101101
msg.warning("comparing to: %s " % (compare_file))
102102
bench_data = io.read(compare_file)
103103

104-
result = compare.compare(my_data, bench_data)
104+
result = compare.compare(my_data, bench_data, rtol)
105105

106106
if result == 0:
107-
msg.success("results match benchmark\n")
107+
msg.success("results match benchmark to within relative tolerance of {}\n".format(rtol))
108108
else:
109109
msg.warning("ERROR: " + compare.errors[result] + "\n")
110110

examples/multigrid/mg_test_vc_dirichlet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def f(x, y):
5757

5858

5959
def test_vc_poisson_dirichlet(N, store_bench=False, comp_bench=False,
60-
make_plot=False, verbose=1):
60+
make_plot=False, verbose=1, rtol=1.e-12):
6161
"""
6262
test the variable-coefficient MG solver. The return value
6363
here is the error compared to the exact solution, UNLESS
@@ -160,10 +160,10 @@ def test_vc_poisson_dirichlet(N, store_bench=False, comp_bench=False,
160160
msg.warning("comparing to: %s " % (compare_file))
161161
bench = io.read(compare_file)
162162

163-
result = compare.compare(my_data, bench)
163+
result = compare.compare(my_data, bench, rtol)
164164

165165
if result == 0:
166-
msg.success("results match benchmark\n")
166+
msg.success("results match benchmark to within relative tolerance of {}\n".format(rtol))
167167
else:
168168
msg.warning("ERROR: " + compare.errors[result] + "\n")
169169

examples/multigrid/mg_test_vc_periodic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def f(x, y):
5757

5858

5959
def test_vc_poisson_periodic(N, store_bench=False, comp_bench=False,
60-
make_plot=False, verbose=1):
60+
make_plot=False, verbose=1, rtol=1.e-12):
6161
"""
6262
test the variable-coefficient MG solver. The return value
6363
here is the error compared to the exact solution, UNLESS
@@ -170,10 +170,10 @@ def test_vc_poisson_periodic(N, store_bench=False, comp_bench=False,
170170
msg.warning("comparing to {}".format(compare_file))
171171
bench = io.read(compare_file)
172172

173-
result = compare.compare(my_data, bench)
173+
result = compare.compare(my_data, bench, rtol)
174174

175175
if result == 0:
176-
msg.success("results match benchmark\n")
176+
msg.success("results match benchmark to within relative tolerance of {}\n".format(rtol))
177177
else:
178178
msg.warning("ERROR: {}\n".format(compare.errors[result]))
179179

pyro.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,17 @@ def __init__(self, solver_name, comp_bench=False,
267267
self.reset_bench_on_fail = reset_bench_on_fail
268268
self.make_bench = make_bench
269269

270-
def run_sim(self):
270+
def run_sim(self, rtol):
271271
"""
272-
Evolve entire simulation and benchmark at the end.
272+
Evolve entire simulation and compare to benchmark at the end.
273273
"""
274274

275275
super().run_sim()
276276

277277
result = 0
278278

279279
if self.comp_bench:
280-
result = self.compare_to_benchmark()
280+
result = self.compare_to_benchmark(rtol)
281281

282282
if self.make_bench or (result != 0 and self.reset_bench_on_fail):
283283
self.store_as_benchmark()
@@ -287,7 +287,7 @@ def run_sim(self):
287287
else:
288288
return self.sim
289289

290-
def compare_to_benchmark(self):
290+
def compare_to_benchmark(self, rtol):
291291
""" Are we comparing to a benchmark? """
292292

293293
basename = self.rp.get_param("io.basename")
@@ -300,10 +300,10 @@ def compare_to_benchmark(self):
300300
msg.warning("ERROR opening compare file")
301301
return "ERROR opening compare file"
302302

303-
result = compare.compare(self.sim.cc_data, sim_bench.cc_data)
303+
result = compare.compare(self.sim.cc_data, sim_bench.cc_data, rtol)
304304

305305
if result == 0:
306-
msg.success("results match benchmark\n")
306+
msg.success("results match benchmark to within relative tolerance of {}\n".format(rtol))
307307
else:
308308
msg.warning("ERROR: " + compare.errors[result] + "\n")
309309

0 commit comments

Comments
 (0)