ENH: simplify add/remove patch/zone groups

STYLE: reuse polyBoundaryMesh patchSizes(), patchStarts()
This commit is contained in:
Mark Olesen 2023-08-21 15:17:54 +02:00
parent 12916cd7a3
commit 2395e493d1
16 changed files with 167 additions and 242 deletions

View File

@ -87,11 +87,7 @@ label addPatch
)
);
auto& pp = *ppPtr;
if (!groupName.empty())
{
pp.inGroups().appendUniq(groupName);
}
pp.addGroup(groupName);
// Add patch, create calculated everywhere

View File

@ -93,6 +93,25 @@ Foam::patchIdentifier::patchIdentifier
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::patchIdentifier::removeGroup(const word& name)
{
label idx = name.empty() ? -1 : inGroups_.find(name);
if (idx >= 0)
{
for (label i = idx + 1; i < inGroups_.size(); ++i)
{
if (inGroups_[i] != name)
{
inGroups_[idx] = std::move(inGroups_[i]);
++idx;
}
}
inGroups_.resize(idx);
}
}
void Foam::patchIdentifier::write(Ostream& os) const
{
if (!physicalType_.empty())

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2013 OpenFOAM Foundation
Copyright (C) 2020-2021 OpenCFD Ltd.
Copyright (C) 2020-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -36,8 +36,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef patchIdentifier_H
#define patchIdentifier_H
#ifndef Foam_patchIdentifier_H
#define Foam_patchIdentifier_H
#include "wordList.H"
@ -133,59 +133,47 @@ public:
// Member Functions
//- The patch name
const word& name() const noexcept
{
return name_;
}
const word& name() const noexcept { return name_; }
//- Modifiable patch name
word& name() noexcept
{
return name_;
}
word& name() noexcept { return name_; }
//- The index of this patch in the boundaryMesh
label index() const noexcept
{
return index_;
}
label index() const noexcept { return index_; }
//- Modifiable index of this patch in the boundaryMesh
label& index() noexcept
{
return index_;
}
label& index() noexcept { return index_; }
//- The (optional) physical type of the patch
const word& physicalType() const noexcept
{
return physicalType_;
}
const word& physicalType() const noexcept { return physicalType_; }
//- Modifiable (optional) physical type of the patch
word& physicalType() noexcept
{
return physicalType_;
}
word& physicalType() noexcept { return physicalType_; }
//- The (optional) groups that the patch belongs to
const wordList& inGroups() const noexcept
{
return inGroups_;
}
const wordList& inGroups() const noexcept { return inGroups_; }
//- Modifiable (optional) groups that the patch belongs to
wordList& inGroups() noexcept
{
return inGroups_;
}
wordList& inGroups() noexcept { return inGroups_; }
//- True if given name is in a group
bool inGroup(const word& name) const
{
return inGroups_.found(name);
return (!name.empty() && inGroups_.contains(name));
}
//- Add (unique) group for the patch
void addGroup(const word& name)
{
if (!name.empty() && !inGroups_.contains(name))
{
inGroups_.push_back(name);
}
}
//- Remove group for the patch
void removeGroup(const word& name);
//- Write (physicalType, inGroups) dictionary entries
//- (without surrounding braces)
void write(Ostream& os) const;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd.
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -92,6 +92,25 @@ Foam::zoneIdentifier::zoneIdentifier
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::zoneIdentifier::removeGroup(const word& name)
{
label idx = name.empty() ? -1 : inGroups_.find(name);
if (idx >= 0)
{
for (label i = idx + 1; i < inGroups_.size(); ++i)
{
if (inGroups_[i] != name)
{
inGroups_[idx] = std::move(inGroups_[i]);
++idx;
}
}
inGroups_.resize(idx);
}
}
void Foam::zoneIdentifier::write(Ostream& os) const
{
if (!physicalType_.empty())

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd.
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -38,8 +38,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef zoneIdentifier_H
#define zoneIdentifier_H
#ifndef Foam_zoneIdentifier_H
#define Foam_zoneIdentifier_H
#include "wordList.H"
@ -121,59 +121,47 @@ public:
// Member Functions
//- The zone name
const word& name() const noexcept
{
return name_;
}
const word& name() const noexcept { return name_; }
//- Modifiable zone name
word& name() noexcept
{
return name_;
}
word& name() noexcept { return name_; }
//- The index of this zone in the zone list
label index() const noexcept
{
return index_;
}
label index() const noexcept { return index_; }
//- Modifiable index of this zone in the zone list
label& index() noexcept
{
return index_;
}
label& index() noexcept { return index_; }
//- The (optional) type of the zone
const word& physicalType() const noexcept
{
return physicalType_;
}
const word& physicalType() const noexcept { return physicalType_; }
//- Modifiable (optional) type of the zone
word& physicalType() noexcept
{
return physicalType_;
}
word& physicalType() noexcept { return physicalType_; }
//- The (optional) groups that the zone belongs to
const wordList& inGroups() const noexcept
{
return inGroups_;
}
const wordList& inGroups() const noexcept { return inGroups_; }
//- Modifiable (optional) groups that the zone belongs to
wordList& inGroups() noexcept
{
return inGroups_;
}
wordList& inGroups() noexcept { return inGroups_; }
//- True if given name is in a group
bool inGroup(const word& name) const
{
return inGroups_.found(name);
return (!name.empty() && inGroups_.contains(name));
}
//- Add (unique) group for the zone
void addGroup(const word& name)
{
if (!name.empty() && !inGroups_.contains(name))
{
inGroups_.push_back(name);
}
}
//- Remove group for the zone
void removeGroup(const word& name);
//- Write (physicalType, inGroups) dictionary entries
//- (without surrounding braces)
void write(Ostream& os) const;

View File

@ -84,9 +84,7 @@ void Foam::polyBoundaryMesh::calcGroupIDs() const
forAll(patches, patchi)
{
const wordList& groups = patches[patchi].inGroups();
for (const word& groupName : groups)
for (const word& groupName : patches[patchi].inGroups())
{
groupLookup(groupName).push_back(patchi);
}
@ -504,34 +502,24 @@ void Foam::polyBoundaryMesh::setGroup
polyPatchList& patches = *this;
boolList donePatch(patches.size(), false);
boolList pending(patches.size(), true);
// Add to specified patches
for (const label patchi : patchIDs)
{
patches[patchi].inGroups().push_uniq(groupName);
donePatch[patchi] = true;
if (pending.test(patchi))
{
pending.unset(patchi);
patches[patchi].addGroup(groupName);
}
}
// Remove from other patches
forAll(patches, patchi)
{
if (!donePatch[patchi])
if (pending.test(patchi))
{
wordList& groups = patches[patchi].inGroups();
if (groups.found(groupName))
{
label newi = 0;
forAll(groups, i)
{
if (groups[i] != groupName)
{
groups[newi++] = groups[i];
}
}
groups.resize(newi);
}
patches[patchi].removeGroup(groupName);
}
}
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2013 OpenFOAM Foundation
Copyright (C) 2021 OpenCFD Ltd.
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -54,7 +54,7 @@ Foam::wallPolyPatch::wallPolyPatch
polyPatch(name, size, start, index, bm, patchType)
{
// wall is not constraint type so add wall group explicitly
inGroups().appendUniq(typeName);
addGroup(typeName);
}
@ -70,7 +70,7 @@ Foam::wallPolyPatch::wallPolyPatch
polyPatch(name, dict, index, bm, patchType)
{
// wall is not constraint type so add wall group explicitly
inGroups().appendUniq(typeName);
addGroup(typeName);
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2022 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -102,7 +102,7 @@ Foam::polyPatch::polyPatch
{
if (constraintType(patchType))
{
inGroups().appendUniq(patchType);
addGroup(patchType);
}
}
@ -158,7 +158,7 @@ Foam::polyPatch::polyPatch
{
if (constraintType(patchType))
{
inGroups().appendUniq(patchType);
addGroup(patchType);
}
}

View File

@ -129,9 +129,7 @@ void Foam::ZoneMesh<ZoneType, MeshType>::calcGroupIDs() const
forAll(zones, zonei)
{
const wordList& groups = zones[zonei].inGroups();
for (const word& groupName : groups)
for (const word& groupName : zones[zonei].inGroups())
{
groupLookup(groupName).push_back(zonei);
}
@ -716,34 +714,24 @@ void Foam::ZoneMesh<ZoneType, MeshType>::setGroup
PtrList<ZoneType>& zones = *this;
boolList doneZone(zones.size(), false);
boolList pending(zones.size(), true);
// Add to specified zones
for (const label zonei : zoneIDs)
{
zones[zonei].inGroups().push_uniq(groupName);
doneZone[zonei] = true;
if (pending.test(zonei))
{
pending.unset(zonei);
zones[zonei].addGroup(groupName);
}
}
// Remove from other zones
forAll(zones, zonei)
{
if (!doneZone[zonei])
if (pending.test(zonei))
{
wordList& groups = zones[zonei].inGroups();
if (groups.found(groupName))
{
label newi = 0;
forAll(groups, i)
{
if (groups[i] != groupName)
{
groups[newi++] = groups[i];
}
}
groups.resize(newi);
}
zones[zonei].removeGroup(groupName);
}
}
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019-2021 OpenCFD Ltd.
Copyright (C) 2019-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -55,41 +55,37 @@ Foam::label Foam::polyMeshAdder::patchIndex
const word& pType = p.type();
const word& pName = p.name();
const label patchi = allPatchNames.find(pName);
label patchi = allPatchNames.find(pName);
if (patchi == -1)
if (patchi < 0)
{
// Patch not found. Append to the list
allPatchNames.append(pName);
allPatchTypes.append(pType);
// Not found - add to the lists
patchi = allPatchNames.size();
return allPatchNames.size() - 1;
allPatchNames.push_back(pName);
allPatchTypes.push_back(pType);
}
else if (allPatchTypes[patchi] == pType)
{
// Found name and types match
return patchi;
}
else
else if (allPatchTypes[patchi] != pType)
{
// Found the name, but type is different
patchi = allPatchNames.size();
// Duplicate name is not allowed. Create a composite name from the
// patch name and case name
const word& caseName = p.boundaryMesh().mesh().time().caseName();
allPatchNames.append(pName + "_" + caseName);
allPatchTypes.append(pType);
allPatchNames.push_back(pName + "_" + caseName);
allPatchTypes.push_back(pType);
Pout<< "label patchIndex(const polyPatch& p) : "
<< "Patch " << p.index() << " named "
<< pName << " in mesh " << caseName
<< " already exists, but patch types"
<< " do not match.\nCreating a composite name as "
<< allPatchNames.last() << endl;
return allPatchNames.size() - 1;
<< allPatchNames.back() << endl;
}
return patchi;
}
@ -100,19 +96,17 @@ Foam::label Foam::polyMeshAdder::zoneIndex
DynamicList<word>& names
)
{
const label zoneI = names.find(curName);
label zonei = names.find(curName);
if (zoneI != -1)
if (zonei < 0)
{
return zoneI;
}
else
{
// Not found. Add new name to the list
names.append(curName);
// Not found - add to the list
zonei = names.size();
return names.size() - 1;
names.push_back(curName);
}
return zonei;
}
@ -129,8 +123,8 @@ void Foam::polyMeshAdder::mergePatchNames
)
{
// Insert the mesh0 patches and zones
allPatchNames.append(patches0.names());
allPatchTypes.append(patches0.types());
allPatchNames.push_back(patches0.names());
allPatchTypes.push_back(patches0.types());
// Patches
@ -165,34 +159,6 @@ void Foam::polyMeshAdder::mergePatchNames
}
Foam::labelList Foam::polyMeshAdder::getPatchStarts
(
const polyBoundaryMesh& patches
)
{
labelList patchStarts(patches.size());
forAll(patches, patchi)
{
patchStarts[patchi] = patches[patchi].start();
}
return patchStarts;
}
Foam::labelList Foam::polyMeshAdder::getPatchSizes
(
const polyBoundaryMesh& patches
)
{
labelList patchSizes(patches.size());
forAll(patches, patchi)
{
patchSizes[patchi] = patches[patchi].size();
}
return patchSizes;
}
Foam::List<Foam::polyPatch*> Foam::polyMeshAdder::combinePatches
(
const polyMesh& mesh0,
@ -916,8 +882,7 @@ void Foam::polyMeshAdder::mergePointZones
}
else if (pointToZone[allPointi] != zoneI)
{
labelList& pZones = addPointToZones[allPointi];
pZones.appendUniq(zoneI);
addPointToZones[allPointi].push_uniq(zoneI);
}
}
}
@ -939,8 +904,7 @@ void Foam::polyMeshAdder::mergePointZones
}
else if (pointToZone[allPointi] != allZoneI)
{
labelList& pZones = addPointToZones[allPointi];
pZones.appendUniq(allZoneI);
addPointToZones[allPointi].push_uniq(allZoneI);
}
}
}
@ -1066,12 +1030,9 @@ void Foam::polyMeshAdder::mergeFaceZones
}
else if (faceToZone[allFacei] != zoneI)
{
labelList& fZones = addFaceToZones[allFacei];
boolList& flipZones = addFaceToFlips[allFacei];
if (fZones.appendUniq(zoneI))
if (addFaceToZones[allFacei].push_uniq(zoneI))
{
flipZones.append(flip0);
addFaceToFlips[allFacei].push_back(flip0);
}
}
}
@ -1108,12 +1069,9 @@ void Foam::polyMeshAdder::mergeFaceZones
}
else if (faceToZone[allFacei] != allZoneI)
{
labelList& fZones = addFaceToZones[allFacei];
boolList& flipZones = addFaceToFlips[allFacei];
if (fZones.appendUniq(allZoneI))
if (addFaceToZones[allFacei].push_uniq(allZoneI))
{
flipZones.append(flip1);
addFaceToFlips[allFacei].push_back(flip1);
}
}
}
@ -1231,8 +1189,7 @@ void Foam::polyMeshAdder::mergeCellZones
}
else if (cellToZone[cell0] != zoneI)
{
labelList& cZones = addCellToZones[cell0];
cZones.appendUniq(zoneI);
addCellToZones[cell0].push_uniq(zoneI);
}
}
}
@ -1253,8 +1210,7 @@ void Foam::polyMeshAdder::mergeCellZones
}
else if (cellToZone[allCelli] != allZoneI)
{
labelList& cZones = addCellToZones[allCelli];
cZones.appendUniq(allZoneI);
addCellToZones[allCelli].push_uniq(allZoneI);
}
}
}
@ -1625,8 +1581,8 @@ Foam::autoPtr<Foam::polyMesh> Foam::polyMeshAdder::add
from0ToAllPatches,
from1ToAllPatches,
getPatchSizes(patches0),
getPatchStarts(patches0)
patches0.patchSizes(),
patches0.patchStarts()
)
);
@ -1793,8 +1749,8 @@ Foam::autoPtr<Foam::mapAddedPolyMesh> Foam::polyMeshAdder::add
// Store mesh0 patch info before modifying patches0.
labelList mesh0PatchSizes(getPatchSizes(patches0));
labelList mesh0PatchStarts(getPatchStarts(patches0));
labelList mesh0PatchSizes(patches0.patchSizes());
labelList mesh0PatchStarts(patches0.patchStarts());
// Map from 0 to all patches (since gets compacted)
labelList from0ToAllPatches(patches0.size(), -1);

View File

@ -86,11 +86,6 @@ class polyMeshAdder
labelList& fromAllTo1Patches
);
//- Get starts of patches
static labelList getPatchStarts(const polyBoundaryMesh&);
//- Get sizes of patches
static labelList getPatchSizes(const polyBoundaryMesh&);
static List<polyPatch*> combinePatches
(
const polyMesh& mesh0,

View File

@ -80,9 +80,7 @@ void Foam::faBoundaryMesh::calcGroupIDs() const
forAll(patches, patchi)
{
const wordList& groups = patches[patchi].inGroups();
for (const word& groupName : groups)
for (const word& groupName : patches[patchi].inGroups())
{
groupLookup(groupName).push_back(patchi);
}
@ -366,34 +364,24 @@ void Foam::faBoundaryMesh::setGroup
faPatchList& patches = *this;
boolList donePatch(patches.size(), false);
boolList pending(patches.size(), true);
// Add to specified patches
for (const label patchi : patchIDs)
{
patches[patchi].inGroups().push_uniq(groupName);
donePatch[patchi] = true;
if (pending.test(patchi))
{
pending.unset(patchi);
patches[patchi].addGroup(groupName);
}
}
// Remove from other patches
forAll(patches, patchi)
{
if (!donePatch[patchi])
if (pending.test(patchi))
{
wordList& groups = patches[patchi].inGroups();
if (groups.found(groupName))
{
label newi = 0;
forAll(groups, i)
{
if (groups[i] != groupName)
{
groups[newi++] = groups[i];
}
}
groups.resize(newi);
}
patches[patchi].removeGroup(groupName);
}
}
}

View File

@ -113,7 +113,7 @@ Foam::faPatch::faPatch
{
if (constraintType(patchType))
{
inGroups().appendUniq(patchType);
addGroup(patchType);
}
}
@ -137,7 +137,7 @@ Foam::faPatch::faPatch
{
if (constraintType(patchType))
{
inGroups().appendUniq(patchType);
addGroup(patchType);
}
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2013 OpenFOAM Foundation
Copyright (C) 2021 OpenCFD Ltd.
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -56,7 +56,7 @@ Foam::mappedPolyPatch::mappedPolyPatch
mappedPatchBase(static_cast<const polyPatch&>(*this))
{
// mapped is not constraint type so add mapped group explicitly
inGroups().appendUniq(typeName);
addGroup(typeName);
}
@ -123,7 +123,7 @@ Foam::mappedPolyPatch::mappedPolyPatch
mappedPatchBase(*this, dict)
{
// mapped is not constraint type so add mapped group explicitly
inGroups().appendUniq(typeName);
addGroup(typeName);
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2013 OpenFOAM Foundation
Copyright (C) 2021 OpenCFD Ltd.
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -62,7 +62,7 @@ Foam::mappedWallPolyPatch::mappedWallPolyPatch
mappedPatchBase(static_cast<const polyPatch&>(*this))
{
// mapped is not constraint type so add mapped group explicitly
inGroups().appendUniq(mappedPolyPatch::typeName);
addGroup(mappedPolyPatch::typeName);
}
@ -129,7 +129,7 @@ Foam::mappedWallPolyPatch::mappedWallPolyPatch
mappedPatchBase(*this, dict)
{
// mapped is not constraint type so add mapped group explicitly
inGroups().appendUniq(mappedPolyPatch::typeName);
addGroup(mappedPolyPatch::typeName);
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -56,7 +56,7 @@ Foam::oversetPolyPatch::oversetPolyPatch
masterPatchID_(-1)
{
// 'overset' is not constraint type so add to group explicitly
inGroups().appendUniq(typeName);
addGroup(typeName);
}
@ -73,7 +73,7 @@ Foam::oversetPolyPatch::oversetPolyPatch
masterPatchID_(-1)
{
// 'overset' is not constraint type so add to group explicitly
inGroups().appendUniq(typeName);
addGroup(typeName);
}