OpenFOAM logo
Open Source CFD Toolkit

Scalar.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2005 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Type
00026     Scalar
00027 
00028 Description
00029     Single floating point number
00030 
00031 SourceFiles
00032     Scalar.C
00033 
00034 \*---------------------------------------------------------------------------*/
00035 
00036 namespace Foam
00037 {
00038 
00039 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00040 
00041 // template specialisation for pTraits<Scalar>
00042 template<>
00043 class pTraits<Scalar>
00044 {
00045     Scalar p_;
00046 
00047 public:
00048 
00049     //- Component type
00050     typedef Scalar cmptType;
00051 
00052     // Member constants
00053 
00054         enum
00055         {
00056             dim = 3,         // Dimensionality of space
00057             rank = 0,        // Rank od Scalar is 0
00058             nComponents = 1  // Number of components in Scalar is 1
00059         };
00060 
00061     // Static data members
00062 
00063         static const char* const typeName;
00064         static const char* componentNames[];
00065         static const Scalar zero;
00066         static const Scalar one;
00067 
00068     // Constructors
00069 
00070         //- Construct from Istream
00071         pTraits(Istream& is);
00072 
00073     // Member Functions
00074 
00075         operator Scalar() const
00076         {
00077             return p_;
00078         }
00079 };
00080 
00081 
00082 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00083 
00084 // Return a string representation of a Scalar
00085 word name(const Scalar s);
00086 
00087 
00088 inline Scalar& setComponent(Scalar& s, const direction)
00089 {
00090     return s;
00091 }
00092 
00093 inline Scalar component(const Scalar s, const direction)
00094 {
00095     return s;
00096 }
00097 
00098 inline Scalar atan2(const Scalar y, const Scalar x)
00099 {
00100     return ::atan2(y, x);
00101 }
00102 
00103 inline Scalar mag(const Scalar s)
00104 {
00105     return ::fabs(s);
00106 }
00107 
00108 inline Scalar sign(const Scalar s)
00109 {
00110     return (s >= 0)? 1: -1;
00111 }
00112 
00113 inline Scalar pos(const Scalar s)
00114 {
00115     return (s >= 0)? 1: 0;
00116 }
00117 
00118 inline Scalar neg(const Scalar s)
00119 {
00120     return (s < 0)? 1: 0;
00121 }
00122 
00123 inline bool equal(const Scalar& s1, const Scalar& s2)
00124 {
00125     return mag(s1 - s2) <= ScalarVSMALL;
00126 }
00127 
00128 inline bool notEqual(const Scalar s1, const Scalar s2)
00129 {
00130     return mag(s1 - s2) > ScalarVSMALL;
00131 }
00132 
00133 inline Scalar limit(const Scalar s1, const Scalar s2)
00134 {
00135     return (mag(s1) < mag(s2))? s1: 0.0;
00136 }
00137 
00138 inline Scalar magSqr(const Scalar s)
00139 {
00140     return s*s;
00141 }
00142 
00143 inline Scalar sqr(const Scalar s)
00144 {
00145     return s*s;
00146 }
00147 
00148 inline Scalar pow3(const Scalar s)
00149 {
00150     return s*s*s;
00151 }
00152 
00153 inline Scalar pow4(const Scalar s)
00154 {
00155     return sqr(sqr(s));
00156 }
00157 
00158 inline Scalar cmptAv(const Scalar s)
00159 {
00160     return s;
00161 }
00162 
00163 inline Scalar cmptMag(const Scalar s)
00164 {
00165     return mag(s);
00166 }
00167 
00168 inline Scalar scale(const Scalar s, const Scalar d)
00169 {
00170     return s*d;
00171 }
00172 
00173 
00174 #define transFunc(func)            \
00175 inline Scalar func(const Scalar s) \
00176 {                                  \
00177     return ::func(s);              \
00178 }
00179 
00180 // Standard C++ transcendental functions
00181 transFunc(sqrt)
00182 transFunc(exp)
00183 transFunc(log)
00184 transFunc(log10)
00185 transFunc(sin)
00186 transFunc(cos)
00187 transFunc(tan)
00188 transFunc(asin)
00189 transFunc(acos)
00190 transFunc(atan)
00191 transFunc(sinh)
00192 transFunc(cosh)
00193 transFunc(tanh)
00194 transFunc(asinh)
00195 transFunc(acosh)
00196 transFunc(atanh)
00197 
00198 // Standard ANSI-C (but not in <cmath>) transcendental functions
00199 
00200 transFunc(erf)
00201 transFunc(erfc)
00202 transFunc(lgamma)
00203 
00204 transFunc(j0)
00205 transFunc(j1)
00206 
00207 inline Scalar jn(const int n, const Scalar s)
00208 {
00209     return ::jn(n, s);
00210 }
00211 
00212 transFunc(y0)
00213 transFunc(y1)
00214 
00215 inline Scalar yn(const int n, const Scalar s)
00216 {
00217     return ::yn(n, s);
00218 }
00219 
00220 #undef transFunc
00221 
00222 
00223 // Stabilisation around zero for division
00224 inline Scalar stabilise(const Scalar s, const Scalar small)
00225 {
00226     if (s >= 0)
00227     {
00228         return s + small;
00229     }
00230     else
00231     {
00232         return s - small;
00233     }
00234 }
00235 
00236 
00237 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
00238 
00239 Scalar readScalar(Istream& is);
00240 Istream& operator>>(Istream&, Scalar&);
00241 Ostream& operator<<(Ostream&, const Scalar);
00242 
00243 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00244 
00245 } // End namespace Foam
00246 
00247 // ************************************************************************* //
For further information go to www.openfoam.org