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-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::Function1
29
30Description
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
68SourceFiles
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
83namespace Foam
84{
85
86// Forward Declarations
87template<class Type> Ostream& operator<<(Ostream&, const Function1<Type>&);
88
89/*---------------------------------------------------------------------------*\
90 Class Function1 Declaration
91\*---------------------------------------------------------------------------*/
92
93template<class Type>
94class 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
112protected:
113
114 // Protected Member Functions
115
116 //- No copy assignment
117 void operator=(const Function1<Type>&) = delete;
118
119
120public:
122 typedef Type returnType;
123
124 //- Runtime type information
125 TypeName("Function1")
126
127 //- Declare runtime constructor selection table
129 (
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
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
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 virtual void writeEntries(Ostream& os) const;
285};
286
287
288/*---------------------------------------------------------------------------*\
289 Class FieldFunction1 Declaration
290\*---------------------------------------------------------------------------*/
291
292template<class Function1Type>
293class FieldFunction1
294:
295 public Function1Type
296{
297public:
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
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// ************************************************************************* //
virtual tmp< Field< Type > > integrate(const scalarField &x1, const scalarField &x2) const
Integrate between two (scalar) values.
Definition: Function1.C:151
virtual tmp< Function1< Type > > clone() const
Construct and return a clone.
Definition: Function1.C:139
virtual tmp< Field< Type > > value(const scalarField &x) const
Return value as a function of (scalar) independent variable.
Definition: Function1.C:110
virtual ~FieldFunction1()=default
Destructor.
Function1Type::returnType Type
Definition: Function1.H:298
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: Function1.H:96
const word const dictionary & dict
Definition: Function1.H:134
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
TypeName("Function1") declareRunTimeSelectionTable(autoPtr
Runtime type information.
const word const dictionary const objectRegistry * obrPtr
Definition: Function1.H:136
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: Function1.C:174
virtual void writeEntries(Ostream &os) const
Write coefficient entries in dictionary format.
Definition: Function1.C:169
virtual bool good() const
Can function be evaluated?
Definition: Function1.H:246
virtual tmp< Function1< Type > > clone() const =0
Construct and return a clone.
void operator=(const Function1< Type > &)=delete
No copy assignment.
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
virtual Type value(const scalar x) const
Return value as a function of (scalar) independent variable.
Definition: Function1.C:69
virtual Type integrate(const scalar x1, const scalar x2) const
Integrate between two (scalar) values.
Definition: Function1.C:88
const word & entryName
Definition: Function1.H:133
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:68
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
Base class for template-invariant parts of Function1.
Definition: function1Base.H:57
A class for handling keywords in dictionaries.
Definition: keyType.H:71
Registry of regIOobjects.
constant condensation/saturation model.
A class for managing references or pointers (no reference counting)
Definition: refPtr.H:58
A class for managing temporary objects.
Definition: tmp.H:65
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
void func(FieldField< Field, Type > &f, const FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
#define declareRunTimeSelectionTable(ptrWrapper, baseType, argNames, argList, parList)
Declare a run-time selection (variables and adder classes)
dictionary dict
#define TypeName(TypeNameString)
Declare a ClassName() with extra virtual type info.
Definition: typeInfo.H:73