CodedFunction1.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) 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->time().libs();
39}
40
41
42template<class Type>
45{
46 return "CodedFunction1 " + 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 Function1s
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 Function1 "
104 << " name " << redirectName_
105 << exit(FatalIOError);
106 }
107
108 // Take no chances - typeName must be identical to redirectName_
109 dynCode.setFilterVariable("typeName", redirectName_);
110
111 // Set TemplateType and FieldType filter variables
112 dynCode.setFieldTemplates<Type>();
113
114 // Compile filtered C template
115 dynCode.addCompileFile(codeTemplateC);
116
117 // Copy filtered H template
118 dynCode.addCopyFile(codeTemplateH);
119
120 #ifdef FULLDEBUG
121 dynCode.setFilterVariable("verbose", "true");
123 <<"compile " << redirectName_ << " sha1: " << context.sha1() << endl;
124 #endif
125
126 // Define Make/options
127 dynCode.setMakeOptions
128 (
129 "EXE_INC = -g \\\n"
130 "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
131 + context.options()
132 + "\n\nLIB_LIBS = \\\n"
133 " -lOpenFOAM \\\n"
134 " -lmeshTools \\\n"
135 + context.libs()
136 );
137}
138
139
140// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
141
142template<class Type>
144(
145 const word& entryName,
146 const dictionary& dict,
147 const objectRegistry* obrPtr
148)
149:
150 Function1<Type>(entryName, dict, obrPtr),
151 codedBase(),
152 dict_(dict),
153 redirectName_(dict.getOrDefault<word>("name", entryName))
154{
155 this->codedBase::setCodeContext(dict_);
156
157 // No additional code chunks...
158
159 updateLibrary(redirectName_);
160}
161
162
163template<class Type>
165(
166 const CodedFunction1<Type>& rhs
167)
168:
169 Function1<Type>(rhs),
170 codedBase(),
171 dict_(rhs.dict_),
172 redirectName_(rhs.redirectName_)
173{}
174
175
176// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
177
178template<class Type>
181{
182 if (!redirectFunctionPtr_)
183 {
184 dictionary constructDict;
185 // Force 'redirectName_' sub-dictionary into existence
186 dictionary& coeffs = constructDict.subDictOrAdd(redirectName_);
187
188 coeffs = dict_; // Copy input code and coefficients
189 coeffs.remove("name"); // Redundant
190 coeffs.set("type", redirectName_); // Specify our new (redirect) type
191
192 redirectFunctionPtr_.reset
193 (
195 (
196 redirectName_,
197 constructDict,
198 this->whichDb()
199 )
200 );
201
202 // Forward copy of codeContext to the code template
203 auto* contentPtr =
204 dynamic_cast<dictionaryContent*>(redirectFunctionPtr_.get());
205
206 if (contentPtr)
207 {
208 contentPtr->dict(this->codeContext());
209 }
210 else
211 {
213 << redirectName_ << " Did not derive from dictionaryContent"
214 << nl << nl;
215 }
216 }
217 return *redirectFunctionPtr_;
218}
219
220
221template<class Type>
223(
224 const scalar x
225) const
226{
227 // Ensure library containing user-defined code is up-to-date
228 updateLibrary(redirectName_);
229
230 return redirectFunction().value(x);
231}
232
233
234template<class Type>
236(
237 Ostream& os
238) const
239{
240 // Should really only output only relevant entries but since using
241 // Function1-from-subdict upon construction our dictionary contains
242 // only the relevant entries.
243 dict_.writeEntry(this->name(), os);
244}
245
246
247// ************************************************************************* //
Function1 with the code supplied by an on-the-fly compiled C++ expression.
virtual const dictionary & codeContext() const
Additional 'codeContext' dictionary to pass through.
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
virtual const dictionary & codeDict() const
virtual void clearRedirect() const
Clear redirected object(s)
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
virtual string description() const
Description (type + name) for the output.
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: Function1.H:96
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
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
Registry of regIOobjects.
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
#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