Peano
Loading...
Searching...
No Matches
compute-errors.py
Go to the documentation of this file.
1# This file is part of the ExaHyPE2 project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3"""!
4
5Error and convergence order of the entropy wave benchmark
6
7Reads the last snapshot of each run directory passed, compares the density
8with the exact solution, and prints the maximum and the root mean square
9error per run plus the convergence orders between subsequent runs. Pass the
10runs from the coarsest to the finest mesh.
11
12The patch files hold the patches' offsets and sizes with seven significant
13digits only. Below an error of roughly 1e-7, this precision would dominate
14the error. The script hence snaps the offsets and sizes onto the regular
15mesh over the domain [-0.1,0.1]^d.
16
17"""
18import argparse
19import math
20import os
21import sys
22
23import numpy as np
24
25from tarch.plotter.api.input.PatchFileParser import PatchFileParser
26from tarch.plotter.api.notebook import read_snapshots
27
28DOMAIN_OFFSET = -0.1
29DOMAIN_WIDTH = 0.2
30VELOCITY = 1.0
31
32
33def exact_density(x, t):
34 return 1.0 + 0.2 * np.sin(
35 2.0 * np.pi * (x - VELOCITY * t - DOMAIN_OFFSET) / DOMAIN_WIDTH
36 )
37
38
39def snap(offset, size):
40 """! Snap a patch's offset and size onto the regular mesh over the domain."""
41 size = np.array([DOMAIN_WIDTH / round(DOMAIN_WIDTH / s) for s in size])
42 offset = np.round((np.array(offset) - DOMAIN_OFFSET) / size) * size + DOMAIN_OFFSET
43 return offset, size
44
45
46def errors(run_directory):
47 """!
48 Return (time stamp, grid cells per axis, max error, rms error) of the last
49 snapshot of a run.
50 """
51 snapshots = read_snapshots(
52 os.path.join(run_directory, "solution-EulerSmooth.peano-patch-file")
53 )
54 time_stamp, files = snapshots[-1]
55 max_error = 0.0
56 squared_error = 0.0
57 number_of_grid_cells = 0
58 min_grid_cell_size = math.inf
59 for file_name in files:
60 parser = PatchFileParser(file_name, "", 0)
61 parser.parse_file()
62 for attributes in parser.unknown_attributes.values():
63 dofs = attributes.dof
64 for patch in attributes.cell_data:
65 offset, size = snap(patch.offset, patch.size)
66 h = size / dofs
67 min_grid_cell_size = min(min_grid_cell_size, h[0])
68 shape = [dofs] * attributes.dimensions + [attributes.unknowns]
69 # The fastest index is x, i.e. values[(z,) y, x, unknown]
70 density = patch.values.reshape(shape)[..., 0]
71 x = offset[0] + (np.arange(dofs) + 0.5) * h[0]
72 error = np.abs(density - exact_density(x, time_stamp))
73 if not np.all(np.isfinite(error)):
74 return time_stamp, None, math.nan, math.nan
75 max_error = max(max_error, float(np.max(error)))
76 squared_error += float(np.sum(error * error))
77 number_of_grid_cells += error.size
78 return (
79 time_stamp,
80 round(DOMAIN_WIDTH / min_grid_cell_size),
81 max_error,
82 math.sqrt(squared_error / number_of_grid_cells),
83 )
84
85
86if __name__ == "__main__":
87 parser = argparse.ArgumentParser(description=__doc__.strip().split("\n")[0])
88 parser.add_argument(
89 "run_directory", nargs="+", help="Runs, from the coarsest to the finest mesh"
90 )
91 parser.add_argument(
92 "--minimum-order",
93 type=float,
94 default=None,
95 help="Exit with a non-zero status if the order of the maximum error between the two finest runs is below this value or not finite",
96 )
97 args = parser.parse_args()
98
99 print(
100 f"{'run':40} {'t':>8} {'cells/axis':>10} {'max error':>12} {'rms error':>12} {'order (max)':>12} {'order (rms)':>12}"
101 )
102 previous = None
103 for run_directory in args.run_directory:
104 time_stamp, cells, max_error, rms_error = errors(run_directory)
105 orders = ["", ""]
106 if previous is not None and cells is not None and previous[0] is not None:
107 ratio = cells / previous[0]
108 orders = [
109 f"{math.log(previous[1] / max_error) / math.log(ratio):.2f}",
110 f"{math.log(previous[2] / rms_error) / math.log(ratio):.2f}",
111 ]
112 print(
113 f"{run_directory:40} {time_stamp:8.4f} {str(cells):>10} {max_error:12.4e} {rms_error:12.4e} {orders[0]:>12} {orders[1]:>12}"
114 )
115 previous = (cells, max_error, rms_error)
116 last_order = float(orders[0]) if orders[0] != "" else math.nan
117
118 if args.minimum_order is not None:
119 if not last_order >= args.minimum_order:
120 print(f"FAILED: order {last_order} is below {args.minimum_order}")
121 sys.exit(1)
122 print(f"PASSED: order {last_order} is at least {args.minimum_order}")
errors(run_directory)
Return (time stamp, grid cells per axis, max error, rms error) of the last snapshot of a run.
snap(offset, size)
Snap a patch's offset and size onto the regular mesh over the domain.
Interactive visualisation of Peano patch files within Jupyter notebooks.
Definition notebook.py:1