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 
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 // ************************************************************************* //
functionObject & redirectFunctionObject() const
Dynamically compiled functionObject.
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
dlLibraryTable & libs() const noexcept
Mutable access to the loaded dynamic libraries.
Definition: Time.H:730
virtual void clearRedirect() const
Clear redirected object(s)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:390
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:346
Abstract base-class for Time/database function objects.
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
virtual const dictionary & codeDict() const
The code dictionary.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
virtual bool write()
Called at each ++ or += of the time-loop.
virtual const dictionary & codeContext() const
Additional &#39;codeContext&#39; dictionary to pass through.
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
const string & options() const noexcept
The code options (Make/options)
A class for handling words, derived from Foam::string.
Definition: word.H:63
const dictionary & dict() const noexcept
Read-access to the content.
virtual bool execute()
Called at each ++ or += of the time-loop.
static const dictionary null
An empty dictionary, which is also the parent for all dictionaries.
Definition: dictionary.H:474
Base class for function objects and boundary conditions using dynamic code that provides methods for ...
Definition: codedBase.H:62
String literal.
Definition: keyType.H:82
#define DetailInfo
Definition: evalEntry.C:30
A table of dynamically loaded libraries.
A wrapper for dictionary content, without operators that could affect inheritance patterns...
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
Definition: codedBase.C:270
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
virtual bool read(const dictionary &)
Read and set the function object if its data have changed.
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:56
Encapsulation of dynamic code dictionaries.
void updateLibrary(const word &name, const dynamicCodeContext &context) const
Update library as required, using the given context.
Definition: codedBase.C:283
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
Definition: dynamicCode.C:381
#define WarningInFunction
Report a warning using Foam::Warning.
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
virtual string description() const
Description (type + name) for the output.
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:352
const string & libs() const noexcept
The code libs (LIB_LIBS)
dynamicCodeContext & codeContext()
Access to the dynamic code context.
Definition: codedBase.H:131
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
static autoPtr< functionObject > New(const word &name, const Time &runTime, const dictionary &dict)
Select from dictionary, based on its "type" entry.
codedFunctionObject(const codedFunctionObject &)=delete
No copy construct.
A class for handling character strings derived from std::string.
Definition: string.H:72
virtual bool end()
Called when Time::run() determines that the time-loop exits.
Virtual base class for function objects with a reference to Time.
Namespace for OpenFOAM.
const SHA1 & sha1() const noexcept
The SHA1 calculated from options, libs, include, code, etc.
const dictionary * findDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary pointer if present (and a sub-dictionary) otherwise return nullptr...
Definition: dictionaryI.H:124
const Time & time_
Reference to the time database.