Peano
Loading...
Searching...
No Matches
test_smoothing_length_computation_1D.py
Go to the documentation of this file.
1import os
2import argparse
3import numpy as np
4import h5py
5
6import peano4.api
7import exaparty
9import dastgen2
10
11import shutil
12
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 "-cs",
39 "--cell-size",
40 dest="cell_size",
41 type=float,
42 default=0.3,
43 help="Cell size (default 0.3)",
44)
45parser.add_argument(
46 "-pd",
47 "--peano-dir",
48 dest="peanodir",
49 default="../../../..",
50 help="Peano4 directory",
51)
52parser.add_argument(
53 "-asserts",
54 "--asserts",
55 dest="asserts",
56 action="store_true",
57 default=False,
58 help="Switch on assertions",
59)
60parser.add_argument(
61 "-debug",
62 "--debug",
63 dest="debug",
64 action="store_true",
65 default=False,
66 help="Switch on assertions",
67)
68parser.add_argument(
69 "-ic",
70 "--ic-file",
71 dest="ic_filename",
72 default="test_sml_1D.hdf5",
73 help="Initial conditions hdf5 file",
74)
75parser.add_argument(
76 "-n",
77 "--neighbours",
78 dest="nneigh",
79 type=int,
80 default=5,
81 help="Number of neighbours to search for",
82)
83parser.add_argument(
84 "-ht",
85 "--h-tolerance",
86 dest="h_tol",
87 type=float,
88 default=1.0e-6,
89 help="Threshold for convergence of smoothing length",
90)
91parser.add_argument(
92 "-k",
93 "--kernel",
94 dest="kernel",
95 type=str,
96 default="quartic_spline",
97 help="SPH kernel function to use.",
98 choices=exaparty.api.sphtools.sph_kernel_list,
99)
100
101args = parser.parse_args()
102nneigh = args.nneigh
103kernel = args.kernel
104h_tol = args.h_tol
105
106
109
110project_namespace = ["tests", "exaparty", "testSML1D"]
111
113 namespace=project_namespace, project_name="TestSML1D", executable="testSML"
114)
115
116cfl_factor = 0.2
117init_dt = 1.0e-16
118
119offset = [0, 0, 0]
120domain_size = [1, 1, 1]
121periodic_boundary_conditions = [False, False, False]
122
123
124# Initialise the SPH particle --------------------------------------------------
125# If multiple particle species are present, we can specify the parameters
126# independently for each case.
127
128name = "HydroPart"
129
130dimensions = 2 # peano needs this for whatever reason...
131hydro_dimensions = 1 # dimensions for the hydro
132
134 name,
135 dimensions_hydro=hydro_dimensions,
136 cfl_factor=cfl_factor,
137 initial_time_step_size=init_dt,
138 constant_time_step_size=True,
139 exaparty_project_namespace=project_namespace,
140 particles_per_cell=20,
141 min_h=args.cell_size,
142 max_h=args.cell_size,
143)
144
145
146
152
153
154# HYDRO_DIMENSION can be smaller than the Peano4 dimensions
155HYDRO_DIMENSIONS = hydro_dimensions
156
157# Particle number along 1 dimension
158hfile = h5py.File(args.ic_filename, "r")
159header = hfile["Header"]
160npart = header.attrs["NumPart_ThisFile"][0]
161npart_base = header.attrs["NumPart_base"][0]
162hfile.close()
163HYDRO_PART_NUMBER = npart
164
165# Time integration parameters
166GLOBAL_TIME_STEP_SIZE = init_dt # make some stuff up and hope it'll be stable enough
167CFL_CONSTANT = cfl_factor # make some stuff up and hope it'll be stable enough
168
169# EoS parameter ---------------------------------------------------------------
170gamma_hydro_list = [str(5 / 3), str(7 / 5), str(4 / 3), str(2 / 1)]
171gamma_hydro_list_symbols = [
172 "HYDRO_GAMMA_5_3",
173 "HYDRO_GAMMA_7_5",
174 "HYDRO_GAMMA_4_3",
175 "HYDRO_GAMMA_2_1",
176]
177GAMMA = gamma_hydro_list[0]
178GAMMA_HYDRO_SYMBOL = gamma_hydro_list_symbols[0]
179
180# SPH kernel -------------------------------------------------------
181KERNEL_SYMBOL = exaparty.api.sphtools.sph_kernel_macro_name[kernel]
182
183# Additional Particle Parameters -----------------------------------------------
184dx = domain_size[0] / npart_base
185hydro_H_max = 20 * dx
186hydro_h_max = exaparty.api.sphtools.sph_kernel_get_smoothing_length_from_search_radius(
187 hydro_H_max, kernel, hydro_dimensions
188)
189particle.h_hydro_max = hydro_h_max
190
191# Modify the eta factor to look for a precise number of neighbours.
192# Then round it up a little to avoid round-down errors.
193# In 1D, N_neigh = 2 * kernel_gamma * eta; see Price 2012
194particle.eta_factor = exaparty.api.sphtools.eta_from_number_of_neighbours(
195 nneigh, kernel=kernel, ndim=hydro_dimensions
196)
197
198particle.h_hydro_min = 1e-10
199particle.h_max_iterations = 50
200particle.h_tolerance = h_tol
201# particle.beta_av = 3.0
202# particle.alpha_av = 0.8
203# Optional: Reduce the mantissa size for particle attributes -------------------
204# Native size is 53 (double precision)
205# particle.mantissa_size = 23
206
207
208# deploy particle properties
209particle.set_parameters()
210
211
212# Add particle to the ExaParty project --------------------------------------------
213# Notice that you any change to the particle will not take effect after adding
214# the particle_set to the project
215particle_set = project.add_particle_species(particle)
216
217if args.debug:
218 build_mode = peano4.api.output.CompileMode.DEBUG
219elif args.asserts:
220 build_mode = peano4.api.output.CompileMode.ASSERTS
221else:
222 build_mode = peano4.api.output.CompileMode.RELEASE
223
224# Set Peano4 simulation parameters ---------------------------------------------
225project.set_global_simulation_parameters(
226 dimensions=dimensions,
227 offset=offset,
228 domain_size=domain_size,
229 min_end_time=init_dt,
230 max_end_time=init_dt,
231 first_plot_time_stamp=0.0,
232 time_in_between_plots=init_dt,
233 periodic_BC=periodic_boundary_conditions,
234 plotter_precision=8,
235)
236
237project.set_Peano4_installation(args.peanodir, build_mode)
238
239# project.set_load_balancing(
240# "toolbox::loadbalancing::strategies::RecursiveBipartition",
241# "new toolbox::loadbalancing::DefaultConfiguration()",
242# )
243
244
245
248
249project.algorithm_step_initial_conditions.add_action_set(
250 exaparty.api.input.InsertParticlesFromHDF5File(
251 particle_set=particle_set, filename=args.ic_filename
252 )
253)
254
255
256
280 "particles",
281 particle_set,
282 "globaldata::{}::getSpecies().getMinTimeStamp()".format(particle.name),
283)
284# for some reason, using hydro_dimensions here writes wrong output.
285particle_plotter.add_attribute_to_plot(particle.velocity, dimensions)
286particle_plotter.add_attribute_to_plot(particle.acceleration, dimensions)
287particle_plotter.add_attribute_to_plot(particle.mass, 1)
288particle_plotter.add_attribute_to_plot(particle.density, 1)
289particle_plotter.add_attribute_to_plot(particle.pressure, 1)
290particle_plotter.add_attribute_to_plot(particle.u, 1)
291particle_plotter.add_attribute_to_plot(particle.smoothL, 1)
292if args.asserts or args.debug:
293 particle_plotter.add_attribute_to_plot(particle.partid, 1)
294project.algorithm_step_plot.add_action_set(particle_plotter)
295
296
297
308
309peano4_project = project.generate_Peano4_project(verbose=args.verbose)
310
311# Some sanity checks ----------------------------------------------------------
312if GAMMA not in gamma_hydro_list:
313 print(f"Please check the value of GAMMA. You have chosen: {GAMMA}")
314
315print("-----------------------------------------------------------------------")
316print("SPH parameters summary:")
317print(f"Effective dimensions of the Simulation: {HYDRO_DIMENSIONS}")
318print(f"Global time-step size: {GLOBAL_TIME_STEP_SIZE}")
319print(f"Number of particles (1D): {HYDRO_PART_NUMBER}")
320print(f"Adiabatic index: {GAMMA}")
321print(f"Smoothing length free factor: {particle.eta_factor}")
322print("-----------------------------------------------------------------------")
323
324
325# Export Constants.h file ------------------------------------------------------
326peano4_project.constants.export_const_with_type(
327 "HYDRO_DIMENSIONS", str(HYDRO_DIMENSIONS), "double"
328)
329peano4_project.constants.export_const_with_type(
330 "HYDRO_PART_NUMBER", str(HYDRO_PART_NUMBER), "double"
331)
332peano4_project.constants.export_const_with_type(
333 "GLOBAL_TIME_STEP_SIZE", str(GLOBAL_TIME_STEP_SIZE), "double"
334)
335peano4_project.constants.export_const_with_type("GAMMA", GAMMA, "double")
336
337# Export symbols for compilation (e.g. for SPH kernel) -----------------------
338peano4_project.output.makefile.add_CXX_flag(
339 "-D" + "HYDRO_DIMENSION=" + str(HYDRO_DIMENSIONS)
340)
341peano4_project.output.makefile.add_CXX_flag("-D" + GAMMA_HYDRO_SYMBOL)
342peano4_project.output.makefile.add_CXX_flag("-D" + KERNEL_SYMBOL)
343
344
345
348
349# If we want to start a project from scratch, we may want to remove some of the generated code
350if args.clean_temporary_directories:
351 dirs = ["vertexdata/", "repositories/", "globaldata/", "observers/"]
352 for dir in dirs:
353 shutil.rmtree(dir)
354
355# It is safe (and recommended) to overwrite any existing main file, since this
356# is generated automatically by the ExaParty graph compiler.
357peano4_project.generate(
358 overwrite=peano4.api.output.Overwrite.Always, throw_away_data_after_generation=True
359)
360peano4_project.build(make_clean_first=True, number_of_parallel_builds=args.j)
361
362# remove output files from previous runs
363output_files = [
364 f
365 for f in os.listdir(".")
366 if f.endswith(".peano-patch-file") or f.endswith(".vtu") or f.endswith(".pvd")
367]
368for f in output_files:
369 os.remove(f)
ExaParty project.
Definition Project.py:27
This file is part of the ExaParty project.
Definition __init__.py:1