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
21
22import numpy as np
23
24from tarch.plotter.api.input.PatchFileParser import PatchFileParser
25from tarch.plotter.api.notebook import read_snapshots
26
27DOMAIN_OFFSET = -0.1
28DOMAIN_WIDTH = 0.2
29VELOCITY = 1.0
30
31
32def exact_density(x, t):
33 return 1.0 + 0.2 * np.sin(
34 2.0 * np.pi * (x - VELOCITY * t - DOMAIN_OFFSET) / DOMAIN_WIDTH
35 )
36
37
38def snap(offset, size):
39 """! Snap a patch's offset and size onto the regular mesh over the domain."""
40 size = np.array([DOMAIN_WIDTH / round(DOMAIN_WIDTH / s) for s in size])
41 offset = np.round((np.array(offset) - DOMAIN_OFFSET) / size) * size + DOMAIN_OFFSET
42 return offset, size
43
44
45def errors(run_directory):
46 """!
47 Return (time stamp, grid cells per axis, max error, rms error) of the last
48 snapshot of a run.
49 """
50 snapshots = read_snapshots(
51 os.path.join(run_directory, "solution-EulerFD4Smooth.peano-patch-file")
52 )
53 time_stamp, files = snapshots[-1]
54 max_error = 0.0
55 squared_error = 0.0
56 number_of_grid_cells = 0
57 min_grid_cell_size = math.inf
58 for file_name in files:
59 parser = PatchFileParser(file_name, "", 0)
60 parser.parse_file()
61 for attributes in parser.unknown_attributes.values():
62 dofs = attributes.dof
63 for patch in attributes.cell_data:
64 offset, size = snap(patch.offset, patch.size)
65 h = size / dofs
66 min_grid_cell_size = min(min_grid_cell_size, h[0])
67 shape = [dofs] * attributes.dimensions + [attributes.unknowns]
68 # The fastest index is x, i.e. values[(z,) y, x, unknown]
69 density = patch.values.reshape(shape)[..., 0]
70 x = offset[0] + (np.arange(dofs) + 0.5) * h[0]
71 error = np.abs(density - exact_density(x, time_stamp))
72 if not np.all(np.isfinite(error)):
73 return time_stamp, None, math.nan, math.nan
74 max_error = max(max_error, float(np.max(error)))
75 squared_error += float(np.sum(error * error))
76 number_of_grid_cells += error.size
77 return (
78 time_stamp,
79 round(DOMAIN_WIDTH / min_grid_cell_size),
80 max_error,
81 math.sqrt(squared_error / number_of_grid_cells),
82 )
83
84
85if __name__ == "__main__":
86 parser = argparse.ArgumentParser(description=__doc__.strip().split("\n")[0])
87 parser.add_argument(
88 "run_directory", nargs="+", help="Runs, from the coarsest to the finest mesh"
89 )
90 args = parser.parse_args()
91
92 print(
93 f"{'run':40} {'t':>8} {'cells/axis':>10} {'max error':>12} {'rms error':>12} {'order (max)':>12} {'order (rms)':>12}"
94 )
95 previous = None
96 for run_directory in args.run_directory:
97 time_stamp, cells, max_error, rms_error = errors(run_directory)
98 orders = ["", ""]
99 if previous is not None and cells is not None and previous[0] is not None:
100 ratio = cells / previous[0]
101 orders = [
102 f"{math.log(previous[1] / max_error) / math.log(ratio):.2f}",
103 f"{math.log(previous[2] / rms_error) / math.log(ratio):.2f}",
104 ]
105 print(
106 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}"
107 )
108 previous = (cells, max_error, rms_error)
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