dimensionSetIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "dimensionSet.H"
27 #include "symbols.H"
28 
29 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
30 
31 void Foam::dimensionSet::round(const scalar tol)
32 {
33  for (int i=0; i<dimensionSet::nDimensions; ++i)
34  {
35  scalar integralPart;
36  scalar fractionalPart = std::modf(exponents_[i], &integralPart);
37 
38  if (mag(fractionalPart - 1.0) <= tol)
39  {
40  exponents_[i] = 1.0 + integralPart;
41  }
42  else if (mag(fractionalPart + 1.0) <= tol)
43  {
44  exponents_[i] = -1.0 + integralPart;
45  }
46  else if (mag(fractionalPart) <= tol)
47  {
48  exponents_[i] = integralPart;
49  }
50  }
51 }
52 
53 
54 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
55 
57 {
58  is >> *this;
59 }
60 
61 
62 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
63 
65 {
66  token nextToken;
67 
68  // Peek at the next token
69  is >> nextToken;
70  is.putBack(nextToken);
71 
72  // If not a number, then these are named dimensions. Parse.
73  if (!nextToken.isNumber())
74  {
76 
77  return is;
78  }
79 
80  // Otherwise these are numbered dimensions. Read directly...
81 
82  // Read first five dimensions
83  for (int i=0; i<dimensionSet::CURRENT; i++)
84  {
85  is >> exponents_[i];
86  }
87 
88  // Peek at the next token
89  is >> nextToken;
90  is.putBack(nextToken);
91 
92  // If next token is another number then read the last two dimensions
93  // and then read another token for the end of the dimensionSet
94  if (nextToken.isNumber())
95  {
96  is >> exponents_[dimensionSet::CURRENT];
97  is >> exponents_[dimensionSet::LUMINOUS_INTENSITY];
98  }
99  else
100  {
101  exponents_[dimensionSet::CURRENT] = 0;
102  exponents_[dimensionSet::LUMINOUS_INTENSITY] = 0;
103  }
104 
105  return is;
106 }
107 
108 
110 {
111  // Read beginning of dimensionSet
112  token startToken(is);
113  if (startToken != token::BEGIN_SQR)
114  {
116  << "expected a " << token::BEGIN_SQR << " in dimensionSet"
117  << endl << "in stream " << is.info()
118  << exit(FatalIOError);
119  }
120 
121  readNoBeginOrEnd(is);
122 
123  // Read end of dimensionSet
124  token endToken(is);
125  if (endToken != token::END_SQR)
126  {
128  << "expected a " << token::END_SQR << " in dimensionSet "
129  << endl << "in stream " << is.info()
130  << exit(FatalIOError);
131  }
132 
133  // Check state of Istream
134  is.check("Istream& operator>>(Istream&, dimensionSet&)");
135 
136  return is;
137 }
138 
139 
141 {
142  if (dimensionless())
143  {
144  // Do nothing
145  }
146  else
147  {
148  for (int i=0; i<dimensionSet::nDimensions-1; i++)
149  {
150  os << exponents_[i] << token::SPACE;
151  }
152 
153  os << exponents_[dimensionSet::nDimensions-1];
154  }
155 
156  return os;
157 }
158 
159 
161 {
162  os << token::BEGIN_SQR;
163 
164  writeNoBeginOrEnd(os);
165 
166  os << token::END_SQR;
167 
168  // Check state of Ostream
169  os.check("Ostream& operator<<(Ostream&, const dimensionSet&)");
170 
171  return os;
172 }
173 
174 
176 {
177  for (int first=true, i=0; i<dimensionSet::nDimensions; i++)
178  {
179  if (mag(exponents_[i]) > dimensionSet::smallExponent)
180  {
181  if (!first)
182  {
183  os << token::SPACE;
184  }
185 
187  [static_cast<dimensionSet::dimensionType>(i)];
188 
189  if (exponents_[i] != 1)
190  {
191  os << '^' << exponents_[i];
192  }
193 
194  first = false;
195  }
196  }
197 
198  return os;
199 }
200 
201 
202 void Foam::writeEntry(Ostream& os, const dimensionSet& value)
203 {
204  os << value;
205 }
206 
207 
208 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
209 
211 {
212  dims.read(is);
213 
214  // Check state of Istream
215  is.check("Istream& operator>>(Istream&, dimensionSet&)");
216 
217  return is;
218 }
219 
220 
222 {
223  dims.write(os);
224 
225  // Check state of Ostream
226  os.check("Ostream& operator<<(Ostream&, const dimensionSet&)");
227 
228  return os;
229 }
230 
231 
233 {
234  os << token::BEGIN_SQR;
235 
236  ip.t_.writeInfoNoBeginOrEnd(os);
237 
238  os << token::END_SQR;
239 
240  // Check state of Ostream
241  os.check("Ostream& operator<<(Ostream&, const InfoProxy<dimensionSet>&)");
242 
243  return os;
244 }
245 
246 
247 // ************************************************************************* //
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:543
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:50
const T & t_
Definition: InfoProxy.H:53
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
void putBack(const token &)
Put back token.
Definition: Istream.C:30
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Dimension set for the base types.
Definition: dimensionSet.H:125
Ostream & writeInfoNoBeginOrEnd(Ostream &os) const
Write info without the square brackets.
dimensionSet(const scalar mass, const scalar length, const scalar time, const scalar temperature, const scalar moles, const scalar current, const scalar luminousIntensity)
Construct given individual dimension exponents for all.
Definition: dimensionSet.C:60
static const scalar smallExponent
A small exponent with which to perform inexact comparisons.
Definition: dimensionSet.H:156
Ostream & writeNoBeginOrEnd(Ostream &os) const
Write without the square brackets.
Ostream & write(Ostream &os) const
Write.
friend dimensionSet mag(const dimensionSet &)
static const NamedEnum< dimensionType, 7 > dimensionTypeNames_
Names of the dimensions.
Definition: dimensionSet.H:150
Istream & readNoBeginOrEnd(Istream &is)
Read without the square brackets.
dimensionType
Define an enumeration for the names of the dimension exponents.
Definition: dimensionSet.H:139
Istream & read(Istream &is)
Read.
A token holds items read from Istream.
Definition: token.H:73
bool isNumber() const
Definition: tokenI.H:745
@ BEGIN_SQR
Definition: token.H:111
@ END_SQR
Definition: token.H:112
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
Type parseNoBeginOrEnd(Istream &is, const Type &identity, const HashTable< Type > &table)
Parse tokens into a dimension set or unit conversion, assuming that the '['.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const HashTable< dimensionSet > & dimensions()
Get the table of dimension sets.
Definition: dimensionSets.C:96
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
const dimensionSet dimless
void mag(LagrangianPatchField< scalar > &f, const LagrangianPatchField< Type > &f1)
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:39
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
Istream & operator>>(Istream &, pistonPointEdgeData &)
IOerror FatalIOError
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)