Function2.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) 2020-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::Function2
26 
27 Description
28  Run-time selectable function of two variables
29 
30  with many options provided from simple constant values to complex
31  functions, interpolated tabulated data etc. etc.
32 
33 SourceFiles
34  Function2.C
35  Function2New.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef Function2_H
40 #define Function2_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 Function2;
56 template<class Type> Ostream& operator<<(Ostream&, const Function2<Type>&);
57 
58 /*---------------------------------------------------------------------------*\
59  Class Function2s::unitConversions Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 namespace Function2s
63 {
64 struct unitConversions
65 {
66  // Public Data
67 
68  //- Unit conversion for x-axis values
70 
71  //- Unit conversion for y-axis values
73 
74  //- Unit conversion for result values
76 };
77 }
78 
79 
80 // Global Functions
81 
82 //- Generate an error in an context where unit conversions are not supported
84 (
85  const word& typeName,
87  const dictionary& dict
88 );
89 
90 
91 /*---------------------------------------------------------------------------*\
92  Class Function2 Declaration
93 \*---------------------------------------------------------------------------*/
94 
95 template<class Type>
96 class Function2
97 :
98  public tmp<Function2<Type>>::refCount
99 {
100 
101 protected:
102 
103  // Protected data
104 
105  //- Name of entry
106  const word name_;
107 
108 
109 public:
110 
111  typedef Type returnType;
112 
113 
114  //- Runtime type information
115  TypeName("Function2");
116 
117 
118  // Declare runtime constructor selection tables
119 
121  (
122  autoPtr,
123  Function2,
124  dictionary,
125  (
126  const word& name,
128  const dictionary& dict
129  ),
130  (name, units, dict)
131  );
132 
134  (
135  autoPtr,
136  Function2,
137  Istream,
138  (
139  const word& name,
141  Istream& is
142  ),
143  (name, units, is)
144  );
145 
146 
147  // Constructors
148 
149  //- Construct from name
150  Function2(const word& name);
151 
152  //- Copy constructor
153  Function2(const Function2<Type>& f2);
154 
155  //- Construct and return a clone
156  virtual tmp<Function2<Type>> clone() const = 0;
157 
158 
159  // Selectors
160 
161  //- Select from dictionary
163  (
164  const word& name,
166  const dictionary& dict
167  );
168 
169  //- Select from dictionary
171  (
172  const word& name,
173  const unitConversion& xUnits,
174  const unitConversion& yUnits,
175  const unitConversion& valueUnits,
176  const dictionary& dict
177  );
178 
179  //- Select from Istream
181  (
182  const word& name,
184  const word& Function2Type,
185  Istream& is
186  );
187 
188  //- Select from Istream
190  (
191  const word& name,
192  const unitConversion& xUnits,
193  const unitConversion& yUnits,
194  const unitConversion& valueUnits,
195  const word& Function2Type,
196  Istream& is
197  );
198 
199 
200  //- Destructor
201  virtual ~Function2();
202 
203 
204  // Member Functions
205 
206  //- Return the name of the entry
207  const word& name() const;
208 
209  //- Return value as a function of two scalars
210  virtual Type value(const scalar x, const scalar y) const = 0;
211 
212  //- Return value as a function of two scalar fields
213  virtual tmp<Field<Type>> value
214  (
215  const scalarField& x,
216  const scalarField& y
217  ) const = 0;
218 
219  //- Write in dictionary format
220  virtual void write
221  (
222  Ostream& os,
224  ) const = 0;
225 
226 
227  // Member Operators
228 
229  //- Assignment
230  void operator=(const Function2<Type>&);
231 
232 
233  // IOstream Operators
234 
235  //- Ostream Operator
236  friend Ostream& operator<< <Type>
237  (
238  Ostream& os,
239  const Function2<Type>& func
240  );
241 };
242 
243 
244 template<class Type>
245 void writeEntry(Ostream& os, const Function2<Type>& f2);
246 
247 template<class Type>
248 void writeEntry
249 (
250  Ostream& os,
252  const Function2<Type>& f2
253 );
254 
255 template<class Type>
256 void writeEntry
257 (
258  Ostream& os,
259  const unitConversion& xUnits,
260  const unitConversion& yUnits,
261  const unitConversion& valueUnits,
262  const Function2<Type>& f2
263 );
264 
265 
266 /*---------------------------------------------------------------------------*\
267  Class FieldFunction2 Declaration
268 \*---------------------------------------------------------------------------*/
269 
270 template<class Type, class Function2Type>
271 class FieldFunction2
272 :
273  public Function2<Type>
274 {
275 
276 public:
277 
278  // Constructors
279 
280  //- Construct from name
281  FieldFunction2(const word& name);
282 
283  //- Construct and return a clone
284  virtual tmp<Function2<Type>> clone() const;
285 
286 
287  //- Destructor
288  virtual ~FieldFunction2();
289 
290 
291  // Member Functions
292 
293  //- Return value as a function of two scalars
294  virtual Type value(const scalar x, const scalar y) const = 0;
295 
296  //- Return value as a function of two scalar fields
297  virtual tmp<Field<Type>> value
298  (
299  const scalarField& x,
300  const scalarField& y
301  ) const;
302 };
303 
304 
305 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
306 
307 } // End namespace Foam
308 
309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
310 
311 #define defineFunction2(Type) \
312  \
313  defineNamedTemplateTypeNameAndDebug(Function2<Type>, 0); \
314  defineTemplateRunTimeSelectionTable(Function2<Type>, dictionary); \
315  defineTemplateRunTimeSelectionTable(Function2<Type>, Istream);
316 
317 
318 #define addFunction2(SS, Type) \
319  \
320  defineNamedTemplateTypeNameAndDebug(SS<Type>, 0); \
321  typedef Function2<Type> Type##Function2; \
322  typedef SS<Type> Type##SS##Function2; \
323  addToRunTimeSelectionTable \
324  ( \
325  Type##Function2, \
326  Type##SS##Function2, \
327  dictionary \
328  )
329 
330 
331 #define addStreamConstructableFunction2(SS, Type) \
332  \
333  addFunction2(SS, Type); \
334  addToRunTimeSelectionTable \
335  ( \
336  Type##Function2, \
337  Type##SS##Function2, \
338  Istream \
339  )
340 
341 
342 #define addScalarFunction2(SS) \
343  \
344  defineTypeNameAndDebug(SS, 0); \
345  typedef Function2<scalar> scalarFunction2; \
346  addToRunTimeSelectionTable(scalarFunction2, SS, dictionary)
347 
348 
349 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350 
351 #ifdef NoRepository
352  #include "Function2.C"
353  #include "Constant2.H"
354 #endif
355 
356 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
357 
358 #endif
359 
360 // ************************************************************************* //
scalar y
Macros for easy insertion into run-time selection tables.
virtual tmp< Function2< Type > > clone() const
Construct and return a clone.
Definition: Function2.C:54
FieldFunction2(const word &name)
Construct from name.
Definition: Function2.C:46
virtual ~FieldFunction2()
Destructor.
Definition: Function2.C:71
virtual Type value(const scalar x, const scalar y) const =0
Return value as a function of two scalars.
Run-time selectable function of two variables.
Definition: Function2.H:98
virtual void write(Ostream &os, const Function2s::unitConversions &) const =0
Write in dictionary format.
declareRunTimeSelectionTable(autoPtr, Function2, dictionary,(const word &name, const Function2s::unitConversions &units, const dictionary &dict),(name, units, dict))
virtual tmp< Function2< Type > > clone() const =0
Construct and return a clone.
static autoPtr< Function2< Type > > New(const word &name, const Function2s::unitConversions &units, const dictionary &dict)
Select from dictionary.
Definition: Function2New.C:32
virtual ~Function2()
Destructor.
Definition: Function2.C:66
Function2(const word &name)
Construct from name.
Definition: Function2.C:31
void operator=(const Function2< Type > &)
Assignment.
Definition: Function2.C:106
const word & name() const
Return the name of the entry.
Definition: Function2.C:78
TypeName("Function2")
Runtime type information.
virtual Type value(const scalar x, const scalar y) const =0
Return value as a function of two scalars.
const word name_
Name of entry.
Definition: Function2.H:105
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
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
unitConversion y
Unit conversion for y-axis values.
Definition: Function2.H:71
unitConversion value
Unit conversion for result values.
Definition: Function2.H:74
unitConversion x
Unit conversion for x-axis values.
Definition: Function2.H:68
Basic run-time type information using word as the type's name. Used to enhance the standard RTTI to c...