CodedFunction1.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2020-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::Function1s::Coded
26 
27 Description
28  Constructs a dynamically compiled Function1.
29 
30 Usage
31  Example of a cosine bell shaped pulse inlet:
32  \verbatim
33  inlet
34  {
35  type uniformFixedValue;
36  uniformValue coded;
37 
38  name pulse;
39 
40  codeInclude
41  #{
42  #include "mathematicalConstants.H"
43  #};
44 
45  code
46  #{
47  return vector
48  (
49  0.5*(1 - cos(constant::mathematical::twoPi*min(x/0.3, 1))),
50  0,
51  0
52  );
53  #};
54  }
55  \endverbatim
56 
57 See also
58  Foam::dynamicCode
59  Foam::functionEntries::codeStream
60 
61 SourceFiles
62  CodedFunction1I.H
63  CodedFunction1.C
64 
65 \*---------------------------------------------------------------------------*/
66 
67 #ifndef CodedFunction1_H
68 #define CodedFunction1_H
69 
70 #include "Function1.H"
71 #include "codedBase.H"
72 
73 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
74 
75 namespace Foam
76 {
77 namespace Function1s
78 {
79 
80 /*---------------------------------------------------------------------------*\
81  Class Coded Declaration
82 \*---------------------------------------------------------------------------*/
83 
84 template<class Type>
85 class Coded
86 :
87  public Function1<Type>,
88  public codedBase
89 {
90  // Private Data
91 
92  //- The dynamically generated Function1 pointer
93  mutable autoPtr<Function1<Type>> redirectFunction1Ptr_;
94 
95  //- Cache of the unit conversions
96  const unitConversions units_;
97 
98 
99  // Private Member Functions
100 
101  //- Get the keywords associated with source code
102  virtual wordList codeKeys() const;
103 
104  //- Get the name of the dictionary variables in the source code
105  virtual wordList codeDictVars() const;
106 
107  //- Adapt the context for the current object
108  virtual void prepare(dynamicCode&, const dynamicCodeContext&) const;
109 
110  //- Clear the ptr to the redirected object
111  virtual void clearRedirect() const;
112 
113  //- Compile, link and return the now coded Function1
114  autoPtr<Function1<Type>> compileNew();
115 
116 
117 public:
118 
119  // Runtime type information
120  TypeName("coded");
121 
122 
123  // Constructors
124 
125  //- Construct from name and dictionary
126  Coded
127  (
128  const word& name,
130  const dictionary& dict
131  );
132 
133  //- Copy constructor
134  Coded(const Coded<Type>& cf1);
135 
136  //- Construct and return a clone
137  virtual tmp<Function1<Type>> clone() const;
138 
139 
140  //- Destructor
141  virtual ~Coded();
142 
143 
144  // Member Functions
145 
146  //- Return value as a function of a scalar
147  virtual inline Type value(const scalar x) const;
148 
149  //- Return value as a function of a scalar field
150  virtual tmp<Field<Type>> value(const scalarField& x) const;
151 
152  //- Integrate between two scalars
153  virtual Type integral(const scalar x1, const scalar x2) const;
154 
155  //- Integrate between two scalar fields
156  virtual tmp<Field<Type>> integral
157  (
158  const scalarField& x1,
159  const scalarField& x2
160  ) const;
161 
162  //- Write data to dictionary stream
163  virtual void write(Ostream& os, const unitConversions& units) const;
164 
165 
166  // Member Operators
167 
168  //- Disallow default bitwise assignment
169  void operator=(const Coded<Type>&) = delete;
170 };
171 
172 
173 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
174 
175 } // End namespace Function1s
176 } // End namespace Foam
177 
178 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
179 
180 #include "CodedFunction1I.H"
181 
182 #ifdef NoRepository
183  #include "CodedFunction1.C"
184 #endif
185 
186 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
187 
188 #endif
189 
190 // ************************************************************************* //
Run-time selectable general function of one variable.
Definition: Function1.H:125
const word & name() const
Return the name of the entry.
Definition: Function1.C:78
Constructs a dynamically compiled Function1.
virtual Type integral(const scalar x1, const scalar x2) const
Integrate between two scalars.
virtual void write(Ostream &os, const unitConversions &units) const
Write data to dictionary stream.
virtual ~Coded()
Destructor.
virtual Type value(const scalar x) const
Return value as a function of a scalar.
virtual tmp< Function1< Type > > clone() const
Construct and return a clone.
void operator=(const Coded< Type > &)=delete
Disallow default bitwise assignment.
Coded(const word &name, const Function1s::unitConversions &units, const dictionary &dict)
Construct from name and dictionary.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
Base class for function objects and boundary conditions using dynamic code.
Definition: codedBase.H:54
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
Encapsulation of dynamic code dictionaries.
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:57
A class for managing temporary objects.
Definition: tmp.H:55
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.
const HashTable< unitConversion > & units()
Get the table of unit conversions.
dictionary dict