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-------------------------------------------------------------------------------
11License
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
38template<class Type>
40{
41 return mesh_.time().libs();
42}
43
44
45template<class Type>
47{
48 return "fvOption::" + name_;
49}
50
51
52template<class Type>
54{
55 redirectOptionPtr_.reset(nullptr);
56}
57
58
59template<class Type>
61{
62 return coeffs_;
63}
64
65
66template<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");
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
139template<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
156template<class Type>
158{
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
207template<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
227template<class Type>
229(
231)
232{
234 << "fv::CodedSource<" << pTraits<Type>::typeName
235 << ">::correct for source " << name_ << endl;
236
237 updateLibrary(name_);
238 redirectOption().correct(field);
239}
240
241
242template<class Type>
244(
245 fvMatrix<Type>& eqn,
246 const label fieldi
247)
248{
250 << "fv::CodedSource<" << pTraits<Type>::typeName
251 << ">::addSup for source " << name_ << endl;
252
253 updateLibrary(name_);
254 redirectOption().addSup(eqn, fieldi);
255}
256
257
258template<class Type>
260(
261 const volScalarField& rho,
262 fvMatrix<Type>& eqn,
263 const label fieldi
264)
265{
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
275template<class Type>
277(
278 fvMatrix<Type>& eqn,
279 const label fieldi
280)
281{
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// ************************************************************************* //
Generic GeometricField class.
virtual void correct()
Solve the turbulence equations and correct the turbulence viscosity.
virtual bool read()
Re-read model coefficients if they have changed.
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
Definition: codedBase.C:341
dynamicCodeContext & codeContext()
Access to the dynamic code context.
Definition: codedBase.H:119
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
bool changeKeyword(const keyType &oldKeyword, const keyType &newKeyword, bool overwrite=false)
Change the keyword for an entry,.
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:780
A table of dynamically loaded libraries.
static dlLibraryTable & libs()
Table of global libraries.
Encapsulation of dynamic code dictionaries.
const string & libs() const noexcept
The code libs (LIB_LIBS)
const SHA1 & sha1() const noexcept
The SHA1 calculated from options, libs, include, code, etc.
const string & options() const noexcept
The code options (Make/options)
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:60
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:359
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
Definition: dynamicCode.C:388
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:353
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:397
A special matrix type and solver, designed for finite volume solutions of scalar equations....
Definition: fvMatrix.H:121
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:91
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition: CodedFvSource.C:68
virtual void addSup(fvMatrix< Type > &eqn, const label fieldi)
Explicit/implicit matrix contributions.
virtual bool read(const dictionary &dict)
Read source dictionary.
virtual void constrain(fvMatrix< Type > &eqn, const label fieldi)
Set value.
virtual const dictionary & codeDict() const
Get the dictionary to initialize the code context.
Definition: CodedFvSource.C:60
fv::option & redirectOption() const
Dynamically compiled fvOption.
virtual void clearRedirect() const
Clear redirected object(s)
Definition: CodedFvSource.C:53
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: CodedFvSource.C:39
virtual string description() const
Description (type + name) for the output.
Definition: CodedFvSource.C:46
Intermediate abstract class for handling cell-set options for the derived fvOptions.
Base abstract class for handling finite volume options (i.e. fvOption).
Definition: fvOption.H:127
void resetApplied()
Resize/reset applied flag list for all fieldNames_ entries.
Definition: fvOption.C:48
@ LITERAL
String literal.
Definition: keyType.H:81
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
A class for handling character strings derived from std::string.
Definition: string.H:79
A class for handling words, derived from Foam::string.
Definition: word.H:68
rDeltaTY field()
dynamicFvMesh & mesh
#define DetailInfo
Definition: evalEntry.C:37
A special matrix type and solver, designed for finite volume solutions of scalar equations.
#define DebugInfo
Report an information message using Foam::Info.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
labelList fv(nPoints)
dictionary dict