-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsph.py
254 lines (189 loc) · 5.92 KB
/
sph.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import gamma
"""
Create Your Own Smoothed-Particle-Hydrodynamics Simulation (With Python)
Philip Mocz (2020) Princeton Univeristy, @PMocz
Simulate the structure of a star with SPH
"""
def W( x, y, z, h ):
"""
Gausssian Smoothing kernel (3D)
x is a vector/matrix of x positions
y is a vector/matrix of y positions
z is a vector/matrix of z positions
h is the smoothing length
w is the evaluated smoothing function
"""
r = np.sqrt(x**2 + y**2 + z**2)
w = (1.0 / (h*np.sqrt(np.pi)))**3 * np.exp( -r**2 / h**2)
return w
def gradW( x, y, z, h ):
"""
Gradient of the Gausssian Smoothing kernel (3D)
x is a vector/matrix of x positions
y is a vector/matrix of y positions
z is a vector/matrix of z positions
h is the smoothing length
wx, wy, wz is the evaluated gradient
"""
r = np.sqrt(x**2 + y**2 + z**2)
n = -2 * np.exp( -r**2 / h**2) / h**5 / (np.pi)**(3/2)
wx = n * x
wy = n * y
wz = n * z
return wx, wy, wz
def getPairwiseSeparations( ri, rj ):
"""
Get pairwise desprations between 2 sets of coordinates
ri is an M x 3 matrix of positions
rj is an N x 3 matrix of positions
dx, dy, dz are M x N matrices of separations
"""
M = ri.shape[0]
N = rj.shape[0]
# positions ri = (x,y,z)
rix = ri[:,0].reshape((M,1))
riy = ri[:,1].reshape((M,1))
riz = ri[:,2].reshape((M,1))
# other set of points positions rj = (x,y,z)
rjx = rj[:,0].reshape((N,1))
rjy = rj[:,1].reshape((N,1))
rjz = rj[:,2].reshape((N,1))
# matrices that store all pairwise particle separations: r_i - r_j
dx = rix - rjx.T
dy = riy - rjy.T
dz = riz - rjz.T
return dx, dy, dz
def getDensity( r, pos, m, h ):
"""
Get Density at sampling loctions from SPH particle distribution
r is an M x 3 matrix of sampling locations
pos is an N x 3 matrix of SPH particle positions
m is the particle mass
h is the smoothing length
rho is M x 1 vector of densities
"""
M = r.shape[0]
dx, dy, dz = getPairwiseSeparations( r, pos );
rho = np.sum( m * W(dx, dy, dz, h), 1 ).reshape((M,1))
return rho
def getPressure(rho, k, n):
"""
Equation of State
rho vector of densities
k equation of state constant
n polytropic index
P pressure
"""
P = k * rho**(1+1/n)
return P
def getAcc( pos, vel, m, h, k, n, lmbda, nu ):
"""
Calculate the acceleration on each SPH particle
pos is an N x 3 matrix of positions
vel is an N x 3 matrix of velocities
m is the particle mass
h is the smoothing length
k equation of state constant
n polytropic index
lmbda external force constant
nu viscosity
a is N x 3 matrix of accelerations
"""
N = pos.shape[0]
# Calculate densities at the position of the particles
rho = getDensity( pos, pos, m, h )
# Get the pressures
P = getPressure(rho, k, n)
# Get pairwise distances and gradients
dx, dy, dz = getPairwiseSeparations( pos, pos )
dWx, dWy, dWz = gradW( dx, dy, dz, h )
# Add Pressure contribution to accelerations
ax = - np.sum( m * ( P/rho**2 + P.T/rho.T**2 ) * dWx, 1).reshape((N,1))
ay = - np.sum( m * ( P/rho**2 + P.T/rho.T**2 ) * dWy, 1).reshape((N,1))
az = - np.sum( m * ( P/rho**2 + P.T/rho.T**2 ) * dWz, 1).reshape((N,1))
# pack together the acceleration components
a = np.hstack((ax,ay,az))
# Add external potential force
a -= lmbda * pos
# Add viscosity
a -= nu * vel
return a
def main():
""" SPH simulation """
# Simulation parameters
N = 400 # Number of particles
t = 0 # current time of the simulation
tEnd = 12 # time at which simulation ends
dt = 0.04 # timestep
M = 2 # star mass
R = 0.75 # star radius
h = 0.1 # smoothing length
k = 0.1 # equation of state constant
n = 1 # polytropic index
nu = 1 # damping
plotRealTime = True # switch on for plotting as the simulation goes along
# Generate Initial Conditions
np.random.seed(42) # set the random number generator seed
lmbda = 2*k*(1+n)*np.pi**(-3/(2*n)) * (M*gamma(5/2+n)/R**3/gamma(1+n))**(1/n) / R**2 # ~ 2.01
m = M/N # single particle mass
pos = np.random.randn(N,3) # randomly selected positions and velocities
vel = np.zeros(pos.shape)
# calculate initial gravitational accelerations
acc = getAcc( pos, vel, m, h, k, n, lmbda, nu )
# number of timesteps
Nt = int(np.ceil(tEnd/dt))
# prep figure
fig = plt.figure(figsize=(4,5), dpi=80)
grid = plt.GridSpec(3, 1, wspace=0.0, hspace=0.3)
ax1 = plt.subplot(grid[0:2,0])
ax2 = plt.subplot(grid[2,0])
rr = np.zeros((100,3))
rlin = np.linspace(0,1,100)
rr[:,0] =rlin
rho_analytic = lmbda/(4*k) * (R**2 - rlin**2)
# Simulation Main Loop
for i in range(Nt):
# (1/2) kick
vel += acc * dt/2
# drift
pos += vel * dt
# update accelerations
acc = getAcc( pos, vel, m, h, k, n, lmbda, nu )
# (1/2) kick
vel += acc * dt/2
# update time
t += dt
# get density for plotting
rho = getDensity( pos, pos, m, h )
# plot in real time
if plotRealTime or (i == Nt-1):
plt.sca(ax1)
plt.cla()
cval = np.minimum((rho-3)/3,1).flatten()
plt.scatter(pos[:,0],pos[:,1], c=cval, cmap=plt.cm.autumn, s=10, alpha=0.5)
ax1.set(xlim=(-1.4, 1.4), ylim=(-1.2, 1.2))
ax1.set_aspect('equal', 'box')
ax1.set_xticks([-1,0,1])
ax1.set_yticks([-1,0,1])
ax1.set_facecolor('black')
ax1.set_facecolor((.1,.1,.1))
plt.sca(ax2)
plt.cla()
ax2.set(xlim=(0, 1), ylim=(0, 3))
ax2.set_aspect(0.1)
plt.plot(rlin, rho_analytic, color='gray', linewidth=2)
rho_radial = getDensity( rr, pos, m, h )
plt.plot(rlin, rho_radial, color='blue')
plt.pause(0.001)
# add labels/legend
plt.sca(ax2)
plt.xlabel('radius')
plt.ylabel('density')
# Save figure
plt.savefig('sph.png',dpi=240)
plt.show()
return 0
if __name__== "__main__":
main()