Peano
Loading...
Searching...
No Matches
makeIC.py
Go to the documentation of this file.
1import h5py
2from numpy import *
3
4# Generates a swift IC file for the 1D Sod Shock in a periodic box
5
6# Parameters
7gamma = 5.0 / 3.0 # Gas adiabatic index
8numPart_L = 800 # Number of particles in the left state
9x_min = -1.0 # left box boundary
10x_max = 1.0 # right box boundary
11rho_L = 1.0 # Density left state
12rho_R = 0.125 # Density right state
13v_L = 0.0 # Velocity left state
14v_R = 0.0 # Velocity right state
15P_L = 1.0 # Pressure left state
16P_R = 0.1 # Pressure right state
17fileName = "sodShock.hdf5"
18
19
20# ---------------------------------------------------
21
22# Find how many particles we actually have
23boxSize = x_max - x_min
24numPart_R = int(numPart_L * (rho_R / rho_L))
25numPart = numPart_L + numPart_R
26
27# Now get the distances
28delta_L = (boxSize / 2) / numPart_L
29delta_R = (boxSize / 2) / numPart_R
30offset_L = delta_L / 2
31offset_R = delta_R / 2
32
33# Build the arrays
34coords = zeros((numPart, 3))
35v = zeros((numPart, 3))
36ids = linspace(1, numPart, numPart)
37m = zeros(numPart)
38h = zeros(numPart)
39u = zeros(numPart)
40
41# Set the particles on the left
42for i in range(numPart_L):
43 coords[i, 0] = x_min + offset_L + i * delta_L
44 u[i] = P_L / (rho_L * (gamma - 1.0))
45 h[i] = 1.2348 * delta_L
46 m[i] = boxSize * rho_L / (2.0 * numPart_L)
47 v[i, 0] = v_L
48
49# Set the particles on the right
50for j in range(numPart_R):
51 i = numPart_L + j
52 coords[i, 0] = offset_R + j * delta_R
53 u[i] = P_R / (rho_R * (gamma - 1.0))
54 h[i] = 1.2348 * delta_R
55 m[i] = boxSize * rho_R / (2.0 * numPart_R)
56 v[i, 0] = v_R
57
58# Shift particles
59coords[:, 0] -= x_min
60coords[:, 1] = 0.5
61
62# File
63file = h5py.File(fileName, "w")
64
65# Header
66grp = file.create_group("/Header")
67grp.attrs["BoxSize"] = boxSize
68grp.attrs["NumPart_Total"] = [numPart, 0, 0, 0, 0, 0]
69grp.attrs["NumPart_Total_HighWord"] = [0, 0, 0, 0, 0, 0]
70grp.attrs["NumPart_ThisFile"] = [numPart, 0, 0, 0, 0, 0]
71grp.attrs["Time"] = 0.0
72grp.attrs["NumFilesPerSnapshot"] = 1
73grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
74grp.attrs["Flag_Entropy_ICs"] = 0
75grp.attrs["Dimension"] = 1
76
77# Units
78grp = file.create_group("/Units")
79grp.attrs["Unit length in cgs (U_L)"] = 1.0
80grp.attrs["Unit mass in cgs (U_M)"] = 1.0
81grp.attrs["Unit time in cgs (U_t)"] = 1.0
82grp.attrs["Unit current in cgs (U_I)"] = 1.0
83grp.attrs["Unit temperature in cgs (U_T)"] = 1.0
84
85# Particle group
86grp = file.create_group("/PartType0")
87grp.create_dataset("Coordinates", data=coords, dtype="d")
88grp.create_dataset("Velocities", data=v, dtype="f")
89grp.create_dataset("Masses", data=m, dtype="f")
90grp.create_dataset("SmoothingLength", data=h, dtype="f")
91grp.create_dataset("InternalEnergy", data=u, dtype="f")
92grp.create_dataset("ParticleIDs", data=ids, dtype="L")
93
94
95file.close()