-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathheatConduction.py
More file actions
391 lines (330 loc) · 14.2 KB
/
Copy pathheatConduction.py
File metadata and controls
391 lines (330 loc) · 14.2 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 31 12:15:28 2019
@author: RickFu
"""
import numpy as np
import pandas as pd
from scipy.linalg import solve_banded
import parameter
import utility
import time
def assemble(para, cache):
""" Assemble linear system Jacobian * dx = F
Supports spatially varying conductivity k(x) as a per-node array.
Uses the divergence form: d/dx[k(x) * dT/dx] with arithmetic-mean
interface conductivities k_{i+1/2} = (k[i] + k[i+1]) / 2.
Return: dictionary containing cache data
"""
k = para['conductivity'] # numpy array of length numberOfNode
rho = para['density'] # numpy array of length numberOfNode
hcp = para['heatCapacity'] # numpy array of length numberOfNode
Q = para.get('volumetricHeatSource', None) # optional per-node source (W/m^3)
dt = para['deltaTime']
numberOfNode = para['numberOfNode']
dx_arr = para['dx_array'] # non-uniform grid spacing (length N-1)
# BC informations
typeX0 = para['x=0 type']
valueX0 = para['x=0 value']
typeXL = para['x=L type']
valueXL = para['x=L value']
reradiate = para.get('re-radiate', False)
reradiate_xL = para.get('re-radiate-xL', False)
convection = para.get('convection', False)
convection_xL = para.get('convection-xL', False)
plate_contact = para.get('plate-contact', False)
plate_contact_xL = para.get('plate-contact-xL', False)
# Re-radiation parameters (per-side with fallback to shared)
if reradiate or reradiate_xL:
sigma = para['stefanBoltzmann']
if reradiate:
eps_r_x0 = para.get('emissivity_x0', para.get('emissivity', 0.9))
T_amb_x0 = para.get('ambientTemperature_x0', para.get('ambientTemperature', 298.0))
if reradiate_xL:
eps_r_xL = para.get('emissivity_xL', para.get('emissivity', 0.9))
T_amb_xL = para.get('ambientTemperature_xL', para.get('ambientTemperature', 298.0))
# Convection parameters (per-side with fallback to shared)
if convection:
h_conv_x0 = para.get('convection_coeff_x0', para.get('convection_coeff', 0.0))
T_amb_conv_x0 = para.get('ambientTemperature_x0', para.get('ambientTemperature', 298.0))
if convection_xL:
h_conv_xL = para.get('convection_coeff_xL', para.get('convection_coeff', 0.0))
T_amb_conv_xL = para.get('ambientTemperature_xL', para.get('ambientTemperature', 298.0))
# Plate contact parameters (per-side with fallback to shared)
if plate_contact or plate_contact_xL:
h_plate = para['plate_conductance']
T_plate = para.get('plate_temperature', para.get('ambientTemperature', 298.0))
# Containers
T = cache['T']; T0 = cache['T0']
F = cache['F']; Jacobian = cache['Jacobian']
# Precompute interface conductivities and spacings (vectorized)
N = numberOfNode
k_half = np.empty(N + 1)
dx_half = np.empty(N + 1)
k_half[1:N] = 0.5 * (k[:-1] + k[1:])
dx_half[1:N] = dx_arr
k_half[0] = k[0]; dx_half[0] = dx_arr[0]
k_half[N] = k[-1]; dx_half[N] = dx_arr[-1]
# Compute effective heat flux at boundaries (including re-radiation, convection, plate contact)
qX0 = valueX0
qXL = valueXL
if reradiate:
if typeX0 == 'heatFlux':
qX0 = valueX0 - eps_r_x0 * sigma * (T[0, 0]**4 - T_amb_x0**4)
if reradiate_xL:
if typeXL == 'heatFlux':
qXL = valueXL - eps_r_xL * sigma * (T[-1, 0]**4 - T_amb_xL**4)
if convection:
if typeX0 == 'heatFlux':
qX0 = qX0 - h_conv_x0 * (T[0, 0] - T_amb_conv_x0)
if convection_xL:
if typeXL == 'heatFlux':
qXL = qXL - h_conv_xL * (T[-1, 0] - T_amb_conv_xL)
if plate_contact:
if typeX0 == 'heatFlux':
qX0 = qX0 - h_plate * (T[0, 0] - T_plate)
if plate_contact_xL:
if typeXL == 'heatFlux':
qXL = qXL - h_plate * (T[-1, 0] - T_plate)
# Boundary ghost values for temperature
if typeX0 == 'heatFlux':
Ug1 = utility.fixedGradient(qX0, k[0], dx_arr[0], T[1])
elif typeX0 == 'fixedTemperature':
Ug1 = utility.fixedValue(valueX0, T[1])
if typeXL == 'heatFlux':
Ug2 = utility.fixedGradient(qXL, k[-1], dx_arr[-1], T[-2])
elif typeXL == 'fixedTemperature':
Ug2 = utility.fixedValue(valueXL, T[-2])
# Vectorized Jacobian assembly
ke = k_half[1:] # east interface conductivities (length N)
kw = k_half[:-1] # west interface conductivities (length N)
dxe = dx_half[1:]
dxw = dx_half[:-1]
h = 0.5 * (dxw + dxe)
ce_arr = dt / (rho * hcp * h * dxe)
cw_arr = dt / (rho * hcp * h * dxw)
# Diagonal: 1 + ce*k_east + cw*k_west
diag = 1.0 + ce_arr * ke + cw_arr * kw
# Off-diagonals (interior nodes)
upper = -ce_arr[:-1] * ke[:-1] # Jacobian[i][i+1] for i=0..N-2
lower = -cw_arr[1:] * kw[1:] # Jacobian[i][i-1] for i=1..N-1
# Boundary corrections for off-diagonals
if typeX0 == 'heatFlux':
upper[0] = -(ce_arr[0] * ke[0] + cw_arr[0] * kw[0])
elif typeX0 == 'fixedTemperature':
upper[0] = -ce_arr[0] * ke[0] + cw_arr[0] * kw[0]
if typeXL == 'heatFlux':
lower[-1] = -(ce_arr[-1] * ke[-1] + cw_arr[-1] * kw[-1])
elif typeXL == 'fixedTemperature':
lower[-1] = ce_arr[-1] * ke[-1] - cw_arr[-1] * kw[-1]
# Re-radiation Jacobian correction at x=0
if reradiate:
if typeX0 == 'heatFlux':
h_0 = 0.5 * (dx_half[0] + dx_half[1])
rad_jac_0 = dt / (rho[0]*hcp[0]) * 2.0 / h_0 * eps_r_x0 * sigma * 4 * T[0, 0]**3
diag[0] += rad_jac_0
upper[0] -= rad_jac_0
# Re-radiation Jacobian correction at x=L
if reradiate_xL:
if typeXL == 'heatFlux':
h_L = 0.5 * (dx_half[N-1] + dx_half[N])
rad_jac_L = dt / (rho[-1]*hcp[-1]) * 2.0 / h_L * eps_r_xL * sigma * 4 * T[-1, 0]**3
diag[-1] += rad_jac_L
lower[-1] -= rad_jac_L
# Convection Jacobian correction at x=0
if convection:
if typeX0 == 'heatFlux':
h_0 = 0.5 * (dx_half[0] + dx_half[1])
conv_jac_0 = dt / (rho[0]*hcp[0]) * 2.0 / h_0 * h_conv_x0
diag[0] += conv_jac_0
upper[0] -= conv_jac_0
# Convection Jacobian correction at x=L
if convection_xL:
if typeXL == 'heatFlux':
h_L = 0.5 * (dx_half[N-1] + dx_half[N])
conv_jac_L = dt / (rho[-1]*hcp[-1]) * 2.0 / h_L * h_conv_xL
diag[-1] += conv_jac_L
lower[-1] -= conv_jac_L
# Plate contact Jacobian correction at x=0
if plate_contact:
if typeX0 == 'heatFlux':
h_0 = 0.5 * (dx_half[0] + dx_half[1])
plate_jac_0 = dt / (rho[0]*hcp[0]) * 2.0 / h_0 * h_plate
diag[0] += plate_jac_0
upper[0] -= plate_jac_0
# Plate contact Jacobian correction at x=L
if plate_contact_xL:
if typeXL == 'heatFlux':
h_L = 0.5 * (dx_half[N-1] + dx_half[N])
plate_jac_L = dt / (rho[-1]*hcp[-1]) * 2.0 / h_L * h_plate
diag[-1] += plate_jac_L
lower[-1] -= plate_jac_L
# Fill Jacobian matrix
Jacobian[:] = 0
np.fill_diagonal(Jacobian, diag)
np.fill_diagonal(Jacobian[:-1, 1:], upper)
np.fill_diagonal(Jacobian[1:, :-1], lower)
# Calculate F using variable-coefficient diffusion with non-uniform grid
diffusion = utility.variableCoefficientDiffusion(T, k, dx_arr, Ug1, Ug2)
rho_cp = (rho * hcp).reshape(-1, 1)
F = T - T0 - dt / rho_cp * diffusion
if Q is not None:
# PDE: rho*cp*dT/dt = d/dx(k dT/dx) + Q → subtract dt/(rho*cp)*Q from F.
# Q is treated frozen within each Newton iteration (dQ/dT=0 in Jacobian);
# it is refreshed externally by material_hook between timesteps.
F = F - dt / rho_cp * np.asarray(Q).reshape(-1, 1)
# Store in cache (ce/cw arrays used by adjoint for grad_k)
cache['F'] = -F; cache['Jacobian'] = Jacobian
cache['ce_arr'] = ce_arr; cache['cw_arr'] = cw_arr
return cache
def initialize(para):
""" Initialize key data
T: current step temperature
T0: last step temperature
TProfile: temperature results in time and space
F: B as right hand side of Ax = B
Jacobian: A as left had side of Ax = B
Return: a dictionary
"""
numberOfNode = para['numberOfNode']
numOfTimeStep = para['numberOfTimeStep']
Tic = para['IC value']
T = np.full((numberOfNode, 1), Tic)
T0 = np.full((numberOfNode, 1), Tic)
TProfile = np.zeros((numberOfNode, numOfTimeStep + 1))
F = np.zeros((numberOfNode, 1))
Jacobian = np.zeros((numberOfNode, numberOfNode))
TProfile[:,0] = T.reshape(1,-1)
cache = {'T':T,'T0':T0,'TProfile':TProfile,
'F':F,'Jacobian':Jacobian,
'Log':pd.DataFrame()}
return cache
def solveLinearSystem(para, cache):
""" Solve Ax=B using tridiagonal (banded) solver — O(N) instead of O(N³).
The Jacobian is tridiagonal: pack into banded form for scipy.solve_banded.
"""
relax = para['relaxation']
A = cache['Jacobian']
B = cache['F']
N = A.shape[0]
# Pack tridiagonal into banded form: ab[0] = upper, ab[1] = diag, ab[2] = lower
ab = np.zeros((3, N))
ab[0, 1:] = np.diag(A, 1) # upper diagonal
ab[1, :] = np.diag(A, 0) # main diagonal
ab[2, :-1] = np.diag(A, -1) # lower diagonal
dT = solve_banded((1, 1), ab, B.ravel()).reshape(-1, 1)
T = cache['T']
T = dT * relax + T
cache['T'] = T
cache['dT'] = dT
return cache
def storeUpdateResult(cache):
""" Store results
Update T0
Store temperaure results into a dataframe and
save it in the cache.
"""
timeStep = cache['ts']
TProfile = cache['TProfile']
T = cache['T']
cache['T0'] = T.copy()
TProfile[:,timeStep] = T.reshape(1,-1)
return cache
def newtonIteration(para, cache, verbose=True):
""" Newton's Iteration for Equation System
Process:
1. Get max iteratino, convergence limit
2. Call assemble function to get Jacobian and F(RHS)
3. Solve for dT, update solution
4. Evaluate F, get value of 2-norm
5. If solution converged, break, output to screen and
return cache.
"""
maxIteration = para['maxIteration']
convergence = para['convergence']
dt = para['deltaTime']
log = cache['Log']
ts = cache['ts']
for n in range(maxIteration):
cache = assemble(para, cache)
F = cache['F']
norm = np.linalg.norm(F)
if norm < convergence:
log.loc[ts,'PhysicalTime'] = dt*ts
log.loc[ts,'Iteration'] = n+1
log.loc[ts,'Residual'] = norm
break
cache = solveLinearSystem(para, cache)
print_freq = int(para.get('print_frequency', 1))
if verbose and ts % print_freq == 0:
T = cache['T']
print(' [','{:3.0f}'.format(ts), ']',
' [','{:6.2f}'.format(ts*dt),']',
' [','{:2.0f}'.format(n+1), ']',
' [','{:8.2E}'.format(norm),']',
' [','{:8.2f}'.format(T[0,0]),']',
' [','{:8.2f}'.format(T[-1,0]),']')
return cache
def solve(para, verbose=True, material_hook=None):
""" Main function to solve heat conduction
Input: a Pandas series containing all parameters
Process:
1. Initialize cache
2. Time marching
3. Newton's iteration for discretized PDE for singe time
step
4. Update T, save result to T profile
Optional material_hook(para, cache, timeStep) is called after each
converged Newton step (and before cache['T0'] is overwritten), with
cache['T0']=T_old and cache['T']=T_new. The hook may mutate
para['conductivity'/'density'/'heatCapacity'/'volumetricHeatSource']
in place; the next timestep's assemble() will read the updated
arrays. See material_coupling.LayeredMaterialCoupler.
Return: temperature profile as final result
"""
if verbose:
print(" Heat Conduction Solver")
start = time.time()
para = parameter.normalize_conductivity(para)
cache = initialize(para)
numOfTimeStep = para['numberOfTimeStep']
dt = para['deltaTime']
# Detect time-varying BCs (2D array [[t0,q0],[t1,q1],...])
val_x0 = para['x=0 value']
val_xL = para['x=L value']
flux_profile_x0 = None
flux_profile_xL = None
if isinstance(val_x0, np.ndarray) and val_x0.ndim == 2:
flux_profile_x0 = val_x0.copy()
if isinstance(val_xL, np.ndarray) and val_xL.ndim == 2:
flux_profile_xL = val_xL.copy()
if verbose:
print(' [Step] [Pysical Time] [Iteration] [Residue] [T_0] [T_L]')
flux_history_x0 = np.zeros(numOfTimeStep + 1)
for timeStep in range(1, numOfTimeStep+1):
cache['ts'] = timeStep
t_phys = timeStep * dt
if flux_profile_x0 is not None:
para['x=0 value'] = float(np.interp(t_phys, flux_profile_x0[:, 0], flux_profile_x0[:, 1]))
if flux_profile_xL is not None:
para['x=L value'] = float(np.interp(t_phys, flux_profile_xL[:, 0], flux_profile_xL[:, 1]))
flux_history_x0[timeStep] = para['x=0 value'] if np.isscalar(para['x=0 value']) else float(para['x=0 value'])
cache = newtonIteration(para, cache, verbose=verbose)
if material_hook is not None:
# Call BEFORE storeUpdateResult so cache['T0']=T_old, cache['T']=T_new.
material_hook(para, cache, timeStep)
cache = storeUpdateResult(cache)
# Store flux history for potential adjoint use; restore original BC values
cache['flux_history_x0'] = flux_history_x0
if flux_profile_x0 is not None:
para['x=0 value'] = flux_profile_x0
if flux_profile_xL is not None:
para['x=L value'] = flux_profile_xL
TProfile = cache['TProfile']
runtime = time.time() - start
if verbose:
print('[Cost] CPU time spent','%.3f'%runtime,'s')
return TProfile, cache
if __name__ == "__main__":
para = parameter.main()
results, cache = solve(para)