OpenFOAM libtorch tutorial step by step
-
#include <torch/torch.h> using namespace std; class NN: public torch::nn::Module { public: torch::nn::Sequential net_; NN() { net_ = register_module ( "net", torch::nn::Sequential ( torch::nn::Linear(9, 10), torch::nn::ReLU(), torch::nn::Linear(10, 1) ) ); } torch::Tensor forward(torch::Tensor x) { return net_->forward(x); } }; int main() { torch::nn::MSELoss crit; NN model; auto input = torch::empty({6, 1, 9}); #include "data.H" torch::optim::Adam adam(model.parameters(), torch::optim::AdamOptions(0.001)); for (int epoch = 0; epoch < 5000; epoch++) { auto outputF = model.forward(input); auto target = torch::empty_like(outputF); #include "target.H" auto loss = crit(outputF, target); adam.zero_grad(); loss.backward(); adam.step(); if ((epoch + 1) % 100 == 0) { cout << "Loss: " << loss.item<float>() << endl; } } auto test = torch::empty({2, 1, 9}); // batch, channel, sequence length #include "test.H" auto output = model.forward(test); std::cout<< output << endl; return 0; }
更新非常简单的一个可以用来识别流场是否存在间断的代码框架
-
@李东岳 老师,好!
还是有很多不明白的地方,多谢您的解答。- 为了在编译的时候能找到“argList.H”,“solver.H”这两个文件,我查找了这个文件的位置,并在options文件里面指定了这两个文件的位置。 :
EXE_INC = \ ... -I$(LIB_SRC)/optimisation/adjointOptimisation/adjoint/lnInclude \ -I$(LIB_SRC)/OpenFOAM/lnInclude \ -I$(LIB_SRC)/OpenFOAM/include \ ... EXE_LIBS = \ ... -libadjointOptimisation \ -libOpenFOAM \ ...
-
此外,从icoFoam solver 里面粘贴了一个createFields.H文件,但继续编译的时候,又出现了新的错误。这个**“simplifiedMeshes”和"fvc"的错误改如何解决**呀?请李老师指点一下。
-
最后,我有一个疑问,您这个代码如何与求解器建立关系呢?它对所有的求解器都是通用的么?
-
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.