Peano
Loading...
Searching...
No Matches
smooth-convergence-test.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
5Entropy wave benchmark of the Euler equations
6
7Generates and builds one executable of the entropy wave benchmark
8(@ref benchmarks_exahype2_euler_smooth_setup) for one numerical scheme and
9one mesh width. Run the script once per mesh width, each time with a
10different output directory, and compare the outcomes with compute-errors.py
11(or let convergence-study.py do all of this).
12
13"""
14import argparse
15import os
16
17import peano4.api
18import exahype2.api
19
20
21
25CFL_CONSTANTS = {
26 "fv": 0.9,
27 "fv-optimistic": 0.9,
28 "rk1fd4": 0.5,
29 "rk2fd4": 0.5,
30 "rk3fd4": 0.5,
31 "rk4fd4": 0.5,
32}
33
34parser = argparse.ArgumentParser(
35 description="ExaHyPE 2 smooth convergence test (Euler entropy wave)"
36)
37parser.add_argument(
38 "--scheme",
39 choices=list(CFL_CONSTANTS.keys()),
40 default="rk4fd4",
41 help="Numerical scheme: Finite Volumes with explicit Euler (fv), the same with "
42 "optimistic time stepping (fv-optimistic), or fourth-order Finite Differences with "
43 "Runge-Kutta of order 1 to 4 (rk1fd4, ..., rk4fd4) (default: rk4fd4)",
44)
45parser.add_argument(
46 "--compiler",
47 choices=["sequential", "fusion", "producer-consumer", "producer-consumer-fusion"],
48 default="fusion",
49 help="Task graph compiler (default: fusion)",
50)
51parser.add_argument(
52 "--cxx",
53 default=None,
54 help="C++ compiler to build the generated code with, e.g. clang++, g++ or icpx "
55 "(default: the one Peano was configured with)",
56)
57parser.add_argument(
58 "-j",
59 "--build-jobs",
60 type=int,
61 default=0,
62 help="Number of parallel make jobs used to build; 0 or less uses all available cores (default: all cores)",
63)
64parser.add_argument(
65 "-t",
66 "--threads",
67 type=int,
68 default=None,
69 help="Number of threads of the generated executable (default: all available cores)",
70)
71parser.add_argument(
72 "--output-directory",
73 default=".",
74 help="Directory the code is generated and built in (default: .)",
75)
76parser.add_argument(
77 "--volume-size",
78 type=float,
79 default=0.0075,
80 help="Maximum grid cell size. 0.0075, 0.0025 and 0.00083 yield 27, 81 and 243 grid cells per axis (default: 0.0075)",
81)
82parser.add_argument(
83 "--patch-size", type=int, default=9, help="Grid cells per patch and axis (default: 9)"
84)
85parser.add_argument(
86 "--end-time", type=float, default=0.05, help="Simulated time (default: 0.05)"
87)
88parser.add_argument(
89 "--plot-interval",
90 type=float,
91 default=None,
92 help="Time between plots (default: the end time, i.e. the initial and the final state)",
93)
94parser.add_argument(
95 "--time-step-size",
96 type=float,
97 default=None,
98 help="Fixed time step size (rkNfd4 only). Without this argument, the time step size is adaptive",
99)
100parser.add_argument(
101 "--periodic",
102 action="store_true",
103 help="Use periodic boundary conditions instead of Dirichlet conditions with the exact solution",
104)
105parser.add_argument(
106 "--dimensions", type=int, default=2, choices=[2, 3], help="Dimension (default: 2)"
107)
108parser.add_argument(
109 "-m",
110 "--build-mode",
111 default="release",
112 choices=["release", "debug"],
113 help="Peano build flavour to link against (default: release)",
114)
115parser.add_argument(
116 "--peano-root",
117 default=os.path.join(
118 os.path.dirname(os.path.abspath(__file__)), "..", "..", "..", ".."
119 ),
120 help="Configured and built Peano directory (default: the repository this script resides in)",
121)
122parser.add_argument(
123 "--verbosity",
124 default=None,
125 choices=["visualisation", "text"],
126 help="Show the task graphs in a window or print them as text (default: text for release, visualisation for debug)",
127)
128parser.add_argument(
129 "--no-clean", action="store_true", help="Do not run make clean before building"
130)
131parser.add_argument(
132 "--no-build", action="store_true", help="Only generate the code, do not build it"
133)
134args = parser.parse_args()
135
136is_finite_volume = args.scheme in ["fv", "fv-optimistic"]
137if args.time_step_size is not None and is_finite_volume:
138 parser.error("--time-step-size is only supported by the rkNfd4 schemes")
139if args.verbosity is None:
140 args.verbosity = "text" if args.build_mode == "release" else "visualisation"
141plot_interval = args.plot_interval if args.plot_interval is not None else args.end_time
142
144task_graph_compiler = {
145 "sequential": compilers.Sequential.translate,
146 "fusion": compilers.FuseSteps.translate,
147 "producer-consumer": compilers.CellTaskProducerConsumer.translate_with_sequential,
148 "producer-consumer-fusion": compilers.CellTaskProducerConsumer.translate_with_fusion,
149}[args.compiler]
150
151
153 namespace=["benchmarks", "exahype2", "euler", "smooth_convergence_test"],
154 project_name="smooth-convergence-test",
155 directory=args.output_directory,
156 executable="smooth-convergence-test",
157)
158
159solver = exahype2.api.Solver("EulerSmooth", {"rho": 1, "j": args.dimensions, "E": 1})
160solver.add_user_solver_includes(
161 """
162#include <algorithm>
163#include <cmath>
164"""
165)
166
167# Pressure from the conserved quantities. The momentum has Dimensions
168# components, the energy is the last unknown.
169PRESSURE = """
170 constexpr double gamma = 1.4;
171 const double irho = 1.0 / Q[0];
172 double kineticEnergy = 0.0;
173 for (int d=0; d<Dimensions; d++) {
174 kineticEnergy += 0.5 * irho * Q[1+d] * Q[1+d];
175 }
176 const double p = (gamma - 1.0) * (Q[Dimensions+1] - kineticEnergy);
177"""
178
179solver.add_pde_term(
181 implementation=PRESSURE
182 + """
183 const double velocity = Q[1+normal] * irho;
184 F[0] = Q[1+normal];
185 for (int d=0; d<Dimensions; d++) {
186 F[1+d] = velocity * Q[1+d];
187 }
188 F[1+normal] += p;
189 F[Dimensions+1] = velocity * (Q[Dimensions+1] + p);
190"""
191 )
192)
193solver.add_pde_term(
195 implementation=PRESSURE
196 + """
197 const double c = std::sqrt(gamma * p * irho);
198 const double u = Q[1+normal] * irho;
199 lambdaMax[0] = std::max(std::abs(u + c), std::abs(u - c));
200"""
201 )
202)
203
204# Entropy wave: one period of a sine density profile over the domain
205# [-0.1,0.1]^d, constant velocity u=1 along x and constant pressure p=1. The
206# exact solution is rho(x-ut), i.e. the wave is back where it started after
207# t = 0.2.
208EXACT_SOLUTION = """
209 constexpr double gamma = 1.4;
210 constexpr double p = 1.0;
211 constexpr double u = 1.0;
212 constexpr double domainLeft = -0.1;
213 constexpr double domainWidth = 0.2;
214 const double rho = 1.0 + 0.2 * std::sin(2.0 * M_PI * (x(0) - u * t - domainLeft) / domainWidth);
215 Q[0] = rho;
216 Q[1] = rho * u;
217 for (int d=1; d<Dimensions; d++) {
218 Q[1+d] = 0.0;
219 }
220 Q[Dimensions+1] = p / (gamma - 1.0) + 0.5 * rho * u * u;
221"""
222solver.add_pde_term(
224 implementation=" constexpr double t = 0.0;\n" + EXACT_SOLUTION
225 )
226)
227if not args.periodic:
228 solver.add_pde_term(
229 exahype2.api.DirichletBoundaryConditions(implementation=EXACT_SOLUTION)
230 )
231
232cfl_constant = CFL_CONSTANTS[args.scheme]
233if is_finite_volume:
234 scheme_class = {
237 }[args.scheme]
238 solver.numerical_scheme = scheme_class(
239 solver, args.patch_size, cfl_constant=cfl_constant
240 )
242 solver, volume_size=args.volume_size
243 )
244 # The plot files carry 14 significant digits, as the errors of the finer
245 # meshes are far below the default precision
247 solver,
248 first_plot_time_stamp=0.0,
249 time_in_between_plots=plot_interval,
250 plotter_precision=14,
251 )
252else:
254 solver,
255 patch_size=args.patch_size,
256 rk_order=int(args.scheme[2]),
257 time_step_size=args.time_step_size,
258 cfl_constant=cfl_constant,
259 )
260 solver.grid_construction = exahype2.api.rkfd4.SetupSimulationOverRegularGrid(
261 solver, volume_size=args.volume_size
262 )
263 solver.plotter = exahype2.api.rkfd4.PlotAsVTKFile(
264 solver,
265 first_plot_time_stamp=0.0,
266 time_in_between_plots=plot_interval,
267 plotter_precision=14,
268 )
269project.add_solver(solver)
270
271project.set_global_simulation_parameters(
272 dimensions=args.dimensions,
273 offset=[-0.1] * args.dimensions,
274 size=[0.2] * args.dimensions,
275 min_end_time=args.end_time,
276 max_end_time=args.end_time,
277 periodic_BC=[args.periodic] * 3,
278)
279project.set_load_balancing(
280 "toolbox::loadbalancing::strategies::SpreadOutOnceGridStagnates",
281 "new ::exahype2::LoadBalancingConfiguration()",
282)
283project.compiler = task_graph_compiler
284if args.threads is not None:
285 project.set_number_of_threads(args.threads)
286
287project.set_Peano4_installation(
288 args.peano_root,
289 peano4.api.output.CompileMode.RELEASE
290 if args.build_mode == "release"
291 else peano4.api.output.CompileMode.DEBUG,
292)
293peano4_project = project.generate_Peano4_project(
294 verbosity=peano4.api.LoggerVerbosity.TEXT
295 if args.verbosity == "text"
296 else peano4.api.LoggerVerbosity.VISUALISATION
297)
298if args.cxx is not None:
299 peano4_project.output.makefile.set_CXX_compiler(args.cxx)
300peano4_project.generate()
301if not args.no_build:
302 peano4_project.build(
303 make_clean_first=not args.no_clean,
304 number_of_parallel_builds=args.build_jobs,
305 )
The conservative flux F(Q) of a hyperbolic PDE.
Definition Flux.py:8
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.
ExaHyPE 2 project.
Definition Project.py:15
Abstract base class for all ExaHyPE 2 solvers.
Definition Solver.py:17
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-...
Finite Volumes with a Rusanov Riemann solver and optimistic explicit Euler time stepping,...
Setup phase counterpart to RusanovWithExplicitEuler: sets the initial condition for a Finite Volume s...
Fourth-order central Finite Differences with explicit Runge-Kutta time stepping, realised over a bloc...
Plotting counterpart of the fourth-order Finite Differences with Runge-Kutta time stepping (exahype2....
Setup phase of the fourth-order Finite Differences with Runge-Kutta time stepping (exahype2....
Compilers are simple functions.
Definition __init__.py:1