pTraits: Added non-const access function to the value.
[OpenFOAM-1.6.x.git] / src / OpenFOAM / primitives / Scalar / Scalar.H
blob58801f23b8bc6c0deb71dfdbee159f3f9a203c06
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.
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         };
59     // Static data members
61         static const char* const typeName;
62         static const char* componentNames[];
63         static const Scalar zero;
64         static const Scalar one;
65         static const Scalar max;
66         static const Scalar min;
69     // Constructors
71         //- Construct from Scalar
72         pTraits(const Scalar s)
73         {
74             p_ = s;
75         }
77         //- Construct from Istream
78         pTraits(Istream&);
81     // Member operators
83         operator Scalar() const
84         {
85             return p_;
86         }
88         operator Scalar&()
89         {
90             return p_;
91         }
95 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
97 //- Return a string representation of a Scalar
98 word name(const Scalar);
101 inline Scalar& setComponent(Scalar& s, const direction)
103     return s;
106 inline Scalar component(const Scalar s, const direction)
108     return s;
111 inline Scalar sign(const Scalar s)
113     return (s >= 0)? 1: -1;
116 inline Scalar pos(const Scalar s)
118     return (s >= 0)? 1: 0;
121 inline Scalar neg(const Scalar s)
123     return (s < 0)? 1: 0;
126 inline bool equal(const Scalar& s1, const Scalar& s2)
128     return mag(s1 - s2) <= ScalarVSMALL;
131 inline bool notEqual(const Scalar s1, const Scalar s2)
133     return mag(s1 - s2) > ScalarVSMALL;
136 inline Scalar limit(const Scalar s1, const Scalar s2)
138     return (mag(s1) < mag(s2)) ? s1: 0.0;
141 inline Scalar minMod(const Scalar s1, const Scalar s2)
143     return (mag(s1) < mag(s2)) ? s1: s2;
146 inline Scalar magSqr(const Scalar s)
148     return s*s;
151 inline Scalar sqr(const Scalar s)
153     return s*s;
156 inline Scalar sqrtSumSqr(const Scalar a, const Scalar b)
158     Scalar maga = mag(a);
159     Scalar magb = mag(b);
161     if (maga > magb)
162     {
163         return maga*sqrt(1.0 + sqr(magb/maga));
164     }
165     else
166     {
167         return magb < ScalarVSMALL ? 0.0 : magb*sqrt(1.0 + sqr(maga/magb));
168     }
171 inline Scalar pow3(const Scalar s)
173     return s*sqr(s);
176 inline Scalar pow4(const Scalar s)
178     return sqr(sqr(s));
181 inline Scalar pow5(const Scalar s)
183     return s*pow4(s);
186 inline Scalar pow6(const Scalar s)
188     return pow3(sqr(s));
191 inline Scalar inv(const Scalar s)
193     return 1.0/s;
196 inline Scalar dot(const Scalar s1, const Scalar s2)
198     return s1*s2;
201 inline Scalar cmptMultiply(const Scalar s1, const Scalar s2)
203     return s1*s2;
206 inline Scalar cmptDivide(const Scalar s1, const Scalar s2)
208     return s1/s2;
211 inline Scalar cmptMax(const Scalar s)
213     return s;
216 inline Scalar cmptMin(const Scalar s)
218     return s;
221 inline Scalar cmptAv(const Scalar s)
223     return s;
226 inline Scalar cmptMag(const Scalar s)
228     return mag(s);
232 // Standard C++ transcendental functions
233 transFunc(sqrt)
234 transFunc(cbrt)
235 transFunc(exp)
236 transFunc(log)
237 transFunc(log10)
238 transFunc(sin)
239 transFunc(cos)
240 transFunc(tan)
241 transFunc(asin)
242 transFunc(acos)
243 transFunc(atan)
244 transFunc(sinh)
245 transFunc(cosh)
246 transFunc(tanh)
247 transFunc(asinh)
248 transFunc(acosh)
249 transFunc(atanh)
251 // Standard ANSI-C (but not in <cmath>) transcendental functions
253 transFunc(erf)
254 transFunc(erfc)
255 transFunc(lgamma)
257 transFunc(j0)
258 transFunc(j1)
260 transFunc(y0)
261 transFunc(y1)
264 // Stabilisation around zero for division
265 inline Scalar stabilise(const Scalar s, const Scalar small)
267     if (s >= 0)
268     {
269         return s + small;
270     }
271     else
272     {
273         return s - small;
274     }
278 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
280 Scalar readScalar(Istream&);
281 Istream& operator>>(Istream&, Scalar&);
282 Ostream& operator<<(Ostream&, const Scalar);
285 // ************************************************************************* //