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-2024 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"
28 #include "dynamicCode.H"
29 #include "dynamicCodeContext.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace fv
37 {
40 }
41 }
42 
43 
44 const Foam::wordList Foam::fv::codedFvModel::codeKeys
45 {
46  "codeAddSup",
47  "codeAddRhoSup",
48  "codeAddAlphaRhoSup",
49  "codeInclude",
50  "localCode"
51 };
52 
53 const Foam::wordList Foam::fv::codedFvModel::codeDictVars
54 {
55  word::null,
56  word::null,
57  word::null,
58  word::null,
59  word::null
60 };
61 
62 
63 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
64 
65 void Foam::fv::codedFvModel::readCoeffs(const dictionary& dict)
66 {
67  fieldName_ = dict.lookup<word>("field");
68 }
69 
70 
71 Foam::word Foam::fv::codedFvModel::fieldPrimitiveTypeName() const
72 {
73  #define fieldPrimitiveTypeNameTernary(Type, nullArg) \
74  mesh().foundObject<VolField<Type>>(fieldName_) \
75  ? pTraits<Type>::typeName \
76  :
77 
79 }
80 
81 
82 void Foam::fv::codedFvModel::prepare
83 (
84  dynamicCode& dynCode,
85  const dynamicCodeContext& context
86 ) const
87 {
88  const word primitiveTypeName = fieldPrimitiveTypeName();
89 
90  // Set additional rewrite rules
91  dynCode.setFilterVariable("typeName", name());
92  dynCode.setFilterVariable("TemplateType", primitiveTypeName);
93  dynCode.setFilterVariable("SourceType", primitiveTypeName + "Source");
94 
95  // compile filtered C template
96  dynCode.addCompileFile("codedFvModelTemplate.C");
97 
98  // copy filtered H template
99  dynCode.addCopyFile("codedFvModelTemplate.H");
100 
101  // Make verbose if debugging
102  dynCode.setFilterVariable("verbose", Foam::name(bool(debug)));
103 
104  // define Make/options
105  dynCode.setMakeOptions
106  (
107  "EXE_INC = -g \\\n"
108  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
109  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
110  "-I$(LIB_SRC)/sampling/lnInclude \\\n"
111  "-I$(LIB_SRC)/fvModels/general/lnInclude \\\n"
112  + context.options()
113  + "\n\nLIB_LIBS = \\\n"
114  + " -lmeshTools \\\n"
115  + " -lfvModels \\\n"
116  + " -lsampling \\\n"
117  + " -lfiniteVolume \\\n"
118  + context.libs()
119  );
120 }
121 
122 
123 Foam::fvModel& Foam::fv::codedFvModel::redirectFvModel() const
124 {
125  if (!redirectFvModelPtr_.valid())
126  {
127  updateLibrary(coeffsDict_);
128 
129  dictionary constructDict(coeffsDict_);
130  constructDict.set("type", name());
131  redirectFvModelPtr_ = fvModel::New
132  (
133  name(),
134  mesh(),
135  constructDict
136  );
137  }
138 
139  return redirectFvModelPtr_();
140 }
141 
142 
143 template<class Type>
144 void Foam::fv::codedFvModel::addSupType
145 (
146  const VolField<Type>& field,
147  fvMatrix<Type>& eqn
148 ) const
149 {
150  if (fieldPrimitiveTypeName() != word::null)
151  {
152  if (debug)
153  {
154  Info<< "codedFvModel::addSup for source " << name() << endl;
155  }
156 
157  redirectFvModel().addSup(field, eqn);
158  }
159 }
160 
161 
162 template<class Type>
163 void Foam::fv::codedFvModel::addSupType
164 (
165  const volScalarField& rho,
166  const VolField<Type>& field,
167  fvMatrix<Type>& eqn
168 ) const
169 {
170  if (fieldPrimitiveTypeName() != word::null)
171  {
172  if (debug)
173  {
174  Info<< "codedFvModel::addSup for source " << name() << endl;
175  }
176 
177  redirectFvModel().addSup(rho, field, eqn);
178  }
179 }
180 
181 
182 template<class Type>
183 void Foam::fv::codedFvModel::addSupType
184 (
185  const volScalarField& alpha,
186  const volScalarField& rho,
187  const VolField<Type>& field,
188  fvMatrix<Type>& eqn
189 ) const
190 {
191  if (fieldPrimitiveTypeName() != word::null)
192  {
193  if (debug)
194  {
195  Info<< "codedFvModel::addSup for source " << name() << endl;
196  }
197 
198  redirectFvModel().addSup(alpha, rho, field, eqn);
199  }
200 }
201 
202 
203 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
204 
206 (
207  const word& name,
208  const word& modelType,
209  const fvMesh& mesh,
210  const dictionary& dict
211 )
212 :
213  fvModel(name, modelType, mesh, dict),
214  codedBase(name, coeffs(dict), codeKeys, codeDictVars),
215  fieldName_(word::null),
216  coeffsDict_(coeffs(dict))
217 {
218  readCoeffs(coeffsDict_);
219 }
220 
221 
222 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
223 
225 {
226  return wordList(1, fieldName_);
227 }
228 
229 
231 
232 
234 
235 
237 (
239  fv::codedFvModel
240 )
241 
242 
243 bool Foam::fv::codedFvModel::movePoints()
244 {
245  return redirectFvModel().movePoints();
246 }
247 
248 
250 {
251  redirectFvModel().topoChange(map);
252 }
253 
254 
256 {
257  redirectFvModel().mapMesh(map);
258 }
259 
260 
262 {
263  redirectFvModel().distribute(map);
264 }
265 
266 
268 {
269  if (fvModel::read(dict))
270  {
271  redirectFvModelPtr_.clear();
272  coeffsDict_ = coeffs(dict);
273  readCoeffs(coeffsDict_);
274  codedBase::read(coeffsDict_);
275  updateLibrary(coeffsDict_);
276  return true;
277  }
278  else
279  {
280  return false;
281  }
282 }
283 
284 
285 // ************************************************************************* //
Macros for easy insertion into run-time selection tables.
Base class for function objects and boundary conditions using dynamic code.
Definition: codedBase.H:52
void read(const dictionary &dict)
Read the dictionary and update the code.
Definition: codedBase.C:455
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:96
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:95
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: fvModel.C:200
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:206
virtual wordList addSupFields() const
Return the list of fields for which the fvModel adds source term.
Definition: codedFvModel.C:224
virtual void topoChange(const polyTopoChangeMap &)
Update topology using the given map.
Definition: codedFvModel.C:249
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
Definition: codedFvModel.C:261
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: codedFvModel.C:267
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
Definition: codedFvModel.C:255
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:62
static const word null
An empty word.
Definition: word.H:77
#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
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:258
messageStream Info
VolField< scalar > volScalarField
Definition: volFieldsFwd.H:62
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
FOR_ALL_FIELD_TYPES(makeFieldSourceTypedef)
labelList fv(nPoints)
dictionary dict