scalar.C
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 OpenFOAM Foundation
9  Copyright (C) 2017-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "scalar.H"
30 #include "IOstreams.H"
31 
32 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
33 
34 Foam::scalar Foam::readScalar(Istream& is)
35 {
36  scalar val(0);
37  is >> val;
38 
39  return val;
40 }
41 
42 
43 Foam::scalar Foam::readScalarOrDefault(Istream& is, const scalar defaultValue)
44 {
45  if (is.good())
46  {
47  token tok(is);
48 
49  if (tok.isNumber())
50  {
51  return tok.scalarToken();
52  }
53 
54  is.putBack(tok);
55  }
56 
57  return defaultValue;
58 }
59 
60 
61 Foam::scalar Foam::readRawScalar(Istream& is)
62 {
63  scalar val(0);
64  readRawScalar(is, &val, 1);
65  return val;
66 }
67 
68 
69 void Foam::readRawScalar(Istream& is, scalar* data, size_t nElem)
70 {
71  // No check for binary vs ascii, the caller knows what they are doing
72 
73  #if defined(WM_SP) || defined(WM_SPDP)
74 
75  // Defined scalar as a float, non-native type is double
76  // Handle type narrowing limits
77 
78  typedef double nonNative;
79 
80  if (is.checkScalarSize<nonNative>())
81  {
82  nonNative other;
83 
84  for (const scalar* endData = data + nElem; data != endData; ++data)
85  {
86  is.readRaw(reinterpret_cast<char*>(&other), sizeof(nonNative));
87 
88  // Type narrowing
89  // Overflow: silently fix, or raise error?
90 
91  if (other < -VGREAT)
92  {
93  *data = -VGREAT;
94  }
95  else if (other > VGREAT)
96  {
97  *data = VGREAT;
98  }
99  else if (other > -VSMALL && other < VSMALL)
100  {
101  // Underflow: round to zero
102  *data = 0;
103  }
104  else
105  {
106  *data = scalar(other);
107  }
108  }
109  }
110  else
111  {
112  // Read with native size
113  is.readRaw(reinterpret_cast<char*>(data), nElem*sizeof(scalar));
114  }
115 
116  #elif defined(WM_DP)
117 
118  // Defined scalar as a double, non-native type is float
119 
120  typedef float nonNative;
121 
122  if (is.checkScalarSize<nonNative>())
123  {
124  nonNative other;
125 
126  for (const scalar* endData = data + nElem; data != endData; ++data)
127  {
128  is.readRaw(reinterpret_cast<char*>(&other), sizeof(nonNative));
129 
130  *data = scalar(other);
131  }
132  }
133  else
134  {
135  // Read with native size
136  is.readRaw(reinterpret_cast<char*>(data), nElem*sizeof(scalar));
137  }
138 
139  #endif
140 }
141 
142 
143 // ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...