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中
-