ENH: additional read guards for dimensionedType. input consistency (#762, #1148)

- provide a lookupOrDefault constructor form, since this is a fairly
  commonly used requirement and simplifies the calling sequence.

  Before

      dimensionedScalar rhoMax
      (
          dimensionedScalar::lookupOrDefault
          (
              "rhoMax",
              pimple.dict(),
              dimDensity,
              GREAT
          )
     );

  After

      dimensionedScalar rhoMax("rhoMax", dimDensity, GREAT, pimple.dict());

- read, readIfPresent methods with alternative lookup names.

- Mark the Istream related constructors with compile-time deprecated
  warnings.

BUG: read, readIfPresent methods not handling optional dimensions (#1148)
This commit is contained in:
Mark Olesen
2019-01-03 13:34:11 +01:00
parent af310075a0
commit 6a448016aa
58 changed files with 443 additions and 688 deletions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -27,10 +27,10 @@ License
#include "pTraits.H"
#include "dictionary.H"
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class Type>
void Foam::dimensioned<Type>::initialize(Istream& is, bool checkDims)
void Foam::dimensioned<Type>::initialize(Istream& is, const bool checkDims)
{
token nextToken(is);
is.putBack(nextToken);
@ -67,13 +67,50 @@ void Foam::dimensioned<Type>::initialize(Istream& is, bool checkDims)
}
template<class Type>
bool Foam::dimensioned<Type>::readEntry
(
const word& key,
const dictionary& dict,
const bool mandatory,
const bool checkDims,
enum keyType::option matchOpt
)
{
// Largely identical to dictionary::readEntry(),
// but with optional handling of checkDims
const auto finder(dict.csearch(key, matchOpt));
if (finder.found())
{
ITstream& is = finder.ptr()->stream();
initialize(is, checkDims);
dict.checkITstream(is, key);
return true;
}
else if (mandatory)
{
FatalIOErrorInFunction(dict)
<< "Entry '" << key << "' not found in dictionary "
<< dict.name()
<< exit(FatalIOError);
}
return false;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
Foam::dimensioned<Type>::dimensioned()
:
name_("0"),
dimensions_(dimless),
dimensions_(),
value_(Zero)
{}
@ -123,10 +160,79 @@ Foam::dimensioned<Type>::dimensioned
{}
template<class Type>
Foam::dimensioned<Type>::dimensioned
(
const word& name,
const dictionary& dict
)
:
name_(name),
dimensions_(),
value_(Zero)
{
// mandatory, no checkDims
readEntry(name, dict, true, false);
}
template<class Type>
Foam::dimensioned<Type>::dimensioned
(
const word& name,
const dimensionSet& dims,
const dictionary& dict
)
:
name_(name),
dimensions_(dims),
value_(Zero)
{
// mandatory, checkDims
readEntry(name, dict);
}
template<class Type>
Foam::dimensioned<Type>::dimensioned
(
const word& name,
const dimensionSet& dims,
const dictionary& dict,
const word& entryName
)
:
name_(name),
dimensions_(dims),
value_(Zero)
{
// mandatory, checkDims
readEntry(entryName, dict);
}
template<class Type>
Foam::dimensioned<Type>::dimensioned
(
const word& name,
const dimensionSet& dims,
const Type& val,
const dictionary& dict
)
:
name_(name),
dimensions_(dims),
value_(val)
{
// non-mandatory, checkDims
readEntry(name, dict, false);
}
template<class Type>
Foam::dimensioned<Type>::dimensioned(Istream& is)
:
dimensions_(dimless)
dimensions_()
{
read(is);
}
@ -140,15 +246,9 @@ Foam::dimensioned<Type>::dimensioned
)
:
name_(name),
dimensions_(dimless)
dimensions_()
{
// Read dimensionSet + multiplier
scalar mult(1.0);
dimensions_.read(is, mult);
// Read value
is >> value_;
value_ *= mult;
read(is, false); // Don't read name. Read dimensionSet + multiplier only.
}
@ -164,40 +264,8 @@ Foam::dimensioned<Type>::dimensioned
dimensions_(dims),
value_(Zero)
{
initialize(is, true); // checkDims
}
template<class Type>
Foam::dimensioned<Type>::dimensioned
(
const word& name,
const dictionary& dict
)
:
dimensioned<Type>(name, dimless, dict, false) // no checkDims
{}
template<class Type>
Foam::dimensioned<Type>::dimensioned
(
const word& name,
const dimensionSet& dims,
const dictionary& dict,
const bool checkDims
)
:
name_(name),
dimensions_(dims),
value_(Zero)
{
// Like dictionary::lookup(), but in two stages to detect input errors
const entry& e = dict.lookupEntry(name, keyType::REGEX);
ITstream& is = e.stream();
initialize(is, checkDims);
e.checkITstream(is);
// checkDims
initialize(is, true);
}
@ -212,14 +280,8 @@ Foam::dimensioned<Type> Foam::dimensioned<Type>::lookupOrDefault
const Type& defaultValue
)
{
if (dict.found(name))
{
return dimensioned<Type>(name, dims, dict);
}
else
{
return dimensioned<Type>(name, dims, defaultValue);
}
// checkDims = true
return dimensioned<Type>(name, dims, defaultValue, dict);
}
@ -231,7 +293,7 @@ Foam::dimensioned<Type> Foam::dimensioned<Type>::lookupOrDefault
const Type& defaultValue
)
{
return lookupOrDefault(name, dict, dimless, defaultValue);
return dimensioned<Type>(name, dimless, defaultValue, dict);
}
@ -244,12 +306,13 @@ Foam::dimensioned<Type> Foam::dimensioned<Type>::lookupOrAddToDict
const Type& defaultValue
)
{
if (!dict.found(name))
if (dict.found(name))
{
(void) dict.add(name, defaultValue);
return dimensioned<Type>(name, dims, dict);
}
return dimensioned<Type>(name, dims, dict);
(void) dict.add(name, defaultValue);
return dimensioned<Type>(name, dims, defaultValue);
}
@ -273,6 +336,7 @@ const Foam::word& Foam::dimensioned<Type>::name() const
return name_;
}
template<class Type>
Foam::word& Foam::dimensioned<Type>::name()
{
@ -286,6 +350,7 @@ const Foam::dimensionSet& Foam::dimensioned<Type>::dimensions() const
return dimensions_;
}
template<class Type>
Foam::dimensionSet& Foam::dimensioned<Type>::dimensions()
{
@ -299,6 +364,7 @@ const Type& Foam::dimensioned<Type>::value() const
return value_;
}
template<class Type>
Type& Foam::dimensioned<Type>::value()
{
@ -335,24 +401,51 @@ void Foam::dimensioned<Type>::replace
template<class Type>
void Foam::dimensioned<Type>::read(const dictionary& dict)
bool Foam::dimensioned<Type>::read(const dictionary& dict)
{
dict.readEntry(name_, value_);
return read(name_, dict);
}
template<class Type>
bool Foam::dimensioned<Type>::readIfPresent(const dictionary& dict)
{
return dict.readIfPresent(name_, value_);
return readIfPresent(name_, dict);
}
template<class Type>
Foam::Istream& Foam::dimensioned<Type>::read(Istream& is)
bool Foam::dimensioned<Type>::read
(
const word& entryName,
const dictionary& dict
)
{
// Read name
is >> name_;
// mandatory, checkDims
return readEntry(entryName, dict);
}
template<class Type>
bool Foam::dimensioned<Type>::readIfPresent
(
const word& entryName,
const dictionary& dict
)
{
// non-mandatory, checkDims
return readEntry(entryName, dict, false);
}
template<class Type>
Foam::Istream& Foam::dimensioned<Type>::read(Istream& is, const bool readName)
{
if (readName)
{
// Read name
is >> name_;
}
// Read dimensionSet + multiplier
scalar mult(1.0);
@ -632,17 +725,15 @@ Foam::Istream& Foam::operator>>(Istream& is, dimensioned<Type>& dt)
template<class Type>
Foam::Ostream& Foam::operator<<(Ostream& os, const dimensioned<Type>& dt)
{
// Write the name
// The name
os << dt.name() << token::SPACE;
// Write the dimensions
// The dimensions
scalar mult(1.0);
dt.dimensions().write(os, mult);
os << token::SPACE;
// Write the value
os << dt.value()/mult;
// The value
os << token::SPACE << dt.value()/mult;
os.check(FUNCTION_NAME);
return os;
@ -743,8 +834,8 @@ Foam::dimensioned<Type> Foam::operator/
return dimensioned<Type>
(
'(' + dt.name() + '|' + ds.name() + ')',
dt.dimensions()/ds.dimensions(),
dt.value()/ds.value()
dt.dimensions() / ds.dimensions(),
dt.value() / ds.value()
);
}

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -52,10 +52,7 @@ class dictionary;
template<class Type> class dimensioned;
template<class Type>
Istream& operator>>(Istream&, dimensioned<Type>&);
template<class Type>
Ostream& operator<<(Ostream&, const dimensioned<Type>&);
Istream& operator>>(Istream& is, dimensioned<Type>& dt);
/*---------------------------------------------------------------------------*\
@ -65,9 +62,9 @@ Ostream& operator<<(Ostream&, const dimensioned<Type>&);
template<class Type>
class dimensioned
{
// Private data
// Private Data
//- Variable name
//- The variable name
word name_;
//- The dimension set
@ -77,9 +74,9 @@ class dimensioned
Type value_;
// Private member functions
// Private Member Functions
//- Constructor helper.
//- Read helper.
// Requires a value, optional preceded with name and/or dimensions.
// \verbatim
// [name] [dims] value
@ -88,8 +85,28 @@ class dimensioned
// If dimensions are present, they are read.
// With checkDims = true, the dimensions read are verified
// against the current (expected) dimensions.
void initialize(Istream& is, bool checkDims);
void initialize(Istream& is, const bool checkDims);
//- Find entry and assign to dimensioned Type
//- FatalIOError if it is found and the number of tokens is incorrect,
//- or it is mandatory and not found.
//
// Requires a value, optional preceded with name and/or dimensions.
// \verbatim
// [name] [dims] value
// \endverbatim
// If the name is present, it is used to rename.
// If dimensions are present, they are read.
// With checkDims = true, the dimensions read are verified
// against the current (expected) dimensions.
bool readEntry
(
const word& key,
const dictionary& dict,
const bool mandatory = true, //!< entry is mandatory
const bool checkDims = true, //!< verify dimensions read
enum keyType::option matchOpt = keyType::REGEX
);
public:
@ -99,7 +116,7 @@ public:
// Constructors
//- Construct null: a dimensionless Zero, named "0"
//- A dimensionless Zero, named "0"
dimensioned();
//- A dimensioned Zero, named "0"
@ -119,7 +136,7 @@ public:
//- Copy construct dimensioned Type with a new name
dimensioned(const word& name, const dimensioned<Type>& dt);
//- Construct from components: name, dimensionSet and a value.
//- Construct from components (name, dimensions, value).
dimensioned
(
const word& name,
@ -145,19 +162,48 @@ public:
// \endverbatim
// If the optional name is found, it is used for renaming.
// If the optional dimensions are present, they are read and
// normally verified against the expected dimensions.
// verified against the expected dimensions.
dimensioned
(
const word& name,
const dimensionSet& dims,
const dictionary& dict
);
//- Construct from dictionary lookup with a given name and dimensions.
// The dictionary entry may contain optional name and dimensions.
// \verbatim
// [name] [dims] value
// \endverbatim
// If the optional name is found, it is used for renaming.
// If the optional dimensions are present, they are read and
// verified against the expected dimensions.
dimensioned
(
const word& name,
const dimensionSet& dims,
const dictionary& dict,
const bool checkDims = true //!< verify dimensions read
const word& entryName //!< dictionary lookup name
);
//- Construct from components (name, dimensions, value) with
//- optional dictionary override.
// The dictionary entry may contain optional name and dimensions.
// \verbatim
// [name] [dims] value
// \endverbatim
dimensioned
(
const word& name,
const dimensionSet& dims,
const Type& val,
const dictionary& dict
);
// Static member functions
// Static Member Functions
//- Construct from dictionary, with default dimensions and value.
//- Construct dimensioned from dictionary, with default value.
//- FatalIOError if there are excess tokens.
static dimensioned<Type> lookupOrDefault
(
@ -167,7 +213,7 @@ public:
const Type& defaultValue = Type(Zero)
);
//- Construct from dictionary, dimensionless and with a value.
//- Construct dimensionless from dictionary, with default value.
// FatalIOError if it is found and there are excess tokens.
static dimensioned<Type> lookupOrDefault
(
@ -176,7 +222,7 @@ public:
const Type& defaultValue = Type(Zero)
);
//- Construct from dictionary, with default value.
//- Construct dimensioned from dictionary, with default value.
// If the value is not found, it is added into the dictionary.
// FatalIOError if it is found and there are excess tokens.
static dimensioned<Type> lookupOrAddToDict
@ -187,7 +233,7 @@ public:
const Type& defaultValue = Type(Zero)
);
//- Construct from dictionary, dimensionless with default value.
//- Construct dimensionless from dictionary, with default value.
// If the value is not found, it is added into the dictionary.
// FatalIOError if it is found and there are excess tokens.
static dimensioned<Type> lookupOrAddToDict
@ -227,24 +273,35 @@ public:
//- Return transpose.
dimensioned<Type> T() const;
//- Update the value of dimensioned\<Type\>
void read(const dictionary& dict);
//- Update the value of dimensioned\<Type\>,
//- lookup in dictionary with the name().
bool read(const dictionary& dict);
//- Update the value of dimensioned\<Type\> if found in the dictionary.
//- Update the value of dimensioned\<Type\> if found in the dictionary,
//- lookup in dictionary with the name().
bool readIfPresent(const dictionary& dict);
//- Update the value of dimensioned\<Type\>,
//- using an alternative entry name
bool read(const word& entryName, const dictionary& dict);
//- Update the value of dimensioned\<Type\> if found in the dictionary,
//- using an alternative entry name
bool readIfPresent(const word& entryName, const dictionary& dict);
// IO
//- Read name, dimensions, value from stream,
//- using units from system table
Istream& read(Istream& is);
//- Read (name, dimensions, value) from stream,
//- using units from system table.
// Optionally skip reading the name
Istream& read(Istream& is, const bool readName = true);
//- Read name, dimensions, value from stream,
//- Read (name, dimensions, value) from stream,
//- using units from dictionary
Istream& read(Istream& is, const dictionary& readSet);
//- Read name, dimensions, value from stream,
//- Read (name, dimensions, value) from stream,
//- using units from table
Istream& read(Istream& is, const HashTable<dimensionedScalar>& readSet);
@ -265,54 +322,52 @@ public:
void operator/=(const scalar s);
// IOstream operators
// IOstream Operators
//- Read from stream. The name and dimensions are optional.
// If the optional dimensions are present, they are used
// used without further verification.
// If the optional dimensions are present,
// they are used without further verification.
friend Istream& operator>> <Type>
(
Istream& is,
dimensioned<Type>& dt
);
friend Ostream& operator<< <Type>
(
Ostream& os,
const dimensioned<Type>& dt
);
// Housekeeping
//- Deprecated(2018-11) Construct from Istream
//- (expects name, dimensions, value)
// \deprecated(2018-11) - should generally use one of the construct
// from dictionary constructors instead. They provide additional
// checks on the input stream.
explicit dimensioned(Istream& is);
// \deprecated(2018-11) - should generally use construct from
// dictionary instead (additional checks on the input stream).
explicit dimensioned(Istream& is)
FOAM_DEPRECATED(2018-11);
//- Deprecated(2018-11) Construct from Istream with given name
//- (expects dimensions, value)
// \deprecated(2018-11) - should generally use one of the construct
// from dictionary constructors instead. They provide additional
// checks on the input stream.
dimensioned(const word& name, Istream& is);
// \deprecated(2018-11) - should generally use construct from
// dictionary instead (additional checks on the input stream).
dimensioned(const word& name, Istream& is)
FOAM_DEPRECATED(2018-11);
//- Deprecated(2018-11) Construct from Istream with given name
//- and expected dimensions.
// Expects value, but supports optional name and dimensions.
// If the optional dimensions are present, they are read and
// verified against the expected dimensions.
// \deprecated(2018-11) - should generally use one of the construct
// from dictionary constructors instead. They provide additional
// checks on the input stream.
dimensioned(const word& name, const dimensionSet& dims, Istream& is);
// \deprecated(2018-11) - should generally use construct from
// dictionary instead (additional checks on the input stream).
dimensioned(const word& name, const dimensionSet& dims, Istream& is)
FOAM_DEPRECATED(2018-11);
};
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
//- Output operator
template<class Type>
Ostream& operator<<(Ostream& os, const dimensioned<Type>& dt);
template<class Type, direction r>
dimensioned<typename powProduct<Type, r>::type>
pow

View File

@ -31,13 +31,7 @@ template<class BasicTurbulenceModel>
void Foam::EddyDiffusivity<BasicTurbulenceModel>::correctNut()
{
// Read Prt if provided
Prt_ = dimensioned<scalar>::lookupOrDefault
(
"Prt",
this->coeffDict(),
1.0
);
Prt_ = dimensionedScalar("Prt", dimless, 1.0, this->coeffDict());
alphat_ = this->rho_*this->nut()/Prt_;
alphat_.correctBoundaryConditions();
}

View File

@ -248,12 +248,7 @@ void alphatJayatillekeWallFunctionFvPatchScalarField::updateCoeffs()
// Molecular Prandtl number
const scalar Pr
(
dimensionedScalar
(
"Pr",
dimless,
transportProperties.lookup("Pr")
).value()
dimensionedScalar("Pr", dimless, transportProperties).value()
);
// Populate boundary values

View File

@ -61,25 +61,9 @@ Maxwell<BasicTurbulenceModel>::Maxwell
propertiesName
),
nuM_
(
dimensioned<scalar>
(
"nuM",
dimViscosity,
this->coeffDict_.lookup("nuM")
)
),
nuM_("nuM", dimViscosity, this->coeffDict_),
lambda_
(
dimensioned<scalar>
(
"lambda",
dimTime,
this->coeffDict_.lookup("lambda")
)
),
lambda_("lambda", dimTime, this->coeffDict_),
sigma_
(

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -36,16 +36,7 @@ bool Foam::functionObjects::reference::calcType()
{
const VolFieldType& vf = *vfPtr;
dimensioned<Type> offset
(
dimensioned<Type>::lookupOrDefault
(
"offset",
localDict_,
vf.dimensions(),
Zero
)
);
dimensioned<Type> offset("offset", vf.dimensions(), Zero, localDict_);
dimensioned<Type> cellValue("value", vf.dimensions(), Zero);

View File

@ -385,12 +385,7 @@ Foam::tmp<Foam::volScalarField> Foam::functionObjects::forces::mu() const
const dictionary& transportProperties =
lookupObject<dictionary>("transportProperties");
dimensionedScalar nu
(
"nu",
dimViscosity,
transportProperties.lookup("nu")
);
dimensionedScalar nu("nu", dimViscosity, transportProperties);
return rho()*nu;
}

View File

@ -208,32 +208,16 @@ Foam::functionObjects::energyTransport::energyTransport
schemesField_("unknown-schemesField"),
fvOptions_(mesh_),
multiphaseThermo_(dict.subOrEmptyDict("phaseThermos")),
Cp_
(
dict.lookupOrDefault
(
"Cp",
dimensionedScalar("Cp", dimEnergy/dimMass/dimTemperature, 0)
)
),
Cp_("Cp", dimEnergy/dimMass/dimTemperature, 0, dict),
kappa_
(
dict.lookupOrDefault
(
"kappa",
dimensionedScalar
(
"kappa",
dimEnergy/dimTime/dimLength/dimTemperature,
0
)
)
"kappa",
dimEnergy/dimTime/dimLength/dimTemperature,
0,
dict
),
rho_
(
dict.lookupOrDefault("rhoInf", dimensionedScalar("rho", dimDensity, 0))
),
Prt_(dict.lookupOrDefault("Prt", dimensionedScalar("Prt", dimless, 1))),
rho_("rhoInf", dimDensity, 0, dict),
Prt_("Prt", dimless, 1, dict),
rhoCp_
(
IOobject
@ -281,7 +265,7 @@ Foam::functionObjects::energyTransport::energyTransport
(
"Cp",
dimEnergy/dimMass/dimTemperature,
dict.lookup("Cp")
dict
)
);
@ -292,7 +276,7 @@ Foam::functionObjects::energyTransport::energyTransport
(
"kappa",
dimEnergy/dimTime/dimLength/dimTemperature,
dict.lookup("kappa")
dict
)
);

View File

@ -63,13 +63,10 @@ solidification::solidification
),
maxSolidificationRate_
(
dimensioned<scalar>::lookupOrDefault
(
"maxSolidificationRate",
coeffDict_,
dimless/dimTime,
GREAT
)
"maxSolidificationRate",
dimless/dimTime,
GREAT,
coeffDict_
),
mass_
(

View File

@ -56,25 +56,25 @@ Foam::compressibilityModels::Chung::Chung
(
"psiv",
dimCompressibility,
compressibilityProperties_.lookup("psiv")
compressibilityProperties_
),
psil_
(
"psil",
dimCompressibility,
compressibilityProperties_.lookup("psil")
compressibilityProperties_
),
rhovSat_
(
"rhovSat",
dimDensity,
compressibilityProperties_.lookup("rhovSat")
compressibilityProperties_
),
rholSat_
(
"rholSat",
dimDensity,
compressibilityProperties_.lookup("rholSat")
compressibilityProperties_
)
{
correct();

View File

@ -56,25 +56,25 @@ Foam::compressibilityModels::Wallis::Wallis
(
"psiv",
dimCompressibility,
compressibilityProperties_.lookup("psiv")
compressibilityProperties_
),
psil_
(
"psil",
dimCompressibility,
compressibilityProperties_.lookup("psil")
compressibilityProperties_
),
rhovSat_
(
"rhovSat",
dimDensity,
compressibilityProperties_.lookup("rhovSat")
compressibilityProperties_
),
rholSat_
(
"rholSat",
dimDensity,
compressibilityProperties_.lookup("rholSat")
compressibilityProperties_
)
{
correct();

View File

@ -56,13 +56,13 @@ Foam::compressibilityModels::linear::linear
(
"psiv",
dimCompressibility,
compressibilityProperties_.lookup("psiv")
compressibilityProperties_
),
psil_
(
"psil",
dimCompressibility,
compressibilityProperties_.lookup("psil")
compressibilityProperties_
)
{
correct();