codedFunctionObject.H
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 Class
28  Foam::functionObjects::codedFunctionObject
29 
30 Group
31  grpUtilitiesFunctionObjects
32 
33 Description
34  Provides a general interface to enable dynamic code compilation.
35 
36  The entries are:
37  \plaintable
38  codeInclude | include files
39  codeOptions | compiler line: added to EXE_INC (Make/options)
40  codeLibs | linker line: added to LIB_LIBS (Make/options)
41  codeData | c++; local member data (default constructed)
42  localCode | c++; local static functions
43  codeRead | c++; upon functionObject::read()
44  codeExecute | c++; upon functionObject::execute()
45  codeWrite | c++; upon functionObject::write()
46  codeEnd | c++; upon functionObject::end()
47  codeContext | additional dictionary context for the code
48  \endplaintable
49 
50 Usage
51  Example of function object specification:
52  \verbatim
53  difference
54  {
55  type coded;
56  libs (utilityFunctionObjects);
57 
58  // Name of on-the-fly generated functionObject
59  name writeMagU;
60  codeWrite
61  #{
62  // Lookup U
63  const volVectorField& U = mesh().lookupObject<volVectorField>("U");
64 
65  // Write
66  mag(U)().write();
67  #};
68  }
69  \endverbatim
70 
71 Note
72  The code context dictionary can be supplied separately as the
73  \c codeContext entry.
74 
75 See also
76  Foam::functionObject
77  Foam::codedBase
78 
79 SourceFiles
80  codedFunctionObject.C
81 
82 \*---------------------------------------------------------------------------*/
83 
84 #ifndef functionObjects_codedFunctionObject_H
85 #define functionObjects_codedFunctionObject_H
86 
87 #include "timeFunctionObject.H"
88 #include "codedBase.H"
89 
90 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
91 
92 namespace Foam
93 {
94 namespace functionObjects
95 {
96 
97 /*---------------------------------------------------------------------------*\
98  Class codedFunctionObject Declaration
99 \*---------------------------------------------------------------------------*/
100 
101 class codedFunctionObject
102 :
103  public functionObjects::timeFunctionObject,
104  public codedBase
105 {
106 protected:
107 
108  // Protected Data
109 
110  //- Input dictionary
111  dictionary dict_;
112 
113  word name_;
114 
115  string codeData_;
116  string codeRead_;
117  string codeExecute_;
118  string codeWrite_;
119  string codeEnd_;
120 
121  //- Underlying functionObject
122  mutable autoPtr<functionObject> redirectFunctionObjectPtr_;
123 
124 
125  // Protected Member Functions
126 
127  //- Mutable access to the loaded dynamic libraries
128  virtual dlLibraryTable& libs() const;
129 
130  //- Description (type + name) for the output
131  virtual string description() const;
132 
133  //- Clear redirected object(s)
134  virtual void clearRedirect() const;
135 
136  //- Additional 'codeContext' dictionary to pass through
137  virtual const dictionary& codeContext() const;
138 
139  //- The code dictionary
140  virtual const dictionary& codeDict() const;
141 
142  //- Adapt the context for the current object
143  virtual void prepare(dynamicCode&, const dynamicCodeContext&) const;
144 
145 
146  //- No copy construct
147  codedFunctionObject(const codedFunctionObject&) = delete;
148 
149  //- No copy assignment
150  void operator=(const codedFunctionObject&) = delete;
151 
152 
153 public:
154 
155  // Static Data Members
156 
157  //- Name of the C code template to be used
158  static constexpr const char* const codeTemplateC
159  = "functionObjectTemplate.C";
160 
161  //- Name of the H code template to be used
162  static constexpr const char* const codeTemplateH
163  = "functionObjectTemplate.H";
164 
165 
166  //- Runtime type information
167  TypeName("coded");
168 
169 
170  // Constructors
171 
172  //- Construct from Time and dictionary
174  (
175  const word& name,
176  const Time& runTime,
177  const dictionary& dict
178  );
179 
180 
181  //- Destructor
182  virtual ~codedFunctionObject() = default;
183 
184 
185  // Member Functions
186 
187  //- Dynamically compiled functionObject
189 
190  //- Called at each ++ or += of the time-loop.
191  // postProcess overrides the usual executeControl behaviour and
192  // forces execution (used in post-processing mode)
193  virtual bool execute();
194 
195  //- Called at each ++ or += of the time-loop.
196  // postProcess overrides the usual writeControl behaviour and
197  // forces writing always (used in post-processing mode)
198  virtual bool write();
199 
200  //- Called when Time::run() determines that the time-loop exits.
201  // By default it simply calls execute().
202  virtual bool end();
203 
204  //- Read and set the function object if its data have changed
205  virtual bool read(const dictionary&);
206 };
207 
208 
209 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
210 
211 } // End namespace functionObjects
212 } // End namespace Foam
213 
214 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
215 
216 #endif
217 
218 // ************************************************************************* //
Foam::functionObjects::codedFunctionObject::dict_
dictionary dict_
Input dictionary.
Definition: codedFunctionObject.H:150
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::functionObjects::codedFunctionObject::redirectFunctionObjectPtr_
autoPtr< functionObject > redirectFunctionObjectPtr_
Underlying functionObject.
Definition: codedFunctionObject.H:161
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::functionObjects::codedFunctionObject::codeTemplateH
static constexpr const char *const codeTemplateH
Name of the H code template to be used.
Definition: codedFunctionObject.H:202
Foam::dynamicCode
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:59
Foam::functionObjects::codedFunctionObject::codeRead_
string codeRead_
Definition: codedFunctionObject.H:155
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::functionObjects::codedFunctionObject::operator=
void operator=(const codedFunctionObject &)=delete
No copy assignment.
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
Foam::functionObjects::codedFunctionObject::codeTemplateC
static constexpr const char *const codeTemplateC
Name of the C code template to be used.
Definition: codedFunctionObject.H:198
Foam::functionObjects::codedFunctionObject::codedFunctionObject
codedFunctionObject(const codedFunctionObject &)=delete
No copy construct.
Foam::functionObjects::codedFunctionObject::codeExecute_
string codeExecute_
Definition: codedFunctionObject.H:156
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
timeFunctionObject.H
Foam::functionObjects::codedFunctionObject
Provides a general interface to enable dynamic code compilation.
Definition: codedFunctionObject.H:140
Foam::functionObjects::codedFunctionObject::redirectFunctionObject
functionObject & redirectFunctionObject() const
Dynamically compiled functionObject.
Definition: codedFunctionObject.C:155
Foam::functionObjects::codedFunctionObject::codeData_
string codeData_
Definition: codedFunctionObject.H:154
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::functionObjects::codedFunctionObject::name_
word name_
Definition: codedFunctionObject.H:152
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionObjects::codedFunctionObject::end
virtual bool end()
Called when Time::run() determines that the time-loop exits.
Definition: codedFunctionObject.C:203
Foam::functionObjects::codedFunctionObject::~codedFunctionObject
virtual ~codedFunctionObject()=default
Destructor.
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::functionObject::name
const word & name() const noexcept
Return the name of this functionObject.
Definition: functionObject.C:143
Foam::functionObjects::codedFunctionObject::libs
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: codedFunctionObject.C:57
Foam::functionObjects::codedFunctionObject::TypeName
TypeName("coded")
Runtime type information.
Foam::functionObjects::codedFunctionObject::codeContext
virtual const dictionary & codeContext() const
Additional 'codeContext' dictionary to pass through.
Definition: codedFunctionObject.C:76
codedBase.H
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::functionObjects::codedFunctionObject::codeEnd_
string codeEnd_
Definition: codedFunctionObject.H:158
Foam::functionObjects::codedFunctionObject::codeWrite_
string codeWrite_
Definition: codedFunctionObject.H:157