Skip to content

【快乐开源】迁移 DeepCFD 案例至 PaddleScience #529

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

Merged
merged 24 commits into from
Sep 24, 2023

Conversation

MayYouBeProsperous
Copy link
Contributor

@MayYouBeProsperous MayYouBeProsperous commented Sep 6, 2023

PR types

Others

PR changes

Others

Describe

题目:#439

复现的验收标准如下:
Total MSE = 2.03±0.136
Ux MSE = 0.773±0.0897
Uy MSE =0.2153±0.0186
p MSE =1.042±0.0431

本次复现的实现指标如下:
Total MSE = 2.07983
Ux MSE = 0.79234
Uy MSE = 0.21595
p MSE = 1.07154

@paddle-bot
Copy link

paddle-bot bot commented Sep 6, 2023

Thanks for your contribution!

@luotao1 luotao1 added the HappyOpenSource 快乐开源活动issue与PR label Sep 7, 2023
Comment on lines 30 to 31
wn=True,
bn=True,
Copy link
Collaborator

Choose a reason for hiding this comment

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

===> weight_norm, batch_norm

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

activation=nn.ReLU,
convolution=nn.Conv2D,
):
assert kernel_size % 2 == 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

对于参数的检查使用if-raise ValueError(xxx),assert一般用于内部生成的变量,不要滥用assert来做参数的检查

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines 52 to 53
wn=True,
bn=True,
Copy link
Collaborator

Choose a reason for hiding this comment

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

同上

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines 131 to 134
constraint=constraint,
output_dir=OUTPUT_DIR,
optimizer=optimizer,
epochs=EPOCHS,
Copy link
Collaborator

Choose a reason for hiding this comment

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

constraint,
OUTPUT_DIR,
optimizer,
epochs=EPOCHS,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

solver.train()

############### evaluation after training ###############
output_dict = solver.predict({"input": test_x})
Copy link
Collaborator

Choose a reason for hiding this comment

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

加一个参数 return_numpy=True,会返回numpy.ndarray

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

p_MSE = ((out[:, 2, :, :] - test_y[:, 2, :, :]) ** 2).sum() / len(test_x)

logger.info(
f"Total MSE is {Total_MSE.detach().numpy()[0]}, Ux MSE is {Ux_MSE.detach().numpy()[0]}, Uy MSE is {Uy_MSE.detach().numpy()[0]}, p MSE is {p_MSE.detach().numpy()[0]}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. 上面改完就可以删除.detach().numpy()[0]了
  2. 建议加上 xxx:.5f,保留一下位数,不然太长很难看

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

from typing import Tuple

import paddle
import paddle.nn as nn
Copy link
Collaborator

Choose a reason for hiding this comment

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

==> from paddle import nn

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

import paddle
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn.utils import weight_norm
Copy link
Collaborator

Choose a reason for hiding this comment

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

删除该行,建议使用 nn.utils.weight_norm

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

tensor = tensors.pop()
size = sizes.pop()
ind = indices.pop()
# 反池化操作,为上采样
Copy link
Collaborator

Choose a reason for hiding this comment

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

==> # upsample operations

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@HydrogenSulfate
Copy link
Collaborator

@MayYouBeProsperous review好了,有时间可以修改一下代码

@MayYouBeProsperous
Copy link
Contributor Author

@MayYouBeProsperous review好了,有时间可以修改一下代码

辛苦了,已修改代码

Copy link
Collaborator

@HydrogenSulfate HydrogenSulfate left a comment

Choose a reason for hiding this comment

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

几处稍微修改下应该没什么问题了

另外案例文档撰写的时候,数据集下载地址可以填写如下:

Comment on lines 44 to 47
with open(os.path.join(DATASET_PATH, "dataX.pkl"), "rb") as file:
x = pickle.load(file)
with open(os.path.join(DATASET_PATH, "dataY.pkl"), "rb") as file:
y = pickle.load(file)
Copy link
Collaborator

Choose a reason for hiding this comment

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

哦哦,那先不加了吧

Comment on lines 33 to 34
tensors (List[np.array]): non-empty tensor list.
ratio (float): split ratio. For example, tensor list A is split to A1 and A2. len(A1) / len(A) = ratio.
Copy link
Collaborator

Choose a reason for hiding this comment

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

英文句子开头大写

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

},
"batch_size": BATCH_SIZE,
"sampler": {
"name": "DistributedBatchSampler",
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里应该只需要用 BatchSampler 就行了,多卡的情况下会在 build_dataloader 中自动替换成 DistributedBatchSampler

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@HydrogenSulfate
Copy link
Collaborator

HydrogenSulfate commented Sep 13, 2023

整个项目的命名是不是错了,应该是CFD而不是CDF,整个目录全部替换一下

@luotao1
Copy link
Collaborator

luotao1 commented Sep 15, 2023

To do: 编写案例文档

记得补充下案例文档哈

Copy link
Collaborator

@HydrogenSulfate HydrogenSulfate left a comment

Choose a reason for hiding this comment

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

辛苦大佬写的文档,主要有一些代码上的小问题和文档整体代码块错位,修改后应该可以合了

| dataX | [dataX.pkl](https://paddle-org.bj.bcebos.com/paddlescience/datasets/DeepCFD/dataX.pkl) |
| dataY | [dataY.pkl](https://paddle-org.bj.bcebos.com/paddlescience/datasets/DeepCFD/dataY.pkl) |

数据集官网为:https://zenodo.org/record/3666056/files/DeepCFD.zip?download=1
Copy link
Collaborator

Choose a reason for hiding this comment

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

官网==》原始下载地址

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines 53 to 57
``` py linenums="145" title="examples/deepcfd/deepcfd.py"
--8<--
examples/deepcfd/deepcfd.py:145:163
--8<--
```
Copy link
Collaborator

Choose a reason for hiding this comment

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

代码块位置不对,
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


第二个参数是损失函数的定义,这里使用自定义的损失函数,分别计算 Ux 和 Uy 的均方误差,以及 p 的标准差,然后三者加权求和。

第三个参数是约束条件的名字,方便后续对其索引。此次命名为 sup_constraint。
Copy link
Collaborator

Choose a reason for hiding this comment

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

sup_constraint ==> "sup_constraint"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

eval_during_train=True,
eval_freq=50,
validator=validator,
# visualizer=visualizer,
Copy link
Collaborator

Choose a reason for hiding this comment

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

这行注释可以删掉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

CHANNELS_WEIGHTS = np.reshape(
np.sqrt(
np.mean(
np.transpose(y, (0, 2, 3, 1)).reshape((981 * 172 * 79, 3)) ** 2, axis=0
Copy link
Collaborator

Choose a reason for hiding this comment

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

981、172、79应该属于magic number,可以用全大写变量表示一下

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

return split1, split2


def predict_and_save_plot(
Copy link
Collaborator

Choose a reason for hiding this comment

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

同样,docstring加上返回值

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个函数没有返回值

Comment on lines 214 to 215
train_x, train_y = train_dataset[:]
test_x, test_y = test_dataset[:]
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里是否需要[:]浅复制操作?后续应该不涉及对输入数据的修改吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

# evaluate after finished training
solver.eval()

PLOT_DIR = os.path.join(OUTPUT_DIR, "visu")
Copy link
Collaborator

Choose a reason for hiding this comment

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

visu ==> visual

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

in_channel (int): Number of channels of input.
out_channel (int): Number of channels of output.
kernel_size (int): Size of kernel of convolution layer. Defaults to 3.
filters (Tuple[int, ...]): Number of filters. Defaults to [16, 32, 64].
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. 默认值非None的参数,docstring中类型后面加上, optional,比如: Tuple[int, ...], optional
  2. 默认值为None的参数,docstring中类型用Optional[...],比如 Optional[Type[nn.Layer]]
  3. docstring中出现了paddle.nn.Layer,这应该是没更新导致的,可以基于autodocstring重新生成最新的docstring进行修改
  4. 默认值最好不要使用可变类型如list、dict,可以使用None然后在代码里面对入参判None,赋予默认值;或者使用小括号 (...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的感谢

Comment on lines 148 to 152
``` py linenums="1" title="examples/deepcfd/deepcfd.py"
--8<--
examples/deepcfd/deepcfd.py:1:304
--8<--
```
Copy link
Collaborator

Choose a reason for hiding this comment

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

完整代码可以不加 :1:304,直接使用整个文件即可

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor Author

@MayYouBeProsperous MayYouBeProsperous left a comment

Choose a reason for hiding this comment

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

好的 已按照要求修改了

Comment on lines 148 to 152
``` py linenums="1" title="examples/deepcfd/deepcfd.py"
--8<--
examples/deepcfd/deepcfd.py:1:304
--8<--
```
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

| dataX | [dataX.pkl](https://paddle-org.bj.bcebos.com/paddlescience/datasets/DeepCFD/dataX.pkl) |
| dataY | [dataY.pkl](https://paddle-org.bj.bcebos.com/paddlescience/datasets/DeepCFD/dataY.pkl) |

数据集官网为:https://zenodo.org/record/3666056/files/DeepCFD.zip?download=1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines 53 to 57
``` py linenums="145" title="examples/deepcfd/deepcfd.py"
--8<--
examples/deepcfd/deepcfd.py:145:163
--8<--
```
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


第二个参数是损失函数的定义,这里使用自定义的损失函数,分别计算 Ux 和 Uy 的均方误差,以及 p 的标准差,然后三者加权求和。

第三个参数是约束条件的名字,方便后续对其索引。此次命名为 sup_constraint。
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

CHANNELS_WEIGHTS = np.reshape(
np.sqrt(
np.mean(
np.transpose(y, (0, 2, 3, 1)).reshape((981 * 172 * 79, 3)) ** 2, axis=0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

eval_during_train=True,
eval_freq=50,
validator=validator,
# visualizer=visualizer,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

# evaluate after finished training
solver.eval()

PLOT_DIR = os.path.join(OUTPUT_DIR, "visu")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

in_channel (int): Number of channels of input.
out_channel (int): Number of channels of output.
kernel_size (int): Size of kernel of convolution layer. Defaults to 3.
filters (Tuple[int, ...]): Number of filters. Defaults to [16, 32, 64].
Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的感谢


$$\rho(\frac{\partial}{\partial t} + \bf{u} \cdot div ) \bf{u} = - \nabla p + - \nabla \tau + \bf{f}$$

其中 $\bf{u}$ 是速度差(具有 x 和 y 两个维度),$\rho$ 是密度, $p$ 是压强场,$\bf{f}$ 是体积力(例如重力)。
Copy link
Collaborator

Choose a reason for hiding this comment

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

速度差 ==> 速度场?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


<figure markdown>
![DeepCFD](https://ai-studio-static-online.cdn.bcebos.com/288c37b569d5400aa7b2265ff13fcf0edad3115e70fe4fafb6736215355771fe){ loading=lazy}
<figcaption>OpenFOAM 计算结果与 DeepCFD 预测结果对比</figcaption>
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. ==> OpenFOAM 计算结果与 DeepCFD 预测结果对比,从左到右分别为:水平速度分量(Ux),垂直速度分量(Uy)以及流体压强(p)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

![DeepCFD](https://ai-studio-static-online.cdn.bcebos.com/288c37b569d5400aa7b2265ff13fcf0edad3115e70fe4fafb6736215355771fe){ loading=lazy}
<figcaption>OpenFOAM 计算结果与 DeepCFD 预测结果对比</figcaption>
</figure>

Copy link
Collaborator

Choose a reason for hiding this comment

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

图片下面加一句简单结论:可以看到DeepCFD方法与OpenFOAM的结果基本一致

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Collaborator

@HydrogenSulfate HydrogenSulfate left a comment

Choose a reason for hiding this comment

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

LGTM

@PaddlePaddle PaddlePaddle locked as resolved and limited conversation to collaborators Sep 24, 2023
@PaddlePaddle PaddlePaddle unlocked this conversation Sep 24, 2023
@HydrogenSulfate HydrogenSulfate merged commit 41e0424 into PaddlePaddle:develop Sep 24, 2023
@luotao1 luotao1 added HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务 and removed HappyOpenSource 快乐开源活动issue与PR labels Nov 20, 2023
huohuohuohuohuo123 pushed a commit to huohuohuohuohuo123/PaddleScience that referenced this pull request Aug 12, 2024
* add deepcdf

* fix bugs

* add comment

* add comment

* add comment

* code style

* add license

* code style

* fix

* fix

* fix

* fix

* fix

* add validator

* fix

* add validator

* add doc

* fix

* fix

* fix

* fix

* refine doc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants