5AMR check of the point source benchmark
7Generates, builds and runs the point source benchmark
8(@ref benchmarks_exahype2_euler_point_source) without AMR (levels 0) and with AMR
9(levels 1 by default), and checks that
11- the dynamic AMR works: the mesh has coarsened, i.e. it has fewer cells than the
12 regular start grid at the end of the run, and
13- the AMR does not harm the quality: the final density and energy differ from the
14 solution on the regular grid by at most --tolerance times the range of the
17Prints a table of the mesh over time, the differences and PASSED or FAILED, and exits
18non-zero on FAILED. Finished runs in the work directory are reused.
33HERE = os.path.dirname(os.path.abspath(__file__))
34SOLVER_NAME =
"EulerPointSource"
35VOLUMES_PER_PATCH_AXIS = 9
38def run(work_directory: str, levels: int, arguments):
40 Generate, build and run the benchmark with the given number of AMR levels
42 @param work_directory: str
43 Directory of all runs. The run of the AMR levels lives in a directory of its own.
46 @param arguments: [str]
47 Further arguments of point-source.py.
51 directory = os.path.join(work_directory, f
"levels-{levels}")
52 if os.path.exists(os.path.join(directory,
"run.log")):
54 os.makedirs(directory, exist_ok=
True)
55 with open(os.path.join(directory,
"generate.log"),
"w")
as log:
59 os.path.join(HERE,
"point-source.py"),
67 str(VOLUMES_PER_PATCH_AXIS),
71 stderr=subprocess.STDOUT,
74 with open(os.path.join(directory,
"run.log.tmp"),
"w")
as log:
79 stderr=subprocess.STDOUT,
83 os.path.join(directory,
"run.log.tmp"), os.path.join(directory,
"run.log")
90 Number of leaf cells after each time step
92 @return [(float, int)]
93 Time stamp and number of cells per time step.
98 with open(os.path.join(run_directory,
"run.log"), errors=
"ignore")
as log:
100 match = re.search(
r"t=([0-9.e+-]+), new dt=", line)
102 if time_stamp
is not None:
103 result.append((time_stamp, cells))
104 time_stamp =
float(match.group(1))
105 match = re.search(
r"#local cells=\((\d+)/\d+\)", line)
107 cells = int(match.group(1))
108 result.append((time_stamp, cells))
114 Last snapshot, injected piecewise constant onto the regular grid
116 @return (float, np.ndarray, int)
117 Time stamp, array [unknown][y][x] and the number of mesh cells of the snapshot.
119 snapshots = read_snapshots(
120 os.path.join(run_directory, f
"solution-{SOLVER_NAME}.peano-patch-file")
122 time_stamp, files = snapshots[-1]
123 volume_size = 1.0 / volumes_per_axis
124 result = np.full((4, volumes_per_axis, volumes_per_axis), np.nan)
126 for file_name
in files:
129 for attributes
in parser.unknown_attributes.values():
130 dofs = attributes.dof
131 for patch
in attributes.cell_data:
133 values = patch.values.reshape(dofs, dofs, attributes.unknowns)
134 factor = int(round(patch.size[0] / dofs / volume_size))
135 x = int(round((patch.offset[0] + 0.5) / volume_size))
136 y = int(round((patch.offset[1] + 0.5) / volume_size))
137 block = np.repeat(np.repeat(values, factor, axis=0), factor, axis=1)
138 for unknown
in range(4):
140 unknown, y : y + dofs * factor, x : x + dofs * factor
141 ] = block[..., unknown]
142 return time_stamp, result, cells
145if __name__ ==
"__main__":
146 parser = argparse.ArgumentParser(description=__doc__.strip().split(
"\n")[0])
148 "--work-directory", required=
True, help=
"Directory for the runs"
154 help=
"AMR levels of the AMR run (default: 1)",
160 help=
"Mesh cells per axis of the regular start grid, a power of three (default: 81)",
163 "--end-time", type=float, default=0.1, help=
"Simulated time (default: 0.1)"
169 help=
"Allowed maximum difference relative to the range (default: 0.02)",
171 args = parser.parse_args()
173 volumes_per_axis = args.patch_count * VOLUMES_PER_PATCH_AXIS
176 str(1.001 / volumes_per_axis),
182 os.makedirs(args.work_directory, exist_ok=
True)
183 regular =
run(args.work_directory, 0, extra)
184 adaptive =
run(args.work_directory, args.amr_levels, extra)
186 start_cells = args.patch_count**2
189 f
"Mesh of the run with {args.amr_levels} AMR level(s) (regular start grid: {start_cells} cells)"
191 print(
"| time stamp | cells | volumes | coarsened volumes | refined volumes |")
192 print(
"|------------|-------|---------|-------------------|-----------------|")
193 previous = start_cells
195 coarsened = refined = 0
196 for time_stamp, cells
in timeline:
197 delta = (cells - previous) * VOLUMES_PER_PATCH_AXIS**2
198 coarsened += max(-delta, 0)
199 refined += max(delta, 0)
202 f
"| {time_stamp:10.4f} | {cells:5d} | {cells * VOLUMES_PER_PATCH_AXIS**2:7d} | {max(-delta, 0):17d} | {max(delta, 0):15d} |"
205 print(f
"total coarsened {coarsened} volumes, refined {refined} volumes")
210 if reference_cells != start_cells:
212 f
"the run without AMR holds {reference_cells} instead of {start_cells} cells"
214 if not cells < start_cells:
216 f
"the mesh has not coarsened ({cells} cells at the end, {start_cells} at the start)"
219 failures.append(
"the mesh never coarsened")
221 f
"final snapshot t={time_stamp:.4f}: {cells} cells ({100.0 * cells / start_cells:.0f}% of the regular grid)"
223 for name, unknown
in [(
"density", 0), (
"energy", 3)]:
224 difference = np.abs(solution[unknown] - reference[unknown])
225 value_range = np.nanmax(reference[unknown]) - np.nanmin(reference[unknown])
226 maximum = np.nanmax(difference)
227 rms = math.sqrt(np.nanmean(difference**2))
229 f
"{name}: range {value_range:.3f}, max difference {maximum:.3e}, rms difference {rms:.3e}"
231 if not np.isfinite(maximum)
or maximum > args.tolerance * value_range:
233 f
"{name} differs by {maximum:.3e}, which is more than {args.tolerance} of its range {value_range:.3f}"
237 for failure
in failures:
mesh_over_time(str run_directory)
Number of leaf cells after each time step.
final_solution(str run_directory, int volumes_per_axis)
Last snapshot, injected piecewise constant onto the regular grid.
run(str work_directory, int levels, arguments)
Generate, build and run the benchmark with the given number of AMR levels.
Interactive visualisation of Peano patch files within Jupyter notebooks.