OpenFOAM libtorch tutorial step by step
-
Info<< "mesh size: " << mesh.C().size() << nl; int cellNum = mesh.C().size(); vectorField uCell = U.field(); auto a = torch::zeros({cellNum, 2}); for (int i = 0; i < cellNum; i++) { a[i][0] = uCell[i].x(); a[i][1] = uCell[i].y(); }; std::cout<< "a:" << a << std::endl;
上面的代码可以把OpenFOAM的U场存储到a中
-
-
一个libtorch下自编码器范例
可以自己添加skip connection变成Unet
class NN : public torch::nn::Module { torch::nn::Sequential net_; public: NN() { net_ = register_module ( "net", torch::nn::Sequential ( // encoder torch::nn::Conv2d(2, 5, 3),// 2 5 138 torch::nn::BatchNorm2d(5), torch::nn::Tanh(), torch::nn::MaxPool2d(2),// 2 5 69 torch::nn::Conv2d(5, 10, 3), //5 10 67 torch::nn::BatchNorm2d(10), torch::nn::Tanh(), torch::nn::MaxPool2d(2), // 5 10 33 torch::nn::Conv2d(10, 10, 3), // 10 10 31 torch::nn::BatchNorm2d(10), torch::nn::Tanh(), torch::nn::MaxPool2d(2), // 10 10 15 torch::nn::Conv2d(10, 10, 3), // 10 10 13 torch::nn::BatchNorm2d(10), torch::nn::Tanh(), torch::nn::MaxPool2d(2), // 10 10 6 // decoder torch::nn::Upsample ( torch::nn::UpsampleOptions().scale_factor ( std::vector<double>{2.0, 2.0} ).mode(torch::kBilinear).align_corners(true) ), // 10 10 12 torch::nn::Conv2d(10, 10, 2), // 10 10 11 torch::nn::Tanh(), torch::nn::Upsample ( torch::nn::UpsampleOptions().scale_factor ( std::vector<double>{2.0, 2.0} ).mode(torch::kBilinear).align_corners(true) ),// 10 10 22 torch::nn::Conv2d(10, 10, 3),// 10 10 20 torch::nn::Tanh(), torch::nn::Upsample ( torch::nn::UpsampleOptions().scale_factor ( std::vector<double>{2.0, 2.0} ).mode(torch::kBilinear).align_corners(true) ),// 10 10 40 torch::nn::Conv2d(10, 10, 3),// 10 10 38 torch::nn::Tanh(), torch::nn::Upsample ( torch::nn::UpsampleOptions().scale_factor ( std::vector<double>{2.0, 2.0} ).mode(torch::kBilinear).align_corners(true) ),// 10 10 76 torch::nn::Conv2d(10, 10, 3),// 10 10 74 torch::nn::Tanh(), torch::nn::Upsample ( torch::nn::UpsampleOptions().scale_factor ( std::vector<double>{2.0, 2.0} ).mode(torch::kBilinear).align_corners(true) ),// 10 10 148 torch::nn::Conv2d(10, 10, 3),// 10 10 146 torch::nn::Tanh(), torch::nn::Conv2d(10, 5, 3),// 10 10 144 torch::nn::Tanh(), torch::nn::Conv2d(5, 2, 3),// 10 10 142 torch::nn::Tanh(), torch::nn::Conv2d(2, 2, 3)// 10 10 140 ) ); } auto forward(torch::Tensor x) { return net_->forward(x); } };
-
@李东岳 李老师,问题解决了,是我把option文件改错了个地方。。新的问题又来了,我在OF11中(仍是该虚拟机)对bed案例进行foamRun或foamRun -solver multiphaseEuler时,为何报错:```
--> FOAM FATAL ERROR:
solvers table is emptyFrom function static Foam::autoPtr<Foam::solver> Foam::solver::New(const Foam::word&, Foam::fvMesh&) in file solver/solverNew.C at line 48.
-
添加几点注意
- 第一步libtorch需要下载到OpenFOAM的目录里面并解压。
- 第二步WSL2 里面可能没装 uzip,用
sudo apt-install zip uzip
安装 - 解压torchFoam.tar.xz 后进入torchFoam
wmake
的时候,需要把torchFoam/Make
里面的options里面所有的dyfluid
换成自己的这个WSL的用户名,pwd
一下看/home/后面是啥就知道了