makeIC.py 4.02 KB
Newer Older
1
2
###############################################################################
 # This file is part of SWIFT.
3
 # Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk),
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 #                    Matthieu Schaller (matthieu.schaller@durham.ac.uk)
 # 
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Lesser General Public License as published
 # by the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 # 
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 # 
 # You should have received a copy of the GNU Lesser General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 # 
 ##############################################################################

import h5py
import random
from numpy import *

# Generates a swift IC file for the Sod Shock in a periodic box

# Parameters
periodic= 1      # 1 For periodic box
Pedro Gonnet's avatar
Pedro Gonnet committed
29
30
factor = 8
boxSize = [ 1.0 , 1.0/factor , 1.0/factor ]
31
32
33
34
35
L = 100           # Number of particles along one axis
P1 = 1.           # Pressure left state
P2 = 0.1795       # Pressure right state
gamma = 5./3.     # Gas adiabatic index
fileName = "sodShock.hdf5" 
Pedro Gonnet's avatar
Pedro Gonnet committed
36
vol = boxSize[0] * boxSize[1] * boxSize[2]
37
38
39
40
41


#---------------------------------------------------

#Read in high density glass
Pedro Gonnet's avatar
Pedro Gonnet committed
42
43
# glass1 = h5py.File("../Glass/glass_200000.hdf5")
glass1 = h5py.File("glass_001.hdf5")
44
pos1 = glass1["/PartType0/Coordinates"][:,:]
Pedro Gonnet's avatar
Pedro Gonnet committed
45
pos1 = pos1 / factor # Particles are in [0:0.25, 0:0.25, 0:0.25]
46
47


48
#Read in high density glass
Pedro Gonnet's avatar
Pedro Gonnet committed
49
50
# glass2 = h5py.File("../Glass/glass_50000.hdf5")
glass2 = h5py.File("glass_002.hdf5")
51
pos2 = glass2["/PartType0/Coordinates"][:,:]
Pedro Gonnet's avatar
Pedro Gonnet committed
52
pos2 = pos2 / factor # Particles are in [0:0.25, 0:0.25, 0:0.25]
53
54


55
#Generate high density region
56
rho1 = 1.
Pedro Gonnet's avatar
Pedro Gonnet committed
57
58
59
60
61
coord1 = append(pos1, pos1 + [0.125, 0, 0], 0)
coord1 = append(coord1, coord1 + [0.25, 0, 0], 0)
# coord1 = append(pos1, pos1 + [0, 0.5, 0], 0)
# coord1 = append(coord1, pos1 + [0, 0, 0.5], 0)
# coord1 = append(coord1, pos1 + [0, 0.5, 0.5], 0)
62
63
N1 = size(coord1)/3
v1 = zeros((N1, 3))
64
65
66
h1 = ones(N1) * 2.251 * 0.5 * vol / (size(pos1)/3)**(1./3.)
u1 = ones(N1) * P1 / ((gamma - 1.) * rho1)
m1 = ones(N1) * vol * 0.5 * rho1 / N1
67
68

#Generate low density region
69
rho2 = 0.25
Pedro Gonnet's avatar
Pedro Gonnet committed
70
71
72
73
74
coord2 = append(pos2, pos2 + [0.125, 0, 0], 0)
coord2 = append(coord2, coord2 + [0.25, 0, 0], 0)
# coord2 = append(pos2, pos2 + [0, 0.5, 0], 0)
# coord2 = append(coord2, pos2 + [0, 0, 0.5], 0)
# coord2 = append(coord2, pos2 + [0, 0.5, 0.5], 0)
75
76
N2 = size(coord2)/3
v2 = zeros((N2, 3))
77
78
79
h2 = ones(N2) * 2.251 * 0.5 * vol / (size(pos2)/3)**(1./3.)
u2 = ones(N2) * P2 / ((gamma - 1.) * rho2)
m2 = ones(N2) * vol * 0.5 * rho2 / N2
80
81
82

#Merge arrays
numPart = N1 + N2
Pedro Gonnet's avatar
Pedro Gonnet committed
83
coords = append(coord1, coord2+[0.5, 0., 0.], 0)
84
85
86
87
v = append(v1, v2,0)
h = append(h1, h2,0)
u = append(u1, u2,0)
m = append(m1, m2,0)
88
ids = zeros(numPart, dtype='L')
89
for i in range(1, numPart+1):
90
    ids[i-1] = i
91

92
93
94
#Final operations
h /= 2

95
96
97
98
99
#File
file = h5py.File(fileName, 'w')

# Header
grp = file.create_group("/Header")
Pedro Gonnet's avatar
Pedro Gonnet committed
100
grp.attrs["BoxSize"] = boxSize
101
102
103
104
105
106
grp.attrs["NumPart_Total"] =  [numPart, 0, 0, 0, 0, 0]
grp.attrs["NumPart_Total_HighWord"] = [0, 0, 0, 0, 0, 0]
grp.attrs["NumPart_ThisFile"] = [numPart, 0, 0, 0, 0, 0]
grp.attrs["Time"] = 0.0
grp.attrs["NumFilesPerSnapshot"] = 1
grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
107
grp.attrs["Flag_Entropy_ICs"] = 0
108
109
110
111
112
113
114

#Runtime parameters
grp = file.create_group("/RuntimePars")
grp.attrs["PeriodicBoundariesOn"] = periodic

#Particle group
grp = file.create_group("/PartType0")
115
116
117
118
119
120
121
grp.create_dataset('Coordinates', data=coords, dtype='d')
grp.create_dataset('Velocities', data=v, dtype='f')
grp.create_dataset('Masses', data=m, dtype='f')
grp.create_dataset('SmoothingLength', data=h, dtype='f')
grp.create_dataset('InternalEnergy', data=u, dtype='f')
grp.create_dataset('ParticleIDs', data=ids, dtype='L')

122
123
124
125

file.close()