Peano 4
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 random particle distribution.
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)
29
30args = parser.parse_args()
31npart = args.particle_number
32seed = args.random_seed
33
34
35# ---------------------------------------------------
36
37# Build the arrays
38coords = np.zeros((npart**2, 3))
39v = np.zeros((npart**2, 3))
40ids = np.linspace(1, npart**2, npart**2)
41m = np.ones(npart**2)
42dx = boxSize / npart
43h = np.ones(npart**2) * 1.2 * dx
44u = np.ones(npart**2)
45
46# Get uniform coordinates.
47ind = 0
48for i in range(npart):
49 x = (i + 0.5) * dx
50 for j in range(npart):
51 y = (j + 0.5) * dx
52 coords[ind, 0] = x
53 coords[ind, 1] = y
54 ind += 1
55
56# Now randomize them a bit.
57# Taking a completely randomly sampled particle distribution may lead
58# to trouble the codes aren't set up to handle, such as particles which
59# are either too close or too far from each other. So use a somewhat
60# regular distribution instead, by randomly displacing an initially
61# uniform particle distribution.
62
63rng = np.random.RandomState(seed=seed)
64# rng.random returns random numbers in [0, 1). Scale that.
65displacement = (2.0 * rng.random(coords.shape) - 1.0) * 0.199 * dx
66# displacement = np.zeros(coords.shape)
67coords[:, 0] += displacement[:, 0]
68coords[:, 1] += displacement[:, 1]
69
70
71# too many particles mean that the comparison python script takes ages.
72# too few particles mean that the search radii are going to be too big
73# for the cell size.
74# So let's scale the particles down into a smaller box than the actual
75# box size.
76scale = 1.0 / 5
77coords *= scale
78h *= scale
79# and shift them into the center
80coords += 0.4
81
82
83from matplotlib import pyplot as plt
84
85plt.figure()
86plt.scatter(coords[:, 0], coords[:, 1], s=1, marker=",")
87plt.show()
88
89
90# File
91file = h5py.File(fileName, "w")
92
93# Header
94grp = file.create_group("/Header")
95grp.attrs["BoxSize"] = boxSize
96grp.attrs["NumPart_Total"] = [npart**2, 0, 0, 0, 0, 0]
97grp.attrs["NumPart_Total_HighWord"] = [0, 0, 0, 0, 0, 0]
98grp.attrs["NumPart_ThisFile"] = [npart**2, 0, 0, 0, 0, 0]
99# This is an addition for this test to be able to estimate the search radius.
100grp.attrs["NumPart_base"] = [npart / scale]
101grp.attrs["Time"] = 0.0
102grp.attrs["NumFilesPerSnapshot"] = 1
103grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
104grp.attrs["Flag_Entropy_ICs"] = 0
105grp.attrs["Dimension"] = 2
106grp.attrs["RandomSeed"] = seed
107
108# Units
109grp = file.create_group("/Units")
110grp.attrs["Unit length in cgs (U_L)"] = 1.0
111grp.attrs["Unit mass in cgs (U_M)"] = 1.0
112grp.attrs["Unit time in cgs (U_t)"] = 1.0
113grp.attrs["Unit current in cgs (U_I)"] = 1.0
114grp.attrs["Unit temperature in cgs (U_T)"] = 1.0
115
116# Particle group
117grp = file.create_group("/PartType0")
118grp.create_dataset("Coordinates", data=coords, dtype="d")
119grp.create_dataset("Velocities", data=v, dtype="f")
120grp.create_dataset("Masses", data=m, dtype="f")
121grp.create_dataset("SmoothingLength", data=h, dtype="f")
122grp.create_dataset("InternalEnergy", data=u, dtype="f")
123grp.create_dataset("ParticleIDs", data=ids, dtype="L")
124
125
126file.close()