Commit 269d0ffd authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: improve flexibility of zoneTo{Cell,Face,Point} (#2130)

- add setter/getter methods for the zone selection

- construct for multiple zone selection

- support explicit zone ids instead of name matcher
parent b2fdc672
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2020 OpenCFD Ltd.
Copyright (C) 2016-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -59,59 +59,103 @@ Foam::faceZoneToCell::faceActionNames_
({
{ faceAction::MASTER, "master" },
{ faceAction::SLAVE, "slave" },
//TBD: { faceAction::BOTH, "attached" },
});
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::faceZoneToCell::combine(topoSet& set, const bool add) const
void Foam::faceZoneToCell::combine
(
topoSet& set,
const labelUList& zoneIDs,
const bool add,
const bool verbosity
) const
{
bool hasMatched = false;
const label nZones = mesh_.faceZones().size();
if (zoneIDs.empty() || !nZones)
{
return; // Nothing to do
}
for (const faceZone& zone : mesh_.faceZones())
for (const label zonei : zoneIDs)
{
if (selectedZones_.match(zone.name()))
if (zonei < 0 || zonei >= nZones)
{
hasMatched = true;
continue;
}
const labelList& cellLabels =
(
option_ == MASTER
? zone.masterCells()
: zone.slaveCells()
);
const auto& zone = mesh_.faceZones()[zonei];
if (verbose_)
{
Info<< " Found matching zone " << zone.name()
<< " with " << cellLabels.size() << " cells on "
<< faceActionNames_[option_] << " side" << endl;
}
const labelList& cellLabels =
(
option_ == MASTER
? zone.masterCells()
: zone.slaveCells()
);
if (verbosity)
{
Info<< " Using matching zone " << zone.name()
<< " with " << cellLabels.size() << " cells on "
<< faceActionNames_[option_] << " side" << endl;
}
for (const label celli : cellLabels)
// NOTE could also handle both sides directly if required
for (const label celli : cellLabels)
{
// Only do active cells
if (celli >= 0 && celli < mesh_.nCells())
{
// Only do active cells
if (celli >= 0 && celli < mesh_.nCells())
{
addOrDelete(set, celli, add);
}
addOrDelete(set, celli, add);
}
}
}
}
void Foam::faceZoneToCell::combine(topoSet& set, const bool add) const
{
if (zoneMatcher_.empty())
{
return; // Nothing to do
}
if (!hasMatched)
const labelList matched(mesh_.faceZones().indices(zoneMatcher_));
if (matched.empty())
{
WarningInFunction
<< "Cannot find any faceZone matching "
<< flatOutput(selectedZones_) << nl
<< "Valid names: " << flatOutput(mesh_.faceZones().names())
<< flatOutput(zoneMatcher_) << nl
<< "Valid names are " << flatOutput(mesh_.faceZones().names())
<< endl;
return; // Nothing to do
}
combine(set, matched, add, verbose_);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::faceZoneToCell::faceZoneToCell
(
const polyMesh& mesh,
const wordRes& zoneSelector,
const faceAction option
)
:
topoSetCellSource(mesh),
zoneMatcher_(zoneSelector),
option_(option)
{}
Foam::faceZoneToCell::faceZoneToCell
(
const polyMesh& mesh,
......@@ -120,7 +164,7 @@ Foam::faceZoneToCell::faceZoneToCell
)
:
topoSetCellSource(mesh),
selectedZones_(one{}, zoneName),
zoneMatcher_(one{}, zoneName),
option_(option)
{}
......@@ -132,14 +176,14 @@ Foam::faceZoneToCell::faceZoneToCell
)
:
topoSetCellSource(mesh),
selectedZones_(),
zoneMatcher_(),
option_(faceActionNames_.get("option", dict))
{
// Look for 'zones' and 'zone', but accept 'name' as well
if (!dict.readIfPresent("zones", selectedZones_))
if (!dict.readIfPresent("zones", zoneMatcher_))
{
selectedZones_.resize(1);
selectedZones_.first() =
zoneMatcher_.resize(1);
zoneMatcher_.first() =
dict.getCompat<wordRe>("zone", {{"name", 1806}});
}
}
......@@ -152,13 +196,32 @@ Foam::faceZoneToCell::faceZoneToCell
)
:
topoSetCellSource(mesh),
selectedZones_(one{}, wordRe(checkIs(is))),
zoneMatcher_(one{}, wordRe(checkIs(is))),
option_(faceActionNames_.read(checkIs(is)))
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
const Foam::wordRes& Foam::faceZoneToCell::zones() const noexcept
{
return zoneMatcher_;
}
void Foam::faceZoneToCell::zones(const wordRes& zonesSelector)
{
zoneMatcher_ = zonesSelector;
}
void Foam::faceZoneToCell::zones(const wordRe& zoneName)
{
zoneMatcher_.resize(1);
zoneMatcher_.first() = zoneName;
}
void Foam::faceZoneToCell::applyToSet
(
const topoSetSource::setAction action,
......@@ -167,22 +230,22 @@ void Foam::faceZoneToCell::applyToSet
{
if (action == topoSetSource::ADD || action == topoSetSource::NEW)
{
if (verbose_)
if (verbose_ && !zoneMatcher_.empty())
{
Info<< " Adding all " << faceActionNames_[option_]
<< " cells of face zones "
<< flatOutput(selectedZones_) << " ..." << endl;
<< flatOutput(zoneMatcher_) << " ..." << endl;
}
combine(set, true);
}
else if (action == topoSetSource::SUBTRACT)
{
if (verbose_)
if (verbose_ && !zoneMatcher_.empty())
{
Info<< " Removing all " << faceActionNames_[option_]
<< " cells of face zones "
<< flatOutput(selectedZones_) << " ..." << endl;
<< flatOutput(zoneMatcher_) << " ..." << endl;
}
combine(set, false);
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -93,8 +93,8 @@ Usage
Options for the conditional mandatory entries:
\verbatim
Entry | Description | Type | Req'd | Dflt
zones | Names of input faceZones | wordList | cond'l | -
zone | Name of input faceZone | word | cond'l | -
zones | Names of input faceZones | wordRes | cond'l | -
zone | Name of input faceZone | wordRe | cond'l | -
\verbatim
Note
......@@ -149,7 +149,7 @@ private:
static addToUsageTable usage_;
//- Matcher for face zones
wordRes selectedZones_;
wordRes zoneMatcher_;
//- Selection type
faceAction option_;
......@@ -157,6 +157,14 @@ private:
// Private Member Functions
void combine
(
topoSet& set,
const labelUList& zoneIDs,
const bool add,
const bool verbosity
) const;
void combine(topoSet& set, const bool add) const;
......@@ -168,7 +176,15 @@ public:
// Constructors
//- Construct from components
//- Construct from mesh, zones selector and selection option
faceZoneToCell
(
const polyMesh& mesh,
const wordRes& zoneSelector,
const faceAction option
);
//- Construct from mesh, single zone selector and selection option
faceZoneToCell
(
const polyMesh& mesh,
......@@ -189,6 +205,16 @@ public:
// Member Functions
//- Return the current zones selector
const wordRes& zones() const noexcept;
//- Define the zones selector
void zones(const wordRes& zoneSelector);
//- Define the zones selector with a single zone selector
void zones(const wordRe& zoneName);
virtual void applyToSet
(
const topoSetSource::setAction action,
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -67,48 +67,91 @@ Foam::topoSetSource::addToUsageTable Foam::zoneToCell::usage_
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::zoneToCell::combine(topoSet& set, const bool add) const
void Foam::zoneToCell::combine
(
topoSet& set,
const labelUList& zoneIDs,
const bool add,
const bool verbosity
) const
{
bool hasMatched = false;
const label nZones = mesh_.cellZones().size();
if (zoneIDs.empty() || !nZones)
{
return; // Nothing to do
}
for (const cellZone& zone : mesh_.cellZones())
for (const label zonei : zoneIDs)
{
if (selectedZones_.match(zone.name()))
if (zonei < 0 || zonei >= nZones)
{
hasMatched = true;
continue;
}
const labelList& cellLabels = zone;
const auto& zone = mesh_.cellZones()[zonei];
if (verbose_)
{
Info<< " Found matching zone " << zone.name()
<< " with " << cellLabels.size() << " cells." << endl;
}
if (verbosity)
{
Info<< " Using zone " << zone.name()
<< " with " << zone.size() << " cells." << endl;
}
for (const label celli : cellLabels)
for (const label celli : zone)
{
// Only do active cells
if (celli >= 0 && celli < mesh_.nCells())
{
// Only do active cells
if (celli >= 0 && celli < mesh_.nCells())
{
addOrDelete(set, celli, add);
}
addOrDelete(set, celli, add);
}
}
}
}
void Foam::zoneToCell::combine(topoSet& set, const bool add) const
{
if (!zoneIDs_.empty())
{
combine(set, zoneIDs_, add, false);
return;
}
if (zoneMatcher_.empty())
{
return; // Nothing to do
}
if (!hasMatched)
const labelList matched(mesh_.cellZones().indices(zoneMatcher_));
if (matched.empty())
{
WarningInFunction
<< "Cannot find any cellZone matching "
<< flatOutput(selectedZones_) << nl
<< flatOutput(zoneMatcher_) << nl
<< "Valid names: " << flatOutput(mesh_.cellZones().names())
<< endl;
return; // Nothing to do
}
combine(set, matched, add, verbose_);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::zoneToCell::zoneToCell
(
const polyMesh& mesh,
const wordRes& zoneSelector
)
:
topoSetCellSource(mesh),
zoneMatcher_(zoneSelector)
{}
Foam::zoneToCell::zoneToCell
(
const polyMesh& mesh,
......@@ -116,7 +159,19 @@ Foam::zoneToCell::zoneToCell
)
:
topoSetCellSource(mesh),
selectedZones_(one{}, zoneName)
zoneMatcher_(one{}, zoneName)
{}
Foam::zoneToCell::zoneToCell
(
const polyMesh& mesh,
const labelUList& zoneIDs
)
:
topoSetCellSource(mesh),
zoneMatcher_(),
zoneIDs_(zoneIDs)
{}
......@@ -127,13 +182,13 @@ Foam::zoneToCell::zoneToCell
)
:
topoSetCellSource(mesh),
selectedZones_()
zoneMatcher_()
{
// Look for 'zones' and 'zone', but accept 'name' as well
if (!dict.readIfPresent("zones", selectedZones_))
if (!dict.readIfPresent("zones", zoneMatcher_))
{
selectedZones_.resize(1);
selectedZones_.first() =
zoneMatcher_.resize(1);
zoneMatcher_.first() =
dict.getCompat<wordRe>("zone", {{"name", 1806}});
}
}
......@@ -145,13 +200,48 @@ Foam::zoneToCell::zoneToCell
Istream& is
)
:
topoSetCellSource(mesh),
selectedZones_(one{}, wordRe(checkIs(is)))
zoneToCell(mesh, wordRe(checkIs(is)))
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
const Foam::wordRes& Foam::zoneToCell::zones() const noexcept
{
return zoneMatcher_;
}
void Foam::zoneToCell::zones(const wordRes& zonesSelector)
{
zoneMatcher_ = zonesSelector;
zoneIDs_.clear();
}
void Foam::zoneToCell::zones(const wordRe& zoneName)
{
zoneMatcher_.resize(1);
zoneMatcher_.first() = zoneName;
zoneIDs_.clear();
}
void Foam::zoneToCell::zones(const labelUList& zoneIDs)
{
zoneMatcher_.clear();
zoneIDs_ = zoneIDs;
}
void Foam::zoneToCell::zones(const label zoneID)
{
zoneMatcher_.clear();
zoneIDs_.resize(1);
zoneIDs_.first() = zoneID;
}
void Foam::zoneToCell::applyToSet
(
const topoSetSource::setAction action,
......@@ -160,20 +250,20 @@ void Foam::zoneToCell::applyToSet
{
if (action == topoSetSource::ADD || action == topoSetSource::NEW)
{
if (verbose_)
if (verbose_ && !zoneMatcher_.empty())
{
Info<< " Adding all cells of cell zones "
<< flatOutput(selectedZones_) << " ..." << endl;
<< flatOutput(zoneMatcher_) << " ..." << endl;
}
combine(set, true);
}
else if (action == topoSetSource::SUBTRACT)
{
if (verbose_)
if (verbose_ && !zoneMatcher_.empty())
{
Info<< " Removing all cells of cell zones "
<< flatOutput(selectedZones_) << " ..." << endl;
<< flatOutput(zoneMatcher_) << " ..." << endl;
}
combine(set, false);
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -84,8 +84,8 @@ Usage
Options for the conditional mandatory entries:
\verbatim
Entry | Description | Type | Req'd | Dflt
zones | Names of input cellZones | wordList | cond'l | -
zone | Name of input cellZone | word | cond'l | -
zones | Names of input cellZones | wordRes | cond'l | -
zone | Name of input cellZone | wordRe | cond'l | -
\verbatim
Note
......@@ -126,11 +126,22 @@ class zoneToCell
static addToUsageTable usage_;
//- Matcher for zones
wordRes selectedZones_;
wordRes zoneMatcher_;
//- Explicitly specified zone ids
labelList zoneIDs_;
// Private Member Functions
void combine
(
topoSet& set,
const labelUList& zoneIDs,
const bool add,
const bool verbosity
) const;
void combine(topoSet& set, const bool add) const;
......@@ -142,9 +153,15 @@ public:
// Constructors
//- Construct from components
//- Construct from mesh and zones selector
zoneToCell(const polyMesh& mesh, const wordRes& zoneSelector);
//- Construct from mesh and single zone selector
zoneToCell(const polyMesh& mesh, const wordRe& zoneName);
//- Construct from mesh and specified zone IDs
zoneToCell(const polyMesh& mesh, const labelUList& zoneIDs);
//- Construct from dictionary
zoneToCell(const polyMesh& mesh, const dictionary& dict);
......@@ -158,6 +175,24 @@ public:
// Member Functions
//- Return the current zones selector
const wordRes& zones() const noexcept;
//- Define the zones selector
void zones(const wordRes& zoneSelector);
//- Define the zones selector with a single zone selector
void zones(const wordRe& zoneName);