5Create the PNG illustrations of the tutorial
645_coupling-sharing-auxiliary-variables.ipynb
8The script builds the two tsunamis which share their sea floor, runs them and
9writes the initial conditions and the difference of their arrival times into
10the directory of this script. It repeats the setup of the notebook, i.e.
11the PDE terms are the ones of tutorial 11_tsunami.ipynb for the surface
12elevation eta as unknown. Call the script from anywhere:
14 python3 tutorials/exahype2/illustrations/45_coupling.py
16All builds and outputs go into a temporary directory (or --work-directory).
25Here = os.path.dirname(os.path.abspath(__file__))
26PeanoRoot = os.path.abspath(os.path.join(Here,
"..",
"..",
".."))
27sys.path.insert(0, os.path.join(PeanoRoot,
"src"))
32import matplotlib.pyplot
as plt
40 constexpr double g = 9.81;
41 const double h = Q[Variables::eta] - bathymetry[Bathymetry::b];
42 const double un = Q[Variables::hu + normal] / h;
43 F[Variables::eta] = Q[Variables::hu + normal];
44 F[Variables::hu] = Q[Variables::hu] * un;
45 F[Variables::hv] = Q[Variables::hv] * un;
46 F[Variables::hu + normal] += 0.5 * g * h * h;
50 constexpr double g = 9.81;
51 const double h = Q[Variables::eta] - bathymetry[Bathymetry::b];
52 const double un = Q[Variables::hu + normal] / h;
53 lambdaMax[0] = std::abs(un) + std::sqrt(g * h);
56NonconservativeProduct =
"""
57 constexpr double g = 9.81;
58 const double h = Q[Variables::eta] - bathymetry[Bathymetry::b];
59 BTimesDeltaQ[Variables::eta] = 0.0;
60 BTimesDeltaQ[Variables::hu] = 0.0;
61 BTimesDeltaQ[Variables::hv] = 0.0;
62 BTimesDeltaQ[Variables::hu + normal] = g * h * deltaBathymetry[Bathymetry::b];
66 const int column = std::clamp(static_cast<int>((x[0] + 1.0) / 2.0 * DurhamLogoSize), 0, DurhamLogoSize - 1);
67 const int row = std::clamp(static_cast<int>((1.0 - x[1]) / 2.0 * DurhamLogoSize), 0, DurhamLogoSize - 1);
68 bathymetry[Bathymetry::b] = DurhamLogo[row][column] == '1' ? -4.0 : -1.0;
72 const double dx = x[0] - {centre};
73 Q[Variables::eta] = 0.1 * std::exp(-100.0 * (dx * dx + x[1] * x[1]));
74 Q[Variables::hu] = 0.0;
75 Q[Variables::hv] = 0.0;
78Bathymetry = os.path.join(Here,
"..",
"bathymetry")
81CentreOfFirstTsunami = 0.0
82CentreOfSecondTsunami = 0.1
86 """! Generate, compile and run the coupled setup within directory """
88 namespace=[
"tutorials",
"exahype2",
"tsunami"],
89 project_name=
"Coupling",
91 executable=
"coupling",
94 for name, centre, owns_sea_floor
in [
95 (
"TsunamiA", CentreOfFirstTsunami,
True),
96 (
"TsunamiB", CentreOfSecondTsunami,
False),
99 solver.add_auxiliary_variables(bathymetry)
100 solver.add_user_solver_includes(
101 '#include <cmath>\n#include <algorithm>\n#include "DurhamLogo.h"\n'
109 implementation=NonconservativeProduct, stateless=
True
114 implementation=InitialWater.format(centre=centre)
115 + (InitialSeaFloor
if owns_sea_floor
else ""),
121 solver, volume_size=0.01
124 solver, first_plot_time_stamp=0.0, time_in_between_plots=0.01
126 project.add_solver(solver)
127 project.set_global_simulation_parameters(
133 periodic_BC=[
True,
True,
True],
135 project.set_Peano4_installation(PeanoRoot, peano4.api.output.CompileMode.RELEASE)
136 peano4_project = project.generate_Peano4_project()
137 makefile = peano4_project.output.makefile
138 makefile.add_cpp_file(os.path.join(Bathymetry,
"DurhamLogo.cpp"))
139 makefile.add_h_file(os.path.join(Bathymetry,
"DurhamLogo.h"))
140 makefile.add_header_search_path(Bathymetry)
141 peano4_project.generate()
142 subprocess.run([
"make",
"-j"], cwd=directory, check=
True, stdout=subprocess.DEVNULL)
143 subprocess.run([
"./coupling"], cwd=directory, check=
True, stdout=subprocess.DEVNULL)
147 """! One unknown of one snapshot as array field[y,x] over the whole domain """
151 for patch_file
in patch_files
154 volume_size = patches[0][1][0] / patches[0][2].shape[1]
155 volumes = round(2.0 / volume_size)
156 field = np.zeros((volumes, volumes))
157 for offset, _, values
in patches:
158 x = round((offset[0] + 1.0) / volume_size)
159 y = round((offset[1] + 1.0) / volume_size)
161 field[y : y + n, x : x + n] = values[..., unknown]
166 """! Time stamps and an array series[snapshot,y,x] of one unknown """
168 times = np.array([time
for time, _
in snapshots])
169 series = np.array([
read_field(file_name, s, unknown)
for s
in range(len(snapshots))])
176 First time at which the surface elevation exceeds the threshold
178 The time is interpolated linearly between the two snapshots which enclose
179 the crossing. Volumes which the wave never reaches hold NaN.
182 exceeded = series >= threshold
183 first = np.argmax(exceeded, axis=0)
184 reached = exceeded.any(axis=0)
185 after = np.take_along_axis(series, first[
None], axis=0)[0]
186 before = np.take_along_axis(series, np.maximum(first - 1, 0)[
None], axis=0)[0]
187 weight = np.where(first > 0, (threshold - before) / np.maximum(after - before, 1e-300), 1.0)
188 arrival = times[np.maximum(first - 1, 0)] + weight * (times[first] - times[np.maximum(first - 1, 0)])
189 return np.where(reached, arrival, np.nan)
193 fig, axes = plt.subplots(1, 2, figsize=(11, 4.5))
194 for ax, file_name, title
in zip(
195 axes, [first_file, second_file], [
"first tsunami (hump at x=0)",
"second tsunami (hump at x=0.1)"]
198 file_name, 0, 0, ax=ax, label=
"surface elevation eta"
201 fig.savefig(os.path.join(Here, prefix +
"initial.png"), dpi=100, bbox_inches=
"tight")
210 difference = arrival_second - arrival_first
211 fig, ax = plt.subplots(figsize=(6.5, 5))
212 bound = np.nanmax(np.abs(difference))
216 extent=(-1, 1, -1, 1),
221 fig.colorbar(image, label=
"arrival time second - arrival time first")
224 ax.set_title(f
"Arrival of eta >= {threshold}")
225 fig.savefig(os.path.join(Here, prefix +
"arrival-difference.png"), dpi=100, bbox_inches=
"tight")
229if __name__ ==
"__main__":
230 parser = argparse.ArgumentParser(
231 description=
"Create the PNGs of the tutorial 45_coupling-sharing-auxiliary-variables.ipynb"
236 help=
"Directory for builds and outputs (default: temporary directory)",
238 args = parser.parse_args()
240 work_directory = args.work_directory
or tempfile.mkdtemp(prefix=
"45_coupling-")
241 print(
"build and run in {}".format(work_directory))
243 first_file = os.path.join(work_directory,
"solution-TsunamiA.peano-patch-file")
244 second_file = os.path.join(work_directory,
"solution-TsunamiB.peano-patch-file")
A set of auxiliary variables of a solver.
The conservative flux F(Q) of a hyperbolic PDE.
Point-wise initial condition: given the physical position of a single degree of freedom,...
The maximum absolute eigenvalue of the flux Jacobian in a given direction, i.e.
The non-conservative product B(Q) applied to a jump deltaQ in a given direction, i....
Abstract base class for all ExaHyPE 2 solvers.
Plotting counterpart to RusanovWithExplicitEuler: maps the patch data of a Finite Volume scheme with ...
Finite Volumes with a Rusanov Riemann solver and explicit Euler time stepping, realised over a block-...
Setup phase counterpart to RusanovWithExplicitEuler: sets the initial condition for a Finite Volume s...
read_time_series(file_name, unknown=0)
Time stamps and an array series[snapshot,y,x] of one unknown.
arrival_times(times, series, threshold)
First time at which the surface elevation exceeds the threshold.
plot_initial_conditions(first_file, second_file, prefix)
read_field(file_name, snapshot, unknown=0)
One unknown of one snapshot as array field[y,x] over the whole domain.
plot_arrival_difference(first_file, second_file, prefix, threshold=0.002)
build_and_run(directory)
Generate, compile and run the coupled setup within directory.
Interactive visualisation of Peano patch files within Jupyter notebooks.
plot_snapshot(file_name, snapshot=-1, unknown=0, clim=None, ax=None, cmap="viridis", label=None)
Plot one unknown of one snapshot.
read_patches(patch_file_name)
Return list of (offset, size, values) for all patches of one patch file.
read_snapshots(file_name)
Return list of (time stamp, list of patch files) from a patch meta file.