Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hackathon 5th No.63 PhyCRNet: Physics-informed Convolutional-Recurrent Network for Solving Spatiotemporal PDEs #557

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions docs/zh/examples/phycrnet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# PhyCRNet

## 1. 背景简介

复杂时空系统通常可以通过偏微分方程(PDE)来建模,它们在许多领域都十分常见,如应用数学、物理学、生物学、化学和工程学。求解PDE系统一直是科学计算领域的一个关键组成部分。
本文的具体目标是为了提出一种新颖的、考虑物理信息的卷积-递归学习架构(PhyCRNet)及其轻量级变体(PhyCRNet-s),用于解决没有任何标签数据的多元时间空间PDEs。我们不试图将我们提出的方法与经典的数值求解器进行比较,而是为复杂PDEs的代理建模提供一种时空深度学习视角。

## 2. 问题定义

在此,我们考虑一组多维(n)、非线性、耦合的参数设置下的偏微分方程(PDE)系统的通用形式:

$$
\mathbf{u}_t+\mathcal{F}\left[\mathbf{u}, \mathbf{u}^2, \cdots, \nabla_{\mathbf{x}} \mathbf{u}, \nabla_{\mathbf{x}}^2 \mathbf{u}, \nabla_{\mathbf{x}} \mathbf{u} \cdot \mathbf{u}, \cdots ; \boldsymbol{\lambda}\right]=\mathbf{0}
$$

我们的目标是开发基于深度神经网络(DNN)的方法,用于解决给定式中的时空PDE系统的正向分析问题。

## 3. 问题求解

接下来开始讲解如何将问题一步一步地转化为 PaddleScience 代码,用深度学习的方法求解该问题。
为了快速理解 PaddleScience,接下来仅对模型构建、方程构建、计算域构建等关键步骤进行阐述,而其余细节请参考 [API文档](../api/arch.md)。

### 3.1 模型构建

在 PhyCRNet 问题中,建立网络,用 PaddleScience 代码表示如下

``` py linenums="105"
--8<--
examples/phycrnet/main.py:163:174
--8<--
```

PhyCRNet 参数 input_channels 是输入通道数,hidden_channels 是隐藏层通道数,input_kernel_size 是内核层大小。

### 3.2 数据构建

运行本问题代码前请按照下方命令生成数据集

``` shell
python burgers_data.py
```

本案例涉及读取数据构建,如下所示

``` py linenums="182"
--8<--
examples/phycrnet/main.py:182:191
--8<--
```

### 3.3 约束构建

设置训练数据集和损失计算函数,返回字段,代码如下所示:

``` py linenums="200"
--8<--
examples/phycrnet/main.py:200:213
--8<--
```

### 3.4 评估器构建

设置评估数据集和损失计算函数,返回字段,代码如下所示:

``` py linenums="216"
--8<--
examples/phycrnet/main.py:216:230
--8<--
```

### 3.5 超参数设定

接下来我们需要指定训练轮数,此处我们按实验经验,使用 200 轮训练轮数。

``` py linenums="143"
--8<--
examples/phycrnet/main.py:143:143
--8<--
```

### 3.6 优化器构建

训练过程会调用优化器来更新模型参数,此处选择 `Adam` 优化器并设定 `learning_rate` 为 1e-4。

``` py linenums="242"
--8<--
examples/phycrnet/main.py:242:242
--8<--
```

### 3.7 模型训练与评估
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

按照
image
翻译,并介绍误差


完成上述设置之后,只需要将上述实例化的对象按顺序传递给 `ppsci.solver.Solver`。

``` py linenums="243"
--8<--
examples/phycrnet/main.py:243:254
--8<--
```

最后启动训练、评估即可:

``` py linenums="256"
--8<--
examples/phycrnet/main.py:256:260
--8<--
```

## 4. 完整代码

``` py linenums="1" title="phycrnet"
--8<--
examples/phycrnet/main.py
--8<--
```

## 5. 结果展示

PhyCRNet 案例针对 epoch=200 和 learning\_rate=1e-4 的参数配置进行了实验,结果返回Loss为 17.86。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compute the error

error = frobenius_norm(np.array(ten_pred)-np.array(ten_true)) / frobenius_norm(
    np.array(ten_true))

print('The predicted error is: ', error)

u_pred = output[:-1, 0, :, :].detach().cpu().numpy()
u_pred = np.swapaxes(u_pred, 1, 2) # [h,w] = [y,x]
u_true = truth[:, 0, :, :]

t_true = np.linspace(0, 2, 1001)
t_pred = np.linspace(0, 2, time_steps)

plt.plot(t_pred, u_pred[:, 32, 32], label='x=32, y=32, CRL')
plt.plot(t_true, u_true[:, 32, 32], '--', label='x=32, y=32, Ref.')
plt.xlabel('t')
plt.ylabel('u')
plt.xlim(0, 2)
plt.legend()
plt.savefig(fig_save_path + "x=32,y=32.png")
plt.close("all")
# plt.show()

# plot train loss
plt.figure()
plt.plot(train_loss, label = 'train loss')
plt.yscale('log')
plt.legend()
plt.savefig(fig_save_path + 'train loss.png', dpi = 300)

科学计算类论文复现,更关注 物理error和最后的物理场效果,参考原文代码复现error和对比图
(可以从checkpoint加载模型直接eval,不需要重新训练)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

增加变量graph,设置为True生成error,对比图
image
image


## 6. 参考资料

- [PhyCRNet: Physics-informed Convolutional-Recurrent Network for Solving Spatiotemporal PDEs](https://arxiv.org/abs/2106.14103)
- <https://github.com/isds-neu/PhyCRNet>
43 changes: 43 additions & 0 deletions examples/phycrnet/README.md
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

删掉不需要

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有价值的部分合入docs

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# PhyCRNet

Physics-informed convolutional-recurrent neural networks for solving spatiotemporal PDEs

Paper link: [[Journal Paper](https://www.sciencedirect.com/science/article/pii/S0045782521006514)], [[ArXiv](https://arxiv.org/pdf/2106.14103.pdf)]

By: [Pu Ren](https://scholar.google.com/citations?user=7FxlSHEAAAAJ&hl=en), [Chengping Rao](https://github.com/Raocp), [Yang Liu](https://coe.northeastern.edu/people/liu-yang/), [Jian-Xun Wang](http://sites.nd.edu/jianxun-wang/) and [Hao Sun](https://web.mit.edu/haosun/www/#/home)

## Highlights

- Present a Physics-informed discrete learning framework for solving spatiotemporal PDEs without any labeled data
- Proposed an encoder-decoder convolutional-recurrent scheme for low-dimensional feature extraction
- Employ hard-encoding of initial and boundary conditions
- Incorporate autoregressive and residual connections to explicitly simulate the time marching

## 参考

- <https://github.com/isds-neu/PhyCRNet/>

## 原仓库环境

- Python 3.6.13,使用Pytorch 1.6.0
- [Pytorch](https://pytorch.org/) 1.6.0,random_fields.py使用的torch.ifft在更高版本不支持。如果不生成数据集,可以使用其他版本。
- matplotlib, numpy, scipy
- post_process 中 `x = x[:-1]` 一行需要注释

## 数据集

生成测试数据集

``` shell
python burgers_data.py
```

## 运行

``` shell
python main.py
```

## 注意

训练网络 steps 可以从较小步骤开始,比如100,然后修改为200
Loading