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-2022 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 "fvMesh.H"
28 #include "fvMatrices.H"
29 #include "dynamicCode.H"
30 #include "dynamicCodeContext.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 namespace fv
38 {
39  defineTypeNameAndDebug(codedFvModel, 0);
40  addToRunTimeSelectionTable(fvModel, codedFvModel, dictionary);
41 }
42 }
43 
44 
45 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
46 
47 void Foam::fv::codedFvModel::readCoeffs()
48 {
49  fieldName_ = coeffs().lookup<word>("field");
50 
51  if (fieldPrimitiveTypeName() != word::null)
52  {
53  updateLibrary();
54  }
55 }
56 
57 
58 Foam::word Foam::fv::codedFvModel::fieldPrimitiveTypeName() const
59 {
60  #define fieldPrimitiveTypeNameTernary(Type, nullArg) \
61  mesh().foundObject<VolField<Type>>(fieldName_) \
62  ? pTraits<Type>::typeName \
63  :
64 
66 }
67 
68 
69 void Foam::fv::codedFvModel::prepare
70 (
71  dynamicCode& dynCode,
72  const dynamicCodeContext& context
73 ) const
74 {
75  const word primitiveTypeName = fieldPrimitiveTypeName();
76 
77  // Set additional rewrite rules
78  dynCode.setFilterVariable("typeName", name());
79  dynCode.setFilterVariable("TemplateType", primitiveTypeName);
80  dynCode.setFilterVariable("SourceType", primitiveTypeName + "Source");
81 
82  // compile filtered C template
83  dynCode.addCompileFile("codedFvModelTemplate.C");
84 
85  // copy filtered H template
86  dynCode.addCopyFile("codedFvModelTemplate.H");
87 
88  // define Make/options
89  dynCode.setMakeOptions
90  (
91  "EXE_INC = -g \\\n"
92  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
93  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
94  "-I$(LIB_SRC)/sampling/lnInclude \\\n"
95  "-I$(LIB_SRC)/fvModels/lnInclude \\\n"
96  + context.options()
97  + "\n\nLIB_LIBS = \\\n"
98  + " -lmeshTools \\\n"
99  + " -lfvModels \\\n"
100  + " -lsampling \\\n"
101  + " -lfiniteVolume \\\n"
102  + context.libs()
103  );
104 }
105 
106 
107 const Foam::word& Foam::fv::codedFvModel::codeName() const
108 {
109  return name();
110 }
111 
112 
113 Foam::string Foam::fv::codedFvModel::description() const
114 {
115  return "fvModel:: " + name();
116 }
117 
118 
119 void Foam::fv::codedFvModel::clearRedirect() const
120 {
121  redirectFvModelPtr_.clear();
122 }
123 
124 
125 const Foam::dictionary& Foam::fv::codedFvModel::codeDict() const
126 {
127  return coeffs();
128 }
129 
130 
131 Foam::wordList Foam::fv::codedFvModel::codeKeys() const
132 {
133 
134  return
135  {
136  "codeAddSup",
137  "codeAddRhoSup",
138  "codeAddAlphaRhoSup",
139  "codeInclude",
140  "localCode"
141  };
142 }
143 
144 
145 Foam::fvModel& Foam::fv::codedFvModel::redirectFvModel() const
146 {
147  if (!redirectFvModelPtr_.valid())
148  {
149  dictionary constructDict(coeffs());
150  constructDict.set("type", name());
151  redirectFvModelPtr_ = fvModel::New
152  (
153  name(),
154  constructDict,
155  mesh()
156  );
157  }
158  return redirectFvModelPtr_();
159 }
160 
161 
162 template<class Type>
163 void Foam::fv::codedFvModel::addSupType
164 (
165  fvMatrix<Type>& eqn,
166  const word& fieldName
167 ) const
168 {
169  if (fieldPrimitiveTypeName() != word::null)
170  {
171  if (debug)
172  {
173  Info<< "codedFvModel::addSup for source " << name() << endl;
174  }
175 
176  updateLibrary();
177  redirectFvModel().addSup(eqn, fieldName);
178  }
179 }
180 
181 
182 template<class Type>
183 void Foam::fv::codedFvModel::addSupType
184 (
185  const volScalarField& rho,
186  fvMatrix<Type>& eqn,
187  const word& fieldName
188 ) const
189 {
190  if (fieldPrimitiveTypeName() != word::null)
191  {
192  if (debug)
193  {
194  Info<< "codedFvModel::addSup for source " << name() << endl;
195  }
196 
197  updateLibrary();
198  redirectFvModel().addSup(rho, eqn, fieldName);
199  }
200 }
201 
202 
203 template<class Type>
204 void Foam::fv::codedFvModel::addSupType
205 (
206  const volScalarField& alpha,
207  const volScalarField& rho,
208  fvMatrix<Type>& eqn,
209  const word& fieldName
210 ) const
211 {
212  if (fieldPrimitiveTypeName() != word::null)
213  {
214  if (debug)
215  {
216  Info<< "codedFvModel::addSup for source " << name() << endl;
217  }
218 
219  updateLibrary();
220  redirectFvModel().addSup(alpha, rho, eqn, fieldName);
221  }
222 }
223 
224 
225 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
226 
228 (
229  const word& name,
230  const word& modelType,
231  const dictionary& dict,
232  const fvMesh& mesh
233 )
234 :
235  fvModel(name, modelType, dict, mesh),
236  fieldName_(word::null)
237 {
238  readCoeffs();
239 }
240 
241 
242 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
243 
245 {
246  return wordList(1, fieldName_);
247 }
248 
249 
251 
252 
254 
255 
257 
258 
260 {
261  return redirectFvModel().movePoints();
262 }
263 
264 
266 {
267  redirectFvModel().topoChange(map);
268 }
269 
270 
272 {
273  redirectFvModel().mapMesh(map);
274 }
275 
276 
278 {
279  redirectFvModel().distribute(map);
280 }
281 
282 
284 {
285  if (fvModel::read(dict))
286  {
287  readCoeffs();
288  return true;
289  }
290  else
291  {
292  return false;
293  }
294 }
295 
296 
297 // ************************************************************************* //
#define IMPLEMENT_FV_MODEL_ADD_ALPHA_RHO_SUP(Type, modelType)
Definition: fvModelM.H:71
defineTypeNameAndDebug(fixedTemperatureConstraint, 0)
Class containing mesh-to-mesh mapping information after a mesh distribution where we send parts of me...
#define fieldPrimitiveTypeNameTernary(Type, nullArg)
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: codedFvModel.C:283
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: fvModel.C:164
virtual bool movePoints()
Update for mesh motion.
Definition: codedFvModel.C:259
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
codedFvModel(const word &name, const word &modelType, const dictionary &dict, const fvMesh &mesh)
Construct from components.
Definition: codedFvModel.C:228
Finite volume model abstract base class.
Definition: fvModel.H:57
fvMesh & mesh
Macros for easy insertion into run-time selection tables.
GeometricField< scalar, fvPatchField, volMesh > volScalarField
Definition: volFieldsFwd.H:58
A class for handling words, derived from string.
Definition: word.H:59
labelList fv(nPoints)
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
static autoPtr< fvModel > New(const word &name, const dictionary &dict, const fvMesh &mesh)
Return a reference to the selected fvModel.
Definition: fvModel.C:94
virtual void topoChange(const polyTopoChangeMap &)
Update topology using the given map.
Definition: codedFvModel.C:265
static const word null
An empty word.
Definition: word.H:77
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
Definition: codedFvModel.C:277
#define IMPLEMENT_FV_MODEL_ADD_SUP(Type, modelType)
Definition: fvModelM.H:33
Constructs on-the-fly fvModel source from user-supplied code.
Definition: codedFvModel.H:88
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
addToRunTimeSelectionTable(fvConstraint, fixedTemperatureConstraint, dictionary)
List< word > wordList
A List of words.
Definition: fileName.H:54
#define IMPLEMENT_FV_MODEL_ADD_RHO_SUP(Type, modelType)
Definition: fvModelM.H:51
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:95
virtual wordList addSupFields() const
Return the list of fields for which the fvModel adds source term.
Definition: codedFvModel.C:244
A special matrix type and solver, designed for finite volume solutions of scalar equations.
messageStream Info
FOR_ALL_FIELD_TYPES(DefineFvWallInfoType)
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
Definition: codedFvModel.C:271
Class containing mesh-to-mesh mapping information.
Definition: polyMeshMap.H:50
A class for handling character strings derived from std::string.
Definition: string.H:76
Namespace for OpenFOAM.