Loading...
Searching...
No Matches
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"
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 * * * * * * * * * * * //
60}
61
64{
65 return "functionObject " + name();
66}
67
68
74
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
194
195
201
202
204{
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.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
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
dynamicCodeContext & codeContext()
Access to the dynamic code context.
Definition codedBase.H:131
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
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition dictionary.C:765
static const dictionary null
An empty dictionary, which is also the parent for all dictionaries.
Definition dictionary.H:487
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:57
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
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.
Abstract base-class for Time/database function objects.
const word & name() const noexcept
Return the name of this functionObject.
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
static autoPtr< functionObject > New(const word &name, const Time &runTime, const dictionary &dict)
Select from dictionary, based on its "type" entry.
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.
autoPtr< functionObject > redirectFunctionObjectPtr_
Underlying functionObject.
functionObject & redirectFunctionObject() const
Dynamically compiled functionObject.
codedFunctionObject(const codedFunctionObject &)=delete
No copy construct.
static constexpr const char *const codeTemplateC
Name of the C code template to be used.
virtual const dictionary & codeDict() const
The code dictionary.
virtual void clearRedirect() const
Clear redirected object(s).
static constexpr const char *const codeTemplateH
Name of the H code template to be used.
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.
timeFunctionObject(const timeFunctionObject &)=delete
No copy construct.
@ LITERAL
String literal.
Definition keyType.H:82
A class for handling character strings derived from std::string.
Definition string.H:76
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
engineTime & runTime
#define DetailInfo
Definition evalEntry.C:30
auto & name
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
#define WarningInFunction
Report a warning using Foam::Warning.
Function objects are OpenFOAM utilities to ease workflow configurations and enhance workflows.
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict