Added constructor from the primitive.
[OpenFOAM-1.6.x.git] / src / OpenFOAM / primitives / Scalar / Scalar.H
blobb464e7fde78e6834e7fb227579cc62ec2558d3cf
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Typedef
26     Foam::Scalar
28 Description
29     Single floating point number
31 SourceFiles
32     Scalar.C
34 \*---------------------------------------------------------------------------*/
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 // template specialisation for pTraits<Scalar>
39 template<>
40 class pTraits<Scalar>
42     Scalar p_;
44 public:
46     //- Component type
47     typedef Scalar cmptType;
49     // Member constants
51         enum
52         {
53             dim = 3,         // Dimensionality of space
54             rank = 0,        // Rank of Scalar is 0
55             nComponents = 1  // Number of components in Scalar is 1
56         };
58     // Static data members
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;
67     // Constructors
69         //- Construct from Scalar
70         pTraits(const Scalar s)
71         {
72             p_ = s;
73         }
75         //- Construct from Istream
76         pTraits(Istream&);
79     // Member Functions
81         operator Scalar() const
82         {
83             return p_;
84         }
88 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
90 //- Return a string representation of a Scalar
91 word name(const Scalar);
94 inline Scalar& setComponent(Scalar& s, const direction)
96     return s;
99 inline Scalar component(const Scalar s, const direction)
101     return s;
104 inline Scalar sign(const Scalar s)
106     return (s >= 0)? 1: -1;
109 inline Scalar pos(const Scalar s)
111     return (s >= 0)? 1: 0;
114 inline Scalar neg(const Scalar s)
116     return (s < 0)? 1: 0;
119 inline bool equal(const Scalar& s1, const Scalar& s2)
121     return mag(s1 - s2) <= ScalarVSMALL;
124 inline bool notEqual(const Scalar s1, const Scalar s2)
126     return mag(s1 - s2) > ScalarVSMALL;
129 inline Scalar limit(const Scalar s1, const Scalar s2)
131     return (mag(s1) < mag(s2)) ? s1: 0.0;
134 inline Scalar minMod(const Scalar s1, const Scalar s2)
136     return (mag(s1) < mag(s2)) ? s1: s2;
139 inline Scalar magSqr(const Scalar s)
141     return s*s;
144 inline Scalar sqr(const Scalar s)
146     return s*s;
149 inline Scalar sqrtSumSqr(const Scalar a, const Scalar b)
151     Scalar maga = mag(a);
152     Scalar magb = mag(b);
154     if (maga > magb)
155     {
156         return maga*sqrt(1.0 + sqr(magb/maga));
157     }
158     else
159     {
160         return magb < ScalarVSMALL ? 0.0 : magb*sqrt(1.0 + sqr(maga/magb));
161     }
164 inline Scalar pow3(const Scalar s)
166     return s*sqr(s);
169 inline Scalar pow4(const Scalar s)
171     return sqr(sqr(s));
174 inline Scalar pow5(const Scalar s)
176     return s*pow4(s);
179 inline Scalar pow6(const Scalar s)
181     return pow3(sqr(s));
184 inline Scalar inv(const Scalar s)
186     return 1.0/s;
189 inline Scalar dot(const Scalar s1, const Scalar s2)
191     return s1*s2;
194 inline Scalar cmptMultiply(const Scalar s1, const Scalar s2)
196     return s1*s2;
199 inline Scalar cmptDivide(const Scalar s1, const Scalar s2)
201     return s1/s2;
204 inline Scalar cmptMax(const Scalar s)
206     return s;
209 inline Scalar cmptMin(const Scalar s)
211     return s;
214 inline Scalar cmptAv(const Scalar s)
216     return s;
219 inline Scalar cmptMag(const Scalar s)
221     return mag(s);
225 // Standard C++ transcendental functions
226 transFunc(sqrt)
227 transFunc(cbrt)
228 transFunc(exp)
229 transFunc(log)
230 transFunc(log10)
231 transFunc(sin)
232 transFunc(cos)
233 transFunc(tan)
234 transFunc(asin)
235 transFunc(acos)
236 transFunc(atan)
237 transFunc(sinh)
238 transFunc(cosh)
239 transFunc(tanh)
240 transFunc(asinh)
241 transFunc(acosh)
242 transFunc(atanh)
244 // Standard ANSI-C (but not in <cmath>) transcendental functions
246 transFunc(erf)
247 transFunc(erfc)
248 transFunc(lgamma)
250 transFunc(j0)
251 transFunc(j1)
253 transFunc(y0)
254 transFunc(y1)
257 // Stabilisation around zero for division
258 inline Scalar stabilise(const Scalar s, const Scalar small)
260     if (s >= 0)
261     {
262         return s + small;
263     }
264     else
265     {
266         return s - small;
267     }
271 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
273 Scalar readScalar(Istream&);
274 Istream& operator>>(Istream&, Scalar&);
275 Ostream& operator<<(Ostream&, const Scalar);
278 // ************************************************************************* //