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 {
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 
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 }},
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 // ************************************************************************* //
dictionary dict
rDeltaTY field()
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:130
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:390
virtual void correct(GeometricField< Type, fvPatchField, volMesh > &)
Correct field.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:518
virtual const dictionary & codeDict() const
Get the dictionary to initialize the code context.
Definition: CodedFvSource.C:53
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:63
virtual void clearRedirect() const
Clear redirected object(s)
Definition: CodedFvSource.C:46
virtual bool read(const dictionary &dict)
Read source dictionary.
Generic GeometricField class.
virtual void constrain(fvMatrix< Type > &eqn, const label fieldi)
Set value.
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:346
static dlLibraryTable & libs()
Table of global libraries.
fv::option & redirectOption() const
Dynamically compiled fvOption.
virtual bool read(const dictionary &dict)
Read source dictionary.
static autoPtr< option > New(const word &name, const dictionary &dict, const fvMesh &mesh)
Return a reference to the selected fvOption model.
Definition: fvOption.C:78
dynamicFvMesh & mesh
const string & options() const noexcept
The code options (Make/options)
A class for handling words, derived from Foam::string.
Definition: word.H:63
labelList fv(nPoints)
A special matrix type and solver, designed for finite volume solutions of scalar equations. Face addressing is used to make all matrix assembly and solution loops vectorise.
virtual string description() const
Description (type + name) for the output.
Definition: CodedFvSource.C:39
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition: CodedFvSource.C:61
String literal.
Definition: keyType.H:82
#define DetailInfo
Definition: evalEntry.C:30
A table of dynamically loaded libraries.
#define DebugInfo
Report an information message using Foam::Info.
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
Definition: codedBase.C:277
auto & name
CodedSource(const word &name, const word &modelType, const dictionary &dict, const fvMesh &mesh)
Construct from components.
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:56
virtual void addSup(fvMatrix< Type > &eqn, const label fieldi)
Explicit/implicit matrix contributions.
Encapsulation of dynamic code dictionaries.
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
Definition: dynamicCode.C:381
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
void resetApplied()
Resize/reset applied flag list for all fieldNames_ entries.
Definition: fvOption.C:41
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: CodedFvSource.C:32
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:352
A special matrix type and solver, designed for finite volume solutions of scalar equations.
const string & libs() const noexcept
The code libs (LIB_LIBS)
dynamicCodeContext & codeContext()
Access to the dynamic code context.
Definition: codedBase.H:131
Intermediate abstract class for handling cell-set options for the derived fvOptions.
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:765
bool changeKeyword(const keyType &oldKeyword, const keyType &newKeyword, bool overwrite=false)
Change the keyword for an entry,.
A class for handling character strings derived from std::string.
Definition: string.H:73
const SHA1 & sha1() const noexcept
The SHA1 calculated from options, libs, include, code, etc.
Base abstract class for handling finite volume options (i.e. fvOption).
Definition: fvOption.H:123