codedFunctionObject.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) 2011-2017 OpenFOAM Foundation
9 Copyright (C) 2019-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 "codedFunctionObject.H"
30#include "volFields.H"
31#include "dictionary.H"
32#include "Time.H"
33#include "dynamicCode.H"
34#include "dynamicCodeContext.H"
35#include "dictionaryContent.H"
37
38// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39
40namespace Foam
41{
42namespace functionObjects
43{
46 (
50 );
51} // End namespace functionObjects
52} // End namespace Foam
53
54
55// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
56
58{
59 return time_.libs();
60}
61
62
64{
65 return "functionObject " + name();
66}
67
68
70{
71 redirectFunctionObjectPtr_.reset(nullptr);
72}
73
74
77{
78 const dictionary* ptr = dict_.findDict("codeContext", keyType::LITERAL);
79 return (ptr ? *ptr : dictionary::null);
80}
81
82
85{
86 return dict_;
87}
88
89
91(
92 dynamicCode& dynCode,
93 const dynamicCodeContext& context
94) const
95{
96 // Set additional rewrite rules
97 dynCode.setFilterVariable("typeName", name_);
98 dynCode.setFilterVariable("codeData", codeData_);
99 dynCode.setFilterVariable("codeRead", codeRead_);
100 dynCode.setFilterVariable("codeExecute", codeExecute_);
101 dynCode.setFilterVariable("codeWrite", codeWrite_);
102 dynCode.setFilterVariable("codeEnd", codeEnd_);
103
104 // Compile filtered C template
105 dynCode.addCompileFile(codeTemplateC);
106
107 // Copy filtered H template
108 dynCode.addCopyFile(codeTemplateH);
109
110 #ifdef FULLDEBUG
111 dynCode.setFilterVariable("verbose", "true");
113 <<"compile " << name_ << " sha1: " << context.sha1() << endl;
114 #endif
115
116 // Define Make/options
117 dynCode.setMakeOptions
118 (
119 "EXE_INC = -g \\\n"
120 "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
121 "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
122 + context.options()
123 + "\n\nLIB_LIBS = \\\n"
124 " -lOpenFOAM \\\n"
125 " -lfiniteVolume \\\n"
126 " -lmeshTools \\\n"
127 + context.libs()
128 );
129}
130
131
132// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
133
135(
136 const word& name,
137 const Time& runTime,
138 const dictionary& dict
139)
140:
142 codedBase(),
143 dict_(dict)
144{
145 read(dict_);
146
149}
150
151
152// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
153
156{
157 if (!redirectFunctionObjectPtr_)
158 {
159 dictionary constructDict(dict_);
160 constructDict.set("type", name_);
161
162 redirectFunctionObjectPtr_ = functionObject::New
163 (
164 name_,
165 time_,
166 constructDict
167 );
168
169
170 // Forward copy of codeContext to the code template
171 auto* contentPtr =
172 dynamic_cast<dictionaryContent*>(redirectFunctionObjectPtr_.get());
173
174 if (contentPtr)
175 {
176 contentPtr->dict(this->codeContext());
177 }
178 else
179 {
181 << name_ << " Did not derive from dictionaryContent"
182 << nl << nl;
183 }
184 }
185 return *redirectFunctionObjectPtr_;
186}
187
188
190{
191 updateLibrary(name_);
192 return redirectFunctionObject().execute();
193}
194
195
197{
198 updateLibrary(name_);
199 return redirectFunctionObject().write();
200}
201
202
204{
205 updateLibrary(name_);
206 return redirectFunctionObject().end();
207}
208
209
211{
213
215
216 dict.readCompat<word>("name", {{"redirectType", 1706}}, name_);
217
218 auto& ctx = codedBase::codeContext();
219
220 // Get code chunks, no short-circuiting
221 int nKeywords = 0;
222 nKeywords += ctx.readIfPresent("codeData", codeData_);
223 nKeywords += ctx.readIfPresent("codeRead", codeRead_);
224 nKeywords += ctx.readIfPresent("codeExecute", codeExecute_);
225 nKeywords += ctx.readIfPresent("codeWrite", codeWrite_);
226 nKeywords += ctx.readIfPresent("codeEnd", codeEnd_);
227
228 if (!nKeywords)
229 {
231 << "No critical \"code\" prefixed keywords found." << nl
232 << "Please check the code documentation for more details." << nl
233 << endl;
234 }
235
236 updateLibrary(name_);
237 return redirectFunctionObject().read(dict);
238}
239
240
241// ************************************************************************* //
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
virtual bool read()
Re-read model coefficients if they have changed.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: Time.H:510
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
dynamicCodeContext & codeContext()
Access to the dynamic code context.
Definition: codedBase.H:119
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 * findDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX)
Find and return a sub-dictionary pointer if present.
Definition: dictionaryI.H:127
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:780
A table of dynamically loaded 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
Abstract base-class for Time/database function objects.
Provides a general interface to enable dynamic code compilation.
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.
functionObject & redirectFunctionObject() const
Dynamically compiled functionObject.
virtual const dictionary & codeDict() const
The code dictionary.
virtual void clearRedirect() const
Clear redirected object(s)
virtual bool execute()
Called at each ++ or += of the time-loop.
virtual bool write()
Called at each ++ or += of the time-loop.
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
virtual bool end()
Called when Time::run() determines that the time-loop exits.
virtual bool read(const dictionary &)
Read and set the function object if its data have changed.
virtual string description() const
Description (type + name) for the output.
Virtual base class for function objects with a reference to Time.
const Time & time_
Reference to the time database.
@ LITERAL
String literal.
Definition: keyType.H:81
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 defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
engineTime & runTime
#define DetailInfo
Definition: evalEntry.C:37
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
#define WarningInFunction
Report a warning using Foam::Warning.
Namespace for OpenFOAM.
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
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict