diff --git a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C index a5b618f42c..17e8d5eb9f 100644 --- a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C +++ b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C @@ -33,7 +33,8 @@ template bool Foam::dimensioned::readDimensions ( Istream& is, - scalar& multiplier + scalar& multiplier, + const bool haveDims ) { token nextToken(is); @@ -44,7 +45,11 @@ bool Foam::dimensioned::readDimensions dimensionSet dims(dimless); dims.read(is, multiplier); - if (dims != dimensions_) + if (!haveDims) + { + dimensions_.reset(dims); + } + else if (dims != dimensions_) { FatalIOErrorInFunction(is) << "The dimensions " << dims @@ -62,14 +67,19 @@ bool Foam::dimensioned::readDimensions template -void Foam::dimensioned::initialise(Istream& is) +bool Foam::dimensioned::initialise +( + Istream& is, + const bool haveName, + const bool haveDims +) { token nextToken(is); is.putBack(nextToken); // Check if the original format is used in which the name is provided // and reset the name to that read - if (nextToken.isWord()) + if (!haveName && nextToken.isWord()) { is >> name_; } @@ -79,7 +89,7 @@ void Foam::dimensioned::initialise(Istream& is) // Read dimensions if they are before the value, // compare with the argument with current // and set the multiplier - const bool dimensionsRead = readDimensions(is, multiplier); + const bool dimensionsRead = readDimensions(is, multiplier, haveDims); is >> value_; @@ -88,10 +98,12 @@ void Foam::dimensioned::initialise(Istream& is) // and set the multiplier if (!dimensionsRead && !is.eof()) { - readDimensions(is, multiplier); + readDimensions(is, multiplier, haveDims); } value_ *= multiplier; + + return !haveName && nextToken.isWord(); } @@ -112,11 +124,7 @@ Foam::dimensioned::dimensioned template -Foam::dimensioned::dimensioned -( - const dimensionSet& dimSet, - const Type& t -) +Foam::dimensioned::dimensioned(const dimensionSet& dimSet, const Type& t) : name_(::Foam::name(t)), dimensions_(dimSet), @@ -147,31 +155,21 @@ Foam::dimensioned::dimensioned template -Foam::dimensioned::dimensioned -( - Istream& is -) +Foam::dimensioned::dimensioned(Istream& is) : dimensions_(dimless) { - read(is); + initialise(is, false, false); } template -Foam::dimensioned::dimensioned -( - const word& name, - Istream& is -) +Foam::dimensioned::dimensioned(const word& name, Istream& is) : name_(name), dimensions_(dimless) { - scalar multiplier; - dimensions_.read(is, multiplier); - is >> value_; - value_ *= multiplier; + initialise(is, true, false); } @@ -187,7 +185,7 @@ Foam::dimensioned::dimensioned dimensions_(dimSet), value_(Zero) { - initialise(is); + initialise(is, true, true); } @@ -203,17 +201,16 @@ Foam::dimensioned::dimensioned dimensions_(dimSet), value_(Zero) { - initialise(dict.lookup(name)); + initialise(dict.lookup(name), true, true); } template -Foam::dimensioned::dimensioned -() +Foam::dimensioned::dimensioned() : - name_("undefined"), + name_("NaN"), dimensions_(dimless), - value_(Zero) + value_(pTraits::nan) {} @@ -349,47 +346,45 @@ void Foam::dimensioned::replace template void Foam::dimensioned::read(const dictionary& dict) { - initialise(dict.lookup(name_)); + initialise(dict.lookup(name_), true, true); } template bool Foam::dimensioned::readIfPresent(const dictionary& dict) { - return dict.readIfPresent(name_, value_); + const entry* entryPtr = dict.lookupEntryPtr(name_, false, true); + + if (entryPtr) + { + initialise(entryPtr->stream(), true, true); + return true; + } + else + { + if (dictionary::writeOptionalEntries) + { + IOInfoInFunction(dict) + << "Optional entry '" << name_ << "' is not present," + << " the default value '" << *this << "' will be used." + << endl; + } + + return false; + } } template Foam::Istream& Foam::dimensioned::read(Istream& is) { - // If the name is present, read it - token nextToken(is); - is.putBack(nextToken); - if (nextToken.isWord()) - { - is >> name_; - } - - // Read the dimensions and multiplier - scalar multiplier; - dimensions_.read(is, multiplier); - - // Read and scale the value - is >> value_; - value_ *= multiplier; - - // If the name is not present, set it - if (!nextToken.isWord()) + if (!initialise(is, false, false)) { name_ = Foam::name(value_); } // Check state of Istream - is.check - ( - "Istream& dimensioned::read(Istream& is)" - ); + is.check("Istream& dimensioned::read(Istream& is)"); return is; } @@ -399,20 +394,14 @@ Foam::Istream& Foam::dimensioned::read(Istream& is) template Foam::dimensioned::cmptType> -Foam::dimensioned::operator[] -( - const direction d -) const +Foam::dimensioned::operator[](const direction d) const { return component(d); } template -void Foam::dimensioned::operator+= -( - const dimensioned& dt -) +void Foam::dimensioned::operator+=(const dimensioned& dt) { dimensions_ += dt.dimensions_; value_ += dt.value_; @@ -420,10 +409,7 @@ void Foam::dimensioned::operator+= template -void Foam::dimensioned::operator-= -( - const dimensioned& dt -) +void Foam::dimensioned::operator-=(const dimensioned& dt) { dimensions_ -= dt.dimensions_; value_ -= dt.value_; @@ -431,20 +417,14 @@ void Foam::dimensioned::operator-= template -void Foam::dimensioned::operator*= -( - const scalar s -) +void Foam::dimensioned::operator*=(const scalar s) { value_ *= s; } template -void Foam::dimensioned::operator/= -( - const scalar s -) +void Foam::dimensioned::operator/=(const scalar s) { value_ /= s; } @@ -477,6 +457,7 @@ Foam::sqr(const dimensioned& dt) ); } + template Foam::dimensioned Foam::magSqr(const dimensioned& dt) { @@ -488,6 +469,7 @@ Foam::dimensioned Foam::magSqr(const dimensioned& dt) ); } + template Foam::dimensioned Foam::mag(const dimensioned& dt) { @@ -515,6 +497,7 @@ Foam::dimensioned Foam::cmptMultiply ); } + template Foam::dimensioned Foam::cmptDivide ( @@ -591,28 +574,7 @@ void Foam::writeEntry(Ostream& os, const dimensioned& dt) template Foam::Istream& Foam::operator>>(Istream& is, dimensioned& dt) { - token nextToken(is); - is.putBack(nextToken); - - // Check if the original format is used in which the name is provided - // and reset the name to that read - if (nextToken.isWord()) - { - is >> dt.name_; - is >> nextToken; - is.putBack(nextToken); - } - - // If the dimensions are provided reset the dimensions to those read - scalar multiplier = 1.0; - if (nextToken == token::BEGIN_SQR) - { - dt.dimensions_.read(is, multiplier); - } - - // Read the value - is >> dt.value_; - dt.value_ *= multiplier; + dt.initialise(is, false, false); // Check state of Istream is.check("Istream& operator>>(Istream&, dimensioned&)"); @@ -792,7 +754,6 @@ Foam::operator op \ ); \ } - PRODUCT_OPERATOR(outerProduct, *, outer) PRODUCT_OPERATOR(crossProduct, ^, cross) PRODUCT_OPERATOR(innerProduct, &, dot) diff --git a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H index 8f425175af..42ea49775a 100644 --- a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H +++ b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H @@ -46,17 +46,14 @@ namespace Foam { // Forward declaration of friend functions and operators - -template class dimensioned; - +class dictionary; +template +class dimensioned; template Istream& operator>>(Istream&, dimensioned&); - template Ostream& operator<<(Ostream&, const dimensioned&); -class dictionary; - /*---------------------------------------------------------------------------*\ Class dimensioned Declaration \*---------------------------------------------------------------------------*/ @@ -78,11 +75,16 @@ class dimensioned // Private Member Functions - //- Read the dimensions if present - bool readDimensions(Istream& is, scalar& multiplier); + //- Read the dimensions if present. Return whether dimensions were read. + bool readDimensions + ( + Istream& is, + scalar& multiplier, + const bool haveDims + ); - //- Initialise from Istream - void initialise(Istream& is); + //- Initialise from Istream. Return whether the name was read. + bool initialise(Istream& is, const bool haveName, const bool haveDims); public: @@ -217,11 +219,8 @@ public: // IOstream Operators - friend Istream& operator>> - (Istream&, dimensioned&); - - friend Ostream& operator<< - (Ostream&, const dimensioned&); + friend Istream& operator>> (Istream&, dimensioned&); + friend Ostream& operator<< (Ostream&, const dimensioned&); };