OpenFOAM libtorch tutorial step by step
-
@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;