-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_gadget.py
86 lines (59 loc) · 1.95 KB
/
test_gadget.py
1
2
3
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
try:
from lenstools.simulations import Gadget2Snapshot
except ImportError:
import sys
sys.path.append("..")
from lenstools.simulations import Gadget2Snapshot
import numpy as np
from astropy.units import Mpc,m,s
def test_read():
#Open the gadget snapshot
snapshot = Gadget2Snapshot.open("Data/gadget/snapshot_001")
#Get the particles positions and play with the indices
pospart = snapshot.getPositions(first=500,last=1000)
pos = snapshot.getPositions()
assert np.all(pospart==pos[500:1000])
#Visualize the snapshot
snapshot.visualize(s=1)
#Save the visualization
snapshot.ax.set_title(r"${0}^3$ particles".format(snapshot.header["num_particles_total_side"]))
snapshot.savefig("gadget_snapshot.png")
#Close the snapshot
snapshot.close()
def test_write():
#Create an empty gadget snapshot
snap = Gadget2Snapshot()
#Generate random positions and velocities
NumPart = 32**3
x = np.random.normal(loc=7.0,scale=5.0,size=(NumPart,3)) * Mpc
v = np.random.uniform(-1,1,size=(NumPart,3)) * m / s
#Put the particles in the snapshot
snap.setPositions(x)
snap.setVelocities(v)
#Generate minimal header
snap.setHeaderInfo()
#Visualize
snap.visualize(s=1)
snap.savefig("gadget_initial_condition.png")
#Write the snapshot
snap.write("gadget_ic")
def test_paramfile():
#Create an empty gadget snapshot
snap = Gadget2Snapshot()
#Generate random positions and velocities
NumPart = 32**3
x = np.random.normal(loc=7.0,scale=5.0,size=(NumPart,3)) * Mpc
v = np.zeros((NumPart,3)) * m / s
#Put the particles in the snapshot
snap.setPositions(x)
snap.setVelocities(v)
#Generate minimal header
snap.setHeaderInfo()
#Split the particles between two files
snap.write("gadget_sphere",files=2)
#Generate the parameter file that will determine the evolution
snap.writeParameterFile("gadget_sphere.param")
#Generate a file with the scale factor of the output snapshots
z = np.arange(90.0,0.0,-10.0)
a = 1.0 / (1 + z)
np.savetxt("outputs.txt",a)