CodedFunction1.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) 2021 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::Function1Types::CodedFunction1
28 
29 Description
30  Function1 with the code supplied by an on-the-fly compiled C++
31  expression.
32 
33  The code entries:
34  \plaintable
35  codeInclude | include files
36  codeOptions | compiler line: added to EXE_INC (Make/options)
37  codeLibs | linker line: added to LIB_LIBS (Make/options)
38  localCode | c++; local static functions
39  code | c++; return the patch values at (scalar x)
40  \endplaintable
41 
42 Usage
43  Example:
44  \verbatim
45  <patchName>
46  {
47  type uniformFixedValue;
48  uniformValue
49  {
50  type coded;
51  name myExpression; // Name of generated PatchFunction1
52 
53  code
54  #{
55  const polyPatch& pp = this->patch();
56  Pout<< "** Patch size:" << pp.size() << endl;
57  return tmp<vectorField>::New(pp.size(), vector(1, 0, 0))
58  #};
59 
60  //codeInclude
61  //#{
62  // #include "volFields.H"
63  //#};
64 
65  //codeOptions
66  //#{
67  // -I$(LIB_SRC)/finiteVolume/lnInclude
68  //#};
69  }
70  }
71  \endverbatim
72 
73 Note
74  The code context dictionary is simply the dictionary used to specify
75  the PatchFunction1 coefficients.
76 
77 See also
78  Foam::dynamicCode
79  Foam::codedFixedValue
80  Foam::functionEntries::codeStream
81 
82 SourceFiles
83  CodedFunction1.C
84 
85 \*---------------------------------------------------------------------------*/
86 
87 #ifndef Function1Types_CodedFunction1_H
88 #define Function1Types_CodedFunction1_H
89 
90 #include "Function1.H"
91 #include "codedBase.H"
92 
93 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
94 
95 namespace Foam
96 {
97 namespace Function1Types
98 {
99 
100 /*---------------------------------------------------------------------------*\
101  Class CodedFunction1 Declaration
102 \*---------------------------------------------------------------------------*/
103 
104 template<class Type>
105 class CodedFunction1
106 :
107  public Function1<Type>,
108  protected codedBase
109 {
110  // Private Data
111 
112  //- Dictionary contents for the function
113  const dictionary dict_;
114 
115  const word redirectName_;
116 
117  mutable autoPtr<Function1<Type>> redirectFunctionPtr_;
118 
119 
120  // Private Member Functions
121 
122  //- Get reference to the underlying Function1
123  const Function1<Type>& redirectFunction() const;
124 
125 
126 protected:
127 
128  // Protected Member Functions
129 
130  //- Mutable access to the loaded dynamic libraries
131  virtual dlLibraryTable& libs() const;
132 
133  //- Description (type + name) for the output
134  virtual string description() const;
135 
136  //- Clear redirected object(s)
137  virtual void clearRedirect() const;
138 
139  //- Additional 'codeContext' dictionary to pass through
140  virtual const dictionary& codeContext() const;
141 
142  // Get the code (sub)dictionary
143  virtual const dictionary& codeDict(const dictionary& dict) const;
144 
145  // Get the code dictionary
146  virtual const dictionary& codeDict() const;
147 
148  //- Adapt the context for the current object
149  virtual void prepare(dynamicCode&, const dynamicCodeContext&) const;
150 
151 
152  // Generated Methods
153 
154  //- No copy assignment
155  void operator=(const CodedFunction1<Type>&) = delete;
156 
157 public:
158 
159  // Static Data Members
160 
161  //- Name of the C code template to be used
162  static constexpr const char* const codeTemplateC
163  = "codedFunction1Template.C";
164 
165  //- Name of the H code template to be used
166  static constexpr const char* const codeTemplateH
167  = "codedFunction1Template.H";
168 
169 
170  //- Runtime type information
171  TypeName("coded");
172 
173 
174  // Constructors
175 
176  //- Construct from entry name, dictionary and optional registry
178  (
179  const word& entryName,
180  const dictionary& dict,
181  const objectRegistry* obrPtr = nullptr
182  );
183 
184  //- Copy construct
185  explicit CodedFunction1(const CodedFunction1<Type>& rhs);
186 
187  //- Construct and return a clone
188  virtual tmp<Function1<Type>> clone() const
189  {
190  return tmp<Function1<Type>>(new CodedFunction1<Type>(*this));
191  }
192 
193 
194  //- Destructor
195  virtual ~CodedFunction1() = default;
196 
197 
198  // Member Functions
199 
200  //- Return value at current time
201  virtual inline Type value(const scalar x) const;
202 
203  // Integrate etc are not implemented!
204 
205  //- Write in dictionary format
206  virtual void writeData(Ostream& os) const;
207 };
208 
209 
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
211 
212 } // End namespace Function1Types
213 } // End namespace Foam
214 
215 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
216 
217 #ifdef NoRepository
218  #include "CodedFunction1.C"
219 #endif
220 
221 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
222 
223 #endif
224 
225 // ************************************************************************* //
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:63
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::Function1::dictionary
dictionary
Definition: Function1.H:131
Foam::dynamicCode
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:59
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::Function1::entryName
const word & entryName
Definition: Function1.H:133
Function1.H
Foam::dynamicCodeContext
Encapsulation of dynamic code dictionaries.
Definition: dynamicCodeContext.H:53
Foam::codedBase
Base class for function objects and boundary conditions using dynamic code that provides methods for ...
Definition: codedBase.H:66
Foam::Function1Types::CodedFunction1::codeContext
virtual const dictionary & codeContext() const
Additional 'codeContext' dictionary to pass through.
Definition: CodedFunction1.C:59
Foam::Function1::dict
const word const dictionary & dict
Definition: Function1.H:134
Foam::Function1Types::CodedFunction1::codeTemplateC
static constexpr const char *const codeTemplateC
Name of the C code template to be used.
Definition: CodedFunction1.H:182
Foam::Function1Types::CodedFunction1::TypeName
TypeName("coded")
Runtime type information.
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: propellerInfo.H:291
Foam::Function1Types::CodedFunction1::codeTemplateH
static constexpr const char *const codeTemplateH
Name of the H code template to be used.
Definition: CodedFunction1.H:186
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::Function1Types::CodedFunction1::CodedFunction1
CodedFunction1(const word &entryName, const dictionary &dict, const objectRegistry *obrPtr=nullptr)
Construct from entry name, dictionary and optional registry.
Definition: CodedFunction1.C:144
Foam::Function1Types::CodedFunction1
Function1 with the code supplied by an on-the-fly compiled C++ expression.
Definition: CodedFunction1.H:124
Foam::Function1::obrPtr
const word const dictionary const objectRegistry * obrPtr
Definition: Function1.H:136
Foam::Function1Types::CodedFunction1::prepare
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition: CodedFunction1.C:95
Foam::Function1Types::CodedFunction1::codeDict
virtual const dictionary & codeDict() const
Definition: CodedFunction1.C:87
Foam::Function1Types::CodedFunction1::value
virtual Type value(const scalar x) const
Return value at current time.
Definition: CodedFunction1.C:223
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam::Function1Types::CodedFunction1::~CodedFunction1
virtual ~CodedFunction1()=default
Destructor.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::Function1Types::CodedFunction1::description
virtual string description() const
Description (type + name) for the output.
Definition: CodedFunction1.C:44
Foam::Function1Types::CodedFunction1::libs
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: CodedFunction1.C:36
Foam::Function1Types::CodedFunction1::clone
virtual tmp< Function1< Type > > clone() const
Construct and return a clone.
Definition: CodedFunction1.H:207
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::Function1Types::CodedFunction1::operator=
void operator=(const CodedFunction1< Type > &)=delete
No copy assignment.
CodedFunction1.C
x
x
Definition: LISASMDCalcMethod2.H:52
codedBase.H
Foam::Function1Types::CodedFunction1::writeData
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: CodedFunction1.C:236
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Function1Types::CodedFunction1::clearRedirect
virtual void clearRedirect() const
Clear redirected object(s)
Definition: CodedFunction1.C:51