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 3D uniform particle distribution with randomized small displacements.
7"""
8
9fileName = "test_sml_3D.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 per dimension",
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 "-p",
31 "--plot",
32 dest="plot",
33 action="store_true",
34 default=False,
35 help="Plot historgram of particle positions",
36)
37parser.add_argument(
38 "-dx",
39 "--displacement",
40 dest="max_displacement",
41 required=False,
42 default=0.2499,
43 type=float,
44 help="Maximal displacement in units of interparticle distance",
45)
46
47
48args = parser.parse_args()
49npart = args.particle_number
50seed = args.random_seed
51max_displacement = args.max_displacement
52
53
54# ---------------------------------------------------
55
56# Build the arrays
57coords = np.zeros((npart**3, 3))
58v = np.zeros((npart**3, 3))
59ids = np.linspace(1, npart**3, npart**3)
60m = np.ones(npart**3)
61dx = boxSize / npart
62h = np.ones(npart**3) * 2.0 * dx
63u = np.ones(npart**3)
64
65# Get uniform coordinates.
66ind = 0
67for i in range(npart):
68 x = (i + 0.5) * dx
69 for j in range(npart):
70 y = (j + 0.5) * dx
71 for k in range(npart):
72 z = (k + 0.5) * dx
73
74 coords[ind, 0] = x
75 coords[ind, 1] = y
76 coords[ind, 2] = z
77 ind += 1
78
79# Now randomize them a bit.
80# Taking a completely randomly sampled particle distribution may lead
81# to trouble the codes aren't set up to handle, such as particles which
82# are either too close or too far from each other. So use a somewhat
83# regular distribution instead, by randomly displacing an initially
84# uniform particle distribution.
85
86rng = np.random.RandomState(seed=seed)
87# rng.random returns random numbers in [0, 1). Scale that.
88displacement = (2.0 * rng.random(coords.shape) - 1.0) * max_displacement * dx
89# displacement = np.zeros(coords.shape)
90coords[:, 0] += displacement[:, 0]
91coords[:, 1] += displacement[:, 1]
92coords[:, 2] += displacement[:, 2]
93
94
95# too many particles mean that the comparison python script takes ages.
96# too few particles mean that the search radii are going to be too big
97# for the cell size.
98# So let's scale the particles down into a smaller box than the actual
99# box size.
100# Also, at full scale, the particles along the boundary/in corners start
101# disagreeing stupidly. So let's not do that.
102scale = 0.6
103coords *= scale
104h *= scale
105# and shift them into the center
106coords += 0.2
107
108if args.plot:
109 from matplotlib import pyplot as plt
110
111 plt.figure()
112 plt.scatter(coords[:, 0], coords[:, 1], s=1, marker=",")
113 plt.show()
114
115
116# File
117file = h5py.File(fileName, "w")
118
119# Header
120grp = file.create_group("/Header")
121grp.attrs["BoxSize"] = boxSize
122grp.attrs["NumPart_Total"] = [npart**3, 0, 0, 0, 0, 0]
123grp.attrs["NumPart_Total_HighWord"] = [0, 0, 0, 0, 0, 0]
124grp.attrs["NumPart_ThisFile"] = [npart**3, 0, 0, 0, 0, 0]
125# This is an addition for this test to be able to estimate the search radius.
126grp.attrs["NumPart_base"] = [npart]
127grp.attrs["Time"] = 0.0
128grp.attrs["NumFilesPerSnapshot"] = 1
129grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
130grp.attrs["Flag_Entropy_ICs"] = 0
131grp.attrs["Dimension"] = 3
132# Non-swift additions
133grp.attrs["RandomSeed"] = seed
134grp.attrs["MaxDisplacement"] = max_displacement
135grp.attrs["NumpyVersion"] = np.__version__
136
137# Units
138grp = file.create_group("/Units")
139grp.attrs["Unit length in cgs (U_L)"] = 1.0
140grp.attrs["Unit mass in cgs (U_M)"] = 1.0
141grp.attrs["Unit time in cgs (U_t)"] = 1.0
142grp.attrs["Unit current in cgs (U_I)"] = 1.0
143grp.attrs["Unit temperature in cgs (U_T)"] = 1.0
144
145# Particle group
146grp = file.create_group("/PartType0")
147grp.create_dataset("Coordinates", data=coords, dtype="d")
148grp.create_dataset("Velocities", data=v, dtype="f")
149grp.create_dataset("Masses", data=m, dtype="f")
150grp.create_dataset("SmoothingLength", data=h, dtype="f")
151grp.create_dataset("InternalEnergy", data=u, dtype="f")
152grp.create_dataset("ParticleIDs", data=ids, dtype="L")
153
154
155file.close()