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-2022 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:
122  typedef Type returnType;
123 
124  //- Runtime type information
125  TypeName("Function1")
126 
127  //- Declare runtime constructor selection table
129  (
130  autoPtr,
131  Function1,
132  dictionary,
133  (
134  const word& entryName,
135  const dictionary& dict,
136  const objectRegistry* obrPtr
137  ),
138  (entryName, dict, obrPtr)
139  );
140 
141 
142  // Constructors
143 
144  //- Construct from entry name
145  explicit Function1
146  (
147  const word& entryName,
148  const objectRegistry* obrPtr = nullptr
149  );
150 
151  //- Construct from entry name, (unused) dictionary
152  //- and optional registry
153  Function1
154  (
155  const word& entryName,
156  const dictionary& dict,
157  const objectRegistry* obrPtr = nullptr
158  );
159 
160  //- Copy construct
161  explicit Function1(const Function1<Type>& rhs);
162 
163  //- Construct and return a clone
164  virtual tmp<Function1<Type>> clone() const = 0;
165 
166 
167  // Selectors
168 
169  //- Selector, with fallback redirection
170  static autoPtr<Function1<Type>> New
171  (
172  const word& entryName,
173  const dictionary& dict,
174  const word& redirectType,
175  const objectRegistry* obrPtr = nullptr,
176  const bool mandatory = true
177  );
178 
179  //- Compatibility selector, with fallback redirection
180  static autoPtr<Function1<Type>> NewCompat
181  (
182  const word& entryName,
183  std::initializer_list<std::pair<const char*,int>> compat,
184  const dictionary& dict,
185  const word& redirectType = word::null,
186  const objectRegistry* obrPtr = nullptr,
187  const bool mandatory = true
188  );
189 
190  //- Selector, without fallback redirection
191  static autoPtr<Function1<Type>> New
192  (
193  const word& entryName,
194  const dictionary& dict,
195  const objectRegistry* obrPtr = nullptr,
196  const bool mandatory = true
197  );
198 
199  //- An optional selector
200  static autoPtr<Function1<Type>> NewIfPresent
201  (
202  const word& entryName,
203  const dictionary& dict,
204  const word& redirectType = word::null,
205  const objectRegistry* obrPtr = nullptr
206  );
207 
208 
209  // Caching Selectors - accept wildcards in dictionary
210 
211  //- Selector with external storage of Function1.
212  //- This also allows wildcard matches in a dictionary
213  static refPtr<Function1<Type>> New
214  (
215  HashPtrTable<Function1<Type>>& cache,
216  const word& entryName,
217  const dictionary& dict,
218  enum keyType::option matchOpt = keyType::LITERAL,
219  const objectRegistry* obrPtr = nullptr,
220  const bool mandatory = true
221  );
222 
235 
236 
237  //- Destructor
238  virtual ~Function1() = default;
239 
240 
241  // Member Functions
242 
243  //- Is value constant (i.e. independent of x)
244  virtual bool constant() const { return false; }
245 
246  //- Can function be evaluated?
247  virtual bool good() const { return true; }
248 
249 
250  // Evaluation
251 
252  //- Return value as a function of (scalar) independent variable
253  virtual Type value(const scalar x) const;
254 
255  //- Return value as a function of (scalar) independent variable
256  virtual tmp<Field<Type>> value(const scalarField& x) const;
257 
258  //- Integrate between two (scalar) values
259  virtual Type integrate(const scalar x1, const scalar x2) const;
260 
261  //- Integrate between two (scalar) values
262  virtual tmp<Field<Type>> integrate
263  (
264  const scalarField& x1,
265  const scalarField& x2
266  ) const;
267 
268 
269  // I/O
270 
271  //- Ostream Operator
272  friend Ostream& operator<< <Type>
273  (
275  const Function1<Type>& func
276  );
277 
278 
279  //- Write in dictionary format.
280  // \note The base output is \em without an END_STATEMENT
281  virtual void writeData(Ostream& os) const;
282 
283  //- Write coefficient entries in dictionary format
284  virtual void writeEntries(Ostream& os) const;
285 };
286 
287 
288 /*---------------------------------------------------------------------------*\
289  Class FieldFunction1 Declaration
290 \*---------------------------------------------------------------------------*/
291 
292 template<class Function1Type>
293 class FieldFunction1
294 :
295  public Function1Type
296 {
297 public:
298 
299  typedef typename Function1Type::returnType Type;
300 
301 
302  // Constructors
303 
304  //- Construct from entry name and dictionary
306  (
307  const word& entryName,
308  const dictionary& dict,
309  const objectRegistry* obrPtr = nullptr
310  );
311 
312  //- Construct and return a clone
313  virtual tmp<Function1<Type>> clone() const;
314 
315 
316  //- Destructor
317  virtual ~FieldFunction1() = default;
318 
319 
320  // Member Functions
321 
322  using Function1Type::value;
323  using Function1Type::integrate;
324 
325  //- Return value as a function of (scalar) independent variable
326  virtual tmp<Field<Type>> value(const scalarField& x) const;
327 
328  //- Integrate between two (scalar) values
329  virtual tmp<Field<Type>> integrate
330  (
331  const scalarField& x1,
332  const scalarField& x2
333  ) const;
334 };
336 
337 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
338 
339 } // End namespace Foam
340 
341 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
342 
343 // Define Function1 run-time selection
344 #define makeFunction1(Type) \
345  \
346  defineNamedTemplateTypeNameAndDebug(Function1<Type>, 0); \
347  \
348  defineTemplateRunTimeSelectionTable \
349  ( \
350  Function1<Type>, \
351  dictionary \
352  );
353 
354 
355 // Define (templated) Function1, add to (templated) run-time selection
356 #define makeFunction1Type(SS, Type) \
357  \
358  defineNamedTemplateTypeNameAndDebug(Function1Types::SS<Type>, 0); \
359  \
360  Function1<Type>::adddictionaryConstructorToTable \
361  <FieldFunction1<Function1Types::SS<Type>>> \
362  add##SS##Type##ConstructorToTable_;
363 
364 
365 // Define a non-templated Function1 and add to (templated) run-time selection
366 #define makeConcreteFunction1(SS, Type) \
367  \
368  defineTypeNameAndDebug(SS, 0); \
369  \
370  Function1<Type>::adddictionaryConstructorToTable \
371  <FieldFunction1<SS>> \
372  add##SS##Type##ConstructorToTable_;
373 
374 
375 // Define scalar Function1 and add to (templated) run-time selection
376 #define makeScalarFunction1(SS) \
377  \
378  makeConcreteFunction1(SS, scalar);
379 
380 
381 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
382 
383 #ifdef NoRepository
384  #include "Function1.C"
385 #endif
386 
387 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
388 
389 #endif
390 
391 // ************************************************************************* //
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
static autoPtr< Function1< Type > > NewIfPresent(const word &entryName, const dictionary &dict, const word &redirectType=word::null, const objectRegistry *obrPtr=nullptr)
An optional selector.
Definition: Function1New.C:209
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:134
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
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:134
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:341
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.
void func(FieldField< Field, Type > &f, const FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
virtual bool good() const
Can function be evaluated?
Definition: Function1.H:274
virtual bool constant() const
Is value constant (i.e. independent of x)
Definition: Function1.H:269
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
virtual Type integrate(const scalar x1, const scalar x2) const
Integrate between two (scalar) values.
Definition: Function1.C:81
OBJstream os(runTime.globalPath()/outputName)
const word const dictionary const objectRegistry * obrPtr
Definition: Function1.H:134
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.