-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrun_coupled_all_cases.py
More file actions
211 lines (171 loc) · 8.94 KB
/
Copy pathrun_coupled_all_cases.py
File metadata and controls
211 lines (171 loc) · 8.94 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
"""
run_coupled_all_cases.py — run_all_cases with MaterialEngine coupling ON.
Same layered TPS cases as run_all_cases.py, but each hc.solve call receives
a MaterialEngine-backed material_hook so k/rho/cp/Q evolve with temperature
from kinetic YAML models in MaterialStateSolver/tps_material_db/models/.
Sections:
A. Forward solve (1, 2, 3 layers) — coupled.
B. optimize_layered (adjoint) — SKIPPED (adjoint assumes constant props).
C. optimize_mass_slsqp (SLSQP + FD) — coupled.
D. Over-designed → mass reduction — coupled.
Run:
python run_coupled_all_cases.py
"""
import sys
from pathlib import Path
# sys.path hack — no install needed. Points at the sibling MaterialStateSolver repo.
_MSS_ROOT = Path(__file__).resolve().parent.parent.parent / 'MaterialStateSolver'
if _MSS_ROOT.exists() and str(_MSS_ROOT) not in sys.path:
sys.path.insert(0, str(_MSS_ROOT))
import numpy as np
import parameter
import heatConduction as hc
import optimizer
from material_coupling import make_layered_coupler
# ── shared settings ─────────────────────────────────────────────────────────
T_BW_TARGET = 450.0 # K
T_MIN = 0.001 # m
STACKS = {
1: {'materials': ['pica'],
'thicknesses': np.array([0.050])},
2: {'materials': ['pica', 'steel_304'],
'thicknesses': np.array([0.035, 0.003])},
3: {'materials': ['pica', 'li900', 'steel_304'],
'thicknesses': np.array([0.030, 0.005, 0.001])},
}
SERVICE_TEMPS = {
1: [np.inf],
2: [np.inf, np.inf],
3: [np.inf, 1530.0, 1200.0],
}
def make_para(n_layers):
cfg = STACKS[n_layers]
para = parameter.main()
para['materials'] = list(cfg['materials'])
t = cfg['thicknesses'].copy()
para['layerThicknesses'] = t.copy()
para['length'] = float(t.sum())
para = parameter.load_materials(para)
if n_layers == 1:
props = parameter.load_material(cfg['materials'][0])
para['material function'] = 'layered'
para['numberOfLayers'] = 1
para['layerConductivities'] = np.array([props['conductivity']])
para['layerDensities'] = np.array([props['density']])
para['layerHeatCapacities'] = np.array([props['heat_capacity']])
para['back_wall_temperature_target'] = T_BW_TARGET
parameter.normalize_conductivity(para)
return para
def build_hook(para):
"""Fresh coupler → fresh material state per hc.solve call."""
coupler = make_layered_coupler(para, list(para['materials']))
return coupler.hook
def section(title):
print('\n' + '='*70)
print(' ' + title)
print('='*70)
def fmt_mm(arr):
return (np.asarray(arr) * 1000).round(2).tolist()
# ═══════════════════════════════════════════════════════════════════════════
# A. FORWARD SOLVE (coupled)
# ═══════════════════════════════════════════════════════════════════════════
for n in [1, 2, 3]:
cfg = STACKS[n]
mats = ' + '.join(cfg['materials'])
section(f'A. FORWARD SOLVE (coupled) — {n} layer(s) ({mats})')
para = make_para(n)
TProfile, cache = hc.solve(para, verbose=False, material_hook=build_hook(para))
T_bw = float(np.max(TProfile[-1, :]))
print(f' T_backwall_max = {T_bw:.1f} K | t = {fmt_mm(para["layerThicknesses"])} mm')
# ═══════════════════════════════════════════════════════════════════════════
# B. optimize_layered — SKIPPED (adjoint is invalid with evolving properties)
# ═══════════════════════════════════════════════════════════════════════════
section('B. optimize_layered — SKIPPED')
print(' Adjoint in differential.py assumes constant k/rho/cp. Use')
print(' optimize_mass_slsqp (FD-based) for coupled optimization.')
# ═══════════════════════════════════════════════════════════════════════════
# C. MINIMIZE MASS (SLSQP, coupled)
# ═══════════════════════════════════════════════════════════════════════════
for n in [1, 2, 3]:
cfg = STACKS[n]
mats = ' + '.join(cfg['materials'])
svc = SERVICE_TEMPS[n]
svc_str = ', '.join(
f'T_{cfg["materials"][i]}≤{int(svc[i])} K' if not np.isinf(svc[i])
else f'T_{cfg["materials"][i]}=free' for i in range(n)
)
section(f'C. MINIMIZE MASS (coupled) — {n} layer(s) ({mats})\n '
f'T_bw≤{int(T_BW_TARGET)} K | {svc_str}')
para = make_para(n)
para['material_engine_yamls'] = list(para['materials']) # enables hook in optimizer
t0 = cfg['thicknesses'].copy()
t_min_arr = np.full(n, T_MIN)
res = optimizer.optimize_mass_slsqp(
para_base=para,
t0=t0,
T_bw_limit=T_BW_TARGET,
layer_service_temps=svc,
t_min=t_min_arr,
)
r = optimizer.compute_thermal_sensitivities(res.para_opt)
print(f'\n Status: {res.message}')
print(f' Optimized mass: {res.fun:.3f} kg/m²')
print(f' t_opt (mm): {fmt_mm(res.x)}')
print(f' T_bw_max: {r["T_bw_max"]:.1f} K (limit={int(T_BW_TARGET)} K)')
print(f' T_layer_max: {r["T_layer_max"].round(1).tolist()} K')
viol_bw = max(0.0, r['T_bw_max'] - T_BW_TARGET)
viol_svc = [max(0.0, r['T_layer_max'][i] - svc[i]) for i in range(n) if not np.isinf(svc[i])]
all_ok = viol_bw < 2.0 and all(v < 2.0 for v in viol_svc)
print(f' Constraints: {"ALL SATISFIED" if all_ok else "VIOLATED — check above"}')
# ═══════════════════════════════════════════════════════════════════════════
# D. OVER-DESIGNED → MASS REDUCTION (coupled)
# ═══════════════════════════════════════════════════════════════════════════
STACKS_OVER = {
1: {'materials': ['pica'],
'thicknesses': np.array([0.080])},
2: {'materials': ['pica', 'steel_304'],
'thicknesses': np.array([0.060, 0.005])},
3: {'materials': ['pica', 'li900', 'steel_304'],
'thicknesses': np.array([0.050, 0.010, 0.002])},
}
for n in [1, 2, 3]:
cfg_over = STACKS_OVER[n]
mats = ' + '.join(cfg_over['materials'])
svc = SERVICE_TEMPS[n]
section(f'D. OVER-DESIGNED → MASS REDUCTION (coupled) — {n} layer(s) ({mats})')
para = make_para(n)
t_over = cfg_over['thicknesses'].copy()
para['layerThicknesses'] = t_over.copy()
para['length'] = float(t_over.sum())
parameter.normalize_conductivity(para)
TProfile_init, _ = hc.solve(para, verbose=False, material_hook=build_hook(para))
T_bw_init = float(np.max(TProfile_init[-1, :]))
rho = np.atleast_1d(np.asarray(para['layerDensities'], dtype=float))
mass_init = float(np.dot(rho, t_over))
print(f' Initial design: t = {fmt_mm(t_over)} mm')
print(f' mass = {mass_init:.3f} kg/m² (static-density basis)')
print(f' T_bw = {T_bw_init:.1f} K (limit={int(T_BW_TARGET)} K)')
para['material_engine_yamls'] = list(para['materials'])
t_min_arr = np.full(n, T_MIN)
res = optimizer.optimize_mass_slsqp(
para_base=para,
t0=t_over,
T_bw_limit=T_BW_TARGET,
layer_service_temps=svc,
t_min=t_min_arr,
)
r = optimizer.compute_thermal_sensitivities(res.para_opt)
mass_saved_pct = 100.0 * (mass_init - res.fun) / mass_init
viol_bw = max(0.0, r['T_bw_max'] - T_BW_TARGET)
viol_svc = [max(0.0, r['T_layer_max'][i] - svc[i]) for i in range(n) if not np.isinf(svc[i])]
all_ok = viol_bw < 2.0 and all(v < 2.0 for v in viol_svc)
print(f'\n Optimized design: t = {fmt_mm(res.x)} mm')
print(f' mass = {res.fun:.3f} kg/m² '
f'(saved {mass_saved_pct:.1f}%)')
print(f' T_bw = {r["T_bw_max"]:.1f} K')
print(f' T_layer_max = {r["T_layer_max"].round(1).tolist()} K')
print(f' Status: {res.message}')
print(f' Constraints: {"ALL SATISFIED" if all_ok else "VIOLATED — check above"}')
print('\n' + '='*70)
print(' All coupled cases complete.')
print('='*70)