Skip to content
  • 最新
  • 版块
  • 东岳流体
  • 随机看[请狂点我]
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(不使用皮肤)
  • 不使用皮肤
折叠
CFD中文网

CFD中文网

  1. CFD中文网
  2. OpenFOAM
  3. OpenFOAM libtorch tutorial step by step

OpenFOAM libtorch tutorial step by step

已定时 已固定 已锁定 已移动 OpenFOAM
77 帖子 16 发布者 56.1k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
回复
  • 在新帖中回复
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • 李东岳李 在线
    李东岳李 在线
    李东岳 管理员
    写于 最后由 编辑
    #50

    一个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);
        }
    };
    

    http://dyfluid.com/index.html
    需要帮助debug算例的看这个 https://cfd-china.com/topic/8018

    1 条回复 最后回复
  • 小 离线
    小 离线
    小泽同学
    在 中回复了 李东岳 最后由 编辑
    #51

    @李东岳 感谢李老师,明白需要配合版本使用,不升级是因为课题组在做的求解器模型是从4.1开始的,我也在高版本上运行过,但是总是报错就只能用4.1了,感谢李老师的回复。

    1 条回复 最后回复
  • 李东岳李 在线
    李东岳李 在线
    李东岳 管理员
    写于 最后由 编辑
    #52

    你可以尝试把openfoam低版本的求解器升级到高版本。

    如果没兴趣不整也行。或者马上毕业了也属实没必要。这东西基本是兴趣驱动。。

    http://dyfluid.com/index.html
    需要帮助debug算例的看这个 https://cfd-china.com/topic/8018

    1 条回复 最后回复
  • 火 离线
    火 离线
    火丙三金
    写于 最后由 编辑
    #53

    请问各位有没有用pytorch的

    1 条回复 最后回复
  • Z 离线
    Z 离线
    Zhy2022
    写于 最后由 编辑
    #54

    想请问李老师:我使用全系列虚拟机(您已提前配置好),再运行第6步这个简单例子,报错未找到torchFoam这个命令,是啥原因造成的呢:135: 编译过程中有很多warning和note应该不影响吧

    1 条回复 最后回复
  • 李东岳李 在线
    李东岳李 在线
    李东岳 管理员
    写于 最后由 编辑
    #55

    warning note不影响,把你log上传一下看看

    http://dyfluid.com/index.html
    需要帮助debug算例的看这个 https://cfd-china.com/topic/8018

    Z 1 条回复 最后回复
  • Z 离线
    Z 离线
    Zhy2022
    在 中回复了 李东岳 最后由 编辑
    #56

    @李东岳 李老师,问题解决了,是我把option文件改错了个地方。。新的问题又来了,我在OF11中(仍是该虚拟机)对bed案例进行foamRun或foamRun -solver multiphaseEuler时,为何报错:```
    --> FOAM FATAL ERROR:
    solvers table is empty

    From function static Foam::autoPtr<Foam::solver> Foam::solver::New(const Foam::word&, Foam::fvMesh&)
    in file solver/solverNew.C at line 48.
    
    1 条回复 最后回复
  • 李东岳李 在线
    李东岳李 在线
    李东岳 管理员
    写于 最后由 编辑
    #57

    openfoam11直接运行foamRun就行了,不需要指定solver

    http://dyfluid.com/index.html
    需要帮助debug算例的看这个 https://cfd-china.com/topic/8018

    Z 1 条回复 最后回复
  • Z 离线
    Z 离线
    Zhy2022
    在 中回复了 李东岳 最后由 编辑
    #58

    @李东岳 李老师,直接foamRun还是报一样的错误

    1 条回复 最后回复
  • hurricane007H 离线
    hurricane007H 离线
    hurricane007
    写于 最后由 编辑
    #59

    添加几点注意

    1. 第一步libtorch需要下载到OpenFOAM的目录里面并解压。
    2. 第二步WSL2 里面可能没装 uzip,用 sudo apt-install zip uzip 安装
    3. 解压torchFoam.tar.xz 后进入torchFoam wmake 的时候,需要把 torchFoam/Make 里面的options里面所有的dyfluid换成自己的这个WSL的用户名,pwd一下看/home/后面是啥就知道了
    1 条回复 最后回复
  • chengan.wangC 离线
    chengan.wangC 离线
    chengan.wang
    在 中回复了 李东岳 最后由 编辑
    #60

    @李东岳 在 OpenFOAM libtorch tutorial step by step 中说:

    更新gcc之后你openfoam就编译不了了。那你尝试安装老版本的libtorch吧

    李老师,我用的是ubuntu18.04,Of2.4.0,您有推荐的libtorch版本么

    李东岳李 1 条回复 最后回复
  • 李东岳李 在线
    李东岳李 在线
    李东岳 管理员
    在 中回复了 chengan.wang 最后由 编辑
    #61

    @chengan-wang 太老了,gcc版本啥的都太低了,我没有折腾过,你得亲自自己都尝试一下看看哪个能用

    http://dyfluid.com/index.html
    需要帮助debug算例的看这个 https://cfd-china.com/topic/8018

    1 条回复 最后回复
  • chengan.wangC 离线
    chengan.wangC 离线
    chengan.wang
    在 中回复了 李东岳 最后由 李东岳 编辑
    #62

    @李东岳 李老师,http://dyfluid.com/pinn.html ,#include "output.H"这个文件能补充上传么,想研究一下您的完整代码,谢谢

    李东岳李 1 条回复 最后回复
  • 李东岳李 在线
    李东岳李 在线
    李东岳 管理员
    在 中回复了 chengan.wang 最后由 李东岳 编辑
    #63

    @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();
            }
    

    http://dyfluid.com/pinn2.html

    http://dyfluid.com/index.html
    需要帮助debug算例的看这个 https://cfd-china.com/topic/8018

    1 条回复 最后回复
  • 李东岳李 在线
    李东岳李 在线
    李东岳 管理员
    写于 最后由 李东岳 编辑
    #64

    我给你手写了一个output.H:

            if (iter % 500 == 0) 
            {
                cout<< iter << " loss = " << loss.item<double>() << endl;
     
                std::ofstream filex("results");
                filex << upred << "\n";
                filex.close();
            }
    

    http://dyfluid.com/index.html
    需要帮助debug算例的看这个 https://cfd-china.com/topic/8018

    chengan.wangC 2 条回复 最后回复
  • chengan.wangC 离线
    chengan.wangC 离线
    chengan.wang
    在 中回复了 李东岳 最后由 编辑
    #65

    @李东岳 谢谢李老师

    1 条回复 最后回复
  • chengan.wangC 离线
    chengan.wangC 离线
    chengan.wang
    在 中回复了 李东岳 最后由 编辑
    #66

    @李东岳 我看了您提供的PINN算例,感觉目前还没有真正跟of结合起来,是吧?只不过用了of的c++编译环境,可以这么理解么?

    李东岳李 1 条回复 最后回复
  • 李东岳李 在线
    李东岳李 在线
    李东岳 管理员
    写于 最后由 编辑
    #67

    是。PINN不需要跟FVM结合,只不过用了of的c++编译环境。想跟OpenFOAM结合,除了画网格能结合一下,其他的也结合不上。

    http://dyfluid.com/index.html
    需要帮助debug算例的看这个 https://cfd-china.com/topic/8018

    1 条回复 最后回复
  • 李东岳李 在线
    李东岳李 在线
    李东岳 管理员
    在 中回复了 chengan.wang 最后由 李东岳 编辑
    #68

    @chengan-wang

    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.

    微信图片_20241017082858.png

        // 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;
    

    http://dyfluid.com/index.html
    需要帮助debug算例的看这个 https://cfd-china.com/topic/8018

    1 条回复 最后回复
  • 水 离线
    水 离线
    水润晨阳
    在 中回复了 李东岳 最后由 编辑
    #69

    @李东岳 在 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上面

    李老师 请问这个如何编译并挂载呢 可以详细的指导一下吗

    1 条回复 最后回复

  • 登录

  • 登录或注册以进行搜索。
  • 第一个帖子
    最后一个帖子
0
  • 最新
  • 版块
  • 东岳流体
  • 随机看[请狂点我]