-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalytical.py
215 lines (168 loc) · 6.9 KB
/
analytical.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
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
class SedovSolution(object):
"""
see: [The Sedov self-similar point blast solutions in nonuniform media](https://link.springer.com/content/pdf/10.1007/BF01414626.pdf)
rho0 = A*r**(-w)
R_s = ((e * t**2)/(alpha * A))**(1/(nu + 2 - w))
"""
def __init__(self, e, rho, gamma=4/3., nu=3, w=0., epsilon=1e-50):
# w = 0 --> uniform background
if not any(nu == np.array([1, 2, 3])):
raise ValueError("nu (dimension of problem) need to be 1, 2 or 3!")
self._epsilon = epsilon
self._e = e
self._gamma = gamma
self._rho0 = rho
self._rho1 = ((gamma + 1.)/(gamma - 1.)) * rho
self._nDim = nu
self._w = w
# Constants for the parametric equations:
self.w1 = (3*nu - 2 + gamma*(2-nu))/(gamma + 1.)
self.w2 = (2.*(gamma-1) + nu)/gamma
self.w3 = nu*(2.-gamma)
self.b0 = 1./(nu*gamma - nu + 2)
self.b2 = (gamma-1.)/(gamma*(self.w2-w))
self.b3 = (nu-w)/(float(gamma)*(self.w2-w))
self.b5 = (2.*nu-w*(gamma+1))/(self.w3-w)
self.b6 = 2./(nu+2-w)
self.b1 = self.b2 + (gamma+1.)*self.b0 - self.b6
self.b4 = self.b1*(nu-w)*(nu+2.-w)/(self.w3-w)
self.b7 = w*self.b6
self.b8 = nu*self.b6
self.c0 = 2*(nu-1)*np.pi + (nu-2)*(nu-3) # simple interpolation of correct function (only for nu=1,2,3)
self.c5 = 2./(gamma - 1)
self.c6 = (gamma + 1)/2.
self.c1 = self.c5*gamma
self.c2 = self.c6/gamma
self.c3 = (nu*gamma - nu + 2.)/((self.w1-w)*self.c6)
self.c4 = (nu + 2. - w)*self.b0*self.c6
# Characterize the solution
f_min = self.c2 if self.w1 > w else self.c6
f = np.logspace(np.log10(f_min), 0, 1e5)
# Sort the etas for our interpolation function
eta = self.parametrized_eta(f)
f = f[eta.argsort()]
eta.sort()
d = self.parametrized_d(f)
p = self.parametrized_p(f)
v = self.parametrized_v(f)
# If min(eta) != 0 then all values for eta < min(eta) = 0
if eta[0] > 0:
e01 = [0., eta[0]*(1-1e-10)]
d01 = [0., 0]
p01 = [0., 0]
v01 = [0., 0]
eta = np.concatenate([np.array(e01), eta])
d = np.concatenate([np.array(d01), d])
p = np.concatenate([np.array(p01), p])
v = np.concatenate([np.array(v01), v])
# Set up our interpolation functions
self._d = interp1d(eta, d, bounds_error=False, fill_value=1./self._rho1)
self._p = interp1d(eta, p, bounds_error=False, fill_value=0.)
self._v = interp1d(eta, v, bounds_error=False, fill_value=0.)
# Finally Calculate the normalization of R_s:
integral = eta**(nu-1)*(d*v**2 + p)
integral = 0.5 * (integral[1:] + integral[:-1])
d_eta = (eta[1:] - eta[:-1])
# calculate integral and multiply by factor
alpha = (integral*d_eta).sum() * (8*self.c0)/((gamma**2-1.)*(nu+2.-w)**2)
self._c = (1./alpha)**(1./(nu+2-w))
def parametrized_eta(self, var):
return (var**-self.b6)*((self.c1*(var-self.c2))**self.b2)*((self.c3*(self.c4-var))**(-self.b1))
def parametrized_d(self, var):
return (var**-self.b7)*((self.c1*(var-self.c2))**(self.b3-self._w*self.b2)) * \
((self.c3*(self.c4-var))**(self.b4+self._w*self.b1))*((self.c5*(self.c6-var))**-self.b5)
def parametrized_p(self, var):
return (var**self.b8)*((self.c3*(self.c4-var))**(self.b4+(self._w-2)*self.b1)) * \
((self.c5*(self.c6-var))**(1-self.b5))
def parametrized_v(self, var):
return self.parametrized_eta(var) * var
# Shock properties
def shock_radius(self, t):
# outer radius at time t
t = np.maximum(t, self._epsilon)
return self._c * (self.e*t**2/self.rho0)**(1./(self._nDim + 2-self._w))
def shock_velocity(self, t):
# velocity of the shock wave
t = np.maximum(t, self._epsilon)
return (2./(self._nDim+2-self._w)) * self.shock_radius(t) / t
def post_shock_pressure(self, t):
# post shock pressure
return (2./(self.gamma+1))*self.rho0*self.shock_velocity(t)**2
@property
def post_shock_density(self, t=0):
# post shock density
return self._rho1
def rho(self, r, t):
# density at radius r and time t
eta = r/self.shock_radius(t)
return self.post_shock_density*self._d(eta)
def pressure(self, r, t):
# pressure at radius r and time t
eta = r/self.shock_radius(t)
return self.post_shock_pressure(t)*self._p(eta)
def velocity(self, r, t):
# velocity at radius r, and time t
eta = r/self.shock_radius(t)
return self._v(eta)*(2/(self.gamma+1))*self.shock_velocity(t)
def internal_energy(self, r, t):
# internal energy at radius r and time t
return self.pressure(r, t)/(self.rho(r, t)*(self.gamma-1))
def entropy(self, r, t):
# entropy at radius, r, and time, t
return self.pressure(r, t)/self.rho(r, t)**self.gamma
# Other properties
@property
def e(self):
# total energy
return self._e
@property
def gamma(self):
# ratio of specific heats
return self._gamma
@property
def rho0(self):
# background density
return self._rho0
class Sedov(object):
"""
Analytical solution for the sedov blast wave problem
"""
def __init__(self, time, r_max):
rho0 = 1.0 #1
e0 = 0.5114776024535798 #1. #1e5
gamma = 5/3. #1.666667 #1.333
w = 0 # Power law index
n_dim = 3
self.sol = SedovSolution(e0, rho0, gamma=gamma, w=w, nu=n_dim)
self.r = np.linspace(0, r_max, 1001)[1:]
self.t = time
print("Shock radius: {}".format(self.sol.shock_radius(self.t)))
def compute(self, y):
return map(self.determine, ['r', y])
def determine(self, x):
if x == 'r':
return self.r
elif x == 'velocity':
return self.sol.velocity(self.r, self.t)
elif x == 'rho':
return self.sol.rho(self.r, self.t)
elif x == 'pressure':
return self.sol.pressure(self.r, self.t)
elif x == 'internal_energy':
return self.sol.internal_energy(self.r, self.t)
else:
raise AttributeError("Sedov solution for variable %s not known"%x)
if __name__ == '__main__':
#sedov = Sedov(0.001, 1)
sedov = Sedov(0.0003-0.0002, 0.5)
fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, sharex=True)
ax1.plot(*sedov.compute('rho'), label="rho")
ax2.plot(*sedov.compute('pressure'), label="pressure")
ax3.plot(*sedov.compute('internal_energy'), label="internal energy")
ax1.legend(loc='best')
ax2.legend(loc='best')
ax3.legend(loc='best')
plt.show()