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-2024 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  //- Return a clone
168  virtual tmp<Function1<Type>> clone() const = 0;
169 
170 
171  // Factory Methods
172 
173  //- Clone a Function1
174  template<class Derived>
175  static tmp<Function1<Type>> Clone(const Derived& fun)
176  {
177  return tmp<Function1<Type>>(new Derived(fun));
178  }
179 
180  //- Selector, with fallback redirection
181  static autoPtr<Function1<Type>> New
182  (
183  const word& entryName,
184  const dictionary& dict,
185  const word& redirectType,
186  const objectRegistry* obrPtr = nullptr,
187  const bool mandatory = true
188  );
189 
190  //- Compatibility selector, with fallback redirection
192  (
193  const word& entryName,
194  std::initializer_list<std::pair<const char*,int>> compat,
195  const dictionary& dict,
196  const word& redirectType = word::null,
197  const objectRegistry* obrPtr = nullptr,
198  const bool mandatory = true
199  );
200 
201  //- Selector, without fallback redirection
202  static autoPtr<Function1<Type>> New
203  (
204  const word& entryName,
205  const dictionary& dict,
206  const objectRegistry* obrPtr = nullptr,
207  const bool mandatory = true
208  );
209 
210  //- An optional selector, with fallback redirection
212  (
213  const word& entryName,
214  const dictionary& dict,
215  const word& redirectType,
216  const objectRegistry* obrPtr = nullptr
217  );
218 
219  //- An optional selector, without fallback redirection
221  (
222  const word& entryName,
223  const dictionary& dict,
224  const objectRegistry* obrPtr = nullptr
225  );
226 
227 
228  // Caching Selectors - accept wildcards in dictionary
229 
230  //- Selector with external storage of Function1.
231  //- This also allows wildcard matches in a dictionary
232  static refPtr<Function1<Type>> New
233  (
235  const word& entryName,
236  const dictionary& dict,
237  enum keyType::option matchOpt = keyType::LITERAL,
238  const objectRegistry* obrPtr = nullptr,
239  const bool mandatory = true
240  );
241 
254 
255 
256  //- Destructor
257  virtual ~Function1() = default;
258 
259 
260  // Member Functions
261 
262  //- Is value constant (i.e. independent of x)
263  virtual bool constant() const { return false; }
264 
265  //- Can function be evaluated?
266  virtual bool good() const { return true; }
267 
268 
269  // Evaluation
270 
271  //- Return value as a function of (scalar) independent variable
272  virtual Type value(const scalar x) const;
273 
274  //- Return value as a function of (scalar) independent variable
275  virtual tmp<Field<Type>> value(const scalarField& x) const;
276 
277  //- Integrate between two (scalar) values
278  virtual Type integrate(const scalar x1, const scalar x2) const;
279 
280  //- Integrate between two (scalar) values
281  virtual tmp<Field<Type>> integrate
282  (
283  const scalarField& x1,
284  const scalarField& x2
285  ) const;
286 
287 
288  // I/O
289 
290  //- Ostream Operator
291  friend Ostream& operator<< <Type>
292  (
293  Ostream& os,
294  const Function1<Type>& func
295  );
296 
297 
298  //- Write in dictionary format.
299  // \note The base output is \em without an END_STATEMENT
300  virtual void writeData(Ostream& os) const;
301 
302  //- Write coefficient entries in dictionary format
303  virtual void writeEntries(Ostream& os) const;
304 };
305 
306 
307 /*---------------------------------------------------------------------------*\
308  Class FieldFunction1 Declaration
309 \*---------------------------------------------------------------------------*/
310 
311 template<class Function1Type>
312 class FieldFunction1
313 :
314  public Function1Type
315 {
316 public:
317 
318  typedef typename Function1Type::returnType Type;
319 
320 
321  // Constructors
322 
323  //- Construct from entry name and dictionary
325  (
326  const word& entryName,
327  const dictionary& dict,
328  const objectRegistry* obrPtr = nullptr
329  );
330 
331  //- Return a clone
332  virtual tmp<Function1<Type>> clone() const
333  {
334  return Function1<Type>::Clone(*this);
335  }
336 
337 
338  //- Destructor
339  virtual ~FieldFunction1() = default;
340 
341 
342  // Member Functions
343 
344  using Function1Type::value;
345  using Function1Type::integrate;
346 
347  //- Return value as a function of (scalar) independent variable
348  virtual tmp<Field<Type>> value(const scalarField& x) const;
349 
350  //- Integrate between two (scalar) values
351  virtual tmp<Field<Type>> integrate
352  (
353  const scalarField& x1,
354  const scalarField& x2
355  ) const;
356 };
357 
358 
359 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
361 } // End namespace Foam
362 
363 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
364 
365 // Define Function1 run-time selection
366 #define makeFunction1(Type) \
367  \
368  defineNamedTemplateTypeNameAndDebug(Function1<Type>, 0); \
369  \
370  defineTemplateRunTimeSelectionTable \
371  ( \
372  Function1<Type>, \
373  dictionary \
374  );
375 
376 
377 // Define (templated) Function1, add to (templated) run-time selection
378 #define makeFunction1Type(SS, Type) \
379  \
380  defineNamedTemplateTypeNameAndDebug(Function1Types::SS<Type>, 0); \
381  \
382  Function1<Type>::adddictionaryConstructorToTable \
383  <FieldFunction1<Function1Types::SS<Type>>> \
384  add##SS##Type##ConstructorToTable_;
385 
386 
387 // Define a non-templated Function1 and add to (templated) run-time selection
388 #define makeConcreteFunction1(SS, Type) \
389  \
390  defineTypeNameAndDebug(SS, 0); \
391  \
392  Function1<Type>::adddictionaryConstructorToTable \
393  <FieldFunction1<SS>> \
394  add##SS##Type##ConstructorToTable_;
395 
396 
397 // Define scalar Function1 and add to (templated) run-time selection
398 #define makeScalarFunction1(SS) \
399  \
400  makeConcreteFunction1(SS, scalar);
401 
402 
403 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
404 
405 #ifdef NoRepository
406  #include "Function1.C"
407 #endif
408 
409 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
410 
411 #endif
412 
413 // ************************************************************************* //
virtual void writeEntries(Ostream &os) const
Write coefficient entries in dictionary format.
Definition: Function1.C:151
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
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
static tmp< Function1< Type > > Clone(const Derived &fun)
Clone a Function1.
Definition: Function1.H:190
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: Function1.C:156
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:366
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.
static const word null
An empty word.
Definition: word.H:84
String literal.
Definition: keyType.H:82
virtual bool good() const
Can function be evaluated?
Definition: Function1.H:299
virtual bool constant() const
Is value constant (i.e. independent of x)
Definition: Function1.H:294
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:133
virtual tmp< Function1< Type > > clone() const =0
Return a clone.
virtual tmp< Function1< Type > > clone() const
Return a clone.
Definition: Function1.H:384
void operator=(const Function1< Type > &)=delete
No copy assignment.
option
Enumeration for the data type and search/match modes (bitmask)
Definition: keyType.H:79
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 ~Function1()=default
Destructor.
virtual ~FieldFunction1()=default
Destructor.
Namespace for OpenFOAM.