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