Loading...
Searching...
No Matches
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 | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2017-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27Typedef
28 Foam::scalar
29
30Description
31 A floating-point number identical to float or double depending on
32 whether WM_SP, WM_SPDP or WM_DP is defined.
33
34SourceFiles
35 scalar.cxx
36
37\*---------------------------------------------------------------------------*/
38
39#ifndef Foam_scalar_H
40#define Foam_scalar_H
41
42#include "floatScalar.H"
43#include "doubleScalar.H"
44
45// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46
47// Typedefs (floatScalar, doubleScalar, scalar, solveScalar) in scalarFwd.H
48
49#if defined(WM_SP) || defined(WM_SPDP)
50
51// With scalar == (float), solveScalar == (float | double)
52
53namespace Foam
54{
55 constexpr scalar GREAT = floatScalarGREAT;
56 constexpr scalar ROOTGREAT = floatScalarROOTGREAT;
57 constexpr scalar VGREAT = floatScalarVGREAT;
58 constexpr scalar ROOTVGREAT = floatScalarROOTVGREAT;
59 constexpr scalar SMALL = floatScalarSMALL;
60 constexpr scalar ROOTSMALL = floatScalarROOTSMALL;
61 constexpr scalar VSMALL = floatScalarVSMALL;
62 constexpr scalar ROOTVSMALL = floatScalarROOTVSMALL;
63
64 #ifdef COMPAT_OPENFOAM_ORG
66 // Accommodate name changes from 2018-01
67 constexpr scalar great = floatScalarGREAT;
68 constexpr scalar rootGreat = floatScalarROOTGREAT;
69 constexpr scalar vGreat = floatScalarVGREAT;
70 constexpr scalar rootVGreat = floatScalarROOTVGREAT;
71 constexpr scalar small = floatScalarSMALL;
72 constexpr scalar rootSmall = floatScalarROOTSMALL;
73 constexpr scalar vSmall = floatScalarVSMALL;
74 constexpr scalar rootVSmall = floatScalarROOTVSMALL;
76 #endif
77
78
79 //- Read scalar from c-string and return value
80 inline scalar readScalar(const char* buf)
81 {
82 return readFloat(buf);
83 }
84
85 //- Read scalar from c-string into argument. Return true on success.
86 inline bool readScalar(const char* buf, scalar& val)
87 {
88 return readFloat(buf, val);
89 }
90
91 //- Read scalar from string and return value
92 inline scalar readScalar(const std::string& str)
93 {
94 return readFloat(str);
95 }
96
97 //- Read scalar from string into argument. Return true on success.
98 inline bool readScalar(const std::string& str, scalar& val)
99 {
100 return readFloat(str, val);
101 }
102
103 //- Read scalar from stream and return value
104 inline scalar readScalar(Istream& is)
105 {
106 return readFloat(is);
107 }
108
109 //- Read scalar from stream if present or return default value
110 scalar readScalarOrDefault(Istream& is, const scalar defaultValue);
111
112 //- Read raw scalar(s) from binary stream.
113 // \note No internal check for binary vs ascii,
114 // the caller knows what they are doing
115 void readRawScalar(Istream& is, scalar* data, size_t nElem = 1);
116
117 //- Read raw scalar from binary stream.
118 // \note No internal check for binary vs ascii,
119 // the caller knows what they are doing
120 inline void readRawScalar(Istream& is, scalar& val)
121 {
122 readRawScalar(is, &val, 1);
123 }
124
125 //- Read raw scalar from binary stream and return value.
126 // \note No internal check for binary vs ascii,
127 // the caller knows what they are doing
128 inline scalar readRawScalar(Istream& is)
129 {
130 scalar val(0);
131 readRawScalar(is, &val, 1);
132 return val;
133 }
134}
135
136#elif defined(WM_DP)
137
138// With scalar == (double), solveScalar == (double)
139
140namespace Foam
141{
142 constexpr scalar GREAT = doubleScalarGREAT;
143 constexpr scalar ROOTGREAT = doubleScalarROOTGREAT;
144 constexpr scalar VGREAT = doubleScalarVGREAT;
145 constexpr scalar ROOTVGREAT = doubleScalarROOTVGREAT;
146 constexpr scalar SMALL = doubleScalarSMALL;
147 constexpr scalar ROOTSMALL = doubleScalarROOTSMALL;
148 constexpr scalar VSMALL = doubleScalarVSMALL;
149 constexpr scalar ROOTVSMALL = doubleScalarROOTVSMALL;
150
151 #ifdef COMPAT_OPENFOAM_ORG
153 // Accommodate name changes from 2018-01
154 constexpr scalar great = doubleScalarGREAT;
155 constexpr scalar rootGreat = doubleScalarROOTGREAT;
156 constexpr scalar vGreat = doubleScalarVGREAT;
157 constexpr scalar rootVGreat = doubleScalarROOTVGREAT;
158 constexpr scalar small = doubleScalarSMALL;
159 constexpr scalar rootSmall = doubleScalarROOTSMALL;
160 constexpr scalar vSmall = doubleScalarVSMALL;
161 constexpr scalar rootVSmall = doubleScalarROOTVSMALL;
163 #endif
164
165
166 //- Read scalar from c-string and return value
167 inline scalar readScalar(const char* buf)
168 {
169 return readDouble(buf);
170 }
171
172 //- Read scalar from c-string into argument. Return true on success.
173 inline bool readScalar(const char* buf, scalar& val)
174 {
175 return readDouble(buf, val);
176 }
177
178 //- Read scalar from string and return value
179 inline scalar readScalar(const std::string& str)
180 {
181 return readDouble(str);
182 }
183
184 //- Read scalar from string into argument. Return true on success.
185 inline bool readScalar(const std::string& str, scalar& val)
186 {
187 return readDouble(str, val);
188 }
189
190 //- Read scalar from stream and return value
191 inline scalar readScalar(Istream& is)
192 {
193 return readDouble(is);
194 }
195
196 //- Read scalar from stream if present or return default value
197 scalar readScalarOrDefault(Istream& is, const scalar defaultValue);
198
199 //- Read raw scalar(s) from binary stream.
200 // \note No internal check for binary vs ascii,
201 // the caller knows what they are doing
202 void readRawScalar(Istream& is, scalar* data, size_t nElem = 1);
203
204 //- Read raw scalar from binary stream.
205 // \note No internal check for binary vs ascii,
206 // the caller knows what they are doing
207 inline void readRawScalar(Istream& is, scalar& val)
208 {
209 readRawScalar(is, &val, 1);
210 }
211
212 //- Read raw scalar from binary stream and return value.
213 // \note No internal check for binary vs ascii,
214 // the caller knows what they are doing
215 inline scalar readRawScalar(Istream& is)
216 {
217 scalar val(0);
218 readRawScalar(is, &val, 1);
219 return val;
220 }
221}
222
223#else
224// #error "PRECISION must be set to WM_SP, WM_SPDP or WM_DP"
225#endif
226
227// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
228
229// Type conversions (narrowing)
230
231namespace Foam
232{
233
234//- Type narrowing from double to float
235// Underflow: round to zero.
236// Overflow: fix silently.
237inline constexpr float narrowFloat(const double val) noexcept
238{
239 return
240 (
243 : (val > -floatScalarVSMALL && val < floatScalarVSMALL) ? 0
244 : static_cast<float>(val)
245 );
246}
247
248} // End namespace Foam
249
250
251// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
252
253namespace Foam
254{
255 //- Type to use for extended precision
256 template<>
257 class typeOfSolve<scalar>
258 {
259 public:
260
261 typedef solveScalar type;
262 };
263}
264
265// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
266
267#endif
268
269// ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
The extended precision type (solveScalar for float).
Definition products.H:81
Namespace for OpenFOAM.
constexpr floatScalar floatScalarVGREAT
Definition floatScalar.H:56
constexpr doubleScalar doubleScalarROOTVGREAT
constexpr floatScalar floatScalarGREAT
Definition floatScalar.H:54
constexpr floatScalar floatScalarROOTVGREAT
Definition floatScalar.H:57
constexpr floatScalar floatScalarROOTVSMALL
Definition floatScalar.H:61
constexpr floatScalar floatScalarSMALL
Definition floatScalar.H:58
constexpr doubleScalar doubleScalarROOTSMALL
constexpr doubleScalar doubleScalarVGREAT
constexpr floatScalar floatScalarROOTSMALL
Definition floatScalar.H:59
constexpr doubleScalar doubleScalarGREAT
constexpr floatScalar floatScalarVSMALL
Definition floatScalar.H:60
constexpr floatScalar floatScalarROOTGREAT
Definition floatScalar.H:55
constexpr doubleScalar doubleScalarROOTGREAT
constexpr doubleScalar doubleScalarVSMALL
constexpr doubleScalar doubleScalarSMALL
constexpr doubleScalar doubleScalarROOTVSMALL
constexpr float narrowFloat(const double val) noexcept
Type narrowing from double to float.
Definition scalar.H:278