codedFvModel.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2012-2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "codedFvModel.H"
27 #include "fvMatrices.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 namespace fv
35 {
38 }
39 }
40 
41 
42 const Foam::wordList Foam::fv::codedFvModel::codeKeys
43 {
44  "codeAddSup",
45  "codeAddRhoSup",
46  "codeAddAlphaRhoSup",
47  "codeInclude",
48  "localCode"
49 };
50 
51 const Foam::wordList Foam::fv::codedFvModel::codeDictVars
52 {
53  word::null,
54  word::null,
55  word::null,
56  word::null,
57  word::null
58 };
59 
60 const Foam::word Foam::fv::codedFvModel::codeOptions
61 (
62  "codedFvModelOptions"
63 );
64 
65 const Foam::wordList Foam::fv::codedFvModel::compileFiles
66 {
67  "codedFvModelTemplate.C"
68 };
69 
70 const Foam::wordList Foam::fv::codedFvModel::copyFiles
71 {
72  "codedFvModelTemplate.H"
73 };
74 
75 
76 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
77 
78 void Foam::fv::codedFvModel::readCoeffs(const dictionary& dict)
79 {
80  fieldName_ = dict.lookup<word>("field");
81 }
82 
83 
84 Foam::word Foam::fv::codedFvModel::fieldPrimitiveTypeName() const
85 {
86  #define fieldPrimitiveTypeNameTernary(Type, nullArg) \
87  mesh().foundObject<VolField<Type>>(fieldName_) \
88  ? pTraits<Type>::typeName \
89  :
90 
92 
93  #undef fieldPrimitiveTypeNameTernary
94 }
95 
96 
97 Foam::fvModel& Foam::fv::codedFvModel::redirectFvModel() const
98 {
99  if (!redirectFvModelPtr_.valid())
100  {
101  const word primitiveTypeName = fieldPrimitiveTypeName();
102 
103  const_cast<codedFvModel&>(*this).varSubstitutions().set
104  (
105  {
106  {"TemplateType", primitiveTypeName},
107  {"SourceType", primitiveTypeName + "Source"}
108  }
109  );
110 
111  updateLibrary(coeffsDict_);
112 
113  dictionary constructDict(coeffsDict_);
114  constructDict.set("type", name());
115  redirectFvModelPtr_ = fvModel::New
116  (
117  name(),
118  mesh(),
119  constructDict
120  );
121  }
122 
123  return redirectFvModelPtr_();
124 }
125 
126 
127 template<class Type>
128 void Foam::fv::codedFvModel::addSupType
129 (
130  const VolField<Type>& field,
131  fvMatrix<Type>& eqn
132 ) const
133 {
134  if (fieldPrimitiveTypeName() != word::null)
135  {
136  if (debug)
137  {
138  Info<< indent
139  << "codedFvModel::addSup for source " << name() << endl;
140  }
141 
142  redirectFvModel().addSup(field, eqn);
143  }
144 }
145 
146 
147 template<class Type>
148 void Foam::fv::codedFvModel::addSupType
149 (
150  const volScalarField& rho,
151  const VolField<Type>& field,
152  fvMatrix<Type>& eqn
153 ) const
154 {
155  if (fieldPrimitiveTypeName() != word::null)
156  {
157  if (debug)
158  {
159  Info<< indent
160  << "codedFvModel::addSup for source " << name() << endl;
161  }
162 
163  redirectFvModel().addSup(rho, field, eqn);
164  }
165 }
166 
167 
168 template<class Type>
169 void Foam::fv::codedFvModel::addSupType
170 (
171  const volScalarField& alpha,
172  const volScalarField& rho,
173  const VolField<Type>& field,
174  fvMatrix<Type>& eqn
175 ) const
176 {
177  if (fieldPrimitiveTypeName() != word::null)
178  {
179  if (debug)
180  {
181  Info<< indent
182  << "codedFvModel::addSup for source " << name() << endl;
183  }
184 
185  redirectFvModel().addSup(alpha, rho, field, eqn);
186  }
187 }
188 
189 
190 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
191 
193 (
194  const word& name,
195  const word& modelType,
196  const fvMesh& mesh,
197  const dictionary& dict
198 )
199 :
200  fvModel(name, modelType, mesh, dict),
201  codedBase
202  (
203  name,
204  coeffs(dict),
205  codeKeys,
206  codeDictVars,
207  codeOptions,
208  compileFiles,
209  copyFiles
210  ),
211  fieldName_(word::null),
212  coeffsDict_(coeffs(dict))
213 {
214  readCoeffs(coeffsDict_);
215 
216  varSubstitutions().set("verbose", Foam::name(bool(debug)));
217 }
218 
219 
220 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
221 
223 {
224  return wordList(1, fieldName_);
225 }
226 
227 
229 
230 
232 
233 
235 (
237  fv::codedFvModel
238 )
239 
240 
241 bool Foam::fv::codedFvModel::movePoints()
242 {
243  return redirectFvModel().movePoints();
244 }
245 
246 
248 {
249  redirectFvModel().topoChange(map);
250 }
251 
252 
254 {
255  redirectFvModel().mapMesh(map);
256 }
257 
258 
260 {
261  redirectFvModel().distribute(map);
262 }
263 
264 
266 {
267  if (fvModel::read(dict))
268  {
269  redirectFvModelPtr_.clear();
270  coeffsDict_ = coeffs(dict);
271  readCoeffs(coeffsDict_);
272  codedBase::read(coeffsDict_);
273  updateLibrary(coeffsDict_);
274  return true;
275  }
276  else
277  {
278  return false;
279  }
280 }
281 
282 
283 // ************************************************************************* //
Macros for easy insertion into run-time selection tables.
Base class for coded functionObjects, fvModels, Function1, Function2.
Definition: codedBase.H:53
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
HashTable< string > & varSubstitutions()
Definition: dynamicCode.H:252
void read(const dictionary &contextDict, const dictionary &codeDict)
Definition: dynamicCode.C:386
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:98
Finite volume model abstract base class.
Definition: fvModel.H:60
static autoPtr< fvModel > New(const word &name, const fvMesh &mesh, const dictionary &dict)
Return a reference to the selected fvModel.
Definition: fvModel.C:92
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: fvModel.C:196
Constructs on-the-fly fvModel source from user-supplied code.
Definition: codedFvModel.H:92
codedFvModel(const word &name, const word &modelType, const fvMesh &mesh, const dictionary &dict)
Construct from components.
Definition: codedFvModel.C:193
virtual wordList addSupFields() const
Return the list of fields for which the fvModel adds source term.
Definition: codedFvModel.C:222
virtual void topoChange(const polyTopoChangeMap &)
Update topology using the given map.
Definition: codedFvModel.C:247
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
Definition: codedFvModel.C:259
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: codedFvModel.C:265
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
Definition: codedFvModel.C:253
Class containing mesh-to-mesh mapping information after a mesh distribution where we send parts of me...
Class containing mesh-to-mesh mapping information.
Definition: polyMeshMap.H:51
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
A class for handling words, derived from string.
Definition: word.H:63
static const word null
An empty word.
Definition: word.H:78
#define fieldPrimitiveTypeNameTernary(Type, nullArg)
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
A special matrix type and solver, designed for finite volume solutions of scalar equations.
#define IMPLEMENT_FV_MODEL_ADD_FIELD_SUP(Type, modelType)
Definition: fvModelM.H:33
#define IMPLEMENT_FV_MODEL_ADD_ALPHA_RHO_FIELD_SUP(Type, modelType)
Definition: fvModelM.H:71
#define IMPLEMENT_FV_MODEL_ADD_RHO_FIELD_SUP(Type, modelType)
Definition: fvModelM.H:51
rho
Definition: pEqn.H:1
volScalarField alpha(IOobject("alpha", runTime.name(), mesh, IOobject::READ_IF_PRESENT, IOobject::AUTO_WRITE), lambda *max(Ua &U, zeroSensitivity))
addToRunTimeSelectionTable(fvConstraint, bound, dictionary)
defineTypeNameAndDebug(bound, 0)
Namespace for OpenFOAM.
List< word > wordList
A List of words.
Definition: fileName.H:54
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
FOR_ALL_FIELD_TYPES(makeDimensionedPointFieldFunctions)
messageStream Info
VolField< scalar > volScalarField
Definition: volFieldsFwd.H:62
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:243
labelList fv(nPoints)
dictionary dict