Function1.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::Function1
26 
27 Description
28  Run-time selectable general function of one variable
29 
30  with many options provided from simple constant values to complex
31  functions, interpolated tabulated data etc. etc.
32 
33 SourceFiles
34  Function1.C
35  Function1New.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef Function1_H
40 #define Function1_H
41 
42 #include "dictionary.H"
43 #include "unitConversion.H"
44 #include "tmp.H"
45 #include "typeInfo.H"
46 #include "Field.H"
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward declaration of friend functions and operators
55 template<class Type> class Function1;
56 template<class Type> Ostream& operator<<(Ostream&, const Function1<Type>&);
57 
58 /*---------------------------------------------------------------------------*\
59  Class Function1s::unitConversions Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 namespace Function1s
63 {
64 struct unitConversions
65 {
66  // Public Data
67 
68  //- Unit conversion for x-axis values
70 
71  //- Unit conversion for result values
73 
74 
75  // Constructors
76 
77  //- Construct from initialiser list
78  unitConversions(std::initializer_list<unitConversion>);
79 
80  //- Construct from stream
82 
83  //- Construct a clone
85 
86 
87  // Member Functions
88 
89  //- Update if found in the dictionary
90  bool readIfPresent(const word& keyword, const dictionary&);
91 };
92 }
93 
94 
95 // Global Functions
96 
97 //- Generate an error in an context where unit conversions are not supported
99 (
100  const word& typeName,
102  const dictionary& dict
103 );
104 
105 //- Write a units entry
107 
108 
109 // IOStream Operators
110 
111 //- Read from stream
113 
114 //- Write to stream
116 
117 
118 /*---------------------------------------------------------------------------*\
119  Class Function1 Declaration
120 \*---------------------------------------------------------------------------*/
121 
122 template<class Type>
123 class Function1
124 :
125  public tmp<Function1<Type>>::refCount
126 {
127 
128 protected:
129 
130  // Protected Data
131 
132  //- Name of entry
133  const word name_;
134 
135 
136 public:
137 
138  typedef Type returnType;
139 
140 
141  //- Runtime type information
142  TypeName("Function1");
143 
144 
145  // Declare runtime constructor selection tables
146 
148  (
149  autoPtr,
150  Function1,
151  dictionary,
152  (
153  const word& name,
155  const dictionary& dict
156  ),
157  (name, units, dict)
158  );
159 
161  (
162  autoPtr,
163  Function1,
164  Istream,
165  (
166  const word& name,
168  Istream& is
169  ),
170  (name, units, is)
171  );
172 
173 
174  // Constructors
175 
176  //- Construct from name
177  Function1(const word& name);
178 
179  //- Copy constructor
180  Function1(const Function1<Type>& f1);
181 
182  //- Construct and return a clone
183  virtual tmp<Function1<Type>> clone() const = 0;
184 
185 
186  // Selectors
187 
188  //- Select from dictionary
190  (
191  const word& name,
193  const dictionary& dict
194  );
195 
196  //- Select from dictionary
198  (
199  const word& name,
200  const unitConversion& xUnits,
201  const unitConversion& valueUnits,
202  const dictionary& dict
203  );
204 
205  //- Select from Istream
207  (
208  const word& name,
210  const word& Function1Type,
211  Istream& is
212  );
213 
214  //- Select from Istream
216  (
217  const word& name,
218  const unitConversion& xUnits,
219  const unitConversion& valueUnits,
220  const word& Function1Type,
221  Istream& is
222  );
223 
224 
225  //- Destructor
226  virtual ~Function1();
227 
228 
229  // Member Functions
230 
231  //- Return the name of the entry
232  const word& name() const;
233 
234  //- Return value as a function of scalar x
235  virtual Type value(const scalar x) const = 0;
236 
237  //- Return value as a function of a scalar field x
238  virtual tmp<Field<Type>> value(const scalarField& x) const = 0;
239 
240  //- Integrate between two scalars
241  virtual Type integral(const scalar x1, const scalar x2) const = 0;
242 
243  //- Integrate between two scalar fields
244  virtual tmp<Field<Type>> integral
245  (
246  const scalarField& x1,
247  const scalarField& x2
248  ) const = 0;
249 
250  //- Is this function guaranteed to be constant?
251  virtual bool constant() const;
252 
253  //- Write data to dictionary stream
254  virtual void write
255  (
256  Ostream& os,
258  ) const = 0;
259 
260 
261  // Member Operators
262 
263  //- Assignment
264  void operator=(const Function1<Type>&);
265 
266 
267  // IOstream Operators
268 
269  //- Ostream Operator
270  friend Ostream& operator<< <Type>
271  (
272  Ostream& os,
273  const Function1<Type>& func
274  );
275 };
276 
277 
278 template<class Type>
279 void writeEntry(Ostream& os, const Function1<Type>& f1);
280 
281 template<class Type>
282 void writeEntry
283 (
284  Ostream& os,
286  const Function1<Type>& f1
287 );
288 
289 template<class Type>
290 void writeEntry
291 (
292  Ostream& os,
293  const unitConversion& xUnits,
294  const unitConversion& valueUnits,
295  const Function1<Type>& f1
296 );
297 
298 
299 /*---------------------------------------------------------------------------*\
300  Class FieldFunction1 Declaration
301 \*---------------------------------------------------------------------------*/
302 
303 template<class Type, class Function1Type>
304 class FieldFunction1
305 :
306  public Function1<Type>
307 {
308 
309 public:
310 
311  // Constructors
312 
313  //- Construct from name
314  FieldFunction1(const word& name);
315 
316  //- Construct and return a clone
317  virtual tmp<Function1<Type>> clone() const;
318 
319 
320  //- Destructor
321  virtual ~FieldFunction1();
322 
323 
324  // Member Functions
325 
326  //- Return value as a function of one scalars
327  virtual Type value(const scalar x) const = 0;
328 
329  //- Return value as a function of one scalar field
330  virtual tmp<Field<Type>> value(const scalarField& x) const;
331 
332  //- Integrate between two scalar values
333  virtual Type integral(const scalar x1, const scalar x2) const = 0;
334 
335  //- Integrate between two scalar fields
336  virtual tmp<Field<Type>> integral
337  (
338  const scalarField& x1,
339  const scalarField& x2
340  ) const;
341 };
342 
343 
344 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
345 
346 } // End namespace Foam
347 
348 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
349 
350 #define defineFunction1(Type) \
351  \
352  defineNamedTemplateTypeNameAndDebug(Function1<Type>, 0); \
353  defineTemplateRunTimeSelectionTable(Function1<Type>, dictionary); \
354  defineTemplateRunTimeSelectionTable(Function1<Type>, Istream);
355 
356 
357 #define addFunction1(SS, Type) \
358  \
359  defineNamedTemplateTypeNameAndDebug(SS<Type>, 0); \
360  typedef Function1<Type> Type##Function1; \
361  typedef SS<Type> Type##SS##Function1; \
362  addToRunTimeSelectionTable \
363  ( \
364  Type##Function1, \
365  Type##SS##Function1, \
366  dictionary \
367  )
368 
369 
370 #define addStreamConstructableFunction1(SS, Type) \
371  \
372  addFunction1(SS, Type); \
373  addToRunTimeSelectionTable \
374  ( \
375  Type##Function1, \
376  Type##SS##Function1, \
377  Istream \
378  )
379 
380 
381 #define addNamedFunction1(SS, Type, Name) \
382  \
383  typedef Function1<Type> Type##Function1; \
384  typedef SS<Type> Type##SS##Function1; \
385  addNamedToRunTimeSelectionTable \
386  ( \
387  Type##Function1, \
388  Type##SS##Function1, \
389  dictionary, \
390  Name \
391  )
392 
393 
394 #define addScalarFunction1(SS) \
395  \
396  defineTypeNameAndDebug(SS, 0); \
397  typedef Function1<scalar> scalarFunction1; \
398  addToRunTimeSelectionTable(scalarFunction1, SS, dictionary)
399 
400 
401 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
402 
403 #ifdef NoRepository
404  #include "Function1.C"
405  #include "Constant.H"
406 #endif
407 
408 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
409 
410 #endif
411 
412 // ************************************************************************* //
Macros for easy insertion into run-time selection tables.
virtual Type integral(const scalar x1, const scalar x2) const =0
Integrate between two scalar values.
FieldFunction1(const word &name)
Construct from name.
Definition: Function1.C:46
virtual Type value(const scalar x) const =0
Return value as a function of one scalars.
virtual tmp< Function1< Type > > clone() const
Construct and return a clone.
Definition: Function1.C:54
virtual ~FieldFunction1()
Destructor.
Definition: Function1.C:71
Run-time selectable general function of one variable.
Definition: Function1.H:125
virtual Type integral(const scalar x1, const scalar x2) const =0
Integrate between two scalars.
TypeName("Function1")
Runtime type information.
declareRunTimeSelectionTable(autoPtr, Function1, dictionary,(const word &name, const Function1s::unitConversions &units, const dictionary &dict),(name, units, dict))
virtual ~Function1()
Destructor.
Definition: Function1.C:66
virtual tmp< Function1< Type > > clone() const =0
Construct and return a clone.
static autoPtr< Function1< Type > > New(const word &name, const Function1s::unitConversions &units, const dictionary &dict)
Select from dictionary.
Definition: Function1New.C:32
Function1(const word &name)
Construct from name.
Definition: Function1.C:31
virtual Type value(const scalar x) const =0
Return value as a function of scalar x.
void operator=(const Function1< Type > &)
Assignment.
Definition: Function1.C:133
virtual bool constant() const
Is this function guaranteed to be constant?
Definition: Function1.C:85
const word & name() const
Return the name of the entry.
Definition: Function1.C:78
virtual void write(Ostream &os, const Function1s::unitConversions &) const =0
Write data to dictionary stream.
const word name_
Name of entry.
Definition: Function1.H:132
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
Reference counter for various OpenFOAM components.
Definition: refCount.H:50
A class for managing temporary objects.
Definition: tmp.H:55
Unit conversion structure. Contains the associated dimensions and the multiplier with which to conver...
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.
void func(FieldField< Field, Type > &f, const FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const HashTable< unitConversion > & units()
Get the table of unit conversions.
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
Istream & operator>>(Istream &, pistonPointEdgeData &)
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
void assertNoConvertUnits(const word &typeName, const Function1s::unitConversions &units, const dictionary &dict)
Generate an error in an context where unit conversions are not supported.
dictionary dict
bool readIfPresent(const word &keyword, const dictionary &)
Update if found in the dictionary.
autoPtr< unitConversions > clone() const
Construct a clone.
unitConversion value
Unit conversion for result values.
Definition: Function1.H:71
unitConversions(std::initializer_list< unitConversion >)
Construct from initialiser list.
unitConversion x
Unit conversion for x-axis values.
Definition: Function1.H:68
Basic run-time type information using word as the type's name. Used to enhance the standard RTTI to c...