当前位置: 代码网 > it编程>前端脚本>Python > PINN解偏微分方程实例1

PINN解偏微分方程实例1

2024年07月31日 Python 我要评论
PINN是一种利用神经网络求解偏微分方程的方法,其计算流程图如下图所示,这里以偏微分方程(1)为例。∂u∂tu∂u∂xv∂2u∂x2∂t∂u​u∂x∂u​v∂x2∂2u​​​神经网络输入位置x,y,z和时间t的值,预测偏微分方程解u在这个时空条件下的数值解。上图中可以看出,PINN的损失函数包含两部分内容,一部分是来源于训练数据误差,另一部分来源于偏微分方程误差,可以记作(2)式。

更新:
最近使用下面代码框架求解了三个具体的偏微分方程,主要是对结果进行了可视化展示(一些漂亮的结果图),包括diffusion,burgers和allen–cahn方程;另外以burgers 方程为例,对解 pde 反问题的结果也进行了可视化展示,具体见pinn解偏微分方程实例5.

更新:
最近使用下面代码框架求解了四个具体的偏微分方程,包括diffusion,burgers, allen–cahn和wave方程,另外重新写了一个 求解反问题的代码框架,以burgers 方程为例,具体见pinn解偏微分方程实例4.

1. pinn简介

   pinn是一种利用神经网络求解偏微分方程的方法,其计算流程图如下图所示,这里以偏微分方程(1)为例。
∂ u ∂ t + u ∂ u ∂ x = v ∂ 2 u ∂ x 2 \begin{align} \frac{\partial u}{\partial t}+u \frac{\partial u}{\partial x}=v\frac{\partial^2 u}{\partial x^2} \end{align} tu+uxu=vx22u
神经网络输入位置x,y,z和时间t的值,预测偏微分方程解u在这个时空条件下的数值解。
在这里插入图片描述
   上图中可以看出,pinn的损失函数包含两部分内容,一部分是来源于训练数据误差,另一部分来源于偏微分方程误差,可以记作(2)式。
l = w d a t a l d a t a + w p d e l p d e \begin{align} \mathcal{l} = w_{data}\mathcal{l}_{data}+w_{pde}\mathcal{l}_{pde} \end{align} l=wdataldata+wpdelpde
其中
l d a t a = 1 n d a t a ∑ i = 1 n d a t a ( u ( x i , t i ) − u i ) 2 l p d e = 1 n d a t a ∑ j = 1 n p d e ( ∂ u ∂ t + u ∂ u ∂ x − v ∂ 2 u ∂ x 2 ) 2 ∣ ( x j , t j ) \begin{align} \begin{aligned} \mathcal{l}_{data} &= \frac{1}{n_{data}}\sum_{i=1}^{n_{data}} (u(x_i,t_i)-u_i)^2 \\ \mathcal{l}_{pde} &= \frac{1}{n_{data}}\sum_{j=1}^{n_{pde}} \left( \frac{\partial u}{\partial t}+u \frac{\partial u}{\partial x}-v\frac{\partial^2 u}{\partial x^2} \right)^2|_{(x_j,t_j)} \end{aligned} \end{align} ldatalpde=ndata1i=1ndata(u(xi,ti)ui)2=ndata1j=1npde(tu+uxuvx22u)2(xj,tj)

2. 偏微分方程实例

   考虑偏微分方程如下:
∂ 2 u ∂ x 2 − ∂ 4 u ∂ y 4 = ( 2 − x 2 ) e − y \begin{align} \begin{aligned} \frac{\partial^2 u}{\partial x^2} - \frac{\partial^4 u}{\partial y^4} = (2-x^2)e^{-y} \end{aligned} \end{align} x22uy44u=(2x2)ey
考虑以下边界条件,
u y y ( x , 0 ) = x 2 u y y ( x , 1 ) = x 2 e u ( x , 0 ) = x 2 u ( x , 1 ) = x 2 e u ( 0 , y ) = 0 u ( 1 , y ) = e − y \begin{align} \begin{aligned} u_{yy}(x,0) &= x^2 \\ u_{yy}(x,1) &= \frac{x^2}{e} \\ u(x,0) &= x^2 \\ u(x,1) &= \frac{x^2}{e} \\ u(0,y) &= 0 \\ u(1,y) &= e^{-y} \\ \end{aligned} \end{align} uyy(x,0)uyy(x,1)u(x,0)u(x,1)u(0,y)u(1,y)=x2=ex2=x2=ex2=0=ey
以上偏微分方程真解为 u ( x , y ) = x 2 e − y u(x,y)=x^2 e^{-y} u(x,y)=x2ey,在区域 [ 0 , 1 ] × [ 0 , 1 ] [0,1]\times[0,1] [0,1]×[0,1]上随机采样配置点和数据点,其中配置点用来构造pde损失函数 l 1 , l 2 , ⋯   , l 7 \mathcal{l}_1,\mathcal{l}_2,\cdots,\mathcal{l}_7 l1,l2,,l7,数据点用来构造数据损失函数 l 8 \mathcal{l}_8 l8.
l 1 = 1 n 1 ∑ ( x i , y i ) ∈ ω ( u ^ x x ( x i , y i ; θ ) − u ^ y y y y ( x i , y i ; θ ) − ( 2 − x i 2 ) e − y i ) 2 l 2 = 1 n 2 ∑ ( x i , y i ) ∈ [ 0 , 1 ] × { 0 } ( u ^ y y ( x i , y i ; θ ) − x i 2 ) 2 l 3 = 1 n 3 ∑ ( x i , y i ) ∈ [ 0 , 1 ] × { 1 } ( u ^ y y ( x i , y i ; θ ) − x i 2 e ) 2 l 4 = 1 n 4 ∑ ( x i , y i ) ∈ [ 0 , 1 ] × { 0 } ( u ^ ( x i , y i ; θ ) − x i 2 ) 2 l 5 = 1 n 5 ∑ ( x i , y i ) ∈ [ 0 , 1 ] × { 1 } ( u ^ ( x i , y i ; θ ) − x i 2 e ) 2 l 6 = 1 n 6 ∑ ( x i , y i ) ∈ { 0 } × [ 0 , 1 ] ( u ^ ( x i , y i ; θ ) − 0 ) 2 l 7 = 1 n 7 ∑ ( x i , y i ) ∈ { 1 } × [ 0 , 1 ] ( u ^ ( x i , y i ; θ ) − e − y i ) 2 l 8 = 1 n 8 ∑ i = 1 n 8 ( u ^ ( x i , y i ; θ ) − u i ) 2 \begin{align} \begin{aligned} \mathcal{l}_1 &= \frac{1}{n_1}\sum_{(x_i,y_i)\in\omega} (\hat{u}_{xx}(x_i,y_i;\theta) - \hat{u}_{yyyy}(x_i,y_i;\theta) - (2-x_i^2)e^{-y_i})^2 \\ \mathcal{l}_2 &= \frac{1}{n_2}\sum_{(x_i,y_i)\in[0,1]\times\{0\}} (\hat{u}_{yy}(x_i,y_i;\theta) - x_i^2)^2 \\ \mathcal{l}_3 &= \frac{1}{n_3}\sum_{(x_i,y_i)\in[0,1]\times\{1\}} (\hat{u}_{yy}(x_i,y_i;\theta) - \frac{x_i^2}{e})^2 \\ \mathcal{l}_4 &= \frac{1}{n_4}\sum_{(x_i,y_i)\in[0,1]\times\{0\}} (\hat{u}(x_i,y_i;\theta) - x_i^2)^2 \\ \mathcal{l}_5 &= \frac{1}{n_5}\sum_{(x_i,y_i)\in[0,1]\times\{1\}} (\hat{u}(x_i,y_i;\theta) - \frac{x_i^2}{e})^2 \\ \mathcal{l}_6 &= \frac{1}{n_6}\sum_{(x_i,y_i)\in\{0\}\times [0,1]}(\hat{u}(x_i,y_i;\theta) - 0)^2 \\ \mathcal{l}_7 &= \frac{1}{n_7}\sum_{(x_i,y_i)\in\{1\}\times [0,1]}(\hat{u}(x_i,y_i;\theta) - e^{-y_i})^2 \\ \mathcal{l}_8 &= \frac{1}{n_{8}}\sum_{i=1}^{n_{8}} (\hat{u}(x_i,y_i;\theta)-u_i)^2 \end{aligned} \end{align} l1l2l3l4l5l6l7l8=n11(xi,yi)ω(u^xx(xi,yi;θ)u^yyyy(xi,yi;θ)(2xi2)eyi)2=n21(xi,yi)[0,1]×{0}(u^yy(xi,yi;θ)xi2)2=n31(xi,yi)[0,1]×{1}(u^yy(xi,yi;θ)exi2)2=n41(xi,yi)[0,1]×{0}(u^(xi,yi;θ)xi2)2=n51(xi,yi)[0,1]×{1}(u^(xi,yi;θ)exi2)2=n61(xi,yi){0}×[0,1](u^(xi,yi;θ)0)2=n71(xi,yi){1}×[0,1](u^(xi,yi;θ)eyi)2=n81i=1n8(u^(xi,yi;θ)ui)2

3. 基于pytorch实现代码

"""
a scratch for pinn solving the following pde
u_xx-u_yyyy=(2-x^2)*exp(-y)
author: st
date: 2023/2/26
"""
import torch
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

epochs = 10000    # 训练代数
h = 100    # 画图网格密度
n = 1000    # 内点配置点数
n1 = 100    # 边界点配置点数
n2 = 1000    # pde数据点

def setup_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = true

# 设置随机数种子
setup_seed(888888)

# domain and sampling
def interior(n=n):
    # 内点
    x = torch.rand(n, 1)
    y = torch.rand(n, 1)
    cond = (2 - x ** 2) * torch.exp(-y)
    return x.requires_grad_(true), y.requires_grad_(true), cond


def down_yy(n=n1):
    # 边界 u_yy(x,0)=x^2
    x = torch.rand(n, 1)
    y = torch.zeros_like(x)
    cond = x ** 2
    return x.requires_grad_(true), y.requires_grad_(true), cond


def up_yy(n=n1):
    # 边界 u_yy(x,1)=x^2/e
    x = torch.rand(n, 1)
    y = torch.ones_like(x)
    cond = x ** 2 / torch.e
    return x.requires_grad_(true), y.requires_grad_(true), cond


def down(n=n1):
    # 边界 u(x,0)=x^2
    x = torch.rand(n, 1)
    y = torch.zeros_like(x)
    cond = x ** 2
    return x.requires_grad_(true), y.requires_grad_(true), cond


def up(n=n1):
    # 边界 u(x,1)=x^2/e
    x = torch.rand(n, 1)
    y = torch.ones_like(x)
    cond = x ** 2 / torch.e
    return x.requires_grad_(true), y.requires_grad_(true), cond


def left(n=n1):
    # 边界 u(0,y)=0
    y = torch.rand(n, 1)
    x = torch.zeros_like(y)
    cond = torch.zeros_like(x)
    return x.requires_grad_(true), y.requires_grad_(true), cond


def right(n=n1):
    # 边界 u(1,y)=e^(-y)
    y = torch.rand(n, 1)
    x = torch.ones_like(y)
    cond = torch.exp(-y)
    return x.requires_grad_(true), y.requires_grad_(true), cond

def data_interior(n=n2):
    # 内点
    x = torch.rand(n, 1)
    y = torch.rand(n, 1)
    cond = (x ** 2) * torch.exp(-y)
    return x.requires_grad_(true), y.requires_grad_(true), cond


# neural network
class mlp(torch.nn.module):
    def __init__(self):
        super(mlp, self).__init__()
        self.net = torch.nn.sequential(
            torch.nn.linear(2, 32),
            torch.nn.tanh(),
            torch.nn.linear(32, 32),
            torch.nn.tanh(),
            torch.nn.linear(32, 32),
            torch.nn.tanh(),
            torch.nn.linear(32, 32),
            torch.nn.tanh(),
            torch.nn.linear(32, 1)
        )

    def forward(self, x):
        return self.net(x)


# 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)

# 以下7个损失是pde损失
def l_interior(u):
    # 损失函数l1
    x, y, cond = interior()
    uxy = u(torch.cat([x, y], dim=1))
    return loss(gradients(uxy, x, 2) - gradients(uxy, y, 4), cond)


def l_down_yy(u):
    # 损失函数l2
    x, y, cond = down_yy()
    uxy = u(torch.cat([x, y], dim=1))
    return loss(gradients(uxy, y, 2), cond)


def l_up_yy(u):
    # 损失函数l3
    x, y, cond = up_yy()
    uxy = u(torch.cat([x, y], dim=1))
    return loss(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)


# training

u = mlp()
opt = torch.optim.adam(params=u.parameters())

for i in range(epochs):
    opt.zero_grad()
    l = l_interior(u) \
        + l_up_yy(u) \
        + l_down_yy(u) \
        + l_up(u) \
        + l_down(u) \
        + l_left(u) \
        + l_right(u) \
        + l_data(u)
    l.backward()
    opt.step()
    if i % 100 == 0:
        print(i)

# inference
xc = torch.linspace(0, 1, h)
xm, ym = torch.meshgrid(xc, xc)
xx = xm.reshape(-1, 1)
yy = ym.reshape(-1, 1)
xy = torch.cat([xx, yy], dim=1)
u_pred = u(xy)
u_real = xx * xx * torch.exp(-yy)
u_error = torch.abs(u_pred-u_real)
u_pred_fig = u_pred.reshape(h,h)
u_real_fig = u_real.reshape(h,h)
u_error_fig = u_error.reshape(h,h)
print("max abs error is: ", float(torch.max(torch.abs(u_pred - xx * xx * torch.exp(-yy)))))
# 仅有pde损失    max abs error:  0.004852950572967529
# 带有数据点损失  max abs error:  0.0018916130065917969

# 作pinn数值解图
fig = plt.figure()
ax = axes3d(fig)
ax.plot_surface(xm.detach().numpy(), ym.detach().numpy(), u_pred_fig.detach().numpy())
ax.text2d(0.5, 0.9, "pinn", transform=ax.transaxes)
plt.show()
fig.savefig("pinn solve.png")

# 作真解图
fig = plt.figure()
ax = axes3d(fig)
ax.plot_surface(xm.detach().numpy(), ym.detach().numpy(), u_real_fig.detach().numpy())
ax.text2d(0.5, 0.9, "real solve", transform=ax.transaxes)
plt.show()
fig.savefig("real solve.png")

# 误差图
fig = plt.figure()
ax = axes3d(fig)
ax.plot_surface(xm.detach().numpy(), ym.detach().numpy(), u_error_fig.detach().numpy())
ax.text2d(0.5, 0.9, "abs error", transform=ax.transaxes)
plt.show()
fig.savefig("abs error.png")

4. 数值解

请添加图片描述
请添加图片描述请添加图片描述

参考资料

[1]. physics-informed machine learning
[2]. 知乎-paperweekly

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com