Function2.C
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 \*---------------------------------------------------------------------------*/
25 
26 #include "Function2.H"
27 
28 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
29 
30 template<class Type>
32 :
33  name_(name)
34 {}
35 
36 
37 template<class Type>
39 :
40  tmp<Function2<Type>>::refCount(),
41  name_(f2.name_)
42 {}
43 
44 
45 template<class Type, class Function2Type>
47 :
48  Function2<Type>(name)
49 {}
50 
51 
52 template<class Type, class Function2Type>
55 {
56  return tmp<Function2<Type>>
57  (
58  new Function2Type(static_cast<const Function2Type&>(*this))
59  );
60 }
61 
62 
63 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
64 
65 template<class Type>
67 {}
68 
69 
70 template<class Type, class Function2Type>
72 {}
73 
74 
75 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
76 
77 template<class Type>
79 {
80  return name_;
81 }
82 
83 
84 template<class Type, class Function2Type>
86 (
87  const scalarField& x,
88  const scalarField& y
89 ) const
90 {
91  tmp<Field<Type>> tfld(new Field<Type>(x.size()));
92  Field<Type>& fld = tfld.ref();
93 
94  forAll(x, i)
95  {
96  fld[i] = static_cast<const Function2Type&>(*this).value(x[i], y[i]);
97  }
98 
99  return tfld;
100 }
101 
102 
103 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
104 
105 template<class Type>
107 {
108  if (this == &f)
109  {
111  << "attempted assignment to self"
112  << abort(FatalError);
113  }
114 }
115 
116 
117 // * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * //
118 
119 template<class Type>
121 {
122  writeEntry(os, {unitAny, unitAny, unitAny}, f2);
123 }
124 
125 
126 template<class Type>
128 (
129  Ostream& os,
131  const Function2<Type>& f2
132 )
133 {
134  writeKeyword(os, f2.name())
135  << nl << indent << token::BEGIN_BLOCK << nl << incrIndent;
136 
137  writeEntry(os, "type", f2.type());
138 
139  f2.write(os, units);
140 
141  os << decrIndent << indent << token::END_BLOCK << endl;
142 }
143 
144 
145 template<class Type>
147 (
148  Ostream& os,
149  const unitConversion& xUnits,
150  const unitConversion& yUnits,
151  const unitConversion& valueUnits,
152  const Function2<Type>& f2
153 )
154 {
155  writeEntry(os, {xUnits, yUnits, valueUnits}, f2);
156 }
157 
158 
159 // * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * //
160 
161 template<class Type>
162 Foam::Ostream& Foam::operator<<
163 (
164  Ostream& os,
165  const Function2<Type>& f2
166 )
167 {
168  f2.write(os);
169 
170  // Check state of Ostream
171  os.check("Ostream& operator<<(Ostream&, const Function2<Type>&)");
172 
173  return os;
174 }
175 
176 
177 // ************************************************************************* //
scalar y
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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.
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
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
virtual Ostream & write(const char)=0
Write character.
Reference counter for various OpenFOAM components.
Definition: refCount.H:50
A class for managing temporary objects.
Definition: tmp.H:55
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:181
@ BEGIN_BLOCK
Definition: token.H:113
@ END_BLOCK
Definition: token.H:114
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
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
gmvFile<< "tracers "<< particles.size()<< nl;{ pointField positions(particles.size());label particlei=0;forAllConstIter(Cloud< passiveParticle >, particles, iter) { positions[particlei++]=iter().position(mesh);} for(i=0;i< pTraits< point >::nComponents;i++) { forAll(positions, particlei) { gmvFile<< component(positions[particlei], i)<< ' ';} gmvFile<< nl;}}forAll(lagrangianScalarNames, i){ const word &name=lagrangianScalarNames[i];IOField< scalar > fld(IOobject(name, runTime.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:241
const unitConversion unitAny
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:234
const HashTable< unitConversion > & units()
Get the table of unit conversions.
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
error FatalError
Ostream & writeKeyword(Foam::Ostream &os, const keyType &kw)
Write the keyword to the Ostream with the current level of indentation.
Definition: keyType.C:155
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:227
static const char nl
Definition: Ostream.H:266
labelList f(nPoints)