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-2026 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 "Function2UnitSets.H"
43 #include "Field.H"
44 #include "runTimeSelectionTables.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 // Forward declaration of friend functions and operators
52 template<class Type> class Function2;
53 template<class Type> Ostream& operator<<(Ostream&, const Function2<Type>&);
54 
55 /*---------------------------------------------------------------------------*\
56  Class Function2 Declaration
57 \*---------------------------------------------------------------------------*/
58 
59 template<class Type>
60 class Function2
61 :
62  public tmp<Function2<Type>>::refCount
63 {
64 
65 protected:
66 
67  // Protected data
68 
69  //- Name of entry
70  const word name_;
71 
72 
73 public:
74 
75  typedef Type returnType;
76 
77 
78  //- Runtime type information
79  TypeName("Function2");
80 
81 
82  // Declare runtime constructor selection tables
83 
85  (
86  autoPtr,
87  Function2,
88  dictionary,
89  (
90  const word& name,
91  const Function2s::unitSets& units,
92  const dictionary& dict
93  ),
94  (name, units, dict)
95  );
96 
98  (
99  autoPtr,
100  Function2,
101  Istream,
102  (
103  const word& name,
104  const Function2s::unitSets& units,
105  Istream& is
106  ),
107  (name, units, is)
108  );
109 
110 
111  // Constructors
112 
113  //- Construct from name
114  Function2(const word& name);
115 
116  //- Copy constructor
117  Function2(const Function2<Type>& f2);
118 
119  //- Construct and return a clone
120  virtual tmp<Function2<Type>> clone() const = 0;
121 
122 
123  // Selectors
124 
125  //- Select from dictionary
127  (
128  const word& name,
129  const Function2s::unitSets& units,
130  const dictionary& dict
131  );
132 
133  //- Select from dictionary
135  (
136  const word& name,
137  const unitSet& xUnits,
138  const unitSet& yUnits,
139  const unitSet& valueUnits,
140  const dictionary& dict
141  );
142 
143  //- Select from Istream
145  (
146  const word& name,
147  const Function2s::unitSets& units,
148  const word& Function2Type,
149  Istream& is
150  );
151 
152  //- Select from Istream
154  (
155  const word& name,
156  const unitSet& xUnits,
157  const unitSet& yUnits,
158  const unitSet& valueUnits,
159  const word& Function2Type,
160  Istream& is
161  );
162 
163 
164  //- Destructor
165  virtual ~Function2();
166 
167 
168  // Member Functions
169 
170  //- Return the name of the entry
171  const word& name() const;
172 
173  //- Return value as a function of two scalars
174  virtual Type value(const scalar x, const scalar y) const = 0;
175 
176  //- Return value as a function of two scalar fields
177  virtual tmp<Field<Type>> value
178  (
179  const scalarField& x,
180  const scalarField& y
181  ) const = 0;
182 
183  //- Write in dictionary format
184  virtual void write
185  (
186  Ostream& os,
187  const Function2s::unitSets&
188  ) const = 0;
189 
190 
191  // Member Operators
192 
193  //- Assignment
194  void operator=(const Function2<Type>&);
195 
196 
197  // IOstream Operators
198 
199  //- Ostream Operator
200  friend Ostream& operator<< <Type>
201  (
202  Ostream& os,
203  const Function2<Type>& func
204  );
205 };
206 
207 
208 template<class Type>
209 void writeEntry(Ostream& os, const Function2<Type>& f2);
210 
211 template<class Type>
212 void writeEntry
213 (
214  Ostream& os,
215  const Function2s::unitSets& units,
216  const Function2<Type>& f2
217 );
218 
219 template<class Type>
220 void writeEntry
221 (
222  Ostream& os,
223  const unitSet& xUnits,
224  const unitSet& yUnits,
225  const unitSet& valueUnits,
226  const Function2<Type>& f2
227 );
228 
229 
230 /*---------------------------------------------------------------------------*\
231  Class FieldFunction2 Declaration
232 \*---------------------------------------------------------------------------*/
233 
234 template<class Type, class Function2Type>
235 class FieldFunction2
236 :
237  public Function2<Type>
238 {
239 
240 public:
241 
242  // Constructors
243 
244  //- Construct from name
245  FieldFunction2(const word& name);
246 
247  //- Construct and return a clone
248  virtual tmp<Function2<Type>> clone() const;
249 
250 
251  //- Destructor
252  virtual ~FieldFunction2();
253 
254 
255  // Member Functions
256 
257  //- Return value as a function of two scalars
258  virtual Type value(const scalar x, const scalar y) const = 0;
259 
260  //- Return value as a function of two scalar fields
261  virtual tmp<Field<Type>> value
262  (
263  const scalarField& x,
264  const scalarField& y
265  ) const;
266 };
267 
268 
269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
270 
271 } // End namespace Foam
272 
273 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
274 
275 #define defineFunction2(Type) \
276  \
277  defineNamedTemplateTypeNameAndDebug(Function2<Type>, 0); \
278  defineTemplateRunTimeSelectionTable(Function2<Type>, dictionary); \
279  defineTemplateRunTimeSelectionTable(Function2<Type>, Istream);
280 
281 
282 #define addFunction2(SS, Type) \
283  \
284  defineNamedTemplateTypeNameAndDebug(SS<Type>, 0); \
285  typedef Function2<Type> Type##Function2; \
286  typedef SS<Type> Type##SS##Function2; \
287  addToRunTimeSelectionTable \
288  ( \
289  Type##Function2, \
290  Type##SS##Function2, \
291  dictionary \
292  )
293 
294 
295 #define addStreamConstructableFunction2(SS, Type) \
296  \
297  addFunction2(SS, Type); \
298  addToRunTimeSelectionTable \
299  ( \
300  Type##Function2, \
301  Type##SS##Function2, \
302  Istream \
303  )
304 
305 
306 #define addScalarFunction2(SS) \
307  \
308  defineTypeNameAndDebug(SS, 0); \
309  typedef Function2<scalar> scalarFunction2; \
310  addToRunTimeSelectionTable(scalarFunction2, SS, dictionary)
311 
312 
313 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
314 
315 #ifdef NoRepository
316  #include "Function2.C"
317  #include "Constant2.H"
318 #endif
319 
320 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
321 
322 #endif
323 
324 // ************************************************************************* //
scalar y
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:62
virtual void write(Ostream &os, const Function2s::unitSets &) const =0
Write in dictionary format.
virtual tmp< Function2< Type > > clone() const =0
Construct and return a clone.
virtual ~Function2()
Destructor.
Definition: Function2.C:66
static autoPtr< Function2< Type > > New(const word &name, const Function2s::unitSets &units, const dictionary &dict)
Select from dictionary.
Definition: Function2New.C:32
Function2(const word &name)
Construct from name.
Definition: Function2.C:31
void operator=(const Function2< Type > &)
Assignment.
Definition: Function2.C:106
declareRunTimeSelectionTable(autoPtr, Function2, dictionary,(const word &name, const Function2s::unitSets &units, const dictionary &dict),(name, units, dict))
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:69
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 keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
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...
Definition: unitSet.H:68
A class for handling words, derived from string.
Definition: word.H:63
Namespace for OpenFOAM.
void func(FieldField< Field, Type > &f, const FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
void writeEntry(Ostream &os, const word &key, const DimensionedFieldFunction< DimensionedFieldType > &f)
Macros to ease declaration of run-time selection tables.
dictionary dict