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

- reduced clutter when iterating over containers
This commit is contained in:
Mark Olesen
2019-01-07 09:20:51 +01:00
parent 1458b4f689
commit 14a404170b
76 changed files with 592 additions and 728 deletions

View File

@ -842,16 +842,11 @@ int main(int argc, char *argv[])
label i = 0;
forAllIter
(
Cloud<indexedParticle>,
lagrangianPositions[cloudI],
iter
)
for (indexedParticle& p : lagrangianPositions[cloudI])
{
iter().index() = i++;
p.index() = i++;
label celli = iter().cell();
label celli = p.cell();
// Check
if (celli < 0 || celli >= mesh.nCells())
@ -859,14 +854,14 @@ int main(int argc, char *argv[])
FatalErrorInFunction
<< "Illegal cell number " << celli
<< " for particle with index "
<< iter().index()
<< p.index()
<< " at position "
<< iter().position() << nl
<< p.position() << nl
<< "Cell number should be between 0 and "
<< mesh.nCells()-1 << nl
<< "On this mesh the particle should"
<< " be in cell "
<< mesh.findCell(iter().position())
<< mesh.findCell(p.position())
<< exit(FatalError);
}
@ -876,7 +871,7 @@ int main(int argc, char *argv[])
new SLList<indexedParticle*>();
}
cellParticles[cloudI][celli]->append(&iter());
cellParticles[cloudI][celli]->append(&p);
}
// Read fields

View File

@ -55,16 +55,16 @@ void Foam::domainDecomposition::addInterProcFace
List<DynamicList<DynamicList<label>>>& interPatchFaces
) const
{
Map<label>::iterator patchiter = nbrToInterPatch[ownerProc].find(nbrProc);
// Introduce turning index only for internal faces (are duplicated).
label ownerIndex = facei+1;
label nbrIndex = -(facei+1);
const label ownerIndex = facei+1;
const label nbrIndex = -(facei+1);
if (patchiter != nbrToInterPatch[ownerProc].end())
const auto patchiter = nbrToInterPatch[ownerProc].cfind(nbrProc);
if (patchiter.found())
{
// Existing interproc patch. Add to both sides.
label toNbrProcPatchi = patchiter();
const label toNbrProcPatchi = *patchiter;
interPatchFaces[ownerProc][toNbrProcPatchi].append(ownerIndex);
if (isInternalFace(facei))
@ -76,8 +76,9 @@ void Foam::domainDecomposition::addInterProcFace
else
{
// Create new interproc patches.
label toNbrProcPatchi = nbrToInterPatch[ownerProc].size();
const label toNbrProcPatchi = nbrToInterPatch[ownerProc].size();
nbrToInterPatch[ownerProc].insert(nbrProc, toNbrProcPatchi);
DynamicList<label> oneFace;
oneFace.append(ownerIndex);
interPatchFaces[ownerProc].append(oneFace);

View File

@ -435,59 +435,55 @@ void Foam::faMeshDecomposition::decomposeMesh()
// inside boundaries for the owner processor and try to find
// this inter-processor patch.
const label ownerProc = faceToProc_[owner[edgeI]];
const label neighbourProc = faceToProc_[neighbour[edgeI]];
SLList<label>::iterator curInterProcBdrsOwnIter =
interProcBoundaries[ownerProc].begin();
SLList<SLList<label>>::iterator curInterProcBEdgesOwnIter =
interProcBEdges[ownerProc].begin();
bool interProcBouFound = false;
const label ownProc = faceToProc_[owner[edgeI]];
const label neiProc = faceToProc_[neighbour[edgeI]];
auto curInterProcBdrsOwnIter =
interProcBoundaries[ownProc].cbegin();
auto curInterProcBEdgesOwnIter =
interProcBEdges[ownProc].begin();
// WARNING: Synchronous SLList iterators
for
(
;
curInterProcBdrsOwnIter
!= interProcBoundaries[ownerProc].end()
&& curInterProcBEdgesOwnIter
!= interProcBEdges[ownerProc].end();
++curInterProcBdrsOwnIter, ++curInterProcBEdgesOwnIter
curInterProcBdrsOwnIter.good()
&& curInterProcBEdgesOwnIter.good();
++curInterProcBdrsOwnIter,
++curInterProcBEdgesOwnIter
)
{
if (curInterProcBdrsOwnIter() == neighbourProc)
if (curInterProcBdrsOwnIter() == neiProc)
{
// the inter - processor boundary exists. Add the face
interProcBouFound = true;
bool neighbourFound = false;
curInterProcBEdgesOwnIter().append(edgeI);
SLList<label>::iterator curInterProcBdrsNeiIter =
interProcBoundaries[neighbourProc].begin();
auto curInterProcBdrsNeiIter =
interProcBoundaries[neiProc].cbegin();
SLList<SLList<label>>::iterator
curInterProcBEdgesNeiIter =
interProcBEdges[neighbourProc].begin();
bool neighbourFound = false;
auto curInterProcBEdgesNeiIter =
interProcBEdges[neiProc].begin();
// WARNING: Synchronous SLList iterators
for
(
;
curInterProcBdrsNeiIter !=
interProcBoundaries[neighbourProc].end()
&& curInterProcBEdgesNeiIter !=
interProcBEdges[neighbourProc].end();
curInterProcBdrsNeiIter.good()
&& curInterProcBEdgesNeiIter.good();
++curInterProcBdrsNeiIter,
++curInterProcBEdgesNeiIter
)
{
if (curInterProcBdrsNeiIter() == ownerProc)
if (curInterProcBdrsNeiIter() == ownProc)
{
// boundary found. Add the face
neighbourFound = true;
@ -504,7 +500,7 @@ void Foam::faMeshDecomposition::decomposeMesh()
("faDomainDecomposition::decomposeMesh()")
<< "Inconsistency in inter - "
<< "processor boundary lists for processors "
<< ownerProc << " and " << neighbourProc
<< ownProc << " and " << neiProc
<< abort(FatalError);
}
}
@ -520,12 +516,13 @@ void Foam::faMeshDecomposition::decomposeMesh()
// set the new addressing information
// owner
interProcBoundaries[ownerProc].append(neighbourProc);
interProcBEdges[ownerProc].append(SLList<label>(edgeI));
interProcBoundaries[ownProc].append(neiProc);
interProcBEdges[ownProc].append(SLList<label>(edgeI));
// neighbour
interProcBoundaries[neighbourProc].append(ownerProc);
interProcBEdges[neighbourProc].append
interProcBoundaries[neiProc].append(ownProc);
interProcBEdges[neiProc]
.append
(
SLList<label>(edgeI)
);
@ -624,64 +621,59 @@ void Foam::faMeshDecomposition::decomposeMesh()
cyclicParallel_ = true;
label ownerProc = faceToProc_[firstEdgeFaces[edgeI]];
label neighbourProc =
bool interProcBouFound = false;
const label ownProc =
faceToProc_[firstEdgeFaces[edgeI]];
const label neiProc =
faceToProc_[secondEdgeFaces[edgeI]];
SLList<label>::iterator curInterProcBdrsOwnIter =
interProcBoundaries[ownerProc].begin();
auto curInterProcBdrsOwnIter =
interProcBoundaries[ownProc].cbegin();
SLList<SLList<label>>::iterator
curInterProcBEdgesOwnIter =
interProcBEdges[ownerProc].begin();
bool interProcBouFound = false;
auto curInterProcBEdgesOwnIter =
interProcBEdges[ownProc].begin();
// WARNING: Synchronous SLList iterators
for
(
;
curInterProcBdrsOwnIter !=
interProcBoundaries[ownerProc].end()
&& curInterProcBEdgesOwnIter !=
interProcBEdges[ownerProc].end();
curInterProcBdrsOwnIter.good()
&& curInterProcBEdgesOwnIter.good();
++curInterProcBdrsOwnIter,
++curInterProcBEdgesOwnIter
)
{
if (curInterProcBdrsOwnIter() == neighbourProc)
if (curInterProcBdrsOwnIter() == neiProc)
{
// the inter - processor boundary exists.
// Add the face
interProcBouFound = true;
curInterProcBEdgesOwnIter().append
(patchStart + edgeI);
SLList<label>::iterator curInterProcBdrsNeiIter
= interProcBoundaries[neighbourProc].begin();
SLList<SLList<label>>::iterator
curInterProcBEdgesNeiIter =
interProcBEdges[neighbourProc].begin();
bool neighbourFound = false;
curInterProcBEdgesOwnIter()
.append(patchStart + edgeI);
auto curInterProcBdrsNeiIter
= interProcBoundaries[neiProc].cbegin();
auto curInterProcBEdgesNeiIter =
interProcBEdges[neiProc].begin();
// WARNING: Synchronous SLList iterators
for
(
;
curInterProcBdrsNeiIter
!= interProcBoundaries[neighbourProc].end()
&& curInterProcBEdgesNeiIter
!= interProcBEdges[neighbourProc].end();
curInterProcBdrsNeiIter.good()
&& curInterProcBEdgesNeiIter.good();
++curInterProcBdrsNeiIter,
++curInterProcBEdgesNeiIter
)
{
if (curInterProcBdrsNeiIter() == ownerProc)
if (curInterProcBdrsNeiIter() == ownProc)
{
// boundary found. Add the face
neighbourFound = true;
@ -705,7 +697,7 @@ void Foam::faMeshDecomposition::decomposeMesh()
"faDomainDecomposition::decomposeMesh()"
) << "Inconsistency in inter-processor "
<< "boundary lists for processors "
<< ownerProc << " and " << neighbourProc
<< ownProc << " and " << neiProc
<< " in cyclic boundary matching"
<< abort(FatalError);
}
@ -722,15 +714,13 @@ void Foam::faMeshDecomposition::decomposeMesh()
// set the new addressing information
// owner
interProcBoundaries[ownerProc]
.append(neighbourProc);
interProcBEdges[ownerProc]
interProcBoundaries[ownProc].append(neiProc);
interProcBEdges[ownProc]
.append(SLList<label>(patchStart + edgeI));
// neighbour
interProcBoundaries[neighbourProc]
.append(ownerProc);
interProcBEdges[neighbourProc]
interProcBoundaries[neiProc].append(ownProc);
interProcBEdges[neiProc]
.append
(
SLList<label>
@ -745,13 +735,13 @@ void Foam::faMeshDecomposition::decomposeMesh()
else
{
// This cyclic edge remains on the processor
label ownerProc = faceToProc_[firstEdgeFaces[edgeI]];
label ownProc = faceToProc_[firstEdgeFaces[edgeI]];
// add the edge
procEdgeList[ownerProc].append(patchStart + edgeI);
procEdgeList[ownProc].append(patchStart + edgeI);
// increment the number of edges for this patch
procPatchSize_[ownerProc][patchI]++;
procPatchSize_[ownProc][patchI]++;
// Note: I cannot add the other side of the cyclic
// boundary here because this would violate the order.
@ -771,14 +761,14 @@ void Foam::faMeshDecomposition::decomposeMesh()
)
{
// This cyclic edge remains on the processor
label ownerProc = faceToProc_[firstEdgeFaces[edgeI]];
label ownProc = faceToProc_[firstEdgeFaces[edgeI]];
// add the second edge
procEdgeList[ownerProc].append
procEdgeList[ownProc].append
(patchStart + cycOffset + edgeI);
// increment the number of edges for this patch
procPatchSize_[ownerProc][patchI]++;
procPatchSize_[ownProc][patchI]++;
}
}
}
@ -806,15 +796,9 @@ void Foam::faMeshDecomposition::decomposeMesh()
// calculate the size
label nEdgesOnProcessor = curProcEdges.size();
for
(
SLList<SLList<label>>::iterator curInterProcBEdgesIter =
interProcBEdges[procI].begin();
curInterProcBEdgesIter != interProcBEdges[procI].end();
++curInterProcBEdgesIter
)
for (const auto& bedges : interProcBEdges)
{
nEdgesOnProcessor += curInterProcBEdgesIter().size();
nEdgesOnProcessor += bedges.size();
}
curProcEdgeAddressing.setSize(nEdgesOnProcessor);
@ -830,16 +814,11 @@ void Foam::faMeshDecomposition::decomposeMesh()
// Add internal and boundary edges
// Remember to increment the index by one such that the
// turning index works properly.
for
(
SLList<label>::iterator curProcEdgeIter = curProcEdges.begin();
curProcEdgeIter != curProcEdges.end();
++curProcEdgeIter
)
for (const label procEdgei : curProcEdges)
{
curProcEdgeAddressing[nEdges] = curProcEdgeIter();
// curProcEdgeAddressing[nEdges] = curProcEdgeIter() + 1;
nEdges++;
curProcEdgeAddressing[nEdges] = procEdgei;
// curProcEdgeAddressing[nEdges] = procEdgei + 1;
++nEdges;
}
// Add inter-processor boundary edges. At the beginning of each
@ -862,18 +841,19 @@ void Foam::faMeshDecomposition::decomposeMesh()
label nProcPatches = 0;
SLList<label>::iterator curInterProcBdrsIter =
interProcBoundaries[procI].begin();
auto curInterProcBdrsIter =
interProcBoundaries[procI].cbegin();
SLList<SLList<label>>::iterator curInterProcBEdgesIter =
interProcBEdges[procI].begin();
auto curInterProcBEdgesIter =
interProcBEdges[procI].cbegin();
for
(
;
curInterProcBdrsIter != interProcBoundaries[procI].end()
&& curInterProcBEdgesIter != interProcBEdges[procI].end();
++curInterProcBdrsIter, ++curInterProcBEdgesIter
curInterProcBdrsIter.good()
&& curInterProcBEdgesIter.good();
++curInterProcBdrsIter,
++curInterProcBEdgesIter
)
{
curProcNeighbourProcessors[nProcPatches] =
@ -889,37 +869,31 @@ void Foam::faMeshDecomposition::decomposeMesh()
// add faces for this processor boundary
for
(
SLList<label>::iterator curEdgesIter =
curInterProcBEdgesIter().begin();
curEdgesIter != curInterProcBEdgesIter().end();
++curEdgesIter
)
for (const label edgei : *curInterProcBEdgesIter)
{
// add the edges
// Remember to increment the index by one such that the
// turning index works properly.
if (faceToProc_[owner[curEdgesIter()]] == procI)
if (faceToProc_[owner[edgei]] == procI)
{
curProcEdgeAddressing[nEdges] = curEdgesIter();
// curProcEdgeAddressing[nEdges] = curEdgesIter() + 1;
curProcEdgeAddressing[nEdges] = edgei;
// curProcEdgeAddressing[nEdges] = edgei + 1;
}
else
{
// turning edge
curProcEdgeAddressing[nEdges] = curEdgesIter();
// curProcEdgeAddressing[nEdges] = -(curEdgesIter() + 1);
curProcEdgeAddressing[nEdges] = edgei;
// curProcEdgeAddressing[nEdges] = -(edgei + 1);
}
// increment the size
curSize++;
++curSize;
nEdges++;
++nEdges;
}
nProcPatches++;
++nProcPatches;
}
}
}

View File

@ -62,7 +62,7 @@ Foam::lagrangianFieldDecomposer::lagrangianFieldDecomposer
{
SLList<indexedParticle*>& particlePtrs = *cellParticles[celli];
forAllConstIter(SLList<indexedParticle*>, particlePtrs, iter)
forAllConstIters(particlePtrs, iter)
{
const indexedParticle& ppi = *iter();
particleIndices_[pi++] = ppi.index();

View File

@ -592,7 +592,7 @@ int main(int argc, char *argv[])
const word cloudName = word::validate(iter.key());
// Objects (on arbitrary processor)
const IOobjectList& cloudObjs = iter.object();
const IOobjectList& cloudObjs = iter.val();
Info<< "Reconstructing lagrangian fields for cloud "
<< cloudName << nl << endl;
@ -831,15 +831,15 @@ int main(int argc, char *argv[])
cellSet& cSet = cellSets[setI];
cSet.instance() = runTime.timeName();
forAllConstIter(cellSet, procSet, iter)
for (const label celli : procSet)
{
cSet.insert(cellMap[iter.key()]);
cSet.insert(cellMap[celli]);
}
}
// faceSets
const labelList& faceMap =
procMeshes.faceProcAddressing()[proci];
procMeshes.faceProcAddressing()[proci];
IOobjectList fSets
(
@ -867,9 +867,9 @@ int main(int argc, char *argv[])
faceSet& fSet = faceSets[setI];
fSet.instance() = runTime.timeName();
forAllConstIter(faceSet, procSet, iter)
for (const label facei : procSet)
{
fSet.insert(mag(faceMap[iter.key()])-1);
fSet.insert(mag(faceMap[facei])-1);
}
}
// pointSets
@ -901,25 +901,26 @@ int main(int argc, char *argv[])
pointSet& pSet = pointSets[setI];
pSet.instance() = runTime.timeName();
forAllConstIter(pointSet, propSet, iter)
for (const label pointi : propSet)
{
pSet.insert(pointMap[iter.key()]);
pSet.insert(pointMap[pointi]);
}
}
}
// Write sets
forAll(cellSets, i)
for (const auto& set : cellSets)
{
cellSets[i].write();
set.write();
}
forAll(faceSets, i)
for (const auto& set : faceSets)
{
faceSets[i].write();
set.write();
}
forAll(pointSets, i)
for (const auto& set : pointSets)
{
pointSets[i].write();
set.write();
}
}

View File

@ -151,10 +151,8 @@ Foam::parLagrangianRedistributor::redistributeLagrangianPositions
labelList destProc(lpi.size());
label particleI = 0;
forAllIter(passivePositionParticleCloud, lpi, iter)
for (passivePositionParticle& ppi : lpi)
{
passivePositionParticle& ppi = iter();
const label destProcI = destinationProcID_[ppi.cell()];
const label destCellI = destinationCell_[ppi.cell()];
@ -222,14 +220,8 @@ Foam::parLagrangianRedistributor::redistributeLagrangianPositions
passivePositionParticle::iNew(tgtMesh_)
);
forAllIter
(
IDLList<passivePositionParticle>,
newParticles,
newpIter
)
for (passivePositionParticle& newp : newParticles)
{
passivePositionParticle& newp = newpIter();
lagrangianPositions.addParticle(newParticles.remove(&newp));
}
}

View File

@ -284,7 +284,7 @@ Foam::label Foam::parLagrangianRedistributor::redistributeStoredFields
label nFields = 0;
forAllIters(fields, iter)
{
Container& field = *(iter.object());
Container& field = *(iter.val());
if (!nFields++)
{

View File

@ -746,7 +746,7 @@ void correctCoupledBoundaryConditions(fvMesh& mesh)
mesh.objectRegistry::lookupClass<GeoField>()
);
forAllIter(typename HashTable<GeoField*>, flds, iter)
forAllIters(flds, iter)
{
GeoField& fld = *iter();
@ -1944,16 +1944,11 @@ void readLagrangian
);
//forAllConstIter
//(
// unmappedPassivePositionParticleCloud,
// clouds[i],
// iter
//)
//for (passivePositionParticle& p : clouds[i]))
//{
// Pout<< "Particle position:" << iter().position()
// << " cell:" << iter().cell()
// << " with cc:" << mesh.cellCentres()[iter().cell()]
// Pout<< "Particle position:" << p.position()
// << " cell:" << p.cell()
// << " with cc:" << mesh.cellCentres()[p.cell()]
// << endl;
//}