faceSetOption.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) 2019-2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "faceSetOption.H"
29 #include "faceSet.H"
30 #include "areaFields.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  namespace fa
37  {
38  defineTypeNameAndDebug(faceSetOption, 0);
39  }
40 }
41 
42 
43 const Foam::Enum
44 <
46 >
48 ({
49  { selectionModeType::smAll, "all" },
50  { selectionModeType::smFaceSet, "faceSet" },
51  { selectionModeType::smFaceZone, "faceZone" },
52  { selectionModeType::smPatch, "patch" },
53  { selectionModeType::smFaceZone, "volFaceZone" } // Compat?
54 });
55 
56 
57 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
58 
59 void Foam::fa::faceSetOption::setSelection(const dictionary& dict)
60 {
62 
63  switch (selectionMode_)
64  {
65  case smAll:
66  {
67  break;
68  }
69  case smFaceSet:
70  {
72  dict.readEntry("faceSet", selectionNames_.first());
73  break;
74  }
75  case smFaceZone:
76  {
77  if
78  (
79  !dict.readIfPresent("faceZones", selectionNames_)
81  )
82  {
84  dict.readEntry("faceZone", selectionNames_.first());
85  }
86  break;
87  }
88  case smPatch:
89  {
90  if
91  (
92  !dict.readIfPresent("patches", selectionNames_)
94  )
95  {
97  dict.readEntry("patch", selectionNames_.first());
98  }
99  break;
100  }
101  default:
102  {
104  << "Unknown selectionMode "
106  << ". Valid selectionMode types : "
108  << exit(FatalError);
109  }
110  }
111 }
112 
113 
115 {
116  // Set area information
117 
118  scalar sumArea = 0;
119  for (const label facei : faces_)
120  {
121  sumArea += regionMesh().S()[facei];
122  }
123  reduce(sumArea, sumOp<scalar>());
124 
125  const scalar old(A_);
126  A_ = sumArea;
127 
128  // Compare area values, stringified using current write precision
129  if
130  (
133  )
134  {
136  << "- selected " << returnReduce(faces_.size(), sumOp<label>())
137  << " face(s) with area " << A_ << endl;
138  }
139 }
140 
141 
143 {
144  switch (selectionMode_)
145  {
146  case smAll:
147  {
148  Info<< indent << "- selecting all faces" << endl;
149  faces_ = identity(regionMesh().nFaces());
150 
151  break;
152  }
153 
154  case smFaceSet:
155  {
156  Info<< indent
157  << "- selecting face subset using volume-mesh faceSet "
158  << zoneName() << nl;
159 
160  const faceSet subset(mesh_, zoneName());
161 
162  const labelUList& faceLabels = regionMesh().faceLabels();
163 
164  faces_.resize_nocopy(faceLabels.size());
165 
166  label nUsed = 0;
167  forAll(faceLabels, facei)
168  {
169  const label meshFacei = faceLabels[facei];
170 
171  if (subset.test(meshFacei))
172  {
173  faces_[nUsed] = facei;
174  ++nUsed;
175  }
176  }
177  faces_.resize(nUsed);
178  break;
179  }
180 
181  case smFaceZone:
182  {
183  Info<< indent
184  << "- selecting face subset using volume-mesh faceZones "
185  << flatOutput(selectionNames_) << nl;
186 
187  const auto& zones = mesh_.faceZones();
188 
189  // Also handles groups, multiple zones etc ...
190  labelList zoneIDs = zones.indices(selectionNames_);
191 
192  if (zoneIDs.empty())
193  {
195  << "No matching faceZones: "
196  << flatOutput(selectionNames_) << nl
197  << "Valid zones : "
198  << flatOutput(zones.names()) << nl
199  << "Valid groups: "
200  << flatOutput(zones.groupNames())
201  << nl
202  << exit(FatalError);
203  }
204 
205  const bitSet subset(mesh_.faceZones().selection(zoneIDs));
206 
207  const labelUList& faceLabels = regionMesh().faceLabels();
208 
209  faces_.resize_nocopy(faceLabels.size());
210 
211  label nUsed = 0;
212  forAll(faceLabels, facei)
213  {
214  const label meshFacei = faceLabels[facei];
215 
216  if (subset.test(meshFacei))
217  {
218  faces_[nUsed] = facei;
219  ++nUsed;
220  }
221  }
222  faces_.resize(nUsed);
223  break;
224  }
225 
226  case smPatch:
227  {
228  Info<< indent
229  << "- selecting face subset using volume-mesh patches "
230  << flatOutput(selectionNames_) << nl;
231 
232  const polyBoundaryMesh& pbm = mesh_.boundaryMesh();
233 
234  // Also handles groups, multiple patches etc ...
235  labelList patchIDs = pbm.indices(selectionNames_);
236 
237  if (patchIDs.empty())
238  {
240  << "No matching patches: "
241  << flatOutput(selectionNames_) << nl
242  << "Valid patches : "
243  << flatOutput(pbm.names()) << nl
244  << "Valid groups: "
245  << flatOutput(pbm.groupNames()) << nl
246  << exit(FatalError);
247  }
248 
249  const List<labelPair>& patchFaces = regionMesh().whichPatchFaces();
250 
251  faces_.resize_nocopy(patchFaces.size());
252 
253  label nUsed = 0;
254  forAll(patchFaces, facei)
255  {
256  const label patchi = patchFaces[facei].first();
257 
258  if (patchIDs.found(patchi))
259  {
260  faces_[nUsed] = facei;
261  ++nUsed;
262  }
263  }
264  faces_.resize(nUsed);
265  break;
266  }
267 
268  default:
269  {
271  << "Unknown selectionMode "
272  << selectionModeTypeNames_[selectionMode_]
273  << ". Valid selectionMode types are "
274  << selectionModeTypeNames_
275  << exit(FatalError);
276  }
277  }
278 
279  if (smAll != selectionMode_ && returnReduceAnd(faces_.empty()))
280  {
282  << "No faces selected!" << endl;
283  }
284 }
285 
286 
287 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
288 
290 (
291  const word& name,
292  const word& modelType,
293  const dictionary& dict,
294  const fvMesh& mesh
295 )
296 :
297  fa::option(name, modelType, dict, mesh),
298  timeStart_(-1),
299  duration_(0),
300  selectionMode_(selectionModeTypeNames_.get("selectionMode", coeffs_)),
301  selectionNames_(),
302  A_(0)
303 {
304  if (isActive())
305  {
306  Info<< incrIndent;
307  read(dict);
310  setArea();
312  }
313 }
314 
315 
316 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
317 
319 {
320  if (fa::option::isActive() && inTimeLimits(mesh_.time().value()))
321  {
322  // Update the face set if the mesh is changing
323  if (mesh_.changing())
324  {
325  if (mesh_.topoChanging())
326  {
327  setArea();
328  // Force printing of new set area
329  A_ = -GREAT;
330  }
331 
332  // Report new area (if changed)
333  setArea();
334  }
335 
336  return true;
337  }
338 
339  return false;
340 }
341 
342 
344 {
345  if (fa::option::read(dict))
346  {
347  timeStart_ = -1;
348 
349  if (coeffs_.readIfPresent("timeStart", timeStart_))
350  {
351  coeffs_.readEntry("duration", duration_);
352  }
353 
354  return true;
355  }
356 
357  return false;
358 }
359 
360 
361 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
const labelList patchIDs(pbm.indices(polyPatchNames, true))
const polyBoundaryMesh & pbm
dictionary dict
const labelIOList & zoneIDs
Definition: correctPhi.H:59
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:493
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:160
void setFaceSelection()
Set face selection based on user input selection mode.
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
bool empty() const noexcept
True if List is empty (ie, size() is zero)
Definition: UList.H:666
"patch" : subset with (volume) patches
T & first()
Access first element of the list, position [0].
Definition: UList.H:853
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
wordRes selectionNames_
Face selection names (for set, zone or patch selections)
virtual bool isActive()
Is the source active?
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:423
const word & timeName() const noexcept
Return the current time name.
Definition: TimeStateI.H:30
labelList faceLabels(nFaceLabels)
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.
UList< label > labelUList
A UList of labels.
Definition: UList.H:78
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
faceSetOption(const word &name, const word &modelType, const dictionary &dict, const fvMesh &mesh)
Construct from components.
bool returnReduceAnd(const bool value, const label comm=UPstream::worldComm)
Perform logical (and) MPI Allreduce on a copy. Uses UPstream::reduceAnd.
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i), works like std::iota() but returning a...
Definition: labelLists.C:44
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:137
A class for handling words, derived from Foam::string.
Definition: word.H:63
defineTypeNameAndDebug(limitHeight, 0)
"faceZone" : subset with (volume) zone faces
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: faOptionIO.C:47
"all" finite-area faces
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:511
static const Enum< selectionModeType > selectionModeTypeNames_
List of selection mode type names.
List< T > subset(const BoolListType &select, const UList< T > &input, const bool invert=false)
Extract elements of the input list when select is true.
#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
selectionModeType selectionMode_
Face selection mode.
void setSelection(const dictionary &dict)
Set face selection name from dictionary input.
Definition: faceSetOption.C:52
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
Reduce inplace (cf. MPI Allreduce) using specified communication schedule.
virtual bool read(const dictionary &dict)
Read source dictionary.
messageStream Info
Information stream (stdout output on master, null elsewhere)
"faceSet" : subset with (volume) face set
virtual bool isActive()
Is the source active?
Definition: faOption.C:112
List< label > labelList
A List of labels.
Definition: List.H:62
selectionModeType
Enumeration for selection mode types.
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:502
void setArea()
Recalculate the area.
dictionary coeffs_
Dictionary containing source coefficients.
Definition: faOption.H:166
Namespace for OpenFOAM.
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225