I am trying to calculate the Pde residual for 1dheat transfer equation.
The function is as follows:-
ef pde_loss(model,x,t,T_S,T_L):
# u_pred.requires_grad = True
x.requires_grad = True
t.requires_grad = True
u_pred = model(x,t).requires_grad_()
u_t = torch.autograd.grad(u_pred, t,
torch.ones_like(u_pred).to(device),
create_graph=True,
allow_unused=True,
)[0] # Calculate the first time derivative
if u_t is None:
raise RuntimeError("u_t is None")
u_x = torch.autograd.grad(u_pred,
x,
torch.ones_like(u_pred).to(device),
create_graph=True,
allow_unused =True)[0] # Calculate the first space derivative
u_xx = torch.autograd.grad(u_x,
x,
torch.ones_like(u_x).to(device),
create_graph=True,
allow_unused=True,
)[0]
T_S_tensor = torch.tensor(T_S, dtype=torch.float32, device=device)
T_L_tensor = torch.tensor(T_L, dtype=torch.float32, device=device)
residual = u_t - alpha_t * u_xx
loss = nn.MSELoss()(residual,torch.zeros_like(residual))
return loss
```python
When I train the model for data loss only using MSE error, the model trains well, but the PDE residual shows error. Ideally if the data loss is less then Pde loss should also be less as this will lead to satisfying physics equation based on which data was generated.