forked from ImpactsWiki/pyko
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsodshock.py
More file actions
285 lines (244 loc) · 8.92 KB
/
sodshock.py
File metadata and controls
285 lines (244 loc) · 8.92 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# sodshock python module from https://github.com/ibackus/sod-shocktube
# Licence
#
# The MIT License (MIT)
#
# Copyright (c) 2015 Jerko Škifić
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#
import numpy as np
import scipy
import scipy.optimize
def sound_speed(gamma, pressure, density, dustFrac=0.):
"""
Calculate sound speed, scaled by the dust fraction according to:
.. math::
\widetilde{c}_s = c_s \sqrt{1 - \epsilon}
Where :math:`\epsilon` is the dustFrac
"""
scale = np.sqrt(1 - dustFrac)
return np.sqrt(gamma * pressure / density) * scale
def shock_tube_function(p4, p1, p5, rho1, rho5, gamma, dustFrac=0.):
"""
Shock tube equation
"""
z = (p4 / p5 - 1.)
c1 = sound_speed(gamma, p1, rho1, dustFrac)
c5 = sound_speed(gamma, p5, rho5, dustFrac)
gm1 = gamma - 1.
gp1 = gamma + 1.
g2 = 2. * gamma
fact = gm1 / g2 * (c5 / c1) * z / np.sqrt(1. + gp1 / g2 * z)
fact = (1. - fact) ** (g2 / gm1)
return p1 * fact - p4
def calculate_regions(pl, ul, rhol, pr, ur, rhor, gamma=1.4, dustFrac=0.):
"""
Compute regions
:rtype : tuple
:return: returns p, rho and u for regions 1,3,4,5 as well as the shock speed
"""
# if pl > pr...
rho1 = rhol
p1 = pl
u1 = ul
rho5 = rhor
p5 = pr
u5 = ur
# unless...
if pl < pr:
rho1 = rhor
p1 = pr
u1 = ur
rho5 = rhol
p5 = pl
u5 = ul
# solve for post-shock pressure
p4 = scipy.optimize.fsolve(shock_tube_function, p1, (p1, p5, rho1, rho5, gamma))[0]
# compute post-shock density and velocity
z = (p4 / p5 - 1.)
c5 = sound_speed(gamma, p5, rho5, dustFrac)
gm1 = gamma - 1.
gp1 = gamma + 1.
gmfac1 = 0.5 * gm1 / gamma
gmfac2 = 0.5 * gp1 / gamma
fact = np.sqrt(1. + gmfac2 * z)
u4 = c5 * z / (gamma * fact)
rho4 = rho5 * (1. + gmfac2 * z) / (1. + gmfac1 * z)
# shock speed
w = c5 * fact
# compute values at foot of rarefaction
p3 = p4
u3 = u4
rho3 = rho1 * (p3 / p1) ** (1. / gamma)
return (p1, rho1, u1), (p3, rho3, u3), (p4, rho4, u4), (p5, rho5, u5), w
def calc_positions(pl, pr, region1, region3, w, xi, t, gamma, dustFrac=0.):
"""
:return: tuple of positions in the following order ->
Head of Rarefaction: xhd, Foot of Rarefaction: xft,
Contact Discontinuity: xcd, Shock: xsh
"""
p1, rho1 = region1[:2] # don't need velocity
p3, rho3, u3 = region3
c1 = sound_speed(gamma, p1, rho1, dustFrac)
c3 = sound_speed(gamma, p3, rho3, dustFrac)
if pl > pr:
xsh = xi + w * t
xcd = xi + u3 * t
xft = xi + (u3 - c3) * t
xhd = xi - c1 * t
else:
# pr > pl
xsh = xi - w * t
xcd = xi - u3 * t
xft = xi - (u3 - c3) * t
xhd = xi + c1 * t
return xhd, xft, xcd, xsh
def region_states(pl, pr, region1, region3, region4, region5):
"""
:return: dictionary (region no.: p, rho, u), except for rarefaction region
where the value is a string, obviously
"""
if pl > pr:
return {'Region 1': region1,
'Region 2': 'RAREFACTION',
'Region 3': region3,
'Region 4': region4,
'Region 5': region5}
else:
return {'Region 1': region5,
'Region 2': region4,
'Region 3': region3,
'Region 4': 'RAREFACTION',
'Region 5': region1}
def create_arrays(pl, pr, xl, xr, positions, state1, state3, state4, state5,
npts, gamma, t, xi, dustFrac=0.):
"""
:return: tuple of x, p, rho and u values across the domain of interest
"""
xhd, xft, xcd, xsh = positions
p1, rho1, u1 = state1
p3, rho3, u3 = state3
p4, rho4, u4 = state4
p5, rho5, u5 = state5
gm1 = gamma - 1.
gp1 = gamma + 1.
x_arr = np.linspace(xl, xr, npts)
rho = np.zeros(npts, dtype=float)
p = np.zeros(npts, dtype=float)
u = np.zeros(npts, dtype=float)
c1 = sound_speed(gamma, p1, rho1, dustFrac)
if pl > pr:
for i, x in enumerate(x_arr):
if x < xhd:
rho[i] = rho1
p[i] = p1
u[i] = u1
elif x < xft:
u[i] = 2. / gp1 * (c1 + (x - xi) / t)
fact = 1. - 0.5 * gm1 * u[i] / c1
rho[i] = rho1 * fact ** (2. / gm1)
p[i] = p1 * fact ** (2. * gamma / gm1)
elif x < xcd:
rho[i] = rho3
p[i] = p3
u[i] = u3
elif x < xsh:
rho[i] = rho4
p[i] = p4
u[i] = u4
else:
rho[i] = rho5
p[i] = p5
u[i] = u5
else:
for i, x in enumerate(x_arr):
if x < xsh:
rho[i] = rho5
p[i] = p5
u[i] = -u1
elif x < xcd:
rho[i] = rho4
p[i] = p4
u[i] = -u4
elif x < xft:
rho[i] = rho3
p[i] = p3
u[i] = -u3
elif x < xhd:
u[i] = -2. / gp1 * (c1 + (xi - x) / t)
fact = 1. + 0.5 * gm1 * u[i] / c1
rho[i] = rho1 * fact ** (2. / gm1)
p[i] = p1 * fact ** (2. * gamma / gm1)
else:
rho[i] = rho1
p[i] = p1
u[i] = -u1
return x_arr, p, rho, u
def solve(left_state, right_state, geometry, t, gamma=1.4, npts=500,
dustFrac=0.):
"""
Solves the Sod shock tube problem (i.e. riemann problem) of discontinuity
across an interface.
Parameters
----------
left_state, right_state: tuple
A tuple of the state (pressure, density, velocity) on each side of the
shocktube barrier for the ICs. In the case of a dusty-gas, the density
should be the gas density.
geometry: tuple
A tuple of positions for (left boundary, right boundary, barrier)
t: float
Time to calculate the solution at
gamma: float
Adiabatic index for the gas.
npts: int
number of points for array of pressure, density and velocity
dustFrac: float
Uniform fraction for the gas, between 0 and 1.
Returns
-------
positions: dict
Locations of the important places (rarefaction wave, shock, etc...)
regions: dict
constant pressure, density and velocity states in distinct regions
values: dict
Arrays of pressure, density, and velocity as a function of position.
The density ('rho') is the gas density, which may differ from the
total density in a dusty-gas.
Also calculates the specific internal energy
"""
pl, rhol, ul = left_state
pr, rhor, ur = right_state
xl, xr, xi = geometry
# basic checking
if xl >= xr:
print('xl has to be less than xr!')
exit()
if xi >= xr or xi <= xl:
print('xi has in between xl and xr!')
exit()
# calculate regions
region1, region3, region4, region5, w = \
calculate_regions(pl, ul, rhol, pr, ur, rhor, gamma, dustFrac)
regions = region_states(pl, pr, region1, region3, region4, region5)
# calculate positions
x_positions = calc_positions(pl, pr, region1, region3, w, xi, t, gamma,
dustFrac)
pos_description = ('Head of Rarefaction', 'Foot of Rarefaction',
'Contact Discontinuity', 'Shock')
positions = dict(zip(pos_description, x_positions))
# create arrays
x, p, rho, u = create_arrays(pl, pr, xl, xr, x_positions,
region1, region3, region4, region5,
npts, gamma, t, xi, dustFrac)
energy = p / (rho * (gamma - 1.0))
rho_total = rho / (1.0 - dustFrac)
val_dict = {'x': x, 'p': p, 'rho': rho, 'u': u, 'energy': energy,
'rho_total': rho_total}
return positions, regions, val_dict