-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathradpydro.py
354 lines (276 loc) · 12.4 KB
/
radpydro.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import numpy as np
from fields import Fields
from geometry import SlabGeometry, CylindricalGeometry, SphericalGeometry
from inputparameters import InputParameters
from lagrangian_hydro import LagrangianHydro
from lagrangian_radiation_predictor import LagrangianRadiationPredictor
from lagrangian_radiation_corrector import LagrangianRadiationCorrector
from materials import Materials
class RadPydro:
def __init__(self, input):
# Store input parameters and check them
self.input = input
self.input.checkInputs()
# Define geometry based on geometry input type
if input.geometry == 'slab':
self.geo = SlabGeometry(self)
elif input.geometry == 'cylindrical':
self.geo = CylindricalGeometry(self)
else:
self.geo = SphericalGeometry(self)
# Initialize material handler now that geometry is initialized
self.mat = Materials(self)
# Initialize field variables
self.fields = Fields(self)
# Time parameters
self.timeSteps = []
self.time = 0.
self.timeStep_num = 0
self.Tf = input.Tf
# Initialize hydro problem
self.hydro = LagrangianHydro(self)
# Initialize radiation problem (if used)
self.radPredictor = LagrangianRadiationPredictor(self)
self.radCorrector = LagrangianRadiationCorrector(self)
# Init storage for energies in conservation check
self.kinetic_energy = []
self.internal_energy = []
self.radiation_energy = []
self.radiation_leakage = []
self.work_energy = []
self.total_energy = []
# Compute initial energies for each
kinetic = 0
internal = 0
radiation = 0
for i in range(self.geo.N + 1):
kinetic += 1/2 * self.mat.m_half[i] * self.fields.u_IC[i]**2
if i < self.geo.N:
internal += self.mat.m[i] * self.fields.e_IC[i]
radiation += self.mat.m[i] * self.fields.E_IC[i] / self.fields.rho_IC[i]
total = kinetic + internal + radiation
self.kinetic_energy.append(kinetic)
self.internal_energy.append(internal)
self.radiation_energy.append(radiation)
self.radiation_leakage.append(0)
self.work_energy.append(0)
self.total_energy.append(total)
self.total_radiation_leakage = 0
self.total_work_energy = 0
def computeTimeStep(self):
dr = self.geo.dr
u = self.fields.u
F_c = self.input.CoFactor
relEFactor = self.input.relEFactor
c_s = (self.mat.gamma * self.fields.P / self.fields.rho)**(1 / 2)
E_k = (self.fields.E + self.fields.E_old) / 2
dE_k = np.zeros(self.geo.N)
if len(self.timeSteps) == 0:
dE_k = E_k
else:
dE_k = abs((self.fields.E - self.fields.E_old) / self.timeSteps[-1])
u_center = np.zeros(self.geo.N)
for i in range(self.geo.N):
u_center = abs((u[i] + u[i+1]) / 2)
dt_E = min(relEFactor * E_k / dE_k)
dt_u = min(dr * F_c / u_center)
dt_cs = min(dr * F_c / c_s)
self.timeSteps.append(min(self.input.maxTimeStep, dt_E, dt_u, dt_cs))
def run(self):
if self.input.running_mode == 'hydro':
self.runHydro()
elif self.input.running_mode == 'rad':
self.runRad()
elif self.input.running_mode == 'radhydro':
self.runRadHydro()
def runHydro(self):
while self.time < self.input.T_final:
# Compute time step size for this time step
self.computeTimeStep()
# Update time and time step number
self.time += self.timeSteps[-1]
self.timeStep_num += 1
print('=========================================================')
print('Starting time step %i, time = %.3e' \
% (self.timeStep_num, self.time))
print('=========================================================\n')
# Add artificial viscosity for this time step
self.fields.addArtificialViscosity()
# Predictor step
self.hydro.recomputeVelocity(True)
self.geo.moveMesh(True)
self.hydro.recomputeDensity(True)
self.hydro.recomputeInternalEnergy(True)
self.fields.recomputeTemperature(True)
self.fields.recomputePressure(True)
# Corrector step
self.hydro.recomputeVelocity(False)
self.geo.moveMesh(False)
self.hydro.recomputeDensity(False)
self.hydro.recomputeInternalEnergy(False)
self.fields.recomputeTemperature(False)
self.fields.recomputePressure(False)
# Energy conservation check
energy_diff = self.recomputeEnergyConservation()
print('Energy conservation check: ', energy_diff, '\n')
# Copy to old containers for next time step
self.fields.stepFields()
self.geo.stepGeometry()
def runRad(self):
while self.time < self.input.T_final:
# Compute time step size for this time step
self.computeTimeStep()
# Update time and time step number
self.time += self.timeSteps[-1]
self.timeStep_num += 1
print('=========================================================')
print('Starting time step %i, time = %.3e' \
% (self.timeStep_num, self.time))
print('=========================================================\n')
# Predictor step
self.radPredictor.recomputeRadiationEnergy()
self.radPredictor.recomputeInternalEnergy()
self.fields.recomputeTemperature(True)
self.fields.recomputePressure(True)
# Corrector step
self.radCorrector.recomputeRadiationEnergy()
self.radCorrector.recomputeInternalEnergy()
self.fields.recomputeTemperature(False)
self.fields.recomputePressure(False)
# Energy conservation check
energy_diff = self.recomputeEnergyConservation()
print('Energy conservation check: ', energy_diff, '\n')
# Copy to old containers for next time step
self.fields.stepFields()
def runRadHydro(self):
while self.time < self.input.T_final:
# Compute time step size for this time step
self.computeTimeStep()
# Update time and time step number
self.time += self.timeSteps[-1]
self.timeStep_num += 1
print('=========================================================')
print('Starting time step %i, time = %.3e' \
% (self.timeStep_num, self.time))
print('=========================================================\n')
# Add artificial viscosity for this time step
self.fields.addArtificialViscosity()
# Predictor step
self.hydro.recomputeVelocity(True)
self.geo.moveMesh(True)
self.hydro.recomputeDensity(True)
self.radPredictor.recomputeRadiationEnergy()
self.radPredictor.recomputeInternalEnergy()
self.fields.recomputeTemperature(True)
self.fields.recomputePressure(True)
# Corrector step
self.hydro.recomputeVelocity(False)
self.geo.moveMesh(False)
self.hydro.recomputeDensity(False)
self.radCorrector.recomputeRadiationEnergy()
self.radCorrector.recomputeInternalEnergy()
self.fields.recomputeTemperature(False)
self.fields.recomputePressure(False)
# Energy conservation check
energy_diff = self.recomputeEnergyConservation()
print('Energy conservation check: ', energy_diff, '\n')
# Copy to old containers for next time step
self.fields.stepFields()
self.geo.stepGeometry()
def recomputeEnergyConservation(self):
kinetic_energy = self.kinetic_energy
internal_energy = self.internal_energy
radiation_energy = self.radiation_energy
radiation_leakage = self.radiation_leakage
work_energy = self.work_energy
total_energy = self.total_energy
c = self.input.c
dt = self.timeSteps[-1]
m = self.mat.m
m_half = self.mat.m_half
u = self.fields.u
e = self.fields.e
E = self.fields.E
rho = self.fields.rho
A_k = (self.geo.A + self.geo.A_old) / 2
A_pk = (self.geo.A_p + self.geo.A_old) / 2
dr_k = (self.geo.dr + self.geo.dr_old) / 2
dr_pk = (self.geo.dr_p + self.geo.dr_old) /2
E_k = (self.fields.E + self.fields.E_old) / 2
E_pk = (self.fields.E_p + self.fields.E_old) / 2
T_k = (self.fields.T + self.fields.T_old) / 2
T_pk = (self.fields.T_p + self.fields.T_old) / 2
rho_k = (self.fields.rho + self.fields.rho_old) / 2
rho_pk = (self.fields.rho_p + self.fields.rho_old) / 2
u_k = (self.fields.u + self.fields.u_old) / 2
P_pk = (self.fields.P_p + self.fields.P_old) / 2
# Recomputing kappa_t at the cell edges and cell centers
self.mat.recomputeKappa_t(T_pk)
kappa_t_pk_edge = self.mat.kappa_t
self.mat.recomputeKappa_a(T_pk)
kappa_t_pk_center = self.mat.kappa_a + self.mat.kappa_s
# Setting up boundary parameters for the radiation terms
# in the momentum equation
if self.input.rad_L is 'source':
E_bL_k = self.fields.E_bL
E_bL_pk = self.fields.E_bL
else:
E_bL_k = E_k[0]
E_bL_pk = E_pk[0]
if self.input.rad_R is 'source':
E_bR_k = self.fields.E_bR
E_bR_pk = self.fields.E_bR
else:
E_bR_k = E_k[-1]
E_bR_pk = E_pk[-1]
# Compute the boundary radiation energies in the momentum eqn
coeff_E_L = 3 * rho_pk[0] * dr_pk[0] * kappa_t_pk_center[0]
coeff_E_R = 3 * rho_pk[-1] * dr_pk[-1] * kappa_t_pk_center[-1]
E_L = (coeff_E_L * E_bL_pk + 4 * E_pk[0]) / (coeff_E_L + 4)
E_R = (coeff_E_R * E_bR_pk + 4 * E_pk[-1]) / (coeff_E_R + 4)
# Compute radiation flux at boundaries
coeff_F_L = -2 * c / (3 * rho_k[0] * dr_k[0] * kappa_t_pk_edge[0] + 4)
coeff_F_R = -2 * c / (3 * rho_k[-1] * dr_k[-1] * kappa_t_pk_edge[-1] + 4)
F_L = coeff_F_L * (E_k[0] - E_bL_k)
F_R = coeff_F_R * (E_bR_k - E_k[-1])
# Setting up boundary parameters for the pressure boundary values
if self.input.hydro_L is 'P':
P_bL_pk = self.fields.P_L
else:
P_bL_pk = P_pk[0] + 1 / 3 * (E_pk[0] - E_L)
if self.input.hydro_R is 'P':
P_bR_pk = self.fields.P_R
else:
P_bR_pk = P_pk[-1] + 1 / 3 * (E_pk[-1] - E_R)
# Compute kinetic, internal, and radiation energies for this timestep
kinetic, internal, radiation = 0, 0, 0
for i in range(self.geo.N + 1):
kinetic += 1/2 * m_half[i] * u[i]**2
if i < self.geo.N:
internal += m[i] * e[i]
radiation += m[i] * E[i] / rho[i]
# Compute radiation leakage
leakage = (A_k[-1] * F_R - A_k[0] * F_L) * dt
# Compute compressive work
work = (A_pk[-1] * 1/3 * E_R * u_k[-1] - A_pk[0] * 1/3 * E_L * u_k[0]) * dt
work += (A_pk[-1] * P_bR_pk * u_k[-1] - A_pk[0] * P_bL_pk * u_k[0]) * dt
# Compute total energy
total = kinetic + internal + radiation + leakage + work
# Compute energy final - initial energies
dKE = kinetic - kinetic_energy[0]
dIE = internal- internal_energy[0]
dRE = radiation - radiation_energy[0]
# Compute energy losses from pressure work, drift, and leakage
total_work = self.total_work_energy + work
total_leak = self.total_radiation_leakage + leakage
# Update loss terms from pressure work, drift, and leakage
self.total_work_energy += work
self.total_radiation_leakage += leakage
# Append to storage
kinetic_energy.append(kinetic)
internal_energy.append(internal)
radiation_energy.append(radiation)
radiation_leakage.append(leakage)
work_energy.append(work)
total_energy.append(total)
return dKE + dIE + dRE + total_work + total_leak