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::readRawScalar(Istream& is)
44 {
45  scalar val(0);
46  readRawScalar(is, &val, 1);
47  return val;
48 }
49 
50 
51 void Foam::readRawScalar(Istream& is, scalar* data, size_t nElem)
52 {
53  // No check for binary vs ascii, the caller knows what they are doing
54 
55  #if defined(WM_SP) || defined(WM_SPDP)
56 
57  // Defined scalar as a float, non-native type is double
58  // Handle type narrowing limits
59 
60  typedef double nonNative;
61 
62  if (is.checkScalarSize<nonNative>())
63  {
64  nonNative other;
65 
66  for (const scalar* endData = data + nElem; data != endData; ++data)
67  {
68  is.readRaw(reinterpret_cast<char*>(&other), sizeof(nonNative));
69 
70  // Type narrowing
71  // Overflow: silently fix, or raise error?
72 
73  if (other < -VGREAT)
74  {
75  *data = -VGREAT;
76  }
77  else if (other > VGREAT)
78  {
79  *data = VGREAT;
80  }
81  else if (other > -VSMALL && other < VSMALL)
82  {
83  // Underflow: round to zero
84  *data = 0;
85  }
86  else
87  {
88  *data = scalar(other);
89  }
90  }
91  }
92  else
93  {
94  // Read with native size
95  is.readRaw(reinterpret_cast<char*>(data), nElem*sizeof(scalar));
96  }
97 
98  #elif defined(WM_DP)
99 
100  // Defined scalar as a double, non-native type is float
101 
102  typedef float nonNative;
103 
104  if (is.checkScalarSize<nonNative>())
105  {
106  nonNative other;
107 
108  for (const scalar* endData = data + nElem; data != endData; ++data)
109  {
110  is.readRaw(reinterpret_cast<char*>(&other), sizeof(nonNative));
111 
112  *data = scalar(other);
113  }
114  }
115  else
116  {
117  // Read with native size
118  is.readRaw(reinterpret_cast<char*>(data), nElem*sizeof(scalar));
119  }
120 
121  #endif
122 }
123 
124 
125 // ************************************************************************* //
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
scalar.H