Peano
Loading...
Searching...
No Matches
noh.py
Go to the documentation of this file.
1import os
2import argparse
3import numpy as np
4
5import peano4.api
6import exaparty
7import exaparty.api.sphtools as sphtools
8import dastgen2
9
10import shutil
11
12
13
16
17parser = argparse.ArgumentParser(description="SPH test script")
18parser.add_argument(
19 "-j",
20 "--parallel-builds",
21 dest="j",
22 type=int,
23 default=-1,
24 help="Parallel builds",
25)
26parser.add_argument(
27 "-v",
28 "--verbose",
29 dest="verbose",
30 action="store_true",
31 default=False,
32 help="Verbose",
33)
34parser.add_argument(
35 "-c",
36 "--clean",
37 dest="clean_temporary_directories",
38 action="store_true",
39 help="Remove temporary directories before script starts",
40)
41parser.add_argument(
42 "-np",
43 "--particle-number",
44 dest="particle_number",
45 required=True,
46 type=int,
47 help="Particle number (1D)",
48)
49parser.add_argument(
50 "-et",
51 "--end-time",
52 dest="end_time",
53 type=float,
54 default=0.2,
55 help="End of simulation",
56)
57parser.add_argument(
58 "-plot",
59 "--plot-delta",
60 dest="plot_delta",
61 type=float,
62 default=0.01,
63 help="Plot delta (0 switches off)",
64)
65parser.add_argument(
66 "-dt",
67 "--timestep-size",
68 dest="timestep_size",
69 type=float,
70 default=1e-4,
71 help="Global timestep size (initial value)",
72)
73parser.add_argument(
74 "-cfl",
75 "--cfl-factor",
76 dest="cfl_factor",
77 type=float,
78 default=0.1,
79 help="Value of the CFL constant",
80)
81parser.add_argument(
82 "-csmax",
83 "--cell-size-max",
84 dest="cell_size_max",
85 type=float,
86 default=0.3,
87 help="Cell size (default 0.3)",
88)
89parser.add_argument(
90 "-csmin",
91 "--cell-size-min",
92 dest="cell_size_min",
93 type=float,
94 default=0.3,
95 help="Cell size (default 0.3)",
96)
97parser.add_argument(
98 "-pd",
99 "--peano-dir",
100 dest="peanodir",
101 default="../../../..",
102 help="Peano4 directory",
103)
104parser.add_argument(
105 "-asserts",
106 "--asserts",
107 dest="asserts",
108 action="store_true",
109 default=False,
110 help="Switch on assertions",
111)
112parser.add_argument(
113 "-debug",
114 "--debug",
115 dest="debug",
116 action="store_true",
117 default=False,
118 help="Switch on full debug checks",
119)
120parser.add_argument(
121 "-k",
122 "--kernel",
123 dest="kernel",
124 type=str,
125 default="quartic_spline",
126 choices=sphtools.sph_kernel_list,
127 help="SPH kernel function to use.",
128)
129parser.add_argument(
130 "-t",
131 "--trees",
132 dest="trees",
133 default=0,
134 type=int,
135 help="Number of trees (subdomains) (0 uses all threads)",
136)
137
138args = parser.parse_args()
139
140
141
144
145project_namespace = ["tests", "exaparty", "noh3D"]
146
147project = exaparty.api.Project(
148 namespace=project_namespace, project_name="Noh3D", executable="noh3D"
149)
150
151# Initialise the SPH particle --------------------------------------------------
152# If multiple particle species are present, we can specify the parameters
153# independently for each case.
154
155name = "HydroPart"
156
157dimensions = 3
158hydro_dimensions = 3 # dimensions for the hydro
159
161 name,
162 dimensions_hydro=hydro_dimensions,
163 cfl_factor=args.cfl_factor,
164 initial_time_step_size=args.timestep_size,
165 constant_time_step_size=True,
166 exaparty_project_namespace=project_namespace,
167 min_h=args.cell_size_min,
168 max_h=args.cell_size_max,
169)
170
171
172
178
179offset = [0, 0, 0]
180domain_size = [2, 2, 2]
181periodic_boundary_conditions = [False, False, False]
182
183# HYDRO_DIMENSION can be smaller than the Peano4 dimensions
184HYDRO_DIMENSIONS = hydro_dimensions
185
186# Particle number along 1 dimension
187HYDRO_PART_NUMBER = args.particle_number
188
189# Time integration parameters
190GLOBAL_TIME_STEP_SIZE = args.timestep_size
191CFL_CONSTANT = args.cfl_factor
192
193# EoS parameter ---------------------------------------------------------------
194gamma_hydro_list = [str(5 / 3), str(7 / 5), str(4 / 3), str(2 / 1)]
195gamma_hydro_list_symbols = [
196 "HYDRO_GAMMA_5_3",
197 "HYDRO_GAMMA_7_5",
198 "HYDRO_GAMMA_4_3",
199 "HYDRO_GAMMA_2_1",
200]
201GAMMA = gamma_hydro_list[0]
202GAMMA_HYDRO_SYMBOL = gamma_hydro_list_symbols[0]
203
204# SPH kernel -------------------------------------------------------
205
206kernel = args.kernel
207KERNEL_SYMBOL = sphtools.sph_kernel_macro_name[kernel]
208KERNEL_SUPPORT_RAD = sphtools.sph_kernel_H_over_h[HYDRO_DIMENSIONS - 1][kernel]
209
210
211# Additional Particle Parameters -----------------------------------------------
212depth = 0
213cellsize = domain_size[0]
214while cellsize > args.cell_size_min:
215 cellsize /= 3.0
216 depth += 1
217 if depth == 100:
218 raise ValueError("Suspicious depth")
219
220dx = domain_size[0] / args.particle_number
221hydro_H_max = 0.499 * cellsize
222if hydro_H_max < 3.0 * dx:
223 raise ValueError("Too small hydro_H_max", 3.0 * dx, hydro_H_max)
224hydro_h_max = exaparty.api.sphtools.sph_kernel_get_smoothing_length_from_interaction_radius(
225 hydro_H_max, kernel, hydro_dimensions
226)
227
228particle.h_hydro_max = hydro_h_max
229
230# particle.h_hydro_min = 1e-6
231# particle.h_max_iterations = 10
232# particle.h_tolerance = 1e-4
233# particle.eta_factor = 1.2348
234# particle.beta_av = 3.0
235# particle.alpha_av = 0.8
236
237# Optional: Reduce the mantissa size for particle attributes -------------------
238# Native size is 53 (double precision)
239# particle.mantissa_size = 23
240
241
242# deploy particle properties
243# This needs to be done if you're changing any of the default values.
244particle.set_parameters()
245
246
247# Add particle to the ExaParty project --------------------------------------------
248# Notice that you any change to the particle will not take effect after adding
249# the particle_set to the project
250particle_set = project.add_particle_species(particle)
251
252if args.debug:
253 build_mode = peano4.api.output.CompileMode.DEBUG
254elif args.asserts:
255 build_mode = peano4.api.output.CompileMode.ASSERTS
256else:
257 build_mode = peano4.api.output.CompileMode.RELEASE
258
259# Set Peano4 simulation parameters ---------------------------------------------
260project.set_global_simulation_parameters(
261 dimensions=dimensions,
262 offset=offset,
263 domain_size=domain_size,
264 min_end_time=args.end_time,
265 max_end_time=0.0,
266 first_plot_time_stamp=0.0,
267 time_in_between_plots=args.plot_delta,
268 periodic_BC=periodic_boundary_conditions,
269 plotter_precision=8,
270)
271
272# Choose realisation
273parallelisation = "domain-decomposition"
274# parallelisation = "task-graph"
275# parallelisation = "multisweep-task-graph"
276# storage = "scattered"
277# storage = "continuous-per-vertex"
278storage = "global-continuous"
279sorting = "multiscale-sort"
280# sorting = "bucket-sort"
281kernel_optimisation = "no-optimisation"
282project.set_realisation(
283 parallelisation=parallelisation,
284 storage=storage,
285 sorting=sorting,
286 kernel_optimisation=kernel_optimisation,
287)
288
289project.set_Peano4_installation(args.peanodir, build_mode)
290
291project.set_load_balancing(
292 "toolbox::loadbalancing::strategies::SpreadOutOnceGridStagnates",
293 f"new toolbox::loadbalancing::DefaultConfiguration({args.trees})",
294)
295
296
297
300
301# Noh problem (3D) snippet --------------------------------------------------
302# The particle attributes are set via an initalisation snippet that sets
303# the initial conditions specific for this problem, i.e. the velocity field
304# that converges towards the center of the domain and that generates the
305# collapse.
306
307ICs = exaparty.api.scenario.NohProblemIC(dimensions=HYDRO_DIMENSIONS)
308initialisation_snippet = ICs.initialisation_snippet
309
310project.algorithm_step_initial_conditions.add_action_set(
312 particle_set=particle_set,
313 initialisation_call=initialisation_snippet,
314 distance_between_particles_along_one_axis=(
315 domain_size[0] / (args.particle_number)
316 ),
317 additional_includes="""
318#include "exaparty/kernels/legacy/equation_of_state.h"
319#include "exaparty/kernels/legacy/kernel_hydro.h"
320""",
321 )
322)
323
324
348 "particles",
349 particle_set,
350 "globaldata::{}::getSpecies().getMinTimeStamp()".format(particle.name),
351)
352particle_plotter.add_attribute_to_plot(particle.velocity, dimensions)
353particle_plotter.add_attribute_to_plot(particle.acceleration, dimensions)
354particle_plotter.add_attribute_to_plot(particle.mass, 1)
355particle_plotter.add_attribute_to_plot(particle.density, 1)
356particle_plotter.add_attribute_to_plot(particle.pressure, 1)
357particle_plotter.add_attribute_to_plot(particle.u, 1)
358particle_plotter.add_attribute_to_plot(particle.smoothL, 1)
359if args.asserts or args.debug:
360 particle_plotter.add_attribute_to_plot(particle.partid, 1)
361 particle_plotter.add_attribute_to_plot(particle.density_neighbourcount, 1)
362 particle_plotter.add_attribute_to_plot(particle.force_neighbourcount, 1)
363project.algorithm_step_plot.add_action_set(particle_plotter)
364
365
366
377
378peano4_project = project.generate_Peano4_project(verbose=args.verbose)
379
380# Some sanity checks ----------------------------------------------------------
381if GAMMA not in gamma_hydro_list:
382 print(f"Please check the value of GAMMA. You have chosen: {GAMMA}")
383
384print("-----------------------------------------------------------------------")
385print("SPH parameters summary:")
386print(f"Effective dimensions of the Simulation: {HYDRO_DIMENSIONS}")
387print(f"Global time-step size: {GLOBAL_TIME_STEP_SIZE}")
388print(f"Number of particles (1D): {HYDRO_PART_NUMBER}")
389print(f"Adiabatic index: {GAMMA}")
390print(f"Smoothing length free factor: {particle.eta_factor}")
391print("-----------------------------------------------------------------------")
392
393
394# Export Constants.h file ------------------------------------------------------
395peano4_project.constants.export_const_with_type(
396 "HYDRO_DIMENSIONS", str(HYDRO_DIMENSIONS), "double"
397)
398peano4_project.constants.export_const_with_type(
399 "HYDRO_PART_NUMBER", str(HYDRO_PART_NUMBER), "double"
400)
401peano4_project.constants.export_const_with_type(
402 "GLOBAL_TIME_STEP_SIZE", str(GLOBAL_TIME_STEP_SIZE), "double"
403)
404peano4_project.constants.export_const_with_type("GAMMA", GAMMA, "double")
405
406# Export symbols for compilation (e.g. for SPH kernel) -----------------------
407peano4_project.output.makefile.add_CXX_flag(
408 "-D" + "HYDRO_DIMENSION=" + str(HYDRO_DIMENSIONS)
409)
410peano4_project.output.makefile.add_CXX_flag("-D" + GAMMA_HYDRO_SYMBOL)
411peano4_project.output.makefile.add_CXX_flag("-D" + KERNEL_SYMBOL)
412
413
414
417
418# If we want to start a project from scratch, we may want to remove some of the generated code
419if args.clean_temporary_directories:
420 dirs = ["vertexdata/", "repositories/", "globaldata/", "observers/"]
421 for dir in dirs:
422 shutil.rmtree(dir)
423
424# It is safe (and recommended) to overwrite any existing main file, since this
425# is generated automatically by the ExaParty graph compiler.
426peano4_project.generate(
427 overwrite=peano4.api.output.Overwrite.Always, throw_away_data_after_generation=True
428)
429peano4_project.build(make_clean_first=True, number_of_parallel_builds=args.j)
430
431# remove output files from previous runs
432output_files = [
433 f
434 for f in os.listdir(".")
435 if f.endswith(".peano-patch-file") or f.endswith(".vtu") or f.endswith(".pvd")
436]
437for f in output_files:
438 os.remove(f)
ExaParty project.
Definition Project.py:27
Initialisation snippet for the Noh problem in different spatial dimensions.
Definition NohProblem.py:5
This file is part of the ExaParty project.
Definition __init__.py:1