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-2023 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 coefficients 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 coefficients 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 Foam_Function1_H
75 #define Foam_Function1_H
76 
77 #include "function1Base.H"
78 #include "Field.H"
79 #include "HashPtrTable.H"
80 
81 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
82 
83 namespace Foam
84 {
85 
86 // Forward Declarations
87 template<class Type> Ostream& operator<<(Ostream&, const Function1<Type>&);
88 
89 /*---------------------------------------------------------------------------*\
90  Class Function1 Declaration
91 \*---------------------------------------------------------------------------*/
92 
93 template<class Type>
94 class Function1
95 :
96  public function1Base
97 {
98  // Private Member Functions
99 
100  //- Selector, with alternative entry, fallback redirection, etc
101  static autoPtr<Function1<Type>> New
102  (
103  const word& entryName, // Entry name for function
104  const entry* eptr, // Eg, dict.findEntry(entryName)
105  const dictionary& dict,
106  const word& redirectType,
107  const objectRegistry* obrPtr,
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  // Data Types
123 
124  //- The return type
125  typedef Type returnType;
127 
128  //- Runtime type information
129  TypeName("Function1")
130 
131  //- Declare runtime constructor selection table
133  (
134  autoPtr,
135  Function1,
136  dictionary,
137  (
138  const word& entryName,
139  const dictionary& dict,
141  ),
142  (entryName, dict, obrPtr)
143  );
144 
145 
146  // Constructors
147 
148  //- Construct from entry name
149  explicit Function1
150  (
151  const word& entryName,
152  const objectRegistry* obrPtr = nullptr
153  );
154 
155  //- Construct from entry name, (unused) dictionary
156  //- and optional registry
157  Function1
158  (
159  const word& entryName,
160  const dictionary& dict,
161  const objectRegistry* obrPtr = nullptr
162  );
163 
164  //- Copy construct
165  explicit Function1(const Function1<Type>& rhs);
166 
167  //- Construct and return a clone
168  virtual tmp<Function1<Type>> clone() const = 0;
169 
170 
171  // Selectors
172 
173  //- Selector, with fallback redirection
174  static autoPtr<Function1<Type>> New
175  (
176  const word& entryName,
177  const dictionary& dict,
178  const word& redirectType,
179  const objectRegistry* obrPtr = nullptr,
180  const bool mandatory = true
181  );
182 
183  //- Compatibility selector, with fallback redirection
184  static autoPtr<Function1<Type>> NewCompat
185  (
186  const word& entryName,
187  std::initializer_list<std::pair<const char*,int>> compat,
188  const dictionary& dict,
189  const word& redirectType = word::null,
190  const objectRegistry* obrPtr = nullptr,
191  const bool mandatory = true
192  );
193 
194  //- Selector, without fallback redirection
195  static autoPtr<Function1<Type>> New
196  (
197  const word& entryName,
198  const dictionary& dict,
199  const objectRegistry* obrPtr = nullptr,
200  const bool mandatory = true
201  );
202 
203  //- An optional selector, with fallback redirection
204  static autoPtr<Function1<Type>> NewIfPresent
205  (
206  const word& entryName,
207  const dictionary& dict,
208  const word& redirectType,
209  const objectRegistry* obrPtr = nullptr
210  );
211 
212  //- An optional selector, without fallback redirection
213  static autoPtr<Function1<Type>> NewIfPresent
214  (
215  const word& entryName,
216  const dictionary& dict,
217  const objectRegistry* obrPtr = nullptr
218  );
219 
220 
221  // Caching Selectors - accept wildcards in dictionary
222 
223  //- Selector with external storage of Function1.
224  //- This also allows wildcard matches in a dictionary
225  static refPtr<Function1<Type>> New
226  (
227  HashPtrTable<Function1<Type>>& cache,
228  const word& entryName,
229  const dictionary& dict,
230  enum keyType::option matchOpt = keyType::LITERAL,
231  const objectRegistry* obrPtr = nullptr,
232  const bool mandatory = true
233  );
234 
247 
248 
249  //- Destructor
250  virtual ~Function1() = default;
251 
252 
253  // Member Functions
254 
255  //- Is value constant (i.e. independent of x)
256  virtual bool constant() const { return false; }
257 
258  //- Can function be evaluated?
259  virtual bool good() const { return true; }
260 
261 
262  // Evaluation
263 
264  //- Return value as a function of (scalar) independent variable
265  virtual Type value(const scalar x) const;
266 
267  //- Return value as a function of (scalar) independent variable
268  virtual tmp<Field<Type>> value(const scalarField& x) const;
269 
270  //- Integrate between two (scalar) values
271  virtual Type integrate(const scalar x1, const scalar x2) const;
272 
273  //- Integrate between two (scalar) values
274  virtual tmp<Field<Type>> integrate
275  (
276  const scalarField& x1,
277  const scalarField& x2
278  ) const;
279 
280 
281  // I/O
282 
283  //- Ostream Operator
284  friend Ostream& operator<< <Type>
285  (
286  Ostream& os,
287  const Function1<Type>& func
288  );
289 
291  //- Write in dictionary format.
292  // \note The base output is \em without an END_STATEMENT
293  virtual void writeData(Ostream& os) const;
294 
295  //- Write coefficient entries in dictionary format
296  virtual void writeEntries(Ostream& os) const;
297 };
298 
299 
300 /*---------------------------------------------------------------------------*\
301  Class FieldFunction1 Declaration
302 \*---------------------------------------------------------------------------*/
303 
304 template<class Function1Type>
305 class FieldFunction1
306 :
307  public Function1Type
308 {
309 public:
310 
311  typedef typename Function1Type::returnType Type;
312 
313 
314  // Constructors
315 
316  //- Construct from entry name and dictionary
318  (
319  const word& entryName,
320  const dictionary& dict,
321  const objectRegistry* obrPtr = nullptr
322  );
323 
324  //- Construct and return a clone
325  virtual tmp<Function1<Type>> clone() const;
326 
327 
328  //- Destructor
329  virtual ~FieldFunction1() = default;
330 
331 
332  // Member Functions
333 
334  using Function1Type::value;
335  using Function1Type::integrate;
336 
337  //- Return value as a function of (scalar) independent variable
338  virtual tmp<Field<Type>> value(const scalarField& x) const;
339 
340  //- Integrate between two (scalar) values
341  virtual tmp<Field<Type>> integrate
342  (
343  const scalarField& x1,
344  const scalarField& x2
345  ) const;
346 };
347 
348 
349 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350 
351 } // End namespace Foam
352 
353 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
354 
355 // Define Function1 run-time selection
356 #define makeFunction1(Type) \
357  \
358  defineNamedTemplateTypeNameAndDebug(Function1<Type>, 0); \
359  \
360  defineTemplateRunTimeSelectionTable \
361  ( \
362  Function1<Type>, \
363  dictionary \
364  );
365 
366 
367 // Define (templated) Function1, add to (templated) run-time selection
368 #define makeFunction1Type(SS, Type) \
369  \
370  defineNamedTemplateTypeNameAndDebug(Function1Types::SS<Type>, 0); \
371  \
372  Function1<Type>::adddictionaryConstructorToTable \
373  <FieldFunction1<Function1Types::SS<Type>>> \
374  add##SS##Type##ConstructorToTable_;
375 
376 
377 // Define a non-templated Function1 and add to (templated) run-time selection
378 #define makeConcreteFunction1(SS, Type) \
379  \
380  defineTypeNameAndDebug(SS, 0); \
381  \
382  Function1<Type>::adddictionaryConstructorToTable \
383  <FieldFunction1<SS>> \
384  add##SS##Type##ConstructorToTable_;
385 
386 
387 // Define scalar Function1 and add to (templated) run-time selection
388 #define makeScalarFunction1(SS) \
389  \
390  makeConcreteFunction1(SS, scalar);
391 
392 
393 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
394 
395 #ifdef NoRepository
396  #include "Function1.C"
397 #endif
398 
399 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
400 
401 #endif
402 
403 // ************************************************************************* //
virtual void writeEntries(Ostream &os) const
Write coefficient entries in dictionary format.
Definition: Function1.C:162
A class for handling keywords in dictionaries.
Definition: keyType.H:66
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
dictionary dict
virtual tmp< Field< Type > > value(const scalarField &x) const
Return value as a function of (scalar) independent variable.
Definition: Function1.C:103
virtual tmp< Function1< Type > > clone() const
Construct and return a clone.
Definition: Function1.C:132
const word const dictionary & dict
Definition: Function1.H:140
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
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 objectRegistry *obrPtr=nullptr, const bool mandatory=true)
Compatibility selector, with fallback redirection.
Definition: Function1New.C:171
#define declareRunTimeSelectionTable(ptrWrapper, baseType, argNames, argList, parList)
Declare a run-time selection (variables and adder classes)
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers...
Definition: HashPtrTable.H:51
const word & entryName
Definition: Function1.H:140
TypeName("Function1") declareRunTimeSelectionTable(autoPtr
Runtime type information.
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: Function1.C:167
virtual Type value(const scalar x) const
Return value as a function of (scalar) independent variable.
Definition: Function1.C:62
Function1Type::returnType Type
Definition: Function1.H:357
FieldFunction1(const word &entryName, const dictionary &dict, const objectRegistry *obrPtr=nullptr)
Construct from entry name and dictionary.
Definition: Function1.C:120
A class for handling words, derived from Foam::string.
Definition: word.H:63
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
virtual bool good() const
Can function be evaluated?
Definition: Function1.H:290
virtual bool constant() const
Is value constant (i.e. independent of x)
Definition: Function1.H:285
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
virtual Type integrate(const scalar x1, const scalar x2) const
Integrate between two (scalar) values.
Definition: Function1.C:81
OBJstream os(runTime.globalPath()/outputName)
static autoPtr< Function1< Type > > NewIfPresent(const word &entryName, const dictionary &dict, const word &redirectType, const objectRegistry *obrPtr=nullptr)
An optional selector, with fallback redirection.
Definition: Function1New.C:209
Type returnType
The return type.
Definition: Function1.H:126
const word const dictionary const objectRegistry * obrPtr
Definition: Function1.H:140
virtual tmp< Field< Type > > integrate(const scalarField &x1, const scalarField &x2) const
Integrate between two (scalar) values.
Definition: Function1.C:144
virtual tmp< Function1< Type > > clone() const =0
Construct and return a clone.
void operator=(const Function1< Type > &)=delete
No copy assignment.
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Registry of regIOobjects.
virtual ~FieldFunction1()=default
Destructor.
Namespace for OpenFOAM.