Commit 1036cf96 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: avoid raw dictionary lookup in functionObjects (issue #762)

Style changes:
    - use lookupObjectRef instead of using const_cast
    - use tmp::New factory
parent 1c354e09
......@@ -59,10 +59,8 @@ Foam::functionObjects::CourantNo::byRho
{
return Co/obr_.lookupObject<volScalarField>(rhoName_);
}
else
{
return Co;
}
return Co;
}
......@@ -85,35 +83,27 @@ bool Foam::functionObjects::CourantNo::calc()
if (foundObject<volScalarField>(resultName_, false))
{
volScalarField& Co
(
const_cast<volScalarField&>
(
lookupObject<volScalarField>(resultName_)
)
);
volScalarField& Co =
lookupObjectRef<volScalarField>(resultName_);
Co.ref() = Coi();
Co.correctBoundaryConditions();
}
else
{
tmp<volScalarField> tCo
auto tCo = tmp<volScalarField>::New
(
new volScalarField
IOobject
(
IOobject
(
resultName_,
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
resultName_,
mesh_.time().timeName(),
mesh_,
dimensionedScalar(dimless, Zero),
zeroGradientFvPatchScalarField::typeName
)
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar(dimless, Zero),
zeroGradientFvPatchScalarField::typeName
);
tCo.ref().ref() = Coi();
tCo.ref().correctBoundaryConditions();
......@@ -122,10 +112,8 @@ bool Foam::functionObjects::CourantNo::calc()
return true;
}
else
{
return false;
}
return false;
}
......
......@@ -75,33 +75,28 @@ bool Foam::functionObjects::Curle::calc()
const volVectorField& C = mesh_.C();
tmp<volScalarField> tpDash
auto tpDash = tmp<volScalarField>::New
(
new volScalarField
IOobject
(
IOobject
(
resultName_,
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
resultName_,
mesh_.time().timeName(),
mesh_,
dimensionedScalar(p.dimensions(), Zero)
)
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar(p.dimensions(), Zero)
);
auto& pDash = tpDash.ref();
volScalarField& pDash = tpDash.ref();
const volVectorField d(scopedName("d"), C - x0_);
pDash = (d/magSqr(d) & dfdt)/(4.0*mathematical::pi*c0_);
return store(resultName_, tpDash);
}
else
{
return false;
}
return false;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
......@@ -136,8 +131,7 @@ bool Foam::functionObjects::Curle::read(const dictionary& dict)
{
if (fieldExpression::read(dict))
{
patchSet_ =
mesh_.boundaryMesh().patchSet(wordReList(dict.lookup("patches")));
patchSet_ = mesh_.boundaryMesh().patchSet(dict.get<wordRes>("patches"));
if (patchSet_.empty())
{
......@@ -149,7 +143,7 @@ bool Foam::functionObjects::Curle::read(const dictionary& dict)
}
// Read the reference speed of sound
dict.lookup("c0") >> c0_;
dict.readEntry("c0", c0_);
// Set the location of the effective point source to the area-average
......
......@@ -75,26 +75,21 @@ Foam::functionObjects::DESModelRegions::DESModelRegions
{
read(dict);
tmp<volScalarField> tDESModelRegions
auto tmodelRegions = tmp<volScalarField>::New
(
IOobject
(
new volScalarField
(
IOobject
(
resultName_,
time_.timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar(dimless, Zero)
)
)
resultName_,
time_.timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar(dimless, Zero)
);
store(resultName_, tDESModelRegions);
store(resultName_, tmodelRegions);
writeFileHeader(file());
}
......@@ -124,10 +119,7 @@ bool Foam::functionObjects::DESModelRegions::execute()
Log << type() << " " << name() << " execute:" << nl;
volScalarField& DESModelRegions =
const_cast<volScalarField&>
(
lookupObject<volScalarField>(resultName_)
);
lookupObjectRef<volScalarField>(resultName_);
if (foundObject<DESModelBase>(turbulenceModel::propertiesName))
......
......@@ -57,10 +57,8 @@ Foam::tmp<Foam::surfaceScalarField> Foam::functionObjects::PecletNo::rhoScale
{
return phi/fvc::interpolate(lookupObject<volScalarField>(rhoName_));
}
else
{
return phi;
}
return phi;
}
......@@ -84,23 +82,19 @@ bool Foam::functionObjects::PecletNo::calc()
const dictionary& model =
mesh_.lookupObject<dictionary>("transportProperties");
nuEff =
tmp<volScalarField>
nuEff = tmp<volScalarField>::New
(
IOobject
(
new volScalarField
(
IOobject
(
"nuEff",
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar(model.lookup("nu"))
)
);
"nuEff",
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar(model.lookup("nu"))
);
}
else
{
......
......@@ -81,22 +81,19 @@ Foam::functionObjects::blendingFactor::blendingFactor
writeFileHeader(file());
setResultName(typeName, "");
tmp<volScalarField> indicatorPtr
auto indicatorPtr = tmp<volScalarField>::New
(
new volScalarField
IOobject
(
IOobject
(
resultName_,
time_.timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
resultName_,
time_.timeName(),
mesh_,
dimensionedScalar(dimless, Zero),
zeroGradientFvPatchScalarField::typeName
)
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar(dimless, Zero),
zeroGradientFvPatchScalarField::typeName
);
store(resultName_, indicatorPtr);
......
......@@ -72,10 +72,8 @@ void Foam::functionObjects::blendingFactor::calcBlendingFactor
// - factor applied to 1st scheme, and (1-factor) to 2nd scheme
// - not using the store(...) mechanism due to need to correct BCs
volScalarField& indicator =
const_cast<volScalarField&>
(
lookupObject<volScalarField>(resultName_)
);
lookupObjectRef<volScalarField>(resultName_);
indicator = 1 - fvc::cellReduce(factorf, minEqOp<scalar>(), GREAT);
indicator.correctBoundaryConditions();
}
......
......@@ -134,7 +134,7 @@ bool Foam::functionObjects::ddt2::read(const dictionary& dict)
return false;
}
dict.lookup("fields") >> selectFields_;
dict.readEntry("fields", selectFields_);
selectFields_.uniq();
Info<< type() << " fields: " << selectFields_ << nl;
......
......@@ -56,28 +56,24 @@ int Foam::functionObjects::ddt2::apply(const word& inputName, int& state)
: magSqr(input.dimensions()/dimTime)
);
tmp<volScalarField> tddt2
auto tddt2 = tmp<volScalarField>::New
(
new volScalarField
IOobject
(
IOobject
(
outputName,
time_.timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
outputName,
time_.timeName(),
mesh_,
dimensionedScalar(dims, Zero)
)
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar(dims, Zero)
);
store(outputName, tddt2);
}
volScalarField& output =
const_cast<volScalarField&>(lookupObject<volScalarField>(outputName));
volScalarField& output = lookupObjectRef<volScalarField>(outputName);
if (mag_)
{
......
......@@ -606,8 +606,8 @@ bool Foam::functionObjects::externalCoupled::read(const dictionary& dict)
const dictionary& groupDict = regionIter().dict();
const label nGroups = groupNames_.size();
const wordList readFields(groupDict.lookup("readFields"));
const wordList writeFields(groupDict.lookup("writeFields"));
const wordList readFields(groupDict.get<wordList>("readFields"));
const wordList writeFields(groupDict.get<wordList>("writeFields"));
auto fnd = regionToGroups_.find(regionGroupNames_.last());
if (fnd.found())
......
......@@ -278,8 +278,8 @@ Foam::functionObjects::externalCoupled::gatherAndCombine
Pstream::gatherList(gatheredValues);
tmp<Field<Type>> tresult(new Field<Type>(0));
Field<Type>& result = tresult.ref();
auto tresult = tmp<Field<Type>>::New();
auto& result = tresult.ref();
if (Pstream::master())
{
......
......@@ -584,8 +584,8 @@ bool Foam::functionObjects::extractEulerianParticles::read
if (fvMeshFunctionObject::read(dict) && writeFile::read(dict))
{
dict.lookup("faceZone") >> faceZoneName_;
dict.lookup("alpha") >> alphaName_;
dict.readEntry("faceZone", faceZoneName_);
dict.readEntry("alpha", alphaName_);
dict.readIfPresent("alphaThreshold", alphaThreshold_);
dict.readIfPresent("U", UName_);
......
......@@ -305,13 +305,13 @@ bool Foam::functionObjects::fieldAverage::read(const dictionary& dict)
dict.readIfPresent("restartOnRestart", restartOnRestart_);
dict.readIfPresent("restartOnOutput", restartOnOutput_);
dict.readIfPresent("periodicRestart", periodicRestart_);
dict.lookup("fields") >> faItems_;
dict.readEntry("fields", faItems_);
const scalar currentTime = obr().time().value();
if (periodicRestart_)
{
scalar userRestartPeriod = readScalar(dict.lookup("restartPeriod"));
scalar userRestartPeriod = dict.get<scalar>("restartPeriod");
restartPeriod_ = obr().time().userTimeToTime(userRestartPeriod);
if (restartPeriod_ > 0)
......
......@@ -192,13 +192,13 @@ void Foam::functionObjects::fieldAverageItem::clear
bool Foam::functionObjects::fieldAverageItem::readState(const dictionary& dict)
{
dict.lookup("totalIter") >> totalIter_;
dict.lookup("totalTime") >> totalTime_;
dict.readEntry("totalIter", totalIter_);
dict.readEntry("totalTime", totalTime_);
if (window_ > 0)
{
dict.lookup("windowTimes") >> windowTimes_;
dict.lookup("windowFieldNames") >> windowFieldNames_;
dict.readEntry("windowTimes", windowTimes_);
dict.readEntry("windowFieldNames", windowFieldNames_);
}
return true;
......
......@@ -66,8 +66,8 @@ Foam::Istream& Foam::functionObjects::operator>>
faItem.active_ = false;
faItem.fieldName_ = entry.keyword();
faItem.mean_ = readBool(entry.lookup("mean"));
faItem.prime2Mean_ = readBool(entry.lookup("prime2Mean"));
faItem.mean_ = entry.get<bool>("mean");
faItem.prime2Mean_ = entry.get<bool>("prime2Mean");
faItem.base_ = faItem.baseTypeNames_.lookup("base", entry);
faItem.window_ = entry.lookupOrDefault<scalar>("window", -1.0);
......@@ -94,7 +94,7 @@ Foam::Istream& Foam::functionObjects::operator>>
if (faItem.windowType_ == fieldAverageItem::windowType::EXACT)
{
faItem.allowRestart_ = readBool(entry.lookup("allowRestart"));
faItem.allowRestart_ = entry.get<bool>("allowRestart");
if (!faItem.allowRestart_)
{
......
......@@ -97,12 +97,12 @@ bool Foam::functionObjects::fieldExpression::read(const dictionary& dict)
if (fieldName_.empty() || dict.found("field"))
{
dict.lookup("field") >> fieldName_;
dict.readEntry("field", fieldName_);
}
if (dict.found("result"))
{
dict.lookup("result") >> resultName_;
dict.readEntry("result", resultName_);
}
return true;
......
......@@ -95,8 +95,8 @@ bool Foam::functionObjects::fieldValue::read(const dictionary& dict)
fvMeshFunctionObject::read(dict);
writeFile::read(dict);
dict.lookup("fields") >> fields_;
dict.lookup("writeFields") >> writeFields_;
dict.readEntry("fields", fields_);
dict.readEntry("writeFields", writeFields_);
scaleFactor_ = dict.lookupOrDefault<scalar>("scaleFactor", 1.0);
return true;
......
......@@ -36,7 +36,7 @@ Foam::functionObjects::fieldValue::New
const bool output
)
{
const word modelType(dict.lookup("type"));
const word modelType(dict.get<word>("type"));
if (output)
{
......
......@@ -484,7 +484,7 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::initialise
const dictionary& dict
)
{
dict.lookup("name") >> regionName_;
dict.readEntry("name", regionName_);
switch (regionType_)
{
......@@ -616,7 +616,7 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::initialise
surfaceWriterPtr_.clear();
if (writeFields_)
{
const word surfaceFormat(dict.lookup("surfaceFormat"));
const word surfaceFormat(dict.get<word>("surfaceFormat"));
if (surfaceFormat != "none")
{
......@@ -687,12 +687,12 @@ Foam::functionObjects::fieldValues::surfaceFieldValue::processValues
{
case opSumDirection:
{
const vector n(dict_.lookup("direction"));
const vector n(dict_.get<vector>("direction"));
return gSum(pos0(values*(Sf & n))*mag(values));
}
case opSumDirectionBalance:
{
const vector n(dict_.lookup("direction"));
const vector n(dict_.get<vector>("direction"));
const scalarField nv(values*(Sf & n));
return gSum(pos0(nv)*mag(values) - neg(nv)*mag(values));
......@@ -758,16 +758,14 @@ Foam::functionObjects::fieldValues::surfaceFieldValue::processValues
{
case opSumDirection:
{
vector n(dict_.lookup("direction"));
n /= mag(n) + ROOTVSMALL;
const vector n(dict_.get<vector>("direction").normalise());
const scalarField nv(n & values);
return gSum(pos0(nv)*n*(nv));
}
case opSumDirectionBalance:
{
vector n(dict_.lookup("direction"));
n /= mag(n) + ROOTVSMALL;
const vector n(dict_.get<vector>("direction").normalise());
const scalarField nv(n & values);
return gSum(pos0(nv)*n*(nv));
......
......@@ -178,7 +178,6 @@ processSameTypeValues
if (canWeight(weightField))
{
tmp<scalarField> weight(weightingFactor(weightField));
result = gSum(weight*values);
}
else
......@@ -466,8 +465,8 @@ Foam::functionObjects::fieldValues::surfaceFieldValue::filterField
const GeometricField<Type, fvPatchField, volMesh>& field
) const
{
tmp<Field<Type>> tvalues(new Field<Type>(faceId_.size()));
Field<Type>& values = tvalues.ref();
auto tvalues = tmp<Field<Type>>::New(faceId_.size());
auto& values = tvalues.ref();
forAll(values, i)
{
......@@ -501,8 +500,8 @@ Foam::functionObjects::fieldValues::surfaceFieldValue::filterField
const GeometricField<Type, fvsPatchField, surfaceMesh>& field
) const
{
tmp<Field<Type>> tvalues(new Field<Type>(faceId_.size()));
Field<Type>& values = tvalues.ref();
auto tvalues = tmp<Field<Type>>::New(faceId_.size());
auto& values = tvalues.ref();
forAll(values, i)
{
......
......@@ -111,12 +111,12 @@ bool Foam::functionObjects::fieldsExpression::read(const dictionary& dict)
if (fieldNames_.empty() || dict.found("fields"))
{
dict.lookup("fields") >> fieldNames_;
dict.readEntry("fields", fieldNames_);
}
if (dict.found("result"))
{
dict.lookup("result") >> resultName_;
dict.readEntry("result", resultName_);
}
return true;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment