Peano
Loading...
Searching...
No Matches
test-density-calculation.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
13
14
17
18parser = argparse.ArgumentParser(description="SPH benchmarking script")
19parser.add_argument(
20 "-j", "--parallel-builds", dest="j", type=int, default=-1, help="Parallel builds"
21)
22parser.add_argument(
23 "-v",
24 "--verbose",
25 dest="verbose",
26 action="store_true",
27 default=False,
28 help="Verbose",
29)
30parser.add_argument(
31 "-c",
32 "--clean",
33 dest="clean_temporary_directories",
34 action="store_true",
35 help="Remove temporary directories before script starts",
36)
37parser.add_argument(
38 "-d",
39 "--dimensions",
40 dest="dimensions",
41 type=int,
42 default=2,
43 help="Spatial dimensions (default 2)",
44)
45parser.add_argument(
46 "-np",
47 "--particle-number",
48 dest="particle_number",
49 required=True,
50 type=int,
51 help="Particle number (1D)",
52)
53parser.add_argument(
54 "-et",
55 "--end-time",
56 dest="end_time",
57 type=float,
58 default=1.0,
59 help="End of simulation",
60)
61parser.add_argument(
62 "-dt",
63 "--timestep-size",
64 dest="timestep_size",
65 type=float,
66 default=1e-3,
67 help="Global timestep size (initial value)",
68)
69parser.add_argument(
70 "-cs",
71 "--cell-size",
72 dest="cell_size",
73 type=float,
74 default=0.3,
75 help="Cell size (default 0.3)",
76)
77parser.add_argument(
78 "-pd",
79 "--peano-dir",
80 dest="peanodir",
81 default="../../..",
82 help="Peano4 directory",
83)
84parser.add_argument(
85 "-asserts",
86 "--asserts",
87 dest="asserts",
88 action="store_true",
89 default=False,
90 help="Switch on assertions",
91)
92parser.add_argument(
93 "-k",
94 "--kernel",
95 dest="kernel",
96 type=str,
97 default="quartic_spline",
98 choices=sphtools.sph_kernel_list,
99 help="SPH kernel function to use.",
100)
101args = parser.parse_args()
102
103
104
107
108project_namespace = ["benchmarks", "exaparty", "densityTest"]
109
110dimensions = args.dimensions
111
113 namespace=project_namespace, project_name="test_density", executable="test_density"
114)
115
116# Effective SPH dimensions ----------------------------------------------------
117HYDRO_DIMENSIONS = 1
118
119# Particle number along 1 dimension -------------------------------------------
120HYDRO_PART_NUMBER = str(args.particle_number)
121
122# Declare the particle type
124 name="testDensityCalculation",
125 dimensions_hydro=HYDRO_DIMENSIONS,
126 cfl_factor=0.1,
127 initial_time_step_size=1e-4,
128 particles_per_cell=5,
129 min_h=args.cell_size,
130 max_h=args.cell_size,
131)
132
133# Add particle to the ExaParty project
134
135
141
142
143# Particle number along 1 dimension
144HYDRO_PART_NUMBER = args.particle_number
145
146# Time integration parameters
147GLOBAL_TIME_STEP_SIZE = args.timestep_size
148CFL_CONSTANT = particle._cfl_factor
149
150# EoS parameter ---------------------------------------------------------------
151gamma_hydro_list = [str(5 / 3), str(7 / 5), str(4 / 3), str(2 / 1)]
152gamma_hydro_list_symbols = [
153 "HYDRO_GAMMA_5_3",
154 "HYDRO_GAMMA_7_5",
155 "HYDRO_GAMMA_4_3",
156 "HYDRO_GAMMA_2_1",
157]
158GAMMA = gamma_hydro_list[0]
159GAMMA_HYDRO_SYMBOL = gamma_hydro_list_symbols[0]
160
161# SPH kernel -------------------------------------------------------
162
163kernel = args.kernel
164KERNEL_SYMBOL = sphtools.sph_kernel_macro_name[kernel]
165KERNEL_SUPPORT_RAD = sphtools.sph_kernel_H_over_h[HYDRO_DIMENSIONS - 1][kernel]
166
167
168# Additional Particle Parameters -----------------------------------------------
169hydro_h_max = args.cell_size / 2
170particle.h_hydro_max = hydro_h_max
171
172# particle.h_hydro_min = 1e-6
173# particle.h_max_iterations = 10
174# particle.h_tolerance = 1e-4
175# particle.eta_factor = 1.2348
176# particle.beta_av = 3.0
177# particle.alpha_av = 0.8
178# Optional: Reduce the mantissa size for particle attributes -------------------
179# Native size is 53 (double precision)
180# particle.mantissa_size = 23
181
182
183# deploy particle properties
184particle.set_parameters()
185
186
187# Add particle to the ExaParty project --------------------------------------------
188# Notice that you any change to the particle will not take effect after adding
189# the particle_set to the project
190particle_set = project.add_particle_species(particle)
191
192if args.asserts:
193 build_mode = peano4.api.output.CompileMode.ASSERTS
194else:
195 build_mode = peano4.api.output.CompileMode.RELEASE
196
197offset = [0, 0, 0]
198domain_size = [1, 1, 1]
199periodic_boundary_conditions = [False, False, False] # Periodic BC
200
201project.set_global_simulation_parameters(
202 dimensions=dimensions,
203 offset=offset,
204 domain_size=domain_size,
205 min_end_time=args.end_time,
206 max_end_time=0.1,
207 first_plot_time_stamp=0,
208 time_in_between_plots=args.end_time,
209 periodic_BC=periodic_boundary_conditions,
210 plotter_precision=8,
211)
212
213project.set_Peano4_installation(args.peanodir, build_mode)
214
215project.set_load_balancing(
216 "toolbox::loadbalancing::strategies::RecursiveBipartition",
217 "new toolbox::loadbalancing::DefaultConfiguration()",
218)
219
220
221
231
232# Snippet from library
234initialisation_snippet = ICs.initialisation_snippet
235
236#
237# Insert particles by coordinate
238#
239
240Npart = args.particle_number
241coords = np.zeros((Npart**HYDRO_DIMENSIONS, 3))
242
243if HYDRO_DIMENSIONS == 1:
244 coords[:, 1] = 0.5
245 coords[:, 0] = np.linspace(0, 1, Npart, endpoint=False)
246
247elif HYDRO_DIMENSIONS == 2:
248 x = np.linspace(offset, 1, Npart, endpoint=False)
249 y = np.linspace(offset, 1, Npart, endpoint=False)
250
251 xp, yp = np.meshgrid(x, y)
252 xp = xp.reshape((np.prod(xp.shape),))
253 yp = yp.reshape((np.prod(yp.shape),))
254
255 coords[:, 0] = xp
256 coords[:, 1] = yp
257
258elif HYDRO_DIMENSIONS == 3:
259 raise NotImplementedError
260
261print("inserting particles at:", coords)
262
263project.algorithm_step_initial_conditions.add_action_set(
265 particle_set=particle_set,
266 initialisation_call=initialisation_snippet,
267 coordinates=coords,
268 )
269)
270
271#
272# Insert particles from file
273#
274# project.algorithm_step_initial_conditions.add_action_set( peano4.api.toolbox.particles.sph.fileReader.InsertParticlesFromHDF5File(
275# particle_set = particle_set,
276# filename='sodShock.hdf5'
277# ))
278
279
280
305 "particles",
306 particle_set,
307 "globaldata::{}::getSpecies().getMinTimeStamp()".format(particle.name),
308)
309particle_plotter.add_attribute_to_plot(particle.velocity, dimensions)
310particle_plotter.add_attribute_to_plot(particle.acceleration, dimensions)
311particle_plotter.add_attribute_to_plot(particle.mass, 1)
312particle_plotter.add_attribute_to_plot(particle.density, 1)
313particle_plotter.add_attribute_to_plot(particle.pressure, 1)
314particle_plotter.add_attribute_to_plot(particle.smoothL, 1)
315if args.asserts:
316 particle_plotter.add_attribute_to_plot(particle.partid, 1)
317project.algorithm_step_plot.add_action_set(particle_plotter)
318
319
320
329
330peano4_project = project.generate_Peano4_project(verbose=args.verbose)
331
332# Some sanity checks ----------------------------------------------------------
333if GAMMA not in gamma_hydro_list:
334 print(f"Please check the value of GAMMA. You have chosen: {GAMMA}")
335
336print("-----------------------------------------------------------------------")
337print("SPH parameters summary:")
338print(f"Effective dimensions of the Simulation: {HYDRO_DIMENSIONS}")
339print(f"Global time-step size: {GLOBAL_TIME_STEP_SIZE}")
340print(f"Number of particles (1D): {HYDRO_PART_NUMBER}")
341print(f"Adiabatic index: {GAMMA}")
342print(f"Smoothing length free factor: {particle.eta_factor}")
343print("-----------------------------------------------------------------------")
344
345
346# Export Constants.h file ------------------------------------------------------
347peano4_project.constants.export_const_with_type(
348 "HYDRO_DIMENSIONS", str(HYDRO_DIMENSIONS), "double"
349)
350peano4_project.constants.export_const_with_type(
351 "HYDRO_PART_NUMBER", str(HYDRO_PART_NUMBER), "double"
352)
353peano4_project.constants.export_const_with_type(
354 "GLOBAL_TIME_STEP_SIZE", str(GLOBAL_TIME_STEP_SIZE), "double"
355)
356peano4_project.constants.export_const_with_type("GAMMA", GAMMA, "double")
357
358# Export symbols for compilation (e.g. for SPH kernel) -----------------------
359peano4_project.output.makefile.add_CXX_flag(
360 "-D" + "HYDRO_DIMENSION=" + str(HYDRO_DIMENSIONS)
361)
362peano4_project.output.makefile.add_CXX_flag("-D" + GAMMA_HYDRO_SYMBOL)
363peano4_project.output.makefile.add_CXX_flag("-D" + KERNEL_SYMBOL)
364
365
366
369
370# If we want to start a project from scratch, we may want to remove some of the generated code
371if args.clean_temporary_directories:
372 dirs = ["vertexdata/", "repositories/", "globaldata/", "observers/"]
373 for dir in dirs:
374 shutil.rmtree(dir)
375
376# It is safe (and recommended) to overwrite any existing main file, since this
377# is generated automatically by the ExaParty graph compiler.
378peano4_project.generate(
379 overwrite=peano4.api.output.Overwrite.Always, throw_away_data_after_generation=True
380)
381peano4_project.build(make_clean_first=True, number_of_parallel_builds=args.j)
382
383# remove output files from previous runs
384output_files = [
385 f
386 for f in os.listdir(".")
387 if f.endswith(".peano-patch-file") or f.endswith(".vtu") or f.endswith(".pvd")
388]
389for f in output_files:
390 os.remove(f)
ExaParty project.
Definition Project.py:27
Initialisation snippet for testing the calculation of the SPH density.
This file is part of the ExaParty project.
Definition __init__.py:1
This file is part of the ExaParty project.
Definition __init__.py:1