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 2D uniform particle distribution with randomized small displacements.
7"""
8
9fileName = "test_sml_2D.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**2, 3))
58v = np.zeros((npart**2, 3))
59ids = np.linspace(1, npart**2, npart**2)
60m = np.ones(npart**2)
61dx = boxSize / npart
62h = np.ones(npart**2) * 1.3 * dx
63u = np.ones(npart**2)
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 coords[ind, 0] = x
72 coords[ind, 1] = y
73 ind += 1
74
75# Now randomize them a bit.
76# Taking a completely randomly sampled particle distribution may lead
77# to trouble the codes aren't set up to handle, such as particles which
78# are either too close or too far from each other. So use a somewhat
79# regular distribution instead, by randomly displacing an initially
80# uniform particle distribution.
81
82rng = np.random.RandomState(seed=seed)
83# rng.random returns random numbers in [0, 1). Scale that.
84displacement = (2.0 * rng.random(coords.shape) - 1.0) * max_displacement * dx
85coords[:, 0] += displacement[:, 0]
86coords[:, 1] += displacement[:, 1]
87
88
89# too many particles mean that the comparison python script takes ages.
90# too few particles mean that the search radii are going to be too big
91# for the cell size.
92# So let's scale the particles down into a smaller box than the actual
93# box size.
94# Also, at full scale, the particles along the boundary/in corners start
95# disagreeing stupidly. So let's not do that.
96scale = 0.8
97coords *= scale
98h *= scale
99# and shift them into the center
100coords += 0.1
101
102if args.plot:
103 from matplotlib import pyplot as plt
104
105 plt.figure()
106 plt.scatter(coords[:, 0], coords[:, 1], s=1, marker=",")
107 plt.show()
108
109
110# File
111file = h5py.File(fileName, "w")
112
113# Header
114grp = file.create_group("/Header")
115grp.attrs["BoxSize"] = boxSize
116grp.attrs["NumPart_Total"] = [npart**2, 0, 0, 0, 0, 0]
117grp.attrs["NumPart_Total_HighWord"] = [0, 0, 0, 0, 0, 0]
118grp.attrs["NumPart_ThisFile"] = [npart**2, 0, 0, 0, 0, 0]
119# This is an addition for this test to be able to estimate the search radius.
120grp.attrs["NumPart_base"] = [npart]
121grp.attrs["Time"] = 0.0
122grp.attrs["NumFilesPerSnapshot"] = 1
123grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
124grp.attrs["Flag_Entropy_ICs"] = 0
125grp.attrs["Dimension"] = 2
126# Non-swift additions
127grp.attrs["RandomSeed"] = seed
128grp.attrs["MaxDisplacement"] = max_displacement
129grp.attrs["NumpyVersion"] = np.__version__
130
131# Units
132grp = file.create_group("/Units")
133grp.attrs["Unit length in cgs (U_L)"] = 1.0
134grp.attrs["Unit mass in cgs (U_M)"] = 1.0
135grp.attrs["Unit time in cgs (U_t)"] = 1.0
136grp.attrs["Unit current in cgs (U_I)"] = 1.0
137grp.attrs["Unit temperature in cgs (U_T)"] = 1.0
138
139# Particle group
140grp = file.create_group("/PartType0")
141grp.create_dataset("Coordinates", data=coords, dtype="d")
142grp.create_dataset("Velocities", data=v, dtype="f")
143grp.create_dataset("Masses", data=m, dtype="f")
144grp.create_dataset("SmoothingLength", data=h, dtype="f")
145grp.create_dataset("InternalEnergy", data=u, dtype="f")
146grp.create_dataset("ParticleIDs", data=ids, dtype="L")
147
148
149file.close()