Skip to content

Commit 6a3db65

Browse files
author
Andre Weiner
committed
Add first implementation of velocity bc and test case.
1 parent 40e26e4 commit 6a3db65

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+35172
-1
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
boundary_conditions/pyTorchCppTest/build/
2+
boundary_conditions/bubbleSurfaceVelocitySimple/lnInclude
3+
boundary_conditions/bubbleSurfaceVelocitySimple/Make/linux64GccDPInt32Opt/
4+
*.so

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ All boundary conditions, utilities, and solvers are compiled using a special Doc
1313
- OpenFOAM-v1906
1414
- [PyTorch](https://pytorch.org/) 1.2.0
1515

16-
The Dockerfile and additional information on the build process can be found [here](https://github.com/AndreWeiner/of_pytorch_docker).
16+
The Dockerfile and additional information on the build process can be found [here](https://github.com/AndreWeiner/of_pytorch_docker). The docker image is hosted on [Dockerhub](https://cloud.docker.com/u/andreweiner/repository/docker/andreweiner/of_pytorch). To pull the image containing OpenFOAM-v1906 and PyTorch 1.2, run
17+
18+
```
19+
docker pull andreweiner/of_pytorch:of1906-py1.2-cpu
20+
```
1721

1822
### Docker
1923

@@ -27,4 +31,8 @@ Any installed version of [Docker](https://docs.docker.com/install/) larger than
2731

2832
## Running test cases
2933

34+
```
35+
source /opt/OpenFOAM/OpenFOAM-v1906/etc/bashrc
36+
```
37+
3038
## How to reference

boundary_conditions/README.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bubbleSurfaceVelocitySimpleFvPatchVectorField.C
2+
LIB = libBubbleSurfaceVelocitySimple
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
EXE_INC = \
2+
-I$(LIB_SRC)/finiteVolume/lnInclude \
3+
-I$(LIB_SRC)/fileFormats/lnInclude \
4+
-I$(LIB_SRC)/surfMesh/lnInclude \
5+
-I$(LIB_SRC)/meshTools/lnInclude \
6+
-I$(TORCH_LIBRARIES)/include \
7+
-I$(TORCH_LIBRARIES)/include/torch/csrc/api/include
8+
9+
LIB_LIBS = \
10+
-lfiniteVolume \
11+
-lfvOptions \
12+
-lmeshTools \
13+
-rdynamic \
14+
-Wl,-rpath,$(TORCH_LIBRARIES)/lib $(TORCH_LIBRARIES)/lib/libtorch.so $(TORCH_LIBRARIES)/lib/libc10.so \
15+
-Wl,--as-needed $(TORCH_LIBRARIES)/lib/libc10.so \
16+
-lpthread
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*---------------------------------------------------------------------------*\
2+
========= |
3+
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4+
\\ / O peration |
5+
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
6+
\\/ M anipulation |
7+
-------------------------------------------------------------------------------
8+
| Copyright (C) 2011-2016 OpenFOAM Foundation
9+
-------------------------------------------------------------------------------
10+
License
11+
This file is part of OpenFOAM.
12+
13+
OpenFOAM is free software: you can redistribute it and/or modify it
14+
under the terms of the GNU General Public License as published by
15+
the Free Software Foundation, either version 3 of the License, or
16+
(at your option) any later version.
17+
18+
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21+
for more details.
22+
23+
You should have received a copy of the GNU General Public License
24+
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25+
26+
\*---------------------------------------------------------------------------*/
27+
28+
#include "bubbleSurfaceVelocitySimpleFvPatchVectorField.H"
29+
#include "addToRunTimeSelectionTable.H"
30+
#include "volFields.H"
31+
#include "surfaceFields.H"
32+
33+
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34+
35+
Foam::bubbleSurfaceVelocitySimpleFvPatchVectorField::
36+
bubbleSurfaceVelocitySimpleFvPatchVectorField
37+
(
38+
const fvPatch& p,
39+
const DimensionedField<vector, volMesh>& iF
40+
)
41+
:
42+
fixedValueFvPatchField<vector>(p, iF),
43+
origin_(Zero),
44+
axis_(Zero),
45+
normal_(Zero),
46+
model_name_("")
47+
{}
48+
49+
50+
Foam::bubbleSurfaceVelocitySimpleFvPatchVectorField::
51+
bubbleSurfaceVelocitySimpleFvPatchVectorField
52+
(
53+
const fvPatch& p,
54+
const DimensionedField<vector, volMesh>& iF,
55+
const dictionary& dict
56+
)
57+
:
58+
fixedValueFvPatchField<vector>(p, iF, dict, false),
59+
origin_(dict.lookup("origin")),
60+
axis_(dict.lookup("axis")),
61+
normal_(dict.lookup("normal")),
62+
model_name_(dict.lookupOrDefault<word>("model", "velocity_model.pt"))
63+
{
64+
pyTorch_model_ = torch::jit::load(model_name_);
65+
if (!dict.found("value"))
66+
{
67+
updateCoeffs();
68+
}
69+
}
70+
71+
72+
Foam::bubbleSurfaceVelocitySimpleFvPatchVectorField::
73+
bubbleSurfaceVelocitySimpleFvPatchVectorField
74+
(
75+
const bubbleSurfaceVelocitySimpleFvPatchVectorField& ptf,
76+
const fvPatch& p,
77+
const DimensionedField<vector, volMesh>& iF,
78+
const fvPatchFieldMapper& mapper
79+
)
80+
:
81+
fixedValueFvPatchField<vector>(ptf, p, iF, mapper),
82+
origin_(ptf.origin_),
83+
axis_(ptf.axis_),
84+
normal_(ptf.normal_),
85+
model_name_(ptf.model_name_),
86+
pyTorch_model_(ptf.pyTorch_model_)
87+
{}
88+
89+
90+
Foam::bubbleSurfaceVelocitySimpleFvPatchVectorField::
91+
bubbleSurfaceVelocitySimpleFvPatchVectorField
92+
(
93+
const bubbleSurfaceVelocitySimpleFvPatchVectorField& rwvpvf
94+
)
95+
:
96+
fixedValueFvPatchField<vector>(rwvpvf),
97+
origin_(rwvpvf.origin_),
98+
axis_(rwvpvf.axis_),
99+
normal_(rwvpvf.normal_),
100+
model_name_(rwvpvf.model_name_),
101+
pyTorch_model_(rwvpvf.pyTorch_model_)
102+
{}
103+
104+
105+
Foam::bubbleSurfaceVelocitySimpleFvPatchVectorField::
106+
bubbleSurfaceVelocitySimpleFvPatchVectorField
107+
(
108+
const bubbleSurfaceVelocitySimpleFvPatchVectorField& rwvpvf,
109+
const DimensionedField<vector, volMesh>& iF
110+
)
111+
:
112+
fixedValueFvPatchField<vector>(rwvpvf, iF),
113+
origin_(rwvpvf.origin_),
114+
axis_(rwvpvf.axis_),
115+
normal_(rwvpvf.normal_),
116+
model_name_(rwvpvf.model_name_),
117+
pyTorch_model_(rwvpvf.pyTorch_model_)
118+
{}
119+
120+
121+
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
122+
123+
void Foam::bubbleSurfaceVelocitySimpleFvPatchVectorField::updateCoeffs()
124+
{
125+
if (updated())
126+
{
127+
return;
128+
}
129+
130+
// compute tangent vector
131+
const vectorField n(patch().nf());
132+
const vectorField tau(n ^ normal_);
133+
134+
// compute polar angle for each patch face
135+
const vectorField Cf(patch().Cf() - origin_);
136+
torch::Tensor phiTensor = torch::ones({Cf.size(), 1});
137+
138+
// scalar pi = constant::mathematical::pi;
139+
forAll(Cf, faceI)
140+
{
141+
scalar r = sqrt(Cf[faceI] & Cf[faceI]);
142+
phiTensor[faceI][0] = acos((Cf[faceI] & axis_) / r);
143+
}
144+
145+
// run forward pass to compute tangential velocity
146+
std::vector<torch::jit::IValue> modelFeatures{phiTensor};
147+
torch::Tensor uTensor = pyTorch_model_.forward(modelFeatures).toTensor();
148+
auto uAccessor = uTensor.accessor<float,1>();
149+
150+
vectorField surfaceVelocity(Cf.size(), Zero);
151+
forAll(surfaceVelocity, faceI)
152+
{
153+
surfaceVelocity[faceI] = tau[faceI] * uAccessor[faceI];
154+
}
155+
156+
vectorField::operator=(surfaceVelocity);
157+
fixedValueFvPatchVectorField::updateCoeffs();
158+
}
159+
160+
161+
void Foam::bubbleSurfaceVelocitySimpleFvPatchVectorField::write(Ostream& os) const
162+
{
163+
fvPatchVectorField::write(os);
164+
os.writeEntry("origin", origin_);
165+
os.writeEntry("axis", axis_);
166+
os.writeEntry("model", model_name_);
167+
writeEntry("value", os);
168+
}
169+
170+
171+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
172+
173+
namespace Foam
174+
{
175+
makePatchTypeField
176+
(
177+
fvPatchVectorField,
178+
bubbleSurfaceVelocitySimpleFvPatchVectorField
179+
);
180+
}
181+
182+
// ************************************************************************* //

0 commit comments

Comments
 (0)