fix: corregge normalizzazione BC loss con _bc_scale analitico
Il termine convettivo H_CONV/K*(T-T_AMB) domina il residuo Robin di un fattore H*L/K=10 rispetto al gradiente, rendendo L_bc ~100x sovrastimata rispetto a L_pde. Sostituisce grad_char=(Q/K)² con _bc_scale=max(Q/K, H*T_char/K)² (~2.25e6) per bilanciare correttamente i termini della loss. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -22,10 +22,12 @@ class HeatPINN(nn.Module):
|
||||
|
||||
# Precomputed loss scales (depend only on config constants)
|
||||
_T_char = config.Q_VAL * config.L / config.K # ~150 °C — temperature scale
|
||||
_grad_char = (config.Q_VAL / config.K) ** 2 # ~22500 — gradient scale²
|
||||
# _pde_scale covers both dT/dt and the Gaussian source peak (dominant with small sigma)
|
||||
_src_peak = config.ALPHA * config.Q_VAL / (config.K * config.GAUSS_SIGMA * (2 * 3.141592653589793) ** 0.5)
|
||||
_pde_scale = max((_T_char / config.T_END) ** 2, _src_peak ** 2) + 1e-8
|
||||
# Robin BC residual scale: max(dT/dx, H_CONV/K * T_char) — convective term dominates when H*L/K >> 1
|
||||
_bc_scale = max(config.Q_VAL / config.K,
|
||||
config.H_CONV * _T_char / config.K) ** 2
|
||||
|
||||
|
||||
def heat_pinn_loss(model, x_f, t_f, x_ic, t_bc,
|
||||
@@ -33,8 +35,7 @@ def heat_pinn_loss(model, x_f, t_f, x_ic, t_bc,
|
||||
if w_pde is None: w_pde = config.W_PDE
|
||||
if w_ic is None: w_ic = config.W_IC
|
||||
if w_bc is None: w_bc = config.W_BC
|
||||
T_char = _T_char
|
||||
grad_char = _grad_char
|
||||
T_char = _T_char
|
||||
|
||||
# PDE residual: dT/dt - alpha * d2T/dx2 - source(x,t) = 0 (normalized by T_char/t_char)
|
||||
x_f = x_f.detach().requires_grad_(True)
|
||||
@@ -59,13 +60,13 @@ def heat_pinn_loss(model, x_f, t_f, x_ic, t_bc,
|
||||
x_left = torch.zeros(t_bc.shape[0], device=t_bc.device).requires_grad_(True)
|
||||
T_left = model(torch.stack([x_left, t_bc.detach()], dim=1))
|
||||
dT_dx_left = torch.autograd.grad(T_left.sum(), x_left, create_graph=True)[0]
|
||||
L_bc_left = ((dT_dx_left + (config.H_CONV / config.K) * (T_left.squeeze() - config.T_AMB)) ** 2).mean() / grad_char
|
||||
L_bc_left = ((dT_dx_left + (config.H_CONV / config.K) * (T_left.squeeze() - config.T_AMB)) ** 2).mean() / _bc_scale
|
||||
|
||||
# BC x=L: Robin — dT/dx + H_CONV/K * (T(L,t) - T_AMB) = 0
|
||||
x_right = torch.full((t_bc.shape[0],), config.L, device=t_bc.device).requires_grad_(True)
|
||||
T_right = model(torch.stack([x_right, t_bc.detach()], dim=1))
|
||||
dT_dx_right = torch.autograd.grad(T_right.sum(), x_right, create_graph=True)[0]
|
||||
L_bc_right = ((dT_dx_right + (config.H_CONV / config.K) * (T_right.squeeze() - config.T_AMB)) ** 2).mean() / grad_char
|
||||
L_bc_right = ((dT_dx_right + (config.H_CONV / config.K) * (T_right.squeeze() - config.T_AMB)) ** 2).mean() / _bc_scale
|
||||
|
||||
L_bc = L_bc_left + L_bc_right
|
||||
total = w_pde * L_pde + w_ic * L_ic + w_bc * L_bc
|
||||
|
||||
Reference in New Issue
Block a user