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 -------------------------------------------------------------------------------
11 License
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 
40 namespace Foam
41 {
42 namespace functionObjects
43 {
44  defineTypeNameAndDebug(codedFunctionObject, 0);
46  (
47  functionObject,
48  codedFunctionObject,
49  dictionary
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 
75 const Foam::dictionary&
77 {
78  const dictionary* ptr = dict_.findDict("codeContext", keyType::LITERAL);
79  return (ptr ? *ptr : dictionary::null);
80 }
81 
82 
83 const Foam::dictionary&
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");
112  DetailInfo
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 
147  updateLibrary(name_);
148  redirectFunctionObject();
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 // ************************************************************************* //
Foam::dictionary::findDict
dictionary * findDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX)
Find and return a sub-dictionary pointer if present.
Definition: dictionaryI.H:127
volFields.H
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::functionObjects::codedFunctionObject::prepare
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition: codedFunctionObject.C:91
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:63
Foam::functionObjects::codedFunctionObject::codeDict
virtual const dictionary & codeDict() const
The code dictionary.
Definition: codedFunctionObject.C:84
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::functionObjects::codedFunctionObject::write
virtual bool write()
Called at each ++ or += of the time-loop.
Definition: codedFunctionObject.C:196
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::functionObject::New
static autoPtr< functionObject > New(const word &name, const Time &runTime, const dictionary &dict)
Select from dictionary, based on its "type" entry.
Definition: functionObject.C:79
Foam::dynamicCode
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:59
Foam::functionObjects::timeFunctionObject::time_
const Time & time_
Reference to the time database.
Definition: timeFunctionObject.H:65
Foam::functionObjects::codedFunctionObject::description
virtual string description() const
Description (type + name) for the output.
Definition: codedFunctionObject.C:63
Foam::dynamicCodeContext
Encapsulation of dynamic code dictionaries.
Definition: dynamicCodeContext.H:53
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::codedBase
Base class for function objects and boundary conditions using dynamic code that provides methods for ...
Definition: codedBase.H:66
Foam::functionObjects::timeFunctionObject
Virtual base class for function objects with a reference to Time.
Definition: timeFunctionObject.H:56
codedFunctionObject.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::dictionary::set
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:780
Foam::functionObjects::codedFunctionObject::codedFunctionObject
codedFunctionObject(const codedFunctionObject &)=delete
No copy construct.
Foam::dictionaryContent
A wrapper for dictionary content, without operators that could affect inheritance patterns.
Definition: dictionaryContent.H:49
dynamicCodeContext.H
Foam::dynamicCode::addCopyFile
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:359
Foam::functionObject
Abstract base-class for Time/database function objects.
Definition: functionObject.H:332
Foam::functionObjects::codedFunctionObject::read
virtual bool read(const dictionary &)
Read and set the function object if its data have changed.
Definition: codedFunctionObject.C:210
Foam::codedBase::setCodeContext
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
Definition: codedBase.C:341
Foam::dictionary::null
static const dictionary null
An empty dictionary, which is also the parent for all dictionaries.
Definition: dictionary.H:392
Foam::dynamicCodeContext::sha1
const SHA1 & sha1() const noexcept
The SHA1 calculated from options, libs, include, code, etc.
Definition: dynamicCodeContext.H:164
Foam::functionObjects::codedFunctionObject::redirectFunctionObject
functionObject & redirectFunctionObject() const
Dynamically compiled functionObject.
Definition: codedFunctionObject.C:155
Foam::dynamicCode::addCompileFile
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:353
dynamicCode.H
Foam::functionObject::read
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Definition: functionObject.C:163
DetailInfo
#define DetailInfo
Definition: evalEntry.C:37
Foam::dynamicCodeContext::libs
const string & libs() const noexcept
The code libs (LIB_LIBS)
Definition: dynamicCodeContext.H:140
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::codedBase::codeContext
dynamicCodeContext & codeContext()
Access to the dynamic code context.
Definition: codedBase.H:119
Foam::Time::libs
dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: Time.H:505
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::dynamicCode::setFilterVariable
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
Definition: dynamicCode.C:388
Foam::functionObjects::codedFunctionObject::end
virtual bool end()
Called when Time::run() determines that the time-loop exits.
Definition: codedFunctionObject.C:203
dictionaryContent.H
Time.H
Foam::dynamicCode::setMakeOptions
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:397
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::dictionaryContent::dict
const dictionary & dict() const noexcept
Read-access to the content.
Definition: dictionaryContent.H:125
Foam::functionObjects::codedFunctionObject::libs
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: codedFunctionObject.C:57
Foam::functionObjects::codedFunctionObject::codeContext
virtual const dictionary & codeContext() const
Additional 'codeContext' dictionary to pass through.
Definition: codedFunctionObject.C:76
dictionary.H
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::functionObjects::codedFunctionObject::execute
virtual bool execute()
Called at each ++ or += of the time-loop.
Definition: codedFunctionObject.C:189
Foam::functionObjects::codedFunctionObject::clearRedirect
virtual void clearRedirect() const
Clear redirected object(s)
Definition: codedFunctionObject.C:69
Foam::keyType::LITERAL
String literal.
Definition: keyType.H:81
IOWarningInFunction
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:340
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
Foam::dynamicCodeContext::options
const string & options() const noexcept
The code options (Make/options)
Definition: dynamicCodeContext.H:134