OpenFOAM libtorch tutorial step by step
-
添加几点注意
- 第一步libtorch需要下载到OpenFOAM的目录里面并解压。
- 第二步WSL2 里面可能没装 uzip,用
sudo apt-install zip uzip
安装 - 解压torchFoam.tar.xz 后进入torchFoam
wmake
的时候,需要把torchFoam/Make
里面的options里面所有的dyfluid
换成自己的这个WSL的用户名,pwd
一下看/home/后面是啥就知道了
-
@李东岳 在 OpenFOAM libtorch tutorial step by step 中说:
更新gcc之后你openfoam就编译不了了。那你尝试安装老版本的libtorch吧
李老师,我用的是ubuntu18.04,Of2.4.0,您有推荐的libtorch版本么
-
@chengan-wang 太老了,gcc版本啥的都太低了,我没有折腾过,你得亲自自己都尝试一下看看哪个能用
-
@李东岳 李老师,http://dyfluid.com/pinn.html ,#include "output.H"这个文件能补充上传么,想研究一下您的完整代码,谢谢
-
@chengan-wang 那个我已经删了,你可以参考这个写个类似的,纯粹是C++的输出数据:
if (iter % 500 == 0) { cout<< iter << " loss = " << loss.item<double>() << endl; auto meshxy = torch::cat({mesh,x,y}, 1); std::ofstream filex("results"); filex << meshxy << "\n"; filex.close(); }
-
You can use the following code to initialize libtorch tensors from OpenFOAM mesh. It is much easier to generate any kinds of geometries. Please see the following clipped cavity geometry. I am using English since I hope it can be helpful for all the people.
// openfoam initialization vectorField ofMesh = mesh.C(); auto meshTorch = torch::full({ofMesh.size(),1,2}, 0.0); forAll(ofMesh, i) { double x = ofMesh[i].x(); double y = ofMesh[i].y(); auto posi = torch::tensor({x,y}).reshape({1,2}); meshTorch[i] = posi; } const surfaceVectorField& Cf = mesh.Cf(); label topID = mesh.boundaryMesh().findPatchID("top"); label leftID = mesh.boundaryMesh().findPatchID("left"); label rightID = mesh.boundaryMesh().findPatchID("right"); label bottomID = mesh.boundaryMesh().findPatchID("bottom"); vectorField cfTop = Cf.boundaryField()[topID]; vectorField cfLeft = Cf.boundaryField()[leftID]; vectorField cfRight = Cf.boundaryField()[rightID]; vectorField cfBottom = Cf.boundaryField()[bottomID]; auto top = torch::full({cfTop.size(),1,2}, 0.0); auto left = torch::full({cfLeft.size(),1,2}, 0.0); auto right = torch::full({cfRight.size(),1,2}, 0.0); auto bottom = torch::full({cfBottom.size(),1,2}, 0.0); forAll(cfTop, i) { double x = cfTop[i].x(); double y = cfTop[i].y(); auto posi = torch::tensor({x,y}).reshape({1,2}); top[i] = posi; } forAll(cfRight, i) { double x = cfRight[i].x(); double y = cfRight[i].y(); auto posi = torch::tensor({x,y}).reshape({1,2}); right[i] = posi; } forAll(cfLeft, i) { double x = cfLeft[i].x(); double y = cfLeft[i].y(); auto posi = torch::tensor({x,y}).reshape({1,2}); left[i] = posi; } forAll(cfBottom, i) { double x = cfBottom[i].x(); double y = cfBottom[i].y(); auto posi = torch::tensor({x,y}).reshape({1,2}); bottom[i] = posi; } meshTorch = meshTorch.reshape({-1,2}); top = top.reshape({-1,2}); left = left.reshape({-1,2}); right = right.reshape({-1,2}); bottom = bottom.reshape({-1,2}); auto u_top = torch::full({cfTop.size()}, vel); auto v_top = torch::full({cfTop.size()}, 0.0); std::cout<< "top: " << top << std::endl; std::cout<< "left: " << left << std::endl; std::cout<< "right: " << right << std::endl; std::cout<< "bottom: " << bottom << std::endl;
-
@李东岳 在 OpenFOAM libtorch tutorial step by step 中说:
net.H
#include <torch/torch.h> class NN : public torch::nn::Module { torch::nn::Sequential net_; public: NN(); torch::Tensor forward(torch::Tensor x); };
net.C
/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org \\ / A nd | Copyright (C) 2022-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License This file is part of OpenFOAM. OpenFOAM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenFOAM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. Class See also SourceFiles \*---------------------------------------------------------------------------*/ #include "net.H" NN::NN() { net_ = register_module ( "net", torch::nn::Sequential ( torch::nn::Linear(2,20), torch::nn::ReLU(), torch::nn::Linear(20,30), torch::nn::ReLU(), torch::nn::Linear(30,30), torch::nn::ReLU(), torch::nn::Linear(30,20), torch::nn::ReLU(), torch::nn::Linear(20,20), torch::nn::ReLU(), torch::nn::Linear(20,1) ) ); }; torch::Tensor NN::forward(torch::Tensor x) { return net_->forward(x); }
这个要编译成库,挂到PINNFoam上面
李老师 请问这个如何编译并挂载呢 可以详细的指导一下吗
-
@李东岳 李老师您好,我在看您发的那个顶盖驱动流的代码的时候有个问题,我的电脑安装了openfoam10和11,同样的文件内容为什么在of10下我可以成功编译,但在of11下不可以呢。在11下,一开始提示找不到libtriSurface.so和libsurfMesh.so,然后我在bashrc中添加了,export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/john/OpenFOAM/OpenFOAM-11/platforms/linux64GccDPInt32Opt/lib,随后再次运行出现了这样的问题。