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
| # Loss loss = torch.nn.MSELoss()
# 递归求导 def gradients(u, x, order=1): if order == 1: return torch.autograd.grad(u, x, grad_outputs=torch.ones_like(u), create_graph=True, only_inputs=True, )[0] else: return gradients(gradients(u, x), x, order=order - 1)
# 以下4个损失是PDE损失
def l_interior(u): # pde x, y, cond = interior() uxy = u(torch.cat([x, y], dim=1)) return loss(-gradients(uxy, x, 2) - gradients(uxy, y, 2), cond)
def l_down(u): # 损失函数L4 x, y, cond = down() uxy = u(torch.cat([x, y], dim=1)) return loss(uxy, cond)
def l_up(u): # 损失函数L5 x, y, cond = up() uxy = u(torch.cat([x, y], dim=1)) return loss(uxy, cond)
def l_left(u): # 损失函数L6 x, y, cond = left() uxy = u(torch.cat([x, y], dim=1)) return loss(uxy, cond)
def l_right(u): # 损失函数L7 x, y, cond = right() uxy = u(torch.cat([x, y], dim=1)) return loss(uxy, cond)
# 构造数据损失 def l_data(u): # 损失函数L8 x, y, cond = data_interior() uxy = u(torch.cat([x, y], dim=1)) return loss(uxy, cond)
|