faceZoneToCell.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "faceZoneToCell.H"
30 #include "polyMesh.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(faceZoneToCell, 0);
38  addToRunTimeSelectionTable(topoSetSource, faceZoneToCell, word);
39  addToRunTimeSelectionTable(topoSetSource, faceZoneToCell, istream);
40  addToRunTimeSelectionTable(topoSetCellSource, faceZoneToCell, word);
41  addToRunTimeSelectionTable(topoSetCellSource, faceZoneToCell, istream);
42 }
43 
44 
45 Foam::topoSetSource::addToUsageTable Foam::faceZoneToCell::usage_
46 (
47  faceZoneToCell::typeName,
48  "\n Usage: faceZoneToCell zone front|back|both\n\n"
49  " Select front, back or both sides of the faceZone."
50  " Note:accepts wildcards for zone.\n\n"
51 );
52 
53 
54 const Foam::Enum
55 <
57 >
58 Foam::faceZoneToCell::faceActionNames_
59 ({
60  { faceAction::FRONT, "front" },
61  { faceAction::BACK, "back" },
62  { faceAction::BOTH, "both" },
63  // Compatibility
64  { faceAction::FRONT, "master" },
65  { faceAction::BACK, "slave" },
66 });
67 
68 
69 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
70 
71 void Foam::faceZoneToCell::combine
72 (
73  topoSet& set,
74  const labelUList& zoneIDs,
75  const bool add,
76  const bool verbosity
77 ) const
78 {
79  const label nZones = mesh_.faceZones().size();
80 
81  if (zoneIDs.empty() || !nZones)
82  {
83  return; // Nothing to do
84  }
85 
86  for (const label zonei : zoneIDs)
87  {
88  if (zonei < 0 || zonei >= nZones)
89  {
90  continue;
91  }
92 
93  const auto& zone = mesh_.faceZones()[zonei];
94 
95  if (verbosity)
96  {
97  Info<< " Using matching zone " << zone.name();
98 
99  if (option_ == faceAction::FRONT)
100  {
101  Info<< " [front] cells:";
102  }
103  else if (option_ == faceAction::BACK)
104  {
105  Info<< " : [back] cells:";
106  }
107  if (option_ == faceAction::BOTH)
108  {
109  Info<< " : [front/back] cells:";
110  }
111  }
112 
113  if (option_ == faceAction::FRONT || option_ == faceAction::BOTH)
114  {
115  const labelList& cellLabels = zone.frontCells();
116 
117  if (verbosity)
118  {
119  Info<< ' ' << returnReduce(cellLabels.size(), sumOp<label>());
120  }
121 
122  for (const label celli : cellLabels)
123  {
124  // Only do active cells
125  if (celli >= 0 && celli < mesh_.nCells())
126  {
127  addOrDelete(set, celli, add);
128  }
129  }
130  }
131 
132  if (option_ == faceAction::BACK || option_ == faceAction::BOTH)
133  {
134  const labelList& cellLabels = zone.backCells();
135 
136  if (verbosity)
137  {
138  Info<< ' ' << returnReduce(cellLabels.size(), sumOp<label>());
139  }
140 
141  for (const label celli : cellLabels)
142  {
143  // Only do active cells
144  if (celli >= 0 && celli < mesh_.nCells())
145  {
146  addOrDelete(set, celli, add);
147  }
148  }
149  }
150 
151  if (verbosity)
152  {
153  Info<< endl;
154  }
155  }
156 }
157 
158 
159 void Foam::faceZoneToCell::combine(topoSet& set, const bool add) const
160 {
161  if (zoneMatcher_.empty())
162  {
163  return; // Nothing to do
164  }
165 
166  const labelList matched(mesh_.faceZones().indices(zoneMatcher_));
167 
168  if (matched.empty())
169  {
171  << "Cannot find any faceZone matching "
172  << flatOutput(zoneMatcher_) << nl
173  << "Valid names: " << flatOutput(mesh_.faceZones().names())
174  << endl;
175 
176  return; // Nothing to do
177  }
178 
179  combine(set, matched, add, verbose_);
180 }
181 
182 
183 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
184 
186 (
187  const polyMesh& mesh,
188  const wordRes& zoneSelector,
189  const faceAction option
190 )
191 :
193  zoneMatcher_(zoneSelector),
194  option_(option)
195 {}
196 
197 
199 (
200  const polyMesh& mesh,
201  const wordRe& zoneName,
202  const faceAction option
203 )
204 :
206  zoneMatcher_(one{}, zoneName),
207  option_(option)
208 {}
209 
210 
212 (
213  const polyMesh& mesh,
214  const dictionary& dict
215 )
216 :
217  topoSetCellSource(mesh),
218  zoneMatcher_(),
219  option_(faceActionNames_.get("option", dict))
220 {
221  // Look for 'zones' and 'zone', but accept 'name' as well
222  if (!dict.readIfPresent("zones", zoneMatcher_))
223  {
224  zoneMatcher_.resize(1);
225  zoneMatcher_.front() = dict.getCompat<wordRe>("zone", {{"name", 1806}});
226  }
227 }
228 
229 
231 (
232  const polyMesh& mesh,
233  Istream& is
234 )
235 :
236  topoSetCellSource(mesh),
237  zoneMatcher_(one{}, wordRe(checkIs(is))),
238  option_(faceActionNames_.read(checkIs(is)))
239 {}
240 
241 
242 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
245 {
246  return zoneMatcher_;
247 }
248 
250 void Foam::faceZoneToCell::zones(const wordRes& zonesSelector)
251 {
252  zoneMatcher_ = zonesSelector;
253 }
254 
255 
256 void Foam::faceZoneToCell::zones(const wordRe& zoneName)
257 {
258  zoneMatcher_.resize(1);
259  zoneMatcher_.front() = zoneName;
260 }
261 
262 
264 (
265  const topoSetSource::setAction action,
266  topoSet& set
267 ) const
268 {
269  if (action == topoSetSource::ADD || action == topoSetSource::NEW)
270  {
271  if (verbose_ && !zoneMatcher_.empty())
272  {
273  Info<< " Adding all " << faceActionNames_[option_]
274  << " cells of face zones "
275  << flatOutput(zoneMatcher_) << " ..." << endl;
276  }
277 
278  combine(set, true);
279  }
280  else if (action == topoSetSource::SUBTRACT)
281  {
282  if (verbose_ && !zoneMatcher_.empty())
283  {
284  Info<< " Removing all " << faceActionNames_[option_]
285  << " cells of face zones "
286  << flatOutput(zoneMatcher_) << " ..." << endl;
287  }
288 
289  combine(set, false);
290  }
291 }
292 
293 
294 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
dictionary dict
const labelIOList & zoneIDs
Definition: correctPhi.H:59
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:160
Create a new set and ADD elements to it.
Add elements to current set.
const wordRes & zones() const noexcept
Return the current zones selector.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
faceZoneToCell(const polyMesh &mesh, const wordRes &zoneSelector, const faceAction option)
Construct from mesh, zones selector and selection option.
T & front()
Access first element of the list, position [0].
Definition: UListI.H:237
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
virtual void applyToSet(const topoSetSource::setAction action, topoSet &set) const
Apply specified action to the topoSet.
The topoSetCellSource is a intermediate class for handling topoSet sources for selecting cells...
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Perform reduction on a copy, using specified binary operation.
void addOrDelete(topoSet &set, const label id, const bool add) const
Add or delete id from set. Add when &#39;add&#39; is true.
Macros for easy insertion into run-time selection tables.
UList< label > labelUList
A UList of labels.
Definition: UList.H:78
faceAction
Enumeration defining the valid options.
AccessType combine(const UList< T > &lists, AccessOp aop=accessOp< T >())
Combines sub-lists into a single list.
Definition: ListListOps.C:62
dynamicFvMesh & mesh
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:106
setAction
Enumeration defining various actions.
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:53
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings...
Definition: wordRe.H:78
const polyMesh & mesh_
Reference to the mesh.
const direction noexcept
Definition: Scalar.H:258
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
const faceZoneMesh & faceZones() const noexcept
Return face zone mesh.
Definition: polyMesh.H:670
defineTypeNameAndDebug(combustionModel, 0)
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:59
Subtract elements from current set.
#define WarningInFunction
Report a warning using Foam::Warning.
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
Class with constructor to add usage string to table.
label nCells() const noexcept
Number of mesh cells.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
List< label > labelList
A List of labels.
Definition: List.H:62
Namespace for OpenFOAM.
addToRunTimeSelectionTable(functionObject, pointHistory, dictionary)
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:56
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225