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-2020 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"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40 namespace functionObjects
41 {
42  defineTypeNameAndDebug(codedFunctionObject, 0);
44  (
45  functionObject,
46  codedFunctionObject,
47  dictionary
48  );
49 }
50 }
51 
52 
53 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
54 
56 (
57  dynamicCode& dynCode,
58  const dynamicCodeContext& context
59 ) const
60 {
61  // Set additional rewrite rules
62  dynCode.setFilterVariable("typeName", name_);
63  dynCode.setFilterVariable("codeData", codeData_);
64  dynCode.setFilterVariable("codeRead", codeRead_);
65  dynCode.setFilterVariable("codeExecute", codeExecute_);
66  dynCode.setFilterVariable("codeWrite", codeWrite_);
67  dynCode.setFilterVariable("codeEnd", codeEnd_);
68 
69  // Compile filtered C template
70  dynCode.addCompileFile(codeTemplateC);
71 
72  // Copy filtered H template
73  dynCode.addCopyFile(codeTemplateH);
74 
75  // Debugging: make verbose
76  // dynCode.setFilterVariable("verbose", "true");
77  // DetailInfo
78  // <<"compile " << name_ << " sha1: "
79  // << context.sha1() << endl;
80 
81  // Define Make/options
82  dynCode.setMakeOptions
83  (
84  "EXE_INC = -g \\\n"
85  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
86  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
87  + context.options()
88  + "\n\nLIB_LIBS = \\\n"
89  " -lOpenFOAM \\\n"
90  " -lfiniteVolume \\\n"
91  " -lmeshTools \\\n"
92  + context.libs()
93  );
94 }
95 
96 
98 {
99  return time_.libs();
100 }
101 
102 
104 {
105  return "functionObject " + name();
106 }
107 
108 
110 {
111  redirectFunctionObjectPtr_.clear();
112 }
113 
114 
115 const Foam::dictionary&
117 {
118  return dict_;
119 }
120 
121 
122 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
123 
125 (
126  const word& name,
127  const Time& runTime,
128  const dictionary& dict
129 )
130 :
132  codedBase(),
133  dict_(dict)
134 {
135  read(dict_);
136 
137  updateLibrary(name_);
138  redirectFunctionObject();
139 }
140 
141 
142 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
143 
146 {
147  if (!redirectFunctionObjectPtr_)
148  {
149  dictionary constructDict(dict_);
150  constructDict.set("type", name_);
151 
152  redirectFunctionObjectPtr_ = functionObject::New
153  (
154  name_,
155  time_,
156  constructDict
157  );
158  }
159  return *redirectFunctionObjectPtr_;
160 }
161 
162 
164 {
165  updateLibrary(name_);
166  return redirectFunctionObject().execute();
167 }
168 
169 
171 {
172  updateLibrary(name_);
173  return redirectFunctionObject().write();
174 }
175 
176 
178 {
179  updateLibrary(name_);
180  return redirectFunctionObject().end();
181 }
182 
183 
185 {
187 
189 
190  dict.readCompat<word>("name", {{"redirectType", 1706}}, name_);
191 
192  label nKeywords = 0;
193 
194  const entry* eptr;
195 
196  codeData_.clear();
197  codedBase::append("<codeData>");
198  if ((eptr = dict.findEntry("codeData", keyType::LITERAL)) != nullptr)
199  {
200  eptr->readEntry(codeData_);
202  codedBase::append(codeData_);
203 
205  (
206  codeData_,
207  eptr->startLineNumber(),
208  dict.name()
209  );
210 
211  ++nKeywords;
212  }
213 
214  codeRead_.clear();
215  codedBase::append("<codeRead>");
216  if ((eptr = dict.findEntry("codeRead", keyType::LITERAL)) != nullptr)
217  {
218  eptr->readEntry(codeRead_);
220  codedBase::append(codeRead_);
221 
223  (
224  codeRead_,
225  eptr->startLineNumber(),
226  dict.name()
227  );
228 
229  ++nKeywords;
230  }
231 
232  codeExecute_.clear();
233  codedBase::append("<codeExecute>");
234  if ((eptr = dict.findEntry("codeExecute", keyType::LITERAL)) != nullptr)
235  {
236  eptr->readEntry(codeExecute_);
238  codedBase::append(codeExecute_);
239 
241  (
242  codeExecute_,
243  eptr->startLineNumber(),
244  dict.name()
245  );
246 
247  ++nKeywords;
248  }
249 
250  codeWrite_.clear();
251  codedBase::append("<codeWrite>");
252  if ((eptr = dict.findEntry("codeWrite", keyType::LITERAL)) != nullptr)
253  {
254  eptr->readEntry(codeWrite_);
256  codedBase::append(codeWrite_);
257 
259  (
260  codeWrite_,
261  eptr->startLineNumber(),
262  dict.name()
263  );
264 
265  ++nKeywords;
266  }
267 
268  codeEnd_.clear();
269  codedBase::append("<codeEnd>");
270  if ((eptr = dict.findEntry("codeEnd", keyType::LITERAL)) != nullptr)
271  {
272  eptr->readEntry(codeEnd_);
274  codedBase::append(codeEnd_);
275 
277  (
278  codeEnd_,
279  eptr->startLineNumber(),
280  dict.name()
281  );
282 
283  ++nKeywords;
284  }
285 
286  if (!nKeywords)
287  {
289  << "No critical \"code\" prefixed keywords found." << nl
290  << "Please check the code documentation for more details." << nl
291  << endl;
292  }
293 
294  updateLibrary(name_);
295  return redirectFunctionObject().read(dict);
296 }
297 
298 
299 // ************************************************************************* //
Foam::dynamicCodeContext::libs
const string & libs() const
Return the code-libs.
Definition: dynamicCodeContext.H:146
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
volFields.H
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::codedBase::append
void append(const std::string &str)
Add content to SHA1 hashing.
Definition: codedBase.C:308
Foam::functionObjects::codedFunctionObject::prepare
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition: codedFunctionObject.C:56
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:63
Foam::functionObjects::codedFunctionObject::codeDict
virtual const dictionary & codeDict() const
The dictionary to initialize the codeContext.
Definition: codedFunctionObject.C:116
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:170
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
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:67
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
Return a description (type + name) for the output.
Definition: codedFunctionObject.C:103
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:67
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:350
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::dictionary::set
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:847
Foam::functionObjects::codedFunctionObject::codedFunctionObject
codedFunctionObject(const codedFunctionObject &)=delete
No copy construct.
Foam::dynamicCode::addCopyFile
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:357
Foam::functionObject
Abstract base-class for Time/database function objects.
Definition: functionObject.H:321
Foam::functionObjects::codedFunctionObject::read
virtual bool read(const dictionary &)
Read and set the function object if its data have changed.
Definition: codedFunctionObject.C:184
Foam::codedBase::setCodeContext
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
Definition: codedBase.C:302
Foam::dynamicCodeContext::inplaceExpand
static void inplaceExpand(string &code, const dictionary &dict)
Cleanup string and expand with dictionary parameters.
Definition: dynamicCodeContext.C:36
Foam::functionObjects::codedFunctionObject::redirectFunctionObject
functionObject & redirectFunctionObject() const
Dynamically compiled functionObject.
Definition: codedFunctionObject.C:145
Foam::dynamicCode::addCompileFile
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:351
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
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:137
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::entry::startLineNumber
virtual label startLineNumber() const =0
Return line number of first token in dictionary.
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
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::dynamicCodeContext::options
const string & options() const
Return the code-options.
Definition: dynamicCodeContext.H:140
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:386
Foam::functionObjects::codedFunctionObject::end
virtual bool end()
Called when Time::run() determines that the time-loop exits.
Definition: codedFunctionObject.C:177
Time.H
Foam::dynamicCode::setMakeOptions
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:395
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::functionObjects::codedFunctionObject::libs
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: codedFunctionObject.C:97
dictionary.H
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::entry::readEntry
void readEntry(T &val) const
Definition: entry.H:270
Foam::functionObjects::codedFunctionObject::execute
virtual bool execute()
Called at each ++ or += of the time-loop.
Definition: codedFunctionObject.C:163
Foam::functionObjects::codedFunctionObject::clearRedirect
virtual void clearRedirect() const
Clear any redirected objects.
Definition: codedFunctionObject.C:109
Foam::keyType::LITERAL
String literal.
Definition: keyType.H:73
IOWarningInFunction
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:315
Foam::dynamicCodeContext::addLineDirective
static unsigned addLineDirective(string &code, label lineNum, const fileName &file)
Prefix a #line directive to code.
Definition: dynamicCodeContext.C:47