Peano
Loading...
Searching...
No Matches
makeIC.py
Go to the documentation of this file.
1import h5py
2import argparse
3import numpy as np
4
5"""!
6Generates 1D uniform particle distribution with randomized small displacements.
7"""
8
9fileName = "test_sml_1D.hdf5"
10boxSize = 1.0
11
12parser = argparse.ArgumentParser(description="SPH benchmarking script")
13parser.add_argument(
14 "-np",
15 "--particle-number",
16 dest="particle_number",
17 required=True,
18 type=int,
19 help="Particle number (1D)",
20)
21parser.add_argument(
22 "-s",
23 "--seed",
24 dest="random_seed",
25 required=True,
26 type=int,
27 help="Seed for RNG",
28)
29parser.add_argument(
30 "-dx",
31 "--displacement",
32 dest="max_displacement",
33 required=False,
34 default=0.2499,
35 type=float,
36 help="Maximal displacement in units of interparticle distance",
37)
38
39
40args = parser.parse_args()
41npart = args.particle_number
42seed = args.random_seed
43max_displacement = args.max_displacement
44
45
46# ---------------------------------------------------
47
48# Build the arrays
49coords = np.zeros((npart, 3))
50v = np.zeros((npart, 3))
51ids = np.linspace(1, npart, npart)
52m = np.ones(npart)
53dx = boxSize / npart
54h = np.ones(npart) * 2.0 * dx
55u = np.ones(npart)
56
57# Get uniform coordinates.
58for i in range(npart):
59 coords[i, 0] = (i + 0.5) * dx
60
61# Now randomize them a bit.
62# Taking a completely randomly sampled particle distribution may lead
63# to trouble the codes aren't set up to handle, such as particles which
64# are either too close or too far from each other. So use a somewhat
65# regular distribution instead, by randomly displacing an initially
66# uniform particle distribution.
67
68rng = np.random.RandomState(seed=seed)
69# rng.random returns random numbers in [0, 1). Scale that.
70displacement = (2.0 * rng.random(coords.shape) - 1.0) * max_displacement * dx
71coords[:, 0] += displacement[:, 0]
72
73# File
74file = h5py.File(fileName, "w")
75
76# Header
77grp = file.create_group("/Header")
78grp.attrs["BoxSize"] = boxSize
79grp.attrs["NumPart_Total"] = [npart, 0, 0, 0, 0, 0]
80grp.attrs["NumPart_Total_HighWord"] = [0, 0, 0, 0, 0, 0]
81grp.attrs["NumPart_ThisFile"] = [npart, 0, 0, 0, 0, 0]
82# This is an addition for this test to be able to estimate the search radius.
83grp.attrs["NumPart_base"] = [npart]
84grp.attrs["Time"] = 0.0
85grp.attrs["NumFilesPerSnapshot"] = 1
86grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
87grp.attrs["Flag_Entropy_ICs"] = 0
88grp.attrs["Dimension"] = 1
89
90# Non-swift additions
91grp.attrs["RandomSeed"] = seed
92grp.attrs["MaxDisplacement"] = max_displacement
93grp.attrs["NumpyVersion"] = np.__version__
94
95# Units
96grp = file.create_group("/Units")
97grp.attrs["Unit length in cgs (U_L)"] = 1.0
98grp.attrs["Unit mass in cgs (U_M)"] = 1.0
99grp.attrs["Unit time in cgs (U_t)"] = 1.0
100grp.attrs["Unit current in cgs (U_I)"] = 1.0
101grp.attrs["Unit temperature in cgs (U_T)"] = 1.0
102
103# Particle group
104grp = file.create_group("/PartType0")
105grp.create_dataset("Coordinates", data=coords, dtype="d")
106grp.create_dataset("Velocities", data=v, dtype="f")
107grp.create_dataset("Masses", data=m, dtype="f")
108grp.create_dataset("SmoothingLength", data=h, dtype="f")
109grp.create_dataset("InternalEnergy", data=u, dtype="f")
110grp.create_dataset("ParticleIDs", data=ids, dtype="L")
111
112
113file.close()