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  // Read next token
67  token nextToken(is);
68 
69  if (!nextToken.isNumber())
70  {
71  // Named dimensions. Parse.
72  is.putBack(nextToken);
74  }
75  else
76  {
77  // Numbered dimensions. Read directly.
78 
79  // Read first five dimensions
80  exponents_[dimensionSet::MASS] = nextToken.number();
81  for (int Dimension=1; Dimension<dimensionSet::CURRENT; Dimension++)
82  {
83  is >> exponents_[Dimension];
84  }
85 
86  // Read next token
87  token nextToken(is);
88 
89  // If next token is another number then read the last two dimensions
90  // and then read another token for the end of the dimensionSet
91  if (nextToken.isNumber())
92  {
93  exponents_[dimensionSet::CURRENT] = nextToken.number();
94  is >> nextToken;
95  exponents_[dimensionSet::LUMINOUS_INTENSITY] = nextToken.number();
96  is >> nextToken;
97  }
98  else
99  {
100  exponents_[dimensionSet::CURRENT] = 0;
101  exponents_[dimensionSet::LUMINOUS_INTENSITY] = 0;
102  }
103 
104  // Check end of dimensionSet
105  if (nextToken != token::END_SQR)
106  {
108  << "expected a " << token::END_SQR << " in dimensionSet "
109  << endl << "in stream " << is.info()
110  << exit(FatalIOError);
111  }
112  }
113 
114  // Check state of Istream
115  is.check("Istream& operator>>(Istream&, dimensionSet&)");
116 
117  return is;
118 }
119 
120 
122 {
123  // Read beginning of dimensionSet
124  token startToken(is);
125 
126  if (startToken != token::BEGIN_SQR)
127  {
129  << "expected a " << token::BEGIN_SQR << " in dimensionSet"
130  << endl << "in stream " << is.info()
131  << exit(FatalIOError);
132  }
133 
134  return readNoBegin(is);
135 }
136 
137 
139 {
140  os << token::BEGIN_SQR;
141 
142  for (int d=0; d<dimensionSet::nDimensions-1; d++)
143  {
144  os << exponents_[d] << token::SPACE;
145  }
146 
147  os << exponents_[dimensionSet::nDimensions-1] << token::END_SQR;
148 
149  // Check state of Ostream
150  os.check("Ostream& operator<<(Ostream&, const dimensionSet&)");
151 
152  return os;
153 }
154 
155 
156 void Foam::writeEntry(Ostream& os, const dimensionSet& value)
157 {
158  os << value;
159 }
160 
161 
162 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
163 
165 {
166  dims.read(is);
167 
168  // Check state of Istream
169  is.check("Istream& operator>>(Istream&, dimensionSet&)");
170 
171  return is;
172 }
173 
174 
176 {
177  dims.write(os);
178 
179  // Check state of Ostream
180  os.check("Ostream& operator<<(Ostream&, const dimensionSet&)");
181 
182  return os;
183 }
184 
185 
187 {
188  const dimensionSet& dims = ip.t_;
189 
190  os << token::BEGIN_SQR;
191 
192  for (int first=true, i=0; i<dimensionSet::nDimensions; i++)
193  {
194  if (mag(dims.exponents_[i]) > dimensionSet::smallExponent)
195  {
196  if (!first)
197  {
198  os << token::SPACE;
199  }
200 
202  [static_cast<dimensionSet::dimensionType>(i)];
203 
204  if (dims.exponents_[i] != 1)
205  {
206  os << '^' << dims.exponents_[i];
207  }
208 
209  first = false;
210  }
211  }
212 
213  os << token::END_SQR;
214 
215  // Check state of Ostream
216  os.check("Ostream& operator<<(Ostream&, const InfoProxy<dimensionSet>&)");
217 
218  return os;
219 }
220 
221 
222 // ************************************************************************* //
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:528
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
Istream & readNoBegin(Istream &is)
Read, assuming the '[' has already been read.
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:63
static const scalar smallExponent
A small exponent with which to perform inexact comparisons.
Definition: dimensionSet.H:156
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
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
scalar number() const
Definition: tokenI.H:755
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
Type parseNoBegin(const label lastPrior, tokeniser &tis, 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:257
const dimensionSet dimless
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 &)
dimensioned< scalar > mag(const dimensioned< Type > &)
IOerror FatalIOError
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)