Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
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:
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,
137 (
138 const word& entryName,
139 const dictionary& dict,
140 const objectRegistry* obrPtr
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
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);
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)
177 return tmp<Function1<Type>>(new Derived(fun));
178 }
179
180 //- Selector, with fallback redirection
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,
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
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;
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
311template<class Function1Type>
313:
314 public Function1Type
315{
316public:
318 typedef typename Function1Type::returnType Type;
319
320
321 // Constructors
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 {
335 }
336
337
338 //- Destructor
339 virtual ~FieldFunction1() = default;
340
341
342 // Member Functions
343
344 using Function1Type::value;
345 using Function1Type::integrate;
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
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 tmp< Function1< Type > > clone() const
Return a clone.
Definition Function1.H:384
virtual tmp< Field< Type > > integrate(const scalarField &x1, const scalarField &x2) const
Integrate between two (scalar) values.
Definition Function1.C:133
virtual tmp< Field< Type > > value(const scalarField &x) const
Return value as a function of (scalar) independent variable.
Definition Function1.C:103
virtual ~FieldFunction1()=default
Destructor.
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
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition Function1.H:92
static autoPtr< Function1< Type > > NewIfPresent(const word &entryName, const dictionary &dict, const word &redirectType, const objectRegistry *obrPtr=nullptr)
An optional selector, with fallback redirection.
static autoPtr< Function1< Type > > New(const word &entryName, const dictionary &dict, const word &redirectType, const objectRegistry *obrPtr=nullptr, const bool mandatory=true)
Selector, with fallback redirection.
virtual tmp< Field< Type > > value(const scalarField &x) const
Return value as a function of (scalar) independent variable.
Definition Function1.C:71
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.
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition Function1.C:156
virtual bool constant() const
Is value constant (i.e. independent of x).
Definition Function1.H:294
Function1(const word &entryName, const objectRegistry *obrPtr=nullptr)
Construct from entry name.
Definition Function1.C:31
virtual void writeEntries(Ostream &os) const
Write coefficient entries in dictionary format.
Definition Function1.C:151
virtual bool good() const
Can function be evaluated?
Definition Function1.H:299
Type returnType
The return type.
Definition Function1.H:126
virtual tmp< Field< Type > > integrate(const scalarField &x1, const scalarField &x2) const
Integrate between two (scalar) values.
Definition Function1.C:90
static autoPtr< Function1< Type > > New(const word &entryName, const dictionary &dict, const objectRegistry *obrPtr=nullptr, const bool mandatory=true)
Selector, without fallback redirection.
virtual tmp< Function1< label > > clone() const=0
void operator=(const Function1< Type > &)=delete
No copy assignment.
virtual Type value(const scalar x) const
Return value as a function of (scalar) independent variable.
Definition Function1.C:62
virtual Type integrate(const scalar x1, const scalar x2) const
Integrate between two (scalar) values.
Definition Function1.C:81
static refPtr< Function1< Type > > New(HashPtrTable< Function1< Type > > &cache, const word &entryName, const dictionary &dict, enum keyType::option matchOpt=keyType::LITERAL, const objectRegistry *obrPtr=nullptr, const bool mandatory=true)
Selector with external storage of Function1. This also allows wildcard matches in a dictionary.
virtual ~Function1()=default
Destructor.
static tmp< Function1< label > > Clone(const Derived &fun)
Definition Function1.H:190
static autoPtr< Function1< Type > > NewIfPresent(const word &entryName, const dictionary &dict, const objectRegistry *obrPtr=nullptr)
An optional selector, without fallback redirection.
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
function1Base(const word &entryName, const objectRegistry *obrPtr=nullptr)
Construct from entry name and optional registry.
option
Enumeration for the data type and search/match modes (bitmask).
Definition keyType.H:80
@ LITERAL
String literal.
Definition keyType.H:82
Registry of regIOobjects.
A class for managing references or pointers (no reference counting).
Definition refPtr.H:54
A class for managing temporary objects.
Definition tmp.H:75
A class for handling words, derived from Foam::string.
Definition word.H:66
static const word null
An empty word.
Definition word.H:84
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
#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:68