CodedField.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) 2020-2021 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28#include "dynamicCode.H"
29#include "dynamicCodeContext.H"
30#include "dictionaryContent.H"
31
32// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
33
34template<class Type>
37{
38 return this->patch_.boundaryMesh().mesh().time().libs();
39}
40
41
42template<class Type>
45{
46 return "CodedField " + redirectName_;
47}
48
49
50template<class Type>
52{
53 redirectFunctionPtr_.reset(nullptr);
54}
55
56
57template<class Type>
60{
61 // What else would make sense?
62 return dict_;
63}
64
65
66template<class Type>
69(
70 const dictionary& dict
71) const
72{
73 // Use named subdictionary if present to provide the code.
74 // This allows running with multiple PatchFunction1s
75
76 return
77 (
78 dict.found("code")
79 ? dict
80 : dict.subDict(redirectName_)
81 );
82}
83
84
85template<class Type>
88{
89 return codeDict(dict_);
90}
91
92
93template<class Type>
95(
96 dynamicCode& dynCode,
97 const dynamicCodeContext& context
98) const
99{
100 if (context.code().empty())
101 {
103 << "No code section in input dictionary for patch "
104 << this->patch_.name()
105 << " name " << redirectName_
106 << exit(FatalIOError);
107 }
108
109 // Take no chances - typeName must be identical to redirectName_
110 dynCode.setFilterVariable("typeName", redirectName_);
111
112 // Set TemplateType and FieldType filter variables
113 dynCode.setFieldTemplates<Type>();
114
115 // Compile filtered C template
116 dynCode.addCompileFile(codeTemplateC);
117
118 // Copy filtered H template
119 dynCode.addCopyFile(codeTemplateH);
120
121 #ifdef FULLDEBUG
122 dynCode.setFilterVariable("verbose", "true");
124 <<"compile " << redirectName_ << " sha1: " << context.sha1() << endl;
125 #endif
126
127 // Define Make/options
128 dynCode.setMakeOptions
129 (
130 "EXE_INC = -g \\\n"
131 "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
132 "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
133 + context.options()
134 + "\n\nLIB_LIBS = \\\n"
135 " -lOpenFOAM \\\n"
136 " -lfiniteVolume \\\n"
137 " -lmeshTools \\\n"
138 + context.libs()
139 );
140}
141
142
143// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
144
145template<class Type>
147(
148 const polyPatch& pp,
149 const word& redirectType,
150 const word& entryName,
151 const dictionary& dict,
152 const bool faceValues
153)
154:
155 PatchFunction1<Type>(pp, entryName, dict, faceValues),
156 codedBase(),
157 dict_(dict),
158 redirectName_(dict.getOrDefault<word>("name", entryName))
159{
160 this->codedBase::setCodeContext(dict_);
161
162 // No additional code chunks...
163
164 updateLibrary(redirectName_);
165}
166
167
168template<class Type>
170(
171 const CodedField<Type>& rhs
172)
173:
174 CodedField<Type>(rhs, rhs.patch())
175{}
176
177
178template<class Type>
180(
181 const CodedField<Type>& rhs,
182 const polyPatch& pp
183)
184:
185 PatchFunction1<Type>(rhs, pp),
186 codedBase(),
187 dict_(rhs.dict_),
188 redirectName_(rhs.redirectName_)
189{}
190
191
192// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
193
194template<class Type>
197{
198 if (!redirectFunctionPtr_)
199 {
200 dictionary constructDict;
201 // Force 'redirectName_' sub-dictionary into existence
202 dictionary& coeffs = constructDict.subDictOrAdd(redirectName_);
203
204 coeffs = dict_; // Copy input code and coefficients
205 coeffs.remove("name"); // Redundant
206 coeffs.set("type", redirectName_); // Specify our new (redirect) type
207
208 redirectFunctionPtr_.reset
209 (
211 (
212 this->patch(),
213 redirectName_,
214 constructDict,
215 this->faceValues()
216 )
217 );
218
219 // Forward copy of codeContext to the code template
220 auto* contentPtr =
221 dynamic_cast<dictionaryContent*>(redirectFunctionPtr_.get());
222
223 if (contentPtr)
224 {
225 contentPtr->dict(this->codeContext());
226 }
227 else
228 {
230 << redirectName_ << " Did not derive from dictionaryContent"
231 << nl << nl;
232 }
233 }
234 return *redirectFunctionPtr_;
235}
236
237
238template<class Type>
241(
242 const scalar x
243) const
244{
245 // Ensure library containing user-defined code is up-to-date
246 updateLibrary(redirectName_);
247
248 return redirectFunction().value(x);
249}
250
251
252template<class Type>
255(
256 const scalar x1,
257 const scalar x2
258) const
259{
260 // Ensure library containing user-defined code is up-to-date
261 updateLibrary(redirectName_);
262
263 return redirectFunction().integrate(x1, x2);
264}
265
266
267template<class Type>
269(
270 const FieldMapper& mapper
271)
272{
274 if (redirectFunctionPtr_)
275 {
276 redirectFunctionPtr_->autoMap(mapper);
277 }
278}
279
280
281template<class Type>
283(
284 const PatchFunction1<Type>& pf1,
285 const labelList& addr
286)
287{
289 if (redirectFunctionPtr_)
290 {
291 redirectFunctionPtr_->rmap(pf1, addr);
292 }
293}
294
295
296template<class Type>
298(
299 Ostream& os
300) const
301{
302 // Should really only output only relevant entries but since using
303 // PatchFunction1-from-subdict upon construction our dictionary contains
304 // only the relevant entries. It would be different if PatchFunction1-from
305 // primitiveEntry when the whole 'value' entry would be present
306 dict_.writeEntry(this->name(), os);
307}
308
309
310// ************************************************************************* //
Abstract base class to hold the Field mapping addressing and weights.
Definition: FieldMapper.H:50
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
PatchFunction1 with the code supplied by an on-the-fly compiled C++ expression.
Definition: CodedField.H:128
virtual tmp< Field< Type > > integrate(const scalar x1, const scalar x2) const
Integrate between two values.
Definition: CodedField.C:255
virtual void rmap(const PatchFunction1< Type > &pf1, const labelList &addr)
Reverse map the given PatchFunction1 onto this PatchFunction1.
Definition: CodedField.C:283
virtual const dictionary & codeContext() const
Additional 'codeContext' dictionary to pass through.
Definition: CodedField.C:59
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition: CodedField.C:95
virtual void autoMap(const FieldMapper &mapper)
Map (and resize as needed) from self given a mapping object.
Definition: CodedField.C:269
virtual const dictionary & codeDict() const
Definition: CodedField.C:87
virtual void clearRedirect() const
Clear redirected object(s)
Definition: CodedField.C:51
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: CodedField.C:36
virtual string description() const
Description (type + name) for the output.
Definition: CodedField.C:44
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
virtual void rmap(const PatchFunction1< Type > &rhs, const labelList &addr)
Reverse map the given PatchFunction1 onto this PatchFunction1.
virtual void autoMap(const FieldMapper &mapper)
Map (and resize as needed) from self given a mapping object.
Base class for function objects and boundary conditions using dynamic code that provides methods for ...
Definition: codedBase.H:67
void updateLibrary(const word &name, const dynamicCodeContext &context) const
Update library as required, using the given context.
Definition: codedBase.C:354
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
Definition: codedBase.C:341
A wrapper for dictionary content, without operators that could affect inheritance patterns.
const dictionary & dict() const noexcept
Read-access to the content.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
dictionary & subDictOrAdd(const word &keyword, enum keyType::option matchOpt=keyType::REGEX)
Find and return a sub-dictionary for manipulation.
Definition: dictionary.C:500
bool remove(const word &keyword)
Remove an entry specified by keyword.
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 & code() const noexcept
The code.
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 setFieldTemplates()
Define a filter variables TemplateType and FieldType.
Definition: dynamicCode.H:286
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 patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:75
A class for handling character strings derived from std::string.
Definition: string.H:79
A class for managing temporary objects.
Definition: tmp.H:65
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define DetailInfo
Definition: evalEntry.C:37
OBJstream os(runTime.globalPath()/outputName)
#define WarningInFunction
Report a warning using Foam::Warning.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
IOerror FatalIOError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict