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-2021 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 Function1_H
75 #define 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  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,
135  const dictionary& dict,
137  ),
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
245  {
246  return false;
247  }
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  (
274  Ostream& os,
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  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 };
335 
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 // ************************************************************************* //
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::FieldFunction1::~FieldFunction1
virtual ~FieldFunction1()=default
Destructor.
Foam::Function1::dictionary
dictionary
Definition: Function1.H:131
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:169
Foam::Function1::dict
const word const dictionary & dict
Definition: Function1.H:134
Foam::Function1::NewIfPresent
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:212
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: propellerInfo.H:291
Foam::FieldFunction1
Definition: Function1.H:292
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
declareRunTimeSelectionTable
#define declareRunTimeSelectionTable(ptrWrapper, baseType, argNames, argList, parList)
Declare a run-time selection (variables and adder classes)
Definition: runTimeSelectionTables.H:174
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:68
Foam::Field< scalar >
Foam::Function1::obrPtr
const word const dictionary const objectRegistry * obrPtr
Definition: Function1.H:136
Foam::Function1::TypeName
TypeName("Function1") declareRunTimeSelectionTable(autoPtr
Runtime type information.
Foam::Function1::returnType
Type returnType
Definition: Function1.H:121
Field.H
Foam::Function1::writeData
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: Function1.C:174
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::FieldFunction1::integrate
virtual tmp< Field< Type > > integrate(const scalarField &x1, const scalarField &x2) const
Integrate between two (scalar) values.
Definition: Function1.C:151
Foam::Function1::value
virtual Type value(const scalar x) const
Return value as a function of (scalar) independent variable.
Definition: Function1.C:69
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:54
function1Base.H
HashPtrTable.H
Foam::FieldFunction1::Type
Function1Type::returnType Type
Definition: Function1.H:298
Foam::FieldFunction1::clone
virtual tmp< Function1< Type > > clone() const
Construct and return a clone.
Definition: Function1.C:139
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:88
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.
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 objectRegistry *obrPtr=nullptr, const bool mandatory=true)
Compatibility selector, with fallback redirection.
Definition: Function1New.C:174
Foam::FieldFunction1::value
virtual tmp< Field< Type > > value(const scalarField &x) const
Return value as a function of (scalar) independent variable.
Definition: Function1.C:110
constant
constant condensation/saturation model.
Foam::FieldFunction1::FieldFunction1
FieldFunction1(const word &entryName, const dictionary &dict, const objectRegistry *obrPtr=nullptr)
Construct from entry name and dictionary.
Definition: Function1.C:127
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60