ENH: for-range, forAllIters() ... in functionObjects/

- reduced clutter when iterating over containers
This commit is contained in:
Mark Olesen
2019-01-07 09:20:51 +01:00
committed by Andrew Heather
parent 60234ab007
commit 24861f5158
10 changed files with 73 additions and 97 deletions

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd. \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -106,17 +106,16 @@ bool Foam::functionObjects::fieldAverageItem::calculateMeanField
// Note: looks up all window fields from the registry // Note: looks up all window fields from the registry
meanField = 0*baseField; meanField = 0*baseField;
FIFOStack<scalar>::const_iterator timeIter =
windowTimes_.begin(); auto timeIter = windowTimes_.cbegin();
FIFOStack<word>::const_iterator nameIter = auto nameIter = windowFieldNames_.cbegin();
windowFieldNames_.begin();
const Type* wOld = nullptr; const Type* wOld = nullptr;
for for
( (
; ;
timeIter != windowTimes_.end(); timeIter.good();
++timeIter, ++nameIter ++timeIter, ++nameIter
) )
{ {
@ -223,10 +222,9 @@ bool Foam::functionObjects::fieldAverageItem::calculatePrime2MeanField
{ {
// Not storing old time mean fields - treat all as TIME (integrated) // Not storing old time mean fields - treat all as TIME (integrated)
prime2MeanField = 0*prime2MeanField; prime2MeanField = 0*prime2MeanField;
FIFOStack<scalar>::const_iterator timeIter =
windowTimes_.begin(); auto timeIter = windowTimes_.cbegin();
FIFOStack<word>::const_iterator nameIter = auto nameIter = windowFieldNames_.cbegin();
windowFieldNames_.begin();
switch (base_) switch (base_)
{ {
@ -236,7 +234,7 @@ bool Foam::functionObjects::fieldAverageItem::calculatePrime2MeanField
++timeIter; ++timeIter;
++nameIter; ++nameIter;
if (timeIter == windowTimes_.end()) return false; if (!timeIter.good()) return false;
break; break;
} }
@ -252,7 +250,7 @@ bool Foam::functionObjects::fieldAverageItem::calculatePrime2MeanField
for for
( (
; ;
timeIter != windowTimes_.end(); timeIter.good();
++timeIter, ++nameIter ++timeIter, ++nameIter
) )
{ {
@ -272,7 +270,6 @@ bool Foam::functionObjects::fieldAverageItem::calculatePrime2MeanField
prime2MeanField /= windowLength; prime2MeanField /= windowLength;
break; break;
} }
default: default:

View File

@ -160,9 +160,8 @@ void Foam::functionObjects::nearWallFields::calcAddressing()
); );
InfoInFunction << "Dumping tracks to " << str.name() << endl; InfoInFunction << "Dumping tracks to " << str.name() << endl;
forAllConstIter(Cloud<findCellParticle>, cloud, iter) for (const findCellParticle& tp : cloud)
{ {
const findCellParticle& tp = iter();
str.write(linePointRef(tp.position(), tp.end())); str.write(linePointRef(tp.position(), tp.end()));
} }
} }
@ -186,9 +185,8 @@ void Foam::functionObjects::nearWallFields::calcAddressing()
{ {
start.setSize(nPatchFaces); start.setSize(nPatchFaces);
nPatchFaces = 0; nPatchFaces = 0;
forAllConstIter(Cloud<findCellParticle>, cloud, iter) for (const findCellParticle& tp : cloud)
{ {
const findCellParticle& tp = iter();
start[nPatchFaces++] = tp.position(); start[nPatchFaces++] = tp.position();
} }
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2013-2016 OpenFOAM Foundation | Copyright (C) 2013-2016 OpenFOAM Foundation
@ -121,7 +121,7 @@ void Foam::functionObjects::regionSizeDistribution::writeAlphaFields
// Knock out any cell not in patchRegions // Knock out any cell not in patchRegions
forAll(liquidCore, celli) forAll(liquidCore, celli)
{ {
label regioni = regions[celli]; const label regioni = regions[celli];
if (patchRegions.found(regioni)) if (patchRegions.found(regioni))
{ {
backgroundAlpha[celli] = 0; backgroundAlpha[celli] = 0;
@ -130,7 +130,7 @@ void Foam::functionObjects::regionSizeDistribution::writeAlphaFields
{ {
liquidCore[celli] = 0; liquidCore[celli] = 0;
scalar regionVol = regionVolume[regioni]; const scalar regionVol = regionVolume[regioni];
if (regionVol < maxDropletVol) if (regionVol < maxDropletVol)
{ {
backgroundAlpha[celli] = 0; backgroundAlpha[celli] = 0;
@ -144,8 +144,8 @@ void Foam::functionObjects::regionSizeDistribution::writeAlphaFields
{ {
Info<< " Volume of liquid-core = " Info<< " Volume of liquid-core = "
<< fvc::domainIntegrate(liquidCore).value() << fvc::domainIntegrate(liquidCore).value()
<< endl; << nl
Info<< " Volume of background = " << " Volume of background = "
<< fvc::domainIntegrate(backgroundAlpha).value() << fvc::domainIntegrate(backgroundAlpha).value()
<< endl; << endl;
} }
@ -549,19 +549,18 @@ bool Foam::functionObjects::regionSizeDistribution::write()
<< token::TAB << "Volume(mesh)" << token::TAB << "Volume(mesh)"
<< token::TAB << "Volume(" << alpha.name() << "):" << token::TAB << "Volume(" << alpha.name() << "):"
<< token::TAB << "nCells" << token::TAB << "nCells"
<< endl; << nl;
scalar meshSumVol = 0.0; scalar meshSumVol = 0.0;
scalar alphaSumVol = 0.0; scalar alphaSumVol = 0.0;
label nCells = 0; label nCells = 0;
Map<scalar>::const_iterator vIter = allRegionVolume.begin(); auto vIter = allRegionVolume.cbegin();
Map<scalar>::const_iterator aIter = allRegionAlphaVolume.begin(); auto aIter = allRegionAlphaVolume.cbegin();
Map<label>::const_iterator numIter = allRegionNumCells.begin(); auto numIter = allRegionNumCells.cbegin();
for for
( (
; ;
vIter != allRegionVolume.end() vIter.good() && aIter.good();
&& aIter != allRegionAlphaVolume.end();
++vIter, ++aIter, ++numIter ++vIter, ++aIter, ++numIter
) )
{ {
@ -569,7 +568,7 @@ bool Foam::functionObjects::regionSizeDistribution::write()
<< token::TAB << vIter() << token::TAB << vIter()
<< token::TAB << aIter() << token::TAB << aIter()
<< token::TAB << numIter() << token::TAB << numIter()
<< endl; << nl;
meshSumVol += vIter(); meshSumVol += vIter();
alphaSumVol += aIter(); alphaSumVol += aIter();
@ -583,20 +582,20 @@ bool Foam::functionObjects::regionSizeDistribution::write()
} }
if (log) if (log)
{ {
Info<< " Patch connected regions (liquid core):" << endl; Info<< " Patch connected regions (liquid core):" << nl;
Info<< token::TAB << " Region" Info<< token::TAB << " Region"
<< token::TAB << "Volume(mesh)" << token::TAB << "Volume(mesh)"
<< token::TAB << "Volume(" << alpha.name() << "):" << token::TAB << "Volume(" << alpha.name() << "):"
<< endl; << nl;
forAllConstIters(patchRegions, iter) forAllConstIters(patchRegions, iter)
{ {
const label regioni = iter.key(); const label regioni = iter.key();
Info<< " " << token::TAB << regioni Info<< " " << token::TAB << regioni
<< token::TAB << allRegionVolume[regioni] << token::TAB << allRegionVolume[regioni]
<< token::TAB << allRegionAlphaVolume[regioni] << endl; << token::TAB << allRegionAlphaVolume[regioni] << nl;
} }
Info<< endl; Info<< endl;
@ -604,19 +603,19 @@ bool Foam::functionObjects::regionSizeDistribution::write()
if (log) if (log)
{ {
Info<< " Background regions:" << endl; Info<< " Background regions:" << nl;
Info<< " " << token::TAB << "Region" Info<< " " << token::TAB << "Region"
<< token::TAB << "Volume(mesh)" << token::TAB << "Volume(mesh)"
<< token::TAB << "Volume(" << alpha.name() << "):" << token::TAB << "Volume(" << alpha.name() << "):"
<< endl; << nl;
Map<scalar>::const_iterator vIter = allRegionVolume.begin();
Map<scalar>::const_iterator aIter = allRegionAlphaVolume.begin(); auto vIter = allRegionVolume.cbegin();
auto aIter = allRegionAlphaVolume.cbegin();
for for
( (
; ;
vIter != allRegionVolume.end() vIter.good() && aIter.good();
&& aIter != allRegionAlphaVolume.end();
++vIter, ++aIter ++vIter, ++aIter
) )
{ {
@ -628,7 +627,7 @@ bool Foam::functionObjects::regionSizeDistribution::write()
{ {
Info<< " " << token::TAB << vIter.key() Info<< " " << token::TAB << vIter.key()
<< token::TAB << vIter() << token::TAB << vIter()
<< token::TAB << aIter() << endl; << token::TAB << aIter() << nl;
} }
} }
Info<< endl; Info<< endl;
@ -651,9 +650,9 @@ bool Foam::functionObjects::regionSizeDistribution::write()
// allRegionAlphaVolume since background might not have alpha in it. // allRegionAlphaVolume since background might not have alpha in it.
// Deleting regions where the volume-alpha-weighted is lower than // Deleting regions where the volume-alpha-weighted is lower than
// threshold // threshold
forAllIter(Map<scalar>, allRegionVolume, vIter) forAllIters(allRegionVolume, vIter)
{ {
label regioni = vIter.key(); const label regioni = vIter.key();
if if
( (
patchRegions.found(regioni) patchRegions.found(regioni)
@ -783,14 +782,14 @@ bool Foam::functionObjects::regionSizeDistribution::write()
<< " " << token::TAB << "Bin" << " " << token::TAB << "Bin"
<< token::TAB << "Min distance" << token::TAB << "Min distance"
<< token::TAB << "Count:" << token::TAB << "Count:"
<< endl; << nl;
scalar delta = 0.0; scalar delta = 0.0;
forAll(binDownCount, bini) forAll(binDownCount, bini)
{ {
Info<< " " << token::TAB << bini Info<< " " << token::TAB << bini
<< token::TAB << delta << token::TAB << delta
<< token::TAB << binDownCount[bini] << endl; << token::TAB << binDownCount[bini] << nl;
delta += deltaX; delta += deltaX;
} }
Info<< endl; Info<< endl;
@ -836,14 +835,14 @@ bool Foam::functionObjects::regionSizeDistribution::write()
<< " " << token::TAB << "Bin" << " " << token::TAB << "Bin"
<< token::TAB << "Min diameter" << token::TAB << "Min diameter"
<< token::TAB << "Count:" << token::TAB << "Count:"
<< endl; << nl;
scalar diam = 0.0; scalar diam = 0.0;
forAll(binCount, bini) forAll(binCount, bini)
{ {
Info<< " " << token::TAB << bini Info<< " " << token::TAB << bini
<< token::TAB << diam << token::TAB << diam
<< token::TAB << binCount[bini] << endl; << token::TAB << binCount[bini] << nl;
diam += delta; diam += delta;
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | \\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2012-2016 OpenFOAM Foundation | Copyright (C) 2012-2016 OpenFOAM Foundation
@ -43,18 +43,10 @@ Foam::Map<Type> Foam::functionObjects::regionSizeDistribution::regionSum
forAll(fld, celli) forAll(fld, celli)
{ {
label regioni = regions[celli]; const label regioni = regions[celli];
regionToSum(regioni, Type(Zero)) += fld[celli];
typename Map<Type>::iterator fnd = regionToSum.find(regioni);
if (fnd == regionToSum.end())
{
regionToSum.insert(regioni, fld[celli]);
}
else
{
fnd() += fld[celli];
}
} }
Pstream::mapCombineGather(regionToSum, plusEqOp<Type>()); Pstream::mapCombineGather(regionToSum, plusEqOp<Type>());
Pstream::mapCombineScatter(regionToSum); Pstream::mapCombineScatter(regionToSum);

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | \\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2017 OpenFOAM Foundation | Copyright (C) 2011-2017 OpenFOAM Foundation
@ -389,11 +389,11 @@ void Foam::streamLineParticle::readFields(Cloud<streamLineParticle>& c)
c.checkFieldIOobject(c, sampledPositions); c.checkFieldIOobject(c, sampledPositions);
label i = 0; label i = 0;
forAllIter(Cloud<streamLineParticle>, c, iter) for (streamLineParticle& p : c)
{ {
iter().lifeTime_ = lifeTime[i]; p.lifeTime_ = lifeTime[i];
iter().sampledPositions_.transfer(sampledPositions[i]); p.sampledPositions_.transfer(sampledPositions[i]);
i++; ++i;
} }
} }
@ -402,7 +402,7 @@ void Foam::streamLineParticle::writeFields(const Cloud<streamLineParticle>& c)
{ {
particle::writeFields(c); particle::writeFields(c);
label np = c.size(); const label np = c.size();
IOField<label> lifeTime IOField<label> lifeTime
( (
@ -416,11 +416,11 @@ void Foam::streamLineParticle::writeFields(const Cloud<streamLineParticle>& c)
); );
label i = 0; label i = 0;
forAllConstIter(Cloud<streamLineParticle>, c, iter) for (const streamLineParticle& p : c)
{ {
lifeTime[i] = iter().lifeTime_; lifeTime[i] = p.lifeTime_;
sampledPositions[i] = iter().sampledPositions_; sampledPositions[i] = p.sampledPositions_;
i++; ++i;
} }
lifeTime.write(np > 0); lifeTime.write(np > 0);

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2015-2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2015-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation | Copyright (C) 2011-2016 OpenFOAM Foundation
@ -46,7 +46,7 @@ void Foam::functionObjects::surfaceInterpolate::interpolateFields()
HashTable<const VolFieldType*> flds(obr_.lookupClass<VolFieldType>()); HashTable<const VolFieldType*> flds(obr_.lookupClass<VolFieldType>());
forAllConstIter(typename HashTable<const VolFieldType*>, flds, iter) forAllConstIters(flds, iter)
{ {
const VolFieldType& fld = *iter(); const VolFieldType& fld = *iter();

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2015-2017 OpenCFD Ltd. \\ / A nd | Copyright (C) 2015-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation | Copyright (C) 2011-2016 OpenFOAM Foundation
@ -305,16 +305,8 @@ bool Foam::functionObjects::wallBoundedStreamLine::read(const dictionary& dict)
forAll(f, fp) forAll(f, fp)
{ {
const edge e(f[fp], f.nextLabel(fp)); const edge e(f[fp], f.nextLabel(fp));
EdgeMap<label>::iterator eFnd = numFacesPerEdge.find(e);
if (eFnd != numFacesPerEdge.end()) ++(numFacesPerEdge(e, 0));
{
eFnd()++;
}
else
{
numFacesPerEdge.insert(e, 1);
}
} }
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd. \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation | Copyright (C) 2011-2016 OpenFOAM Foundation
@ -218,11 +218,11 @@ void Foam::wallBoundedStreamLineParticle::readFields
c.checkFieldIOobject(c, sampledPositions); c.checkFieldIOobject(c, sampledPositions);
label i = 0; label i = 0;
forAllIter(Cloud<wallBoundedStreamLineParticle>, c, iter) for (wallBoundedStreamLineParticle& p : c)
{ {
iter().lifeTime_ = lifeTime[i]; p.lifeTime_ = lifeTime[i];
iter().sampledPositions_.transfer(sampledPositions[i]); p.sampledPositions_.transfer(sampledPositions[i]);
i++; ++i;
} }
} }
@ -234,7 +234,7 @@ void Foam::wallBoundedStreamLineParticle::writeFields
{ {
wallBoundedParticle::writeFields(c); wallBoundedParticle::writeFields(c);
label np = c.size(); const label np = c.size();
IOField<label> lifeTime IOField<label> lifeTime
( (
@ -248,11 +248,11 @@ void Foam::wallBoundedStreamLineParticle::writeFields
); );
label i = 0; label i = 0;
forAllConstIter(Cloud<wallBoundedStreamLineParticle>, c, iter) for (const wallBoundedStreamLineParticle& p : c)
{ {
lifeTime[i] = iter().lifeTime_; lifeTime[i] = p.lifeTime_;
sampledPositions[i] = iter().sampledPositions_; sampledPositions[i] = p.sampledPositions_;
i++; ++i;
} }
lifeTime.write(); lifeTime.write();

View File

@ -240,7 +240,7 @@ void Foam::functionObjects::forces::initialiseBins()
const scalarField dd(mesh_.C() & binDir_); const scalarField dd(mesh_.C() & binDir_);
forAllConstIter(HashTable<const porosityModel*>, models, iter) forAllConstIters(models, iter)
{ {
const porosityModel& pm = *iter(); const porosityModel& pm = *iter();
const labelList& cellZoneIDs = pm.cellZoneIDs(); const labelList& cellZoneIDs = pm.cellZoneIDs();

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2015-2016 OpenCFD Ltd. \\ / A nd | Copyright (C) 2015-2016, 2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2015 OpenFOAM Foundation | Copyright (C) 2015 OpenFOAM Foundation
@ -121,10 +121,8 @@ void Foam::functionObjects::runTimeControls::averageCondition::calc
windowValues.push(currentValue); windowValues.push(currentValue);
// Calculate the window average // Calculate the window average
typename FIFOStack<scalar>::const_iterator timeIter = auto timeIter = windowTimes.cbegin();
windowTimes.begin(); auto valueIter = windowValues.cbegin();
typename FIFOStack<Type>::const_iterator valueIter =
windowValues.begin();
meanValue = pTraits<Type>::zero; meanValue = pTraits<Type>::zero;
Type valueOld(pTraits<Type>::zero); Type valueOld(pTraits<Type>::zero);
@ -132,7 +130,7 @@ void Foam::functionObjects::runTimeControls::averageCondition::calc
for for
( (
label i = 0; label i = 0;
timeIter != windowTimes.end(); timeIter.good();
++i, ++timeIter, ++valueIter ++i, ++timeIter, ++valueIter
) )
{ {