Loading...
Searching...
No Matches
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"
30#include "dictionaryContent.H"
31
32// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
33
34template<class Type>
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>
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 const polyPatch& pp
173)
174:
175 PatchFunction1<Type>(rhs, pp),
177 dict_(rhs.dict_),
178 redirectName_(rhs.redirectName_)
179{}
180
181
182template<class Type>
184(
185 const CodedField<Type>& rhs
186)
187:
188 CodedField<Type>(rhs, rhs.patch())
189{}
190
191
192// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
193
194template<class Type>
196Foam::PatchFunction1Types::CodedField<Type>::redirectFunction() const
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 (
210 PatchFunction1<Type>::New
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 }
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_);
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_)
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_)
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// ************************************************************************* //
uindirectPrimitivePatch pp(UIndirectList< face >(mesh.faces(), faceLabels), mesh.points())
Abstract base class to hold the Field mapping addressing and weights.
Definition FieldMapper.H:44
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
PatchFunction1 with the code supplied by an on-the-fly compiled C++ expression.
Definition CodedField.H:124
virtual tmp< Field< Type > > integrate(const scalar x1, const scalar x2) const
Integrate between two values.
Definition CodedField.C:248
virtual void rmap(const PatchFunction1< Type > &pf1, const labelList &addr)
Reverse map the given PatchFunction1 onto this PatchFunction1.
Definition CodedField.C:276
virtual const dictionary & codeContext() const
Additional 'codeContext' dictionary to pass through.
Definition CodedField.C:52
virtual const dictionary & codeDict(const dictionary &dict) const
Definition CodedField.C:62
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition CodedField.C:88
virtual void autoMap(const FieldMapper &mapper)
Map (and resize as needed) from self given a mapping object.
Definition CodedField.C:262
CodedField(const polyPatch &pp, const word &redirectType, const word &entryName, const dictionary &dict, const bool faceValues=true)
Construct from entry name and dictionary.
Definition CodedField.C:140
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition CodedField.C:291
virtual tmp< Field< Type > > value(const scalar x) const
Return CodedField value.
Definition CodedField.C:234
static constexpr const char *const codeTemplateC
Name of the C code template to be used.
Definition CodedField.H:194
virtual const dictionary & codeDict() const
Definition CodedField.C:80
virtual void clearRedirect() const
Clear redirected object(s).
Definition CodedField.C:44
static constexpr const char *const codeTemplateH
Name of the H code template to be used.
Definition CodedField.H:200
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition CodedField.C:29
virtual string description() const
Description (type + name) for the output.
Definition CodedField.C:37
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
const polyPatch const word const word & entryName
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.
const polyPatch const word const word const dictionary & dict
const polyPatch & pp
const polyPatch const word const word const dictionary const bool faceValues
Base class for function objects and boundary conditions using dynamic code that provides methods for ...
Definition codedBase.H:63
void updateLibrary(const word &name, const dynamicCodeContext &context) const
Update library as required, using the given context.
Definition codedBase.C:283
codedBase(const codedBase &)=delete
No copy construct.
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
Definition codedBase.C:270
A wrapper for dictionary content, without operators that could affect inheritance patterns.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
dictionary & subDictOrAdd(const word &keyword, enum keyType::option matchOpt=keyType::REGEX)
Find and return a sub-dictionary for manipulation.
Definition dictionary.C:481
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:765
A table of dynamically loaded 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:57
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
void setFieldTemplates()
Define a filter variables TemplateType and FieldType.
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
void setMakeOptions(const std::string &content)
Define contents for Make/options.
const polyPatch & patch_
Reference to the patch.
const polyPatch & patch() const noexcept
Reference to the patch.
A patch is a list of labels that address the faces in the global face list.
Definition polyPatch.H:73
A class for handling character strings derived from std::string.
Definition string.H:76
A class for managing temporary objects.
Definition tmp.H:75
A class for handling words, derived from Foam::string.
Definition word.H:66
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
#define DetailInfo
Definition evalEntry.C:30
OBJstream os(runTime.globalPath()/outputName)
auto & name
#define WarningInFunction
Report a warning using Foam::Warning.
List< label > labelList
A List of labels.
Definition List.H:62
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict