{ "cells": [ { "cell_type": "markdown", "id": "08bceb91-45c4-48ad-aa62-4acb2b5a2817", "metadata": { "tags": [] }, "source": [ "\n", "# 1. DeepCFD 模型简介\n", "\n", "计算流体力学(Computational fluid dynamics, CFD)通过求解Navier-Stokes方程(N-S方程),可以获得流体的各种物理量的分布,如密度、压力和速度等,在微电子系统、土木工程和航空航天等领域应用广泛。然而,在某些复杂的应用场景中,如机翼优化和流体与结构相互作用方面,需要使用千万级甚至上亿的网格对问题进行建模(如下图所示,下图展示了F-18战斗机的全机内外流一体结构化网格模型),导致CFD的计算量非常巨大。因此,目前亟需发展出一种相比于传统CFD方法更高效,且可以保持计算精度的方法。这篇文章的作者提到,可以使用深度学习的方法,通过训练少量传统CFD仿真的数据,构建一种数据驱动(data-driven)的CFD计算模型,来解决上述的问题。\n", "\n", "\"img\"\n", "\n", "本模型是针对论文:[DeepCFD: Efficient steady-state laminar flow approximation with deep convolutional neural networks](https://arxiv.org/abs/2004.08826)[J]基于飞桨框架进行模型复现,文中作者提出了一个基于卷积神经网络(Convolutional neural networks, CNN)的CFD计算模型,称作DeepCFD,该模型可以同时计算流体流过任意障碍物的流场。该方法有以下几个特点:\n", "\n", "1. DeepCFD本质上是一种基于CNN的代理模型,可以用于快速计算二维非均匀稳态层流流动,相比于传统的CFD方法,该方法可以在保证计算精度的情况下达到至少三个数量级的加速。\n", "\n", "2. DeepCFD可以同时计算流体在x方向和y方向的流体速度,同时还能计算流体压强。\n", "\n", "3. 训练该模型的数据由OpenFOAM(一种开源CFD计算软件)计算得到。\n", "\n", "\n", "# 2. 模型效果及应用场景\n", "\n", "\n", "# 3. 模型如何使用\n", "\n", "## 3.1 环境安装\n", "\n", "* 硬件:GPU、CPU\n", "* 框架:PaddlePaddle >= 2.0.0\n", "## 3.2 数据集\n", "\n", "数据集使用原作者利用OpenFOAM计算的CFD算例,共981组,分为两个文件(dataX.pkl, dataY.pkl),两个文件大小都是152 MB,形状均为[981, 3, 172, 79]。dataX.pkl包括三种输入:障碍物的SDF、计算域边界的SDF和流动区域的标签;dataY.pkl包括三种输出:流体的x方向速度、y方向速度和流体压强。数据获取使用的计算网格为172×79。\n", "\n", "数据集地址:https://aistudio.baidu.com/aistudio/datasetdetail/162674\n", "\n", "或https://www.dropbox.com/s/kg0uxjnbhv390jv/Data_DeepCFD.7z?dl=0\n", "\n", "## 3.3 快速开始\n", "\n", "**step1:克隆本项目**\n", "\n", "搜索DeepCFD_with_PaddlePaddle,选择对应的版本,Fork。\n", "\n", "![fork.png](https://github.com/zbyandmoon/Picture/blob/main/picture_DeepCFD/fork.png?raw=true)\n", "\n", "**step2:开始训练**\n", "\n", "选择进入终端。\n", "\n", "![click_terminal.png](https://github.com/zbyandmoon/Picture/blob/main/picture_DeepCFD/click_terminal.png?raw=true)\n", "\n", "**单卡训练**\n", "\n", "```python\n", "python -m paddle.distributed.launch --gpus=0 train.py\n", "```\n", "\n", "**多卡训练**\n", "\n", "以四卡为例\n", "\n", "```python\n", "python -m paddle.distributed.launch --gpus=0,1,2,3 train.py\n", "```\n", "\n", "结果保存在result文件中(注:result文件夹中已经包含了一个完整的训练过程,可在训练前将其清空)。多卡训练会额外生成一个./log/文件夹,存放训练日志\n", "\n", "```python\n", ".\n", "├── log\n", "│ ├── workerlog.0\n", "│ ├── workerlog.1\n", "│ ├── workerlog.2\n", "│ └── workerlog.3\n", "└── train.py\n", "```\n", "\n", "部分训练日志如下所示\n", "\n", "```python\n", "Epoch #1\n", " Train Loss = 884808909.0\n", " Train Total MSE = 10197.3000353043\n", " Train Ux MSE = 3405.3426083044824\n", " Train Uy MSE = 4334.0962839376825\n", " Train p MSE = 2457.8616943359375\n", " Validation Loss = 53205074.5\n", " Validation Total MSE = 1027.7523040254237\n", " Validation Ux MSE = 419.7688029661017\n", " Validation Uy MSE = 543.9674920550848\n", " Validation p MSE = 64.01604872881356\n", "Epoch #2\n", " Train Loss = 75408434.25\n", " Train Total MSE = 603.198411591199\n", " Train Ux MSE = 277.9321616481414\n", " Train Uy MSE = 303.4222437021684\n", " Train p MSE = 21.843986488987337\n", " Validation Loss = 17892356.5\n", " Validation Total MSE = 312.7194186970339\n", " Validation Ux MSE = 169.64230501853814\n", " Validation Uy MSE = 140.46789757680085\n", " Validation p MSE = 2.6092084981627384\n", "```\n", "\n", "**step3:评估模型**\n", "\n", "```python\n", "python eval.py\n", "```\n", "\n", "此时的输出为:\n", "\n", "```python\n", "Total MSE is 1.895322561264038, Ux MSE is 0.6951090097427368, Uy MSE is 0.21001490950584412, p MSE is 0.9901986718177795\n", "```\n", "\n", "**step4:使用预训练模型预测**\n", "\n", "考虑到需要展示流场图像对比结果,单独写了一个predict.ipynb来进行模型的验证,需要在Jupyter notebook环境中运行。\n", "\n", "运行predict.ipynb,某个障碍物的流场预测结果展示如下:\n", "\n", "![paddle_contour.png](https://github.com/zbyandmoon/Picture/blob/main/picture_DeepCFD/paddle_contour.png?raw=true)\n", "\n", "## 3.4 代码结构与参数说明\n", "\n", "### 3.4.1 代码结构\n", "\n", "```python\n", "DeepCFD_with_PaddlePaddle\n", "├─ config\n", "│ └─ config.ini\n", "├─ data\n", "│ └─ README.md\n", "├─ model\n", "│ └─ UNetEx.py\n", "├─ result\n", "│ ├─ DeepCFD_965.pdparams\n", "│ ├─ results.json\n", "│ └─ train_log.txt\n", "└─ utils\n", " ├─ functions.py\n", " └─ train_functions.py \n", "├─ README.md\n", "├─ README_cn.md\n", "├─ eval.py\n", "├─ train.py\n", "├─ predict.ipynb\n", "├─ requirements.txt\n", "```\n", "\n", "### 3.4.2 参数说明\n", "\n", "可以在/DeepCFD_with_PaddlePaddle/config/config.ini中设置训练的参数,包括以下内容:\n", "\n", "| 参数 | 推荐值 | 额外说明 |\n", "| ---------------- | -------------------- | --------------------------------------------- |\n", "| batch_size | 64 | |\n", "| train_test_ratio | 0.7 | 训练集占数据集的比例,0.7即训练集70%测试集30% |\n", "| learning_rate | 0.001 | |\n", "| weight_decay | 0.005 | AdamW专用,若修改优化算法需要修改train.py |\n", "| epochs | 1000 | |\n", "| kernel_size | 5 | 卷积核大小 |\n", "| filters | 8, 16, 32, 32 | 卷积层channel数目 |\n", "| batch_norm | 0 | 批量正则化,0为False,1为True |\n", "| weight_norm | 0 | 权重正则化,0为False,1为True |\n", "| data_path | ./data | 数据集路径,视具体情况设置 |\n", "| save_path | ./result | 模型和训练记录的保存路径,视具体情况设置 |\n", "| model_name | DeepCFD_965.pdparams | 具体加载的模型名称,后缀不能省略 |\n", "\n", "\n", "## 3.5 预测结果\n", "\n", "下图展示了原文的预测结果,文中评价模型的优劣共包含四个指标:Total MSE、Ux MSE、Uy MSE、p MSE(MSE的意思是均方根误差)。\n", "\n", "![metrics.png](https://github.com/zbyandmoon/Picture/blob/main/picture_DeepCFD/metrics.png?raw=true)\n", "\n", "下图展示了某种形状障碍物的CFD(注:simpleFOAM是OpenFOAM求解器的一种)和DeepCFD流场计算结果对比。\n", "\n", "![pytorch_contour.png](https://github.com/zbyandmoon/Picture/blob/main/picture_DeepCFD/pytorch_contour.png?raw=true)\n", "# 4. 模型原理\n", "\n", "下面两张图分别为该方法的计算示意图和网络结构图。文中使用的DeepCFD网络基本结构为有3个输入和3个输出的U-net型网络。该模型输入为计算域中障碍物的符号距离函数(Signed distance function, SDF)、计算域边界的SDF和流动区域的标签;输出为流体的x方向速度、y方向速度以及流体压强。该模型的基本原理就是利用编码器部分的卷积层将3个输入下采样,变为中间量,然后使用相同结构的解码器中的反卷积层将中间量上采样为3个流体物理量输出。\n", "\n", "![compute_process.png](https://github.com/zbyandmoon/Picture/blob/main/picture_DeepCFD/compute_process.png?raw=true)\n", "\n", "![DeepCFD_Net.png](https://github.com/zbyandmoon/Picture/blob/main/picture_DeepCFD/DeepCFD_Net.png?raw=true)\n", "\n", "\n", "# 5. 注意事项\n", "## 5.1 模型验收标准\n", "\n", "复现的验收标准如下:\n", "\n", "![standard.png](https://github.com/zbyandmoon/Picture/blob/main/picture_DeepCFD/standard.png?raw=true)\n", "\n", "## 5.2 指标实现情况\n", "\n", "复现的实现指标如下:\n", "\n", "```python\n", "Total MSE = 1.8955801725387573\n", "Ux MSE = 0.6953578591346741\n", "Uy MSE = 0.21001338958740234\n", "p MSE = 0.9902092218399048\n", "```\n", "\n", "其中,Total MSE、Ux MSE和Uy MSE在验收标准范围内,p MSE略小于验收标准的最小值。\n", "\n", "## 5.3 复现地址\n", "\n", "论文复现地址:\n", "\n", "AI Studio: https://aistudio.baidu.com/aistudio/projectdetail/4400677?contributionType=1\n", "\n", "github: https://github.com/zbyandmoon/DeepCFD_with_PaddlePaddle/tree/main/paddle\n", "\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "py35-paddle1.2.0" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" }, "toc-autonumbering": true }, "nbformat": 4, "nbformat_minor": 5 }