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 "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 {
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  // Make verbose if debugging
89  dynCode.setFilterVariable("verbose", Foam::name(bool(debug)));
90 
91  // define Make/options
92  dynCode.setMakeOptions
93  (
94  "EXE_INC = -g \\\n"
95  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
96  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
97  "-I$(LIB_SRC)/sampling/lnInclude \\\n"
98  "-I$(LIB_SRC)/fvModels/general/lnInclude \\\n"
99  + context.options()
100  + "\n\nLIB_LIBS = \\\n"
101  + " -lmeshTools \\\n"
102  + " -lfvModels \\\n"
103  + " -lsampling \\\n"
104  + " -lfiniteVolume \\\n"
105  + context.libs()
106  );
107 }
108 
109 
110 const Foam::word& Foam::fv::codedFvModel::codeName() const
111 {
112  return name();
113 }
114 
115 
116 Foam::string Foam::fv::codedFvModel::description() const
117 {
118  return "fvModel:: " + name();
119 }
120 
121 
122 void Foam::fv::codedFvModel::clearRedirect() const
123 {
124  redirectFvModelPtr_.clear();
125 }
126 
127 
128 const Foam::dictionary& Foam::fv::codedFvModel::codeDict() const
129 {
130  return coeffs();
131 }
132 
133 
134 Foam::wordList Foam::fv::codedFvModel::codeKeys() const
135 {
136  return
137  {
138  "codeAddSup",
139  "codeAddRhoSup",
140  "codeAddAlphaRhoSup",
141  "codeInclude",
142  "localCode"
143  };
144 }
145 
146 
147 Foam::wordList Foam::fv::codedFvModel::codeDictVars() const
148 {
149  return
150  {
151  word::null,
152  word::null,
153  word::null,
154  word::null,
155  word::null
156  };
157 }
158 
159 
160 Foam::fvModel& Foam::fv::codedFvModel::redirectFvModel() const
161 {
162  if (!redirectFvModelPtr_.valid())
163  {
164  dictionary constructDict(coeffs());
165  constructDict.set("type", name());
166  redirectFvModelPtr_ = fvModel::New
167  (
168  name(),
169  mesh(),
170  constructDict
171  );
172  }
173  return redirectFvModelPtr_();
174 }
175 
176 
177 template<class Type>
178 void Foam::fv::codedFvModel::addSupType
179 (
180  const VolField<Type>& field,
181  fvMatrix<Type>& eqn
182 ) const
183 {
184  if (fieldPrimitiveTypeName() != word::null)
185  {
186  if (debug)
187  {
188  Info<< "codedFvModel::addSup for source " << name() << endl;
189  }
190 
191  updateLibrary();
192  redirectFvModel().addSup(field, eqn);
193  }
194 }
195 
196 
197 template<class Type>
198 void Foam::fv::codedFvModel::addSupType
199 (
200  const volScalarField& rho,
201  const VolField<Type>& field,
202  fvMatrix<Type>& eqn
203 ) const
204 {
205  if (fieldPrimitiveTypeName() != word::null)
206  {
207  if (debug)
208  {
209  Info<< "codedFvModel::addSup for source " << name() << endl;
210  }
211 
212  updateLibrary();
213  redirectFvModel().addSup(rho, field, eqn);
214  }
215 }
216 
217 
218 template<class Type>
219 void Foam::fv::codedFvModel::addSupType
220 (
221  const volScalarField& alpha,
222  const volScalarField& rho,
223  const VolField<Type>& field,
224  fvMatrix<Type>& eqn
225 ) const
226 {
227  if (fieldPrimitiveTypeName() != word::null)
228  {
229  if (debug)
230  {
231  Info<< "codedFvModel::addSup for source " << name() << endl;
232  }
233 
234  updateLibrary();
235  redirectFvModel().addSup(alpha, rho, field, eqn);
236  }
237 }
238 
239 
240 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
241 
243 (
244  const word& name,
245  const word& modelType,
246  const fvMesh& mesh,
247  const dictionary& dict
248 )
249 :
250  fvModel(name, modelType, mesh, dict),
251  fieldName_(word::null)
252 {
253  readCoeffs();
254 }
255 
256 
257 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
258 
260 {
261  return wordList(1, fieldName_);
262 }
263 
264 
266 
267 
269 
270 
272 (
274  fv::codedFvModel
275 )
276 
277 
278 bool Foam::fv::codedFvModel::movePoints()
279 {
280  return redirectFvModel().movePoints();
281 }
282 
283 
285 {
286  redirectFvModel().topoChange(map);
287 }
288 
289 
291 {
292  redirectFvModel().mapMesh(map);
293 }
294 
295 
297 {
298  redirectFvModel().distribute(map);
299 }
300 
301 
303 {
304  if (fvModel::read(dict))
305  {
306  readCoeffs();
307  return true;
308  }
309  else
310  {
311  return false;
312  }
313 }
314 
315 
316 // ************************************************************************* //
Macros for easy insertion into run-time selection tables.
void updateLibrary() const
Update library as required.
Definition: codedBase.C:314
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:710
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:99
Finite volume model abstract base class.
Definition: fvModel.H:59
static autoPtr< fvModel > New(const word &name, const fvMesh &mesh, const dictionary &dict)
Return a reference to the selected fvModel.
Definition: fvModel.C:94
const dictionary & coeffs() const
Return dictionary.
Definition: fvModelI.H:59
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: fvModel.C:199
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:243
virtual wordList addSupFields() const
Return the list of fields for which the fvModel adds source term.
Definition: codedFvModel.C:259
virtual void topoChange(const polyTopoChangeMap &)
Update topology using the given map.
Definition: codedFvModel.C:284
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
Definition: codedFvModel.C:296
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: codedFvModel.C:302
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
Definition: codedFvModel.C:290
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 character strings derived from std::string.
Definition: string.H:79
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)
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:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
messageStream Info
VolField< scalar > volScalarField
Definition: volFieldsFwd.H:64
FOR_ALL_FIELD_TYPES(makeFieldSourceTypedef)
labelList fv(nPoints)
dictionary dict