-
Notifications
You must be signed in to change notification settings - Fork 2
/
lagrangian_radiation.py
278 lines (202 loc) · 8.54 KB
/
lagrangian_radiation.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import numpy as np
from scipy.sparse import spdiags
from scipy.sparse.linalg import spsolve
class LagrangianRadiation:
def __init__(self, rp):
self.rp = rp
self.input = rp.input
self.geo = rp.geo
self.mat = rp.mat
self.fields = rp.fields
diag = np.zeros(self.geo.N)
lowerdiag = np.zeros(self.geo.N-1)
upperdiag = np.zeros(self.geo.N-1)
rhs = np.zeros(self.geo.N)
rho_k = np.zeros(self.geo.N)
dr_k = np.zeros(self.geo.N)
u_k = np.zeros(self.geo.N + 1)
A_k = np.zeros(self.geo.N + 1)
nu = np.zeros(self.geo.N)
xi = np.zeros(self.geo.N)
def assembleSystem(self, predictor, dt):
self.mat.recomputeKappa_t(T_old)
self.mat.recomputeKappa_a(T_old)
computeAuxiliaryFields(predictor)
self.assembleInnerCells(dt)
applyLeftBoundary(dt)
applyRightBoundary(dt)
def computeAuxiliaryFields(self, dt, predictor):
m = self.mat.m
a = self.mat.a
c = self.mat.c
C_v = self.mat.C_v
rho_old = self.fields.rho_old
dr_old = self.geo.dr_old
u_old = self.fields.u_old
A_old = self.geo.A_old
T_old = self.fields.T_old
if predictor:
rho = self.fields.rho_p
dr = self.geo.dr_p
u = self.fields.u_p
A = self.geo.A_p
else:
rho = self.fields.rho
dr = self.geo.dr
u = self.fields.u
A = self.geo.A
kappa_t = self.mat.kappa_t
kappa_a = self.mat.kappa_a
self.rho_k = (rho + rho_old) / 2
self.dr_k = (dr + dr_old) / 2
self.u_k = (u + u_old) / 2
self.A_k = (A + A_old) / 2
self.nu = dt * kappa_a * c * 2 * a * T_old**3
self.nu /= C_v + dt * kappa_a * c * 2 *a * T_old**3
for i in range(0, self.geo.N):
self.xi[i] = -P_old[i] * (A_old[i+1] * self.u_k[i+1] - A_old[i] * self.u_k[i])
def assembleInnerCells(self, dt):
m = self.mat.m
a = self.mat.a
c = self.mat.c
C_v = self.mat.C_v
rho_old = self.fields.rho_old
A_old = self.geo.A_old
E_old = self.fields.E_old
T_old = self.fields.T_old
p_old = self.fields.P_old
rho_k = self.rho_k
dr_k = self.dr_k
u_k = self.u_k
A_k = self.A_k
self.mat.recomputeKappa_t(T_old)
self.mat.recomputeKappa_a(T_old)
kappa_t = self.mat.kappa_t
kappa_a = self.mat.kappa_a
nu = self.nu
xi = self.xi
for i in range(1, N-1):
denom1 = 3 * (rho_k[i] * dr_k[i] * kappa_t[i+1] + rho_k[i+1] * dr_k[i+1] * kappa_t[i+1])
denom2 = 3 * (rho_k[i-1] * dr_k[i-1] * kappa_t[i] + rho_k[i] * dr_k[i] * kappa_t[i])
diag[i] += m[i] / (dt * rho[i]) + A_k[i+1] * c / denom1 + A_k[i] * c / denom2
diag[i] += m[i] / 2 * (1 - nu[i]) * m[i] * c * kappa_a[i]
upperdiag[i] = - A_k[i+1] * c / denom1
lowerdiag[i-1] = - A_k[i] * c / denom2
rhs[i] += (- m[i] / (dt * rho_old[i]) \
- m[i] / 2 * kappa_a[i] * c * (1 - nu[i]) \
- 1 / 3 * (A_old[i+1] * u_k[i+1] - A_old[i] * u_k[i]))*E_old[i]
rhs[i] += nu[i] * xi[i]
rhs[i] += A_k[i+1] * c / denom1 * (E_old[i+1] - E_old[i])
rhs[i] += A_k[i] * c / denom2 * (E_old[i] - E_old[i-1])
def applyLeftBoundary(self, dt):
m = self.mat.m
a = self.mat.a
c = self.mat.c
C_v = self.mat.C_v
rho_old = self.fields.rho_old
A_old = self.geo.A_old
E_old = self.fields.E_old
T_old = self.fields.T_old
p_old = self.fields.P_old
rho_k = self.rho_k
dr_k = self.dr_k
u_k = self.u_k
A_k = self.A_k
kappa_t = self.mat.kappa_t
kappa_a = self.mat.kappa_a
nu = self.nu
xi = self.xi
denom1 = 3 * (rho_k[0] * dr_k[0] * kappa_t[1] + rho_k[1] * dr_k[1] * kappa_t[1])
if self.input.E_BC is None:
self.diag[0] += m[0] / (dt * rho[0]) + A_k[1] * c / denom1
self.diag[0] += m[0] / 2 * (1 - nu[0]) * c * kappa_a[0]
self.upperdiag[0] = - A_k[1] * c / denom1
self.rhs[0] += (- m[0] / (dt * rho_old[0]) \
- m[0] / 2 * kappa_a[0] * c * (1 - nu[0]) \
- 1 / 3 * (A_old[1] * u_k[1] - A_old[0] * u_k[0]))*E_old[0]
self.rhs[0] += nu[0] * xi[0]
self.rhs[0] += A_k[1] * c / denom1 * (E_old[1] - E_old[0])
else:
E_left = self.input.E_BC[0]
T_left = ((1 / a * E_left + T_old[0]**4) / 2)**(1 / 4)
kappa_left = self.mat.kappa_func(T_left) + self.kappa_s
denom2 = 3 * rho_k[0] * dr_k [0] * kappa_left + 4
self.diag[0] += m[0] / (dt * rho[0]) + A_k[1] * c / denom1
self.diag[0] += A_k[0] * c / denom2
self.diag[0] += m[0] / 2 * (1 - nu[0]) * c * kappa_a[0]
self.upperdiag[0] = - A_k[1] * c / denom1
self.rhs[0] += (- m[0] / (dt * rho_old[0]) \
- m[0] / 2 * kappa_a[0] * c * (1 - nu[0]) \
- 1 / 3 * (A_old[1] * u_k[1] - A_old[0] * u_k[0]))*E_old[0]
self.rhs[0] += nu[0] * xi[0]
self.rhs[0] += c / denom1 * (E_old[1] - E_old[0])
self.rhs[0] += - A_k[0] * 2 * c / denom2 * E_old[0]
self.rhs[0] += A_k[0] * 2 * c / denom2 * E_left
def applyRightBoundary(self, dt):
m = self.mat.m
a = self.mat.a
c = self.mat.c
C_v = self.mat.C_v
rho_old = self.fields.rho_old
A_old = self.geo.A_old
E_old = self.fields.E_old
T_old = self.fields.T_old
p_old = self.fields.P_old
rho_k = self.rho_k
dr_k = self.dr_k
u_k = self.u_k
A_k = self.A_k
kappa_t = self.mat.kappa_t
kappa_a = self.mat.kappa_a
nu = self.nu
xi = self.xi
denom2 = 3 * (rho_k[N-2] * dr_k[N-2] * kappa_t[N-1] + rho_k[N-1] * dr_k[N-1] * kappa_t[N-1])
if self.input.E_BC is None:
diag[N-1] += m[N-1] / (dt * rho[N-1]) + A_k[N-1] * c / denom2
diag[N-1] += m[N-1] / 2 * (1 - nu[N-1]) * m[N-1] * c * kappa_a[N-1]
lowerdiag[N-2] = - A_k[N-1] * c / denom2
rhs[N-1] += (- m[N-1] / (dt * rho_old[N-1]) \
- m[N-1] / 2 * kappa_a[N-1] * c * (1 - nu[N-1]) \
- 1 / 3 * (A_old[N] * u_k[N] - A_old[N-1] * u_k[N-1]))*E_old[N-1]
rhs[N-1] += nu[N-1] * xi[N-1]
rhs[N-1] += - A_k[N-1] * c / denom2 * (E_old[N-1] - E_old[N-2])
else:
E_right = self.input.E_BC[1]
T_right = ((1 / a * E_right + T_old[N-1]**4) / 2)**(1 / 4)
kappa_right = self.mat.kappa_func(T_right) + self.kappa_s
denom1 = 3 * rho_k[N-1] * dr_k[N-1] * kappa_right + 4
diag[N-1] += m[N-1] / (dt * rho[N-1]) + A_k[N] * c / denom1 + A_k[N-1] * c / denom2
diag[N-1] += m[N-1] / 2 * (1 - nu[N-1]) * m[N-1] * c * kappa_a[N-1]
lowerdiag[N-2] = - A_k[N-1] * c / denom2
rhs[N-1] += (- m[N-1] / (dt * rho_old[N-1]) \
- m[N-1] / 2 * kappa_a[N-1] * c * (1 - nu[N-1]) \
- 1 / 3 * (A_old[N] * u_k[N] - A_old[N-1] * u_k[N-1]))*E_old[N-1]
rhs[N-1] += nu[N-1] * xi[N-1]
rhs[N-1] += - A_k[N-1] * c / denom2 * (E_old[N-1] - E_old[N-2])
rhs[N-1] += - A_k[N] * c / denom1 * (E_old[N-1])
rhs[N-1] += A_k[N] * 2 * c * E_right / denom1
def solveSystem(self, predictor):
systemMatrix = diags([lowerdiag, diag, upperdiag], [-1, 0, 1])
if predictor:
self.fields.E_p = spsolve(systemMatrix, rhs)
else:
self.fields.E = spsolve(systemMatrix, rhs)
def recomputeInternalEnergy(self, dt, predictor):
m = self.mat.m
a = self.mat.a
c = self.mat.c
C_v = self.mat.C_v
T_old = self.fields.T_old
e_old = self.fields.e_old
kappa_a = self.mat.kappa_a
xi = self.xi
if predictor:
E_k = 0.5 * (self.fields.E_p + self.fields.E_old)
else:
E_k = 0.5 * (self.fields.E + self.fields.E_old)
increment = dt * C_v * (m * kappa_a * c * (E_k - a * T_old**4) + xi)
increment /= m*C_v + dt * m * kappa_a * c * 2 * a * T_old**3
if predictor:
self.fields.e_p = e_old + increment
else:
self.fields.e = e_old + increment