FreeFOAM The Cross-Platform CFD Toolkit
Scalar_.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 | Copyright (C) 1991-2010 OpenCFD Ltd.
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 Typedef
25  Foam::Scalar
26 
27 Description
28  Single floating point number
29 
30 SourceFiles
31  Scalar.C
32 
33 \*---------------------------------------------------------------------------*/
34 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 
37 // template specialisation for pTraits<Scalar>
38 template<>
39 class pTraits<Scalar>
40 {
41  Scalar p_;
42 
43 public:
44 
45  //- Component type
46  typedef Scalar cmptType;
47 
48  // Member constants
49 
50  enum
51  {
52  dim = 3, // Dimensionality of space
53  rank = 0, // Rank of Scalar is 0
54  nComponents = 1 // Number of components in Scalar is 1
55  };
56 
57 
58  // Static data members
59 
60  static const char* const typeName;
61  static const char* componentNames[];
62  static const Scalar zero;
63  static const Scalar one;
64  static const Scalar max;
65  static const Scalar min;
66 
67 
68  // Constructors
69 
70  //- Construct from Scalar
71  pTraits(const Scalar s)
72  {
73  p_ = s;
74  }
75 
76  //- Construct from Istream
77  pTraits(Istream&);
78 
79 
80  // Member operators
81 
82  operator Scalar() const
83  {
84  return p_;
85  }
86 
87  operator Scalar&()
88  {
89  return p_;
90  }
91 };
92 
93 
94 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
95 
96 //- Return a string representation of a Scalar
97 word name(const Scalar);
98 
99 
100 inline Scalar& setComponent(Scalar& s, const direction)
101 {
102  return s;
103 }
105 inline Scalar component(const Scalar s, const direction)
106 {
107  return s;
108 }
110 inline Scalar sign(const Scalar s)
111 {
112  return (s >= 0)? 1: -1;
113 }
115 inline Scalar pos(const Scalar s)
116 {
117  return (s >= 0)? 1: 0;
118 }
120 inline Scalar neg(const Scalar s)
121 {
122  return (s < 0)? 1: 0;
123 }
125 inline bool equal(const Scalar& s1, const Scalar& s2)
126 {
127  return mag(s1 - s2) <= ScalarVSMALL;
128 }
130 inline bool notEqual(const Scalar s1, const Scalar s2)
131 {
132  return mag(s1 - s2) > ScalarVSMALL;
133 }
135 inline Scalar limit(const Scalar s1, const Scalar s2)
136 {
137  return (mag(s1) < mag(s2)) ? s1: 0.0;
138 }
140 inline Scalar minMod(const Scalar s1, const Scalar s2)
141 {
142  return (mag(s1) < mag(s2)) ? s1: s2;
143 }
145 inline Scalar magSqr(const Scalar s)
146 {
147  return s*s;
148 }
150 inline Scalar sqr(const Scalar s)
151 {
152  return s*s;
153 }
155 inline Scalar sqrtSumSqr(const Scalar a, const Scalar b)
156 {
157  Scalar maga = mag(a);
158  Scalar magb = mag(b);
159 
160  if (maga > magb)
161  {
162  return maga*sqrt(1.0 + sqr(magb/maga));
163  }
164  else
165  {
166  return magb < ScalarVSMALL ? 0.0 : magb*sqrt(1.0 + sqr(maga/magb));
167  }
168 }
170 inline Scalar pow3(const Scalar s)
171 {
172  return s*sqr(s);
173 }
175 inline Scalar pow4(const Scalar s)
176 {
177  return sqr(sqr(s));
178 }
180 inline Scalar pow5(const Scalar s)
181 {
182  return s*pow4(s);
183 }
185 inline Scalar pow6(const Scalar s)
186 {
187  return pow3(sqr(s));
188 }
190 inline Scalar inv(const Scalar s)
191 {
192  return 1.0/s;
193 }
195 inline Scalar dot(const Scalar s1, const Scalar s2)
196 {
197  return s1*s2;
198 }
200 inline Scalar cmptMultiply(const Scalar s1, const Scalar s2)
201 {
202  return s1*s2;
203 }
205 inline Scalar cmptDivide(const Scalar s1, const Scalar s2)
206 {
207  return s1/s2;
208 }
210 inline Scalar cmptMax(const Scalar s)
211 {
212  return s;
213 }
215 inline Scalar cmptMin(const Scalar s)
216 {
217  return s;
218 }
220 inline Scalar cmptAv(const Scalar s)
221 {
222  return s;
223 }
225 inline Scalar cmptMag(const Scalar s)
226 {
227  return mag(s);
228 }
229 
230 
231 // Standard C++ transcendental functions
234 transFunc(exp)
235 transFunc(log)
237 transFunc(sin)
238 transFunc(cos)
239 transFunc(tan)
249 
250 // Standard ANSI-C (but not in <cmath>) transcendental functions
251 
252 transFunc(erf)
255 
256 transFunc(j0)
257 transFunc(j1)
258 
259 transFunc(y0)
260 transFunc(y1)
261 
262 
263 // Stabilisation around zero for division
264 inline Scalar stabilise(const Scalar s, const Scalar small)
265 {
266  if (s >= 0)
267  {
268  return s + small;
269  }
270  else
271  {
272  return s - small;
273  }
274 }
275 
276 
277 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
278 
279 Scalar readScalar(Istream&);
280 Istream& operator>>(Istream&, Scalar&);
281 Ostream& operator<<(Ostream&, const Scalar);
282 
283 
284 // ************************ vim: set sw=4 sts=4 et: ************************ //