forked from ibackus/ICgen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pos_class.py
194 lines (141 loc) · 5.53 KB
/
pos_class.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-
"""
Defines a function to randomly generate particle positions according to
the desired surface density profile (sigma vs r) and the vertical profile
(rho vs r,z).
Created on Mon Jan 27 18:48:04 2014
@author: ibackus
"""
__version__ = "$Revision: 1 $"
# $Source$
__iversion__ = int(filter(str.isdigit,__version__))
# External packages
import pynbody
SimArray = pynbody.array.SimArray
import numpy as np
# ICgen packages
import isaac
import ICgen_utils
class pos:
"""
position class. Generates particle positions from rho and sigma
USAGE:
# method = 'grid' or 'random'
pos = pos_class.pos(ICobj, method)
ICobj should be an initial conditions object (ICgen.IC) with rho already
calculated.
"""
def __init__(self, ICobj, method = None, generate=True, seed=None):
self._seed = seed
# Set version
self.__version__ = __iversion__
# Link to parent initial conditions object
self._parent = ICobj
# Check that sigma and rho have been generated
if not hasattr(ICobj, 'rho'):
raise NameError,'rho could not be found in the IC object'
if not hasattr(ICobj,'sigma'):
raise NameError,'sigma could not be found in the IC object'
if method == None:
self.method = ICobj.settings.pos_gen.method
else:
self.method = method
# Update settings in ICobj
ICobj.settings.pos_gen.method = method
self.nParticles = ICobj.settings.pos_gen.nParticles
print 'Generating {0} particle positions using method: {1}'.format(\
self.nParticles, self.method)
# Generate positions
self._generate_r()
self.xyz = SimArray(np.zeros([self.nParticles, 3], dtype=np.float32), self.r.units)
self._generate_z()
self._generate_theta()
self._cartesian_pos()
# To save on memory, delete theta. It can be re-calculated later
# if absolutely needed
del self.theta
def __getstate__(self):
"""
This is required to make the object pickle-able
"""
# Define a dictionary containing everything needed.
# Ignore self.parent
state = self.__dict__.copy()
state.pop('_parent', None)
# Now handle the possibly large arrays (too large to pickle)
for key,val in state.iteritems():
if isinstance(val, np.ndarray):
state[key] = ICgen_utils.listify(val, 1001)
return state
def __setstate__(self, d):
"""
This is required to make the object un-pickleable
"""
for key, val in d.iteritems():
if isinstance(val, ICgen_utils.larray):
d[key] = val.delistify()
self.__dict__ = d
def _generate_r(self):
"""
Generate radial positions
"""
print 'Generating r positions'
cdf_inv_r = self._parent.sigma.cdf_inv
if self.method == 'grid':
# Generate linearly increasing values of m, using 2 more than
# necessary to avoid boundary issues
m = np.linspace(0,1,self.nParticles + 2)
# Calculate r from inverse CDF
r = cdf_inv_r(m[1:-1]).astype(np.float32)
# Assign output
self.r = r
if self.method == 'random':
np.random.seed(self._seed)
m = np.random.rand(self.nParticles)
r = cdf_inv_r(m).astype(np.float32)
self.r = r
def _generate_z(self):
"""
Generate z positions
"""
print 'Generating z positions'
# The inverse CDF over z as a function of r
cdf_inv_z = self._parent.rho.cdf_inv
# Random numbers between 0 and 1
np.random.seed(self._seed)
m = np.random.rand(self.nParticles)
# Calculate z
z = cdf_inv_z(m, self.r)
# Randomly select sign of z
z = z * np.random.choice(np.array([-1,1]), self.nParticles)
# Assign output
self.xyz[:,2] = z
def _generate_theta(self):
"""
Generate angular positions
"""
nParticles = self.nParticles
if self.method == 'grid':
r = self.r
dtheta = np.sqrt(2*np.pi*(1 - r[0:-1]/r[1:]))
dtheta = isaac.strip_units(dtheta)
theta = np.zeros(nParticles)
for n in range(nParticles - 1):
# NOTE: it's import to subtract (not add) dtheta. The particles
# will be moving counter-clockwise. To prevent the particle
# spirals from kinking, the particle spirals must go out
# clockwise
theta[n+1] = theta[n] - dtheta[n]
self.theta = theta
if self.method == 'random':
np.random.seed(self._seed)
theta = 2*np.pi*np.random.rand(nParticles)
self.theta = theta
def _cartesian_pos(self):
"""
Generate x,y
"""
r = self.r
theta = self.theta
self.xyz[:,0] = r*np.cos(theta)
self.xyz[:,1] = r*np.sin(theta)