PINN解偏微分方程实例1

这篇具有很好参考价值的文章主要介绍了PINN解偏微分方程实例1。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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,深度学习解PDE,算法,深度学习,pytorch
   上图中可以看出,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. 数值解

pinn,深度学习解PDE,算法,深度学习,pytorch
pinn,深度学习解PDE,算法,深度学习,pytorchpinn,深度学习解PDE,算法,深度学习,pytorch

参考资料

[1]. Physics-informed machine learning
[2]. 知乎-PaperWeekly文章来源地址https://www.toymoban.com/news/detail-781245.html

到了这里,关于PINN解偏微分方程实例1的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 0702可分类变量的微分方程-微分方程

    本节至第四节我们学习的都是一阶微分方程 ​ y ′ = f ( x , y ) y^{\\\'}=f(x,y) y ′ = f ( x , y ) (2-1) 一阶微分方程对称形式 p ( x , y ) d x + Q ( x , y ) d y = 0 ( 2 − 2 ) p(x,y)dx+Q(x,y)dy=0qquad (2-2) p ( x , y ) d x + Q ( x , y ) d y = 0 ( 2 − 2 ) 若以x为自变量,y为因变量,则 d y d x = − P ( x , y ) Q (

    2024年02月04日
    浏览(17)
  • 常微分方程建模R包ecode(一)——构建常微分方程系统

    常微分方程建模R包ecode(一)——构建常微分方程系统

    常微分方程在诸多研究领域中有着广泛应用,本文希望向大家介绍笔者于近期开发的R包 ecode ,该包 采用简洁易懂的语法帮助大家在R环境中构建常微分方程 ,并便利地调用R图形接口,研究常微分方程系统的相速矢量场、平衡点、稳定点等解析性质,或进行数值模拟,进行敏

    2024年02月16日
    浏览(18)
  • 【数学建模】常微分,偏微分方程

    【数学建模】常微分,偏微分方程

    普通边界   已知t0时刻的初值    ode45()  龙格-库塔法 一阶,高阶都一样 如下: s(1) = y , s(2)=y\\\'  s(3) = x , s(4)=x\\\'   分段边界 非匿名函数    手写改进的ode45()函数代码 复杂边界值(即已知初始值,也知道末尾值),用bvp4c()函数 1. pdepe()函数 椭圆-抛物线型 控制方程  左边界

    2024年02月09日
    浏览(12)
  • (矩阵)一阶微分方程和伯努利方程

    (矩阵)一阶微分方程和伯努利方程

    伯努利方程的标准形式: 伯努利方程解法: 方程两边同时除以y的n次, 做变量替换y-z: 转换为线性微分方程: 最后换回原来的变量即可得到伯努利方程。 一阶线性微分方程的标准形式: 当Q(x)=0,为齐次方程;当Q(x)≠0,为非齐次方程。 已知如下矩阵,求解一阶线性微分方

    2024年02月05日
    浏览(11)
  • 一阶常微分方程

    一阶常微分方程

    第一次写博客记录自己的学习过程,写的不好希望大家斧正。 讲的更多的是常微分方程的解法的理解,也是我在学习中遇到各种证明的关键点,希望通过记录博客深化对于证明的理解,建立起数学思维,而不是知其然而不知其所以然。因此阅读中需要读者有一定的基础,与实

    2024年02月05日
    浏览(12)
  • matlab解微分方程

    matlab解微分方程

    f=@(变量) 表达式; x1为2 3 4 5;x2为3 4 5 6的情况下求解函数f的值 用“dsolve” step1: 申明自变量和因变量 syms y(x) step2:编程 得到: step1: 申明自变量和因变量 syms y(x) step2:编程 得到 step1.写函数文件 step2.主函数 相当于定义了一个新向量y,然后列 匿名函数 ,方程的 左边都是一阶

    2024年02月13日
    浏览(11)
  • 高等数学(微分方程)

    高等数学(微分方程)

    x y ′ ′ ′ + ( y ′ ) 3 + y 4 xy\\\'\\\'\\\'+(y\\\')^3+y^4 x y ′′′ + ( y ′ ) 3 + y 4 quad quad 三阶 y ′ = 2 x y\\\'=2x y ′ = 2 x quad quad quad quad quad quad 一阶 d y = 2 x d x dy=2xdx d y = 2 x d x quad quad quad quad 一阶 ( y ′ ′ ) 5 + 2 y ′ = 3 (y\\\'\\\')^5+2y\\\'=3 ( y ′′ ) 5 + 2 y ′ = 3 quad quad quad 二阶 quad 例1: 已知

    2024年02月10日
    浏览(10)
  • 微分方程应用——笔记整理

    微分方程应用——笔记整理

    首先,根据正常思路走,化简得到式子:    不难发现,设  后面得出该方程的通解: 这里要注意什么等于这个通解   --- z 又因为该曲线过点  所以可以求出c为3 该题虽然简单,但是要注意几个问题,该定义域在第一象限,还有就是求解中变量的代换

    2024年02月11日
    浏览(12)
  • MATLAB-常微分方程求解

    MATLAB-常微分方程求解

    MATLAB中可以用来求解常微分方程(组)的函数有ode23、 ode23s、 ode23t、 ode23tb 、ode45、ode15s和odel13等,见下表。它们的具体调用方法类似,为了方便后面的描述, 在后面的介绍中将使用solver统一代替它们。 函数的具体调用方法如下。 [T,Y] =solver( odefun, tspan,y0) [T,Y] = solver( odefun,

    2024年02月02日
    浏览(9)
  • 【数学建模】常微分方程

    【数学建模】常微分方程

    博客园解释 https://www.cnblogs.com/docnan/p/8126460.html https://www.cnblogs.com/hanxi/archive/2011/12/02/2272597.html https://www.cnblogs.com/b0ttle/p/ODEaid.html matlab求解常微分方程 https://www.cnblogs.com/xxfx/p/12460628.html https://www.cnblogs.com/SunChuangYu/p/13415439.html https://www.cnblogs.com/tensory/p/6590783.html 高等数学-常微分

    2024年02月16日
    浏览(14)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包