Function1.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) 2018-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::Function1
29 
30 Description
31  Top level data entry class for use in dictionaries. Provides a mechanism
32  to specify a variable as a certain type, e.g. constant or table, and
33  provide functions to return the (interpolated) value, and integral between
34  limits.
35 
36  The New factory method attempts to deal with varying types of input.
37  It accepts primitive or dictionary entries for dispatching to different
38  function types, but wraps unspecified types as "constant".
39 
40  In the dictionary form, the coefficents are the dictionary itself.
41  This is arguably the more readable form.
42  For example,
43  \verbatim
44  <entryName>
45  {
46  type linearRamp;
47  start 10;
48  duration 20;
49  }
50  \endverbatim
51 
52  In the primitive form, the coefficents are provided separately.
53  For example,
54  \verbatim
55  <entryName> linearRamp;
56  <entryName>Coeffs
57  {
58  start 10;
59  duration 20;
60  }
61  \endverbatim
62  The coeffs dictionary is optional, since it is not required by all types.
63  For example,
64  \verbatim
65  <entryName> zero;
66  \endverbatim
67 
68 SourceFiles
69  Function1.C
70  Function1New.C
71 
72 \*---------------------------------------------------------------------------*/
73 
74 #ifndef Function1_H
75 #define Function1_H
76 
77 #include "function1Base.H"
78 #include "Field.H"
79 
80 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
81 
82 namespace Foam
83 {
84 
85 // Forward Declarations
86 class Time;
87 template<class Type> class Function1;
88 template<class Type> Ostream& operator<<(Ostream&, const Function1<Type>&);
89 
90 /*---------------------------------------------------------------------------*\
91  Class Function1 Declaration
92 \*---------------------------------------------------------------------------*/
93 
94 template<class Type>
95 class Function1
96 :
97  public function1Base
98 {
99  // Private Member Functions
100 
101  //- Selector, with alternative entry, fallback redirection, etc
102  static autoPtr<Function1<Type>> New
103  (
104  const word& entryName, // Entry name for function
105  const entry* eptr, // Eg, dict.findEntry(entryName)
106  const dictionary& dict,
107  const word& redirectType,
108  const bool mandatory
109  );
110 
111 
112 protected:
113 
114  // Protected Member Functions
115 
116  //- No copy assignment
117  void operator=(const Function1<Type>&) = delete;
118 
119 
120 public:
121 
122  typedef Type returnType;
123 
124  //- Runtime type information
125  TypeName("Function1")
126 
127  //- Declare runtime constructor selection table
129  (
132  dictionary,
133  (
134  const word& entryName,
136  ),
138  );
139 
140 
141  // Constructors
142 
143  //- Construct from entry name
144  explicit Function1(const word& entryName);
145 
146  //- Construct from entry name and dictionary
147  Function1(const word& entryName, const dictionary& dict);
148 
149  //- Copy construct
150  explicit Function1(const Function1<Type>& rhs);
151 
152  //- Construct and return a clone
153  virtual tmp<Function1<Type>> clone() const = 0;
154 
155 
156  // Selectors
157 
158  //- Selector, with fallback redirection
159  static autoPtr<Function1<Type>> New
160  (
161  const word& entryName,
162  const dictionary& dict,
163  const word& redirectType,
164  const bool mandatory = true
165  );
166 
167  //- Compatibility selector, with fallback redirection
168  static autoPtr<Function1<Type>> NewCompat
169  (
170  const word& entryName,
171  std::initializer_list<std::pair<const char*,int>> compat,
172  const dictionary& dict,
173  const word& redirectType = word::null,
174  const bool mandatory = true
175  );
176 
177  //- Selector, without fallback redirection
178  static autoPtr<Function1<Type>> New
179  (
180  const word& entryName,
181  const dictionary& dict,
182  const bool mandatory = true
183  );
184 
185  //- An optional selector
186  static autoPtr<Function1<Type>> NewIfPresent
187  (
188  const word& entryName,
189  const dictionary& dict,
190  const word& redirectType = word::null
191  );
192 
193 
194  //- Destructor
195  virtual ~Function1() = default;
196 
197 
198  // Member Functions
199 
200  // Evaluation
201 
202  //- Return value as a function of (scalar) independent variable
203  virtual Type value(const scalar x) const;
204 
205  //- Return value as a function of (scalar) independent variable
206  virtual tmp<Field<Type>> value(const scalarField& x) const;
207 
208  //- Integrate between two (scalar) values
209  virtual Type integrate(const scalar x1, const scalar x2) const;
210 
211  //- Integrate between two (scalar) values
212  virtual tmp<Field<Type>> integrate
213  (
214  const scalarField& x1,
215  const scalarField& x2
216  ) const;
217 
218 
219  // I/O
220 
221  //- Ostream Operator
222  friend Ostream& operator<< <Type>
223  (
224  Ostream& os,
225  const Function1<Type>& func
226  );
227 
228 
229  //- Write in dictionary format.
230  // \note The base output is \em without an END_STATEMENT
231  virtual void writeData(Ostream& os) const;
232 
233  //- Write coefficient entries in dictionary format
234  void writeEntries(Ostream& os) const;
235 };
236 
237 
238 /*---------------------------------------------------------------------------*\
239  Class FieldFunction1 Declaration
240 \*---------------------------------------------------------------------------*/
241 
242 template<class Function1Type>
243 class FieldFunction1
244 :
245  public Function1Type
246 {
247 public:
248 
249  typedef typename Function1Type::returnType Type;
250 
251 
252  // Constructors
253 
254  //- Construct from entry name and dictionary
255  FieldFunction1(const word& entryName, const dictionary& dict);
256 
257  //- Construct and return a clone
258  virtual tmp<Function1<Type>> clone() const;
259 
260 
261  //- Destructor
262  virtual ~FieldFunction1() = default;
263 
264 
265  // Member Functions
266 
267  // Evaluation
268 
269  using Function1Type::value;
270  using Function1Type::integrate;
271 
272  //- Return value as a function of (scalar) independent variable
273  virtual tmp<Field<Type>> value(const scalarField& x) const;
274 
275  //- Integrate between two (scalar) values
276  virtual tmp<Field<Type>> integrate
277  (
278  const scalarField& x1,
279  const scalarField& x2
280  ) const;
281 };
282 
283 
284 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
285 
286 } // End namespace Foam
287 
288 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
289 
290 // Define Function1 run-time selection
291 #define makeFunction1(Type) \
292  \
293  defineNamedTemplateTypeNameAndDebug(Function1<Type>, 0); \
294  \
295  defineTemplateRunTimeSelectionTable \
296  ( \
297  Function1<Type>, \
298  dictionary \
299  );
300 
301 
302 // Define (templated) Function1, add to (templated) run-time selection
303 #define makeFunction1Type(SS, Type) \
304  \
305  defineNamedTemplateTypeNameAndDebug(Function1Types::SS<Type>, 0); \
306  \
307  Function1<Type>::adddictionaryConstructorToTable \
308  <FieldFunction1<Function1Types::SS<Type>>> \
309  add##SS##Type##ConstructorToTable_;
310 
311 
312 // Define a non-templated Function1 and add to (templated) run-time selection
313 #define makeConcreteFunction1(SS, Type) \
314  \
315  defineTypeNameAndDebug(SS, 0); \
316  \
317  Function1<Type>::adddictionaryConstructorToTable \
318  <FieldFunction1<SS>> \
319  add##SS##Type##ConstructorToTable_;
320 
321 
322 // Define scalar Function1 and add to (templated) run-time selection
323 #define makeScalarFunction1(SS) \
324  \
325  makeConcreteFunction1(SS, scalar);
326 
327 
328 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
329 
330 #ifdef NoRepository
331  #include "Function1.C"
332 #endif
333 
334 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
335 
336 #endif
337 
338 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::Function1::entryName
const word & entryName
Definition: Function1.H:133
Foam::Function1::writeEntries
void writeEntries(Ostream &os) const
Write coefficient entries in dictionary format.
Definition: Function1.C:161
Foam::Function1::operator
friend Ostream & operator(Ostream &os, const Function1< Type > &func)
Ostream Operator.
Foam::Function1::dict
const word const dictionary & dict
Definition: Function1.H:135
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: Function1.H:86
Foam::FieldFunction1
Definition: Function1.H:242
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::Function1::TypeName
TypeName("Function1") declareRunTimeSelectionTable(autoPtr
Runtime type information.
Foam::Function1::returnType
Type returnType
Definition: Function1.H:121
Field.H
Foam::function1Base
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: function1Base.H:58
Foam::Function1::writeData
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: Function1.C:166
Foam::Function1::operator=
void operator=(const Function1< Type > &)=delete
No copy assignment.
Foam::func
void func(FieldField< Field, Type > &f, const FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::Function1::value
virtual Type value(const scalar x) const
Return value as a function of (scalar) independent variable.
Definition: Function1.C:60
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::Function1::NewIfPresent
static autoPtr< Function1< Type > > NewIfPresent(const word &entryName, const dictionary &dict, const word &redirectType=word::null)
An optional selector.
Definition: Function1New.C:197
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
function1Base.H
Foam::Function1::NewCompat
static autoPtr< Function1< Type > > NewCompat(const word &entryName, std::initializer_list< std::pair< const char *, int >> compat, const dictionary &dict, const word &redirectType=word::null, const bool mandatory=true)
Compatibility selector, with fallback redirection.
Definition: Function1New.C:162
declareRunTimeSelectionTable
#define declareRunTimeSelectionTable(autoPtr, baseType, argNames, argList, parList)
Declare a run-time selection.
Definition: runTimeSelectionTables.H:49
Foam::FieldFunction1::Type
Function1Type::returnType Type
Definition: Function1.H:248
x
x
Definition: LISASMDCalcMethod2.H:52
Function1.C
Foam::Function1::integrate
virtual Type integrate(const scalar x1, const scalar x2) const
Integrate between two (scalar) values.
Definition: Function1.C:80
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Function1::clone
virtual tmp< Function1< Type > > clone() const =0
Construct and return a clone.