39 const word& entryName,
56Foam::dimensionSet::tokeniser::tokeniser(Istream& is)
67void Foam::dimensionSet::tokeniser::push(
const token& t)
69 const label
end = (start_+size_)%tokens_.size();
71 if (size_ == tokens_.size())
73 start_ = tokens_.fcIndex(start_);
82Foam::token Foam::dimensionSet::tokeniser::pop()
84 token t = tokens_[start_];
85 start_ = tokens_.fcIndex(start_);
91void Foam::dimensionSet::tokeniser::unpop(
const token& t)
94 start_ = tokens_.rcIndex(start_);
99bool Foam::dimensionSet::tokeniser::hasToken()
const
101 return size_ || is_.good();
105bool Foam::dimensionSet::tokeniser::valid(
char c)
126Foam::label Foam::dimensionSet::tokeniser::priority(
const token& t)
128 if (t.isPunctuation())
138 else if (t.pToken() ==
'^')
149void Foam::dimensionSet::tokeniser::splitWord(
const word& w)
152 for (
size_t i=0; i<w.size(); ++i)
158 const word subWord = w.substr(start, i-start);
161 push(
token(readScalar(subWord)));
165 push(
token(subWord));
173 const scalar val = (w[i] -
'0');
184 if (start < w.size())
186 const word subWord = w.substr(start);
189 push(
token(readScalar(subWord)));
193 push(
token(subWord));
199Foam::token Foam::dimensionSet::tokeniser::nextToken()
206 splitWord(t.wordToken());
221void Foam::dimensionSet::tokeniser::putBack(
const token& t)
236void Foam::dimensionSet::round(
const scalar tol)
239 for (scalar& val : exponents_)
241 const scalar fractionalPart = std::modf(val, &integralPart);
243 if (
mag(fractionalPart-1.0) <= tol)
245 val = 1.0+integralPart;
247 else if (
mag(fractionalPart+1.0) <= tol)
249 val = -1.0+integralPart;
251 else if (
mag(fractionalPart) <= tol)
261 const label lastPrior,
269 token nextToken(tis.nextToken());
273 bool haveReadSymbol =
false;
278 if (nextToken.isWord())
280 const word& unitName = nextToken.wordToken();
282 ds.dimensions() *= unitDim.dimensions();
283 ds.value() *= unitDim.value();
284 haveReadSymbol =
true;
286 else if (nextToken.isNumber())
289 ds.value() *= nextToken.number();
290 haveReadSymbol =
true;
292 else if (nextToken.isPunctuation())
294 label nextPrior = tokeniser::priority(nextToken);
299 tis.putBack(nextToken);
304 tis.putBack(nextToken);
311 token t = tis.nextToken();
318 ds.dimensions() *= sub.dimensions();
319 ds.value() *= sub.value();
321 haveReadSymbol =
true;
325 tis.putBack(nextToken);
330 if (nextPrior > lastPrior)
334 ds.dimensions() *= sub.dimensions();
335 ds.value() *= sub.value();
340 tis.putBack(nextToken);
343 haveReadSymbol =
false;
347 if (nextPrior > lastPrior)
351 ds.dimensions() /= sub.dimensions();
352 ds.value() /= sub.value();
356 tis.putBack(nextToken);
359 haveReadSymbol =
false;
361 else if (nextToken.pToken() ==
'^')
363 if (nextPrior > lastPrior)
367 ds.dimensions().reset(
pow(ds.dimensions(),
expon.value()));
369 ds.dimensions().round(10*smallExponent);
374 tis.putBack(nextToken);
377 haveReadSymbol =
false;
397 nextToken = tis.nextToken();
398 if (nextToken.error())
403 if (haveReadSymbol && (nextToken.isWord() || nextToken.isNumber()))
406 tis.putBack(nextToken);
417 const word& entryName,
443 <<
"Entry '" << entryName <<
"' not found in dictionary "
456 const HashTable<dimensionedScalar>& readSet
462 token startToken(is);
468 <<
"in stream " << is.info() <<
nl
475 if (!nextToken.isNumber())
477 is.putBack(nextToken);
483 multiplier = ds.value();
484 exponents_ = ds.dimensions().values();
501 if (nextToken.isNumber())
519 <<
"in stream " << is.info() <<
nl
549 token startToken(is);
555 <<
"in stream " << is.
info() <<
nl
562 if (nextToken.isWord())
564 bool continueParsing =
true;
567 word symbolPow = nextToken.wordToken();
570 symbolPow.resize(symbolPow.size()-1);
571 continueParsing =
false;
578 const auto index = symbolPow.find(
'^');
579 if (index != std::string::npos)
581 const word symbol = symbolPow.substr(0, index);
582 const scalar exponent = readScalar(symbolPow.substr(index+1));
587 symbolSet.reset(
pow(
s.dimensions(), exponent));
590 symbolSet.round(10*smallExponent);
598 symbolSet.reset(
s.dimensions());
599 multiplier *=
s.value();
605 exponents_[i] += symbolSet[i];
610 nextToken =
token(is);
614 continueParsing =
false;
618 while (continueParsing);
635 if (nextToken.isNumber())
653 <<
"in stream " << is.
info() <<
nl
667 const dimensionSets& writeUnits
679 exponents[d] = exponents_[d];
681 writeUnits.coefficients(exponents);
683 bool hasPrinted =
false;
688 std::numeric_limits<scalar>::digits10
693 if (
mag(exponents[i]) > smallExponent)
703 if (
mag(exponents[i]-1) > smallExponent)
705 os <<
'^' << exponents[i];
707 multiplier *=
Foam::pow(ds.value(), exponents[i]);
711 multiplier *= ds.value();
755 <<
"Cannot use scaled units in dimensionSet"
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
A HashTable similar to std::unordered_map.
static bool isReadRequired(readOption opt) noexcept
True if (MUST_READ | READ_MODIFIED) bits are set.
readOption
Enumeration defining read preferences.
@ NO_READ
Nothing to be read.
@ ASCII
"ascii" (normal default)
virtual bool check(const char *operation) const
Check IOstream status for given operation.
InfoProxy< IOstream > info() const noexcept
Return info proxy, used to print IOstream information to a stream.
An input stream of tokens.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
virtual const fileName & name() const override
Read/write access to the name of the stream.
virtual int precision() const override
Get precision of output field.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
const entry * findEntry(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry (const access) with the given keyword.
ITstream & lookup(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return an entry data stream. FatalIOError if not found, or not a stream.
Dimension set for the base types, which can be used to implement rigorous dimension checking for alge...
Ostream & write(Ostream &os, scalar &multiplier, const dimensionSets &writeUnits) const
Write using provided write units, return scaling in multiplier.
static constexpr int nDimensions
There are 7 base dimensions.
Istream & read(Istream &is, scalar &multiplier, const dictionary &)
Read using provided units, return scaling in multiplier. Used only in initial parsing.
dimensionSet()
Default construct (dimensionless).
bool readEntry(const word &entryName, const dictionary &dict, IOobjectOption::readOption readOpt=IOobjectOption::MUST_READ)
Update the dimensions from dictionary entry. FatalIOError if it is found and the number of tokens is ...
const FixedList< scalar, 7 > & values() const noexcept
Const access to the exponents as a list.
static const scalar smallExponent
Tolerance for 'small' exponents, for near-zero rounding.
void reset(const dimensionSet &ds)
Copy assign the exponents from the dimensionSet.
@ LUMINOUS_INTENSITY
Candela cd.
Construction of unit sets.
const PtrList< dimensionedScalar > & units() const noexcept
Return the units.
void coefficients(scalarField &exponents) const
(if valid) obtain set of coefficients of unitNames
bool good() const noexcept
Is there a valid inverse of the selected unit.
const dimensionSet & dimensions() const noexcept
Return const reference to dimensions.
const word & name() const noexcept
Return const reference to name.
const Type & value() const noexcept
Return const reference to value.
A keyword and a list of tokens is an 'entry'.
A token holds an item read from Istream.
bool isNumber() const noexcept
Token is (signed/unsigned) integer type, FLOAT or DOUBLE.
punctuationToken
Standard punctuation tokens (a character).
@ DIVIDE
Divide [isseparator].
@ BEGIN_SQR
Begin dimensions [isseparator].
@ BEGIN_LIST
Begin list [isseparator].
@ SUBTRACT
Subtract or start of negative number.
@ END_LIST
End list [isseparator].
@ END_SQR
End dimensions [isseparator].
@ MULTIPLY
Multiply [isseparator].
const word & wordToken() const
Return const reference to the word contents.
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE).
scalar number() const
Return label, float or double value.
A class for handling words, derived from Foam::string.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
OBJstream os(runTime.globalPath()/outputName)
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
const dimensionSet dimless
Dimensionless.
bool read(const char *buf, int32_t &val)
Same as readInt32.
constexpr bool isspace(char c) noexcept
Test for whitespace (C-locale only).
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
const HashTable< dimensionedScalar > & unitSet()
Set of all dimensions.
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Istream & operator>>(Istream &, directionInfo &)
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
const dimensionSets & writeUnitSet()
Set of units.
static constexpr const zero Zero
Global zero (0).
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
errorManipArg< error, int > exit(error &err, const int errNo=1)
constexpr bool isdigit(char c) noexcept
Test for digits [0-9] (C-locale only).
constexpr char nl
The newline '\n' character (0x0a).
#define forAll(list, i)
Loop across all elements in list.