CodedFvSource.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2012-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "CodedFvSource.H"
30 #include "fvMesh.H"
31 #include "fvMatrices.H"
32 #include "dynamicCode.H"
33 #include "dynamicCodeContext.H"
34 
35 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
36 
37 
38 template<class Type>
40 {
41  return mesh_.time().libs();
42 }
43 
44 
45 template<class Type>
47 {
48  return "fvOption::" + name_;
49 }
50 
51 
52 template<class Type>
54 {
55  redirectOptionPtr_.reset(nullptr);
56 }
57 
58 
59 template<class Type>
61 {
62  return coeffs_;
63 }
64 
65 
66 template<class Type>
68 (
69  dynamicCode& dynCode,
70  const dynamicCodeContext& context
71 ) const
72 {
73  const word sourceType(pTraits<Type>::typeName);
74 
75  // Set additional rewrite rules
76  dynCode.setFilterVariable("typeName", name_);
77  dynCode.setFilterVariable("TemplateType", sourceType);
78  dynCode.setFilterVariable("SourceType", sourceType + "Source");
79 
80  //dynCode.removeFilterVariable("code");
81  dynCode.setFilterVariable("codeCorrect", codeCorrect_);
82  dynCode.setFilterVariable("codeConstrain", codeConstrain_);
83 
84  // Missing codeAddSup or codeAddSupRho handled on input,
85  // but also check for empty() and inject "NotImplemented" to avoid
86  // cases where the user has specified or used the wrong one
87  // (ie, compresssible vs incompressible)
88 
89  if (codeAddSup_.empty())
90  {
91  dynCode.setFilterVariable("codeAddSup", "NotImplemented");
92  }
93  else
94  {
95  dynCode.setFilterVariable("codeAddSup", codeAddSup_);
96  }
97  if (codeAddSupRho_.empty())
98  {
99  dynCode.setFilterVariable("codeAddSupRho", "NotImplemented");
100  }
101  else
102  {
103  dynCode.setFilterVariable("codeAddSupRho", codeAddSupRho_);
104  }
105 
106  // Compile filtered C template
107  dynCode.addCompileFile(codeTemplateC);
108 
109  // Copy filtered H template
110  dynCode.addCopyFile(codeTemplateH);
111 
112  #ifdef FULLDEBUG
113  dynCode.setFilterVariable("verbose", "true");
114  DetailInfo
115  <<"compile " << name_ << " sha1: " << context.sha1() << endl;
116  #endif
117 
118  // define Make/options
119  dynCode.setMakeOptions
120  (
121  "EXE_INC = -g \\\n"
122  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
123  "-I$(LIB_SRC)/fvOptions/lnInclude \\\n"
124  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
125  "-I$(LIB_SRC)/sampling/lnInclude \\\n"
126  + context.options()
127  + "\n\nLIB_LIBS = \\\n"
128  " -lfiniteVolume \\\n"
129  " -lfvOptions \\\n"
130  " -lmeshTools \\\n"
131  " -lsampling \\\n"
132  + context.libs()
133  );
134 }
135 
136 
137 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
138 
139 template<class Type>
141 (
142  const word& name,
143  const word& modelType,
144  const dictionary& dict,
145  const fvMesh& mesh
146 )
147 :
148  fv::cellSetOption(name, modelType, dict, mesh)
149 {
150  read(dict);
151 }
152 
153 
154 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
155 
156 template<class Type>
158 {
159  codedBase::setCodeContext(coeffs_);
160 
162  {
163  return false;
164  }
165 
166  coeffs_.readEntry("fields", fieldNames_);
167 
168  fv::option::resetApplied();
169 
170  dict.readCompat<word>("name", {{"redirectType", 1706}}, name_);
171 
172 
173  // Code context chunks
174 
175  auto& ctx = codedBase::codeContext();
176 
177  ctx.readEntry("codeCorrect", codeCorrect_);
178 
179  // Need one or both codeAddSup/codeAddSupRho.
180  // - do precheck and let the usual mechanism fail
181 
182  const bool mandatory =
183  (
184  !ctx.findEntry("codeAddSup")
185  && !ctx.findEntry("codeAddSupRho")
186  );
187 
188  ctx.readEntry("codeAddSup", codeAddSup_, mandatory);
189  ctx.readEntry("codeAddSupRho", codeAddSupRho_, mandatory);
190 
191  // ctx.readEntry("codeConstrain", codeConstrain_);
192  ctx.readEntry // Compatibility
193  (
194  coeffs_.lookupEntryCompat
195  (
196  "codeConstrain",
197  {{ "codeSetValue", 1812 }},
198  keyType::LITERAL
199  ).keyword(),
200  codeConstrain_
201  );
202 
203  return true;
204 }
205 
206 
207 template<class Type>
209 {
210  if (!redirectOptionPtr_)
211  {
212  dictionary constructDict(dict_);
213  constructDict.set("type", name_);
214  constructDict.changeKeyword(modelType_ & "Coeffs", name_ & "Coeffs");
215 
216  redirectOptionPtr_ = fv::option::New
217  (
218  name_,
219  constructDict,
220  mesh_
221  );
222  }
223  return *redirectOptionPtr_;
224 }
225 
226 
227 template<class Type>
229 (
231 )
232 {
233  DebugInfo
234  << "fv::CodedSource<" << pTraits<Type>::typeName
235  << ">::correct for source " << name_ << endl;
236 
237  updateLibrary(name_);
238  redirectOption().correct(field);
239 }
240 
241 
242 template<class Type>
244 (
245  fvMatrix<Type>& eqn,
246  const label fieldi
247 )
248 {
249  DebugInfo
250  << "fv::CodedSource<" << pTraits<Type>::typeName
251  << ">::addSup for source " << name_ << endl;
252 
253  updateLibrary(name_);
254  redirectOption().addSup(eqn, fieldi);
255 }
256 
257 
258 template<class Type>
260 (
261  const volScalarField& rho,
262  fvMatrix<Type>& eqn,
263  const label fieldi
264 )
265 {
266  DebugInfo
267  << "fv::CodedSource<" << pTraits<Type>::typeName
268  << ">::addSup(rho) for source " << name_ << endl;
269 
270  updateLibrary(name_);
271  redirectOption().addSup(rho, eqn, fieldi);
272 }
273 
274 
275 template<class Type>
277 (
278  fvMatrix<Type>& eqn,
279  const label fieldi
280 )
281 {
282  DebugInfo
283  << "fv::CodedSource<" << pTraits<Type>::typeName
284  << ">::constrain for source " << name_ << endl;
285 
286  updateLibrary(name_);
287  redirectOption().constrain(eqn, fieldi);
288 }
289 
290 
291 // ************************************************************************* //
Foam::fv::CodedSource::libs
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: CodedFvSource.C:39
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:63
Foam::dictionary::changeKeyword
bool changeKeyword(const keyType &oldKeyword, const keyType &newKeyword, bool overwrite=false)
Change the keyword for an entry,.
Definition: dictionarySearch.C:611
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fv::cellSetOption
Intermediate abstract class for handling cell-set options for the derived fvOptions.
Definition: cellSetOption.H:163
Foam::dynamicCode
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:59
Foam::dynamicCodeContext
Encapsulation of dynamic code dictionaries.
Definition: dynamicCodeContext.H:53
CodedFvSource.H
Foam::fv::CodedSource::clearRedirect
virtual void clearRedirect() const
Clear redirected object(s)
Definition: CodedFvSource.C:53
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::dictionary::set
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:780
dynamicCodeContext.H
rho
rho
Definition: readInitialConditions.H:88
Foam::dynamicCode::addCopyFile
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:359
Foam::dynamicCodeContext::sha1
const SHA1 & sha1() const noexcept
The SHA1 calculated from options, libs, include, code, etc.
Definition: dynamicCodeContext.H:164
fvMatrices.H
A special matrix type and solver, designed for finite volume solutions of scalar equations.
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
Foam::dynamicCode::addCompileFile
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:353
Foam::fv::CodedSource::correct
virtual void correct(GeometricField< Type, fvPatchField, volMesh > &)
Correct field.
Definition: CodedFvSource.C:229
Foam::fv::option
Base abstract class for handling finite volume options (i.e. fvOption).
Definition: fvOption.H:126
dynamicCode.H
field
rDeltaTY field()
DetailInfo
#define DetailInfo
Definition: evalEntry.C:37
Foam::dynamicCodeContext::libs
const string & libs() const noexcept
The code libs (LIB_LIBS)
Definition: dynamicCodeContext.H:140
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:85
fvMesh.H
Foam::fv::CodedSource::CodedSource
CodedSource(const word &name, const word &modelType, const dictionary &dict, const fvMesh &mesh)
Construct from components.
Definition: CodedFvSource.C:141
Foam::dynamicCode::setFilterVariable
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
Definition: dynamicCode.C:388
Foam::dlLibraryTable::libs
static dlLibraryTable & libs()
Table of global libraries.
Definition: dlLibraryTable.C:80
Foam::fv::CodedSource::codeDict
virtual const dictionary & codeDict() const
Get the dictionary to initialize the code context.
Definition: CodedFvSource.C:60
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::dynamicCode::setMakeOptions
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:397
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:382
Foam::fv::CodedSource::description
virtual string description() const
Description (type + name) for the output.
Definition: CodedFvSource.C:46
Foam::fv::CodedSource::constrain
virtual void constrain(fvMatrix< Type > &eqn, const label fieldi)
Set value.
Definition: CodedFvSource.C:277
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::fv::CodedSource::addSup
virtual void addSup(fvMatrix< Type > &eqn, const label fieldi)
Explicit/implicit matrix contributions.
Definition: CodedFvSource.C:244
Foam::fvMatrix
A special matrix type and solver, designed for finite volume solutions of scalar equations....
Definition: fvPatchField.H:68
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::fv::CodedSource::redirectOption
fv::option & redirectOption() const
Dynamically compiled fvOption.
Definition: CodedFvSource.C:208
Foam::GeometricField< Type, fvPatchField, volMesh >
Foam::fv::CodedSource::read
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: CodedFvSource.C:157
Foam::dynamicCodeContext::options
const string & options() const noexcept
The code options (Make/options)
Definition: dynamicCodeContext.H:134
Foam::fv::CodedSource::prepare
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition: CodedFvSource.C:68