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-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 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 | include paths; inserted into EXE_INC in Make/options
40  codeLibs | link line; inserted into LIB_LIBS in Make/options
41  codeData | c++; local member data (null 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  \endplaintable
48 
49 Usage
50  Example of function object specification:
51  \verbatim
52  difference
53  {
54  type coded;
55  libs (utilityFunctionObjects);
56 
57  // Name of on-the-fly generated functionObject
58  name writeMagU;
59  codeWrite
60  #{
61  // Lookup U
62  const volVectorField& U = mesh().lookupObject<volVectorField>("U");
63  // Write
64  mag(U)().write();
65  #};
66  }
67  \endverbatim
68 
69 See also
70  Foam::functionObject
71  Foam::codedBase
72 
73 SourceFiles
74  codedFunctionObject.C
75 
76 \*---------------------------------------------------------------------------*/
77 
78 #ifndef functionObjects_codedFunctionObject_H
79 #define functionObjects_codedFunctionObject_H
80 
81 #include "timeFunctionObject.H"
82 #include "codedBase.H"
83 
84 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
85 
86 namespace Foam
87 {
88 namespace functionObjects
89 {
90 
91 /*---------------------------------------------------------------------------*\
92  Class codedFunctionObject Declaration
93 \*---------------------------------------------------------------------------*/
94 
95 class codedFunctionObject
96 :
97  public functionObjects::timeFunctionObject,
98  public codedBase
99 {
100 protected:
101 
102  // Protected data
103 
104  //- Input dictionary
105  dictionary dict_;
106 
107  word name_;
108 
109  string codeData_;
110  string codeRead_;
111  string codeExecute_;
112  string codeWrite_;
113  string codeEnd_;
114 
115  //- Underlying functionObject
116  mutable autoPtr<functionObject> redirectFunctionObjectPtr_;
117 
118 
119  // Protected Member Functions
120 
121  //- Get the loaded dynamic libraries
122  virtual dlLibraryTable& libs() const;
123 
124  //- Adapt the context for the current object
125  virtual void prepare(dynamicCode&, const dynamicCodeContext&) const;
126 
127  //- Return a description (type + name) for the output
128  virtual string description() const;
129 
130  //- Clear any redirected objects
131  virtual void clearRedirect() const;
132 
133  //- The dictionary to initialize the codeContext
134  virtual const dictionary& codeDict() const;
135 
136 
137  //- No copy construct
138  codedFunctionObject(const codedFunctionObject&) = delete;
139 
140  //- No copy assignment
141  void operator=(const codedFunctionObject&) = delete;
142 
143 
144 public:
145 
146  // Static Data Members
147 
148  //- Name of the C code template to be used
149  static constexpr const char* const codeTemplateC
150  = "functionObjectTemplate.C";
151 
152  //- Name of the H code template to be used
153  static constexpr const char* const codeTemplateH
154  = "functionObjectTemplate.H";
155 
156 
157  //- Runtime type information
158  TypeName("coded");
159 
160 
161  // Constructors
162 
163  //- Construct from Time and dictionary
165  (
166  const word& name,
167  const Time& runTime,
168  const dictionary& dict
169  );
170 
171 
172  //- Destructor
173  virtual ~codedFunctionObject() = default;
174 
175 
176  // Member Functions
177 
178  //- Dynamically compiled functionObject
180 
181  //- Called at each ++ or += of the time-loop.
182  // postProcess overrides the usual executeControl behaviour and
183  // forces execution (used in post-processing mode)
184  virtual bool execute();
185 
186  //- Called at each ++ or += of the time-loop.
187  // postProcess overrides the usual writeControl behaviour and
188  // forces writing always (used in post-processing mode)
189  virtual bool write();
190 
191  //- Called when Time::run() determines that the time-loop exits.
192  // By default it simply calls execute().
193  virtual bool end();
194 
195  //- Read and set the function object if its data have changed
196  virtual bool read(const dictionary&);
197 };
198 
199 
200 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
201 
202 } // End namespace functionObjects
203 } // End namespace Foam
204 
205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
206 
207 #endif
208 
209 // ************************************************************************* //
Foam::functionObjects::codedFunctionObject::dict_
dictionary dict_
Input dictionary.
Definition: codedFunctionObject.H:140
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:56
Foam::functionObjects::codedFunctionObject::redirectFunctionObjectPtr_
autoPtr< functionObject > redirectFunctionObjectPtr_
Underlying functionObject.
Definition: codedFunctionObject.H:151
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:57
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::functionObjects::codedFunctionObject::codeTemplateH
static constexpr const char *const codeTemplateH
Name of the H code template to be used.
Definition: codedFunctionObject.H:189
Foam::dynamicCode
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:59
Foam::functionObjects::codedFunctionObject::codeRead_
string codeRead_
Definition: codedFunctionObject.H:145
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::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:185
Foam::functionObjects::codedFunctionObject::codedFunctionObject
codedFunctionObject(const codedFunctionObject &)=delete
No copy construct.
Foam::functionObjects::codedFunctionObject::codeExecute_
string codeExecute_
Definition: codedFunctionObject.H:146
Foam::functionObject
Abstract base-class for Time/database function objects.
Definition: functionObject.H:309
Foam::functionObjects::codedFunctionObject::read
virtual bool read(const dictionary &)
Read and set the function object if its data have changed.
Definition: codedFunctionObject.C:184
timeFunctionObject.H
Foam::functionObjects::codedFunctionObject
Provides a general interface to enable dynamic code compilation.
Definition: codedFunctionObject.H:130
Foam::functionObjects::codedFunctionObject::redirectFunctionObject
functionObject & redirectFunctionObject() const
Dynamically compiled functionObject.
Definition: codedFunctionObject.C:145
Foam::functionObjects::codedFunctionObject::codeData_
string codeData_
Definition: codedFunctionObject.H:144
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:121
Foam::functionObjects::codedFunctionObject::name_
word name_
Definition: codedFunctionObject.H:142
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:177
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
Return the name of this functionObject.
Definition: functionObject.C:131
Foam::functionObjects::codedFunctionObject::libs
virtual dlLibraryTable & libs() const
Get the loaded dynamic libraries.
Definition: codedFunctionObject.C:97
Foam::functionObjects::codedFunctionObject::TypeName
TypeName("coded")
Runtime type information.
codedBase.H
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::functionObjects::codedFunctionObject::codeEnd_
string codeEnd_
Definition: codedFunctionObject.H:148
Foam::functionObjects::codedFunctionObject::codeWrite_
string codeWrite_
Definition: codedFunctionObject.H:147