faceToCell.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "faceToCell.H"
27 #include "polyMesh.H"
28 #include "faceSet.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
37 }
38 
41 {
42  "neighbour",
43  "owner",
44  "any",
45  "all"
46 };
47 
48 
49 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
50 
51 void Foam::faceToCell::combine(topoSet& set, const bool add) const
52 {
53  // Load the set
54  faceSet loadedSet(mesh_, setName_);
55 
56 
57  // Handle owner/neighbour/any selection
58  forAllConstIter(faceSet, loadedSet, iter)
59  {
60  const label facei = iter.key();
61 
62  if ((option_ == OWNER) || (option_ == ANY))
63  {
64  const label celli = mesh_.faceOwner()[facei];
65 
66  addOrDelete(set, celli, add);
67  }
68 
69  if (mesh_.isInternalFace(facei))
70  {
71  if ((option_ == NEIGHBOUR) || (option_ == ANY))
72  {
73  const label celli = mesh_.faceNeighbour()[facei];
74 
75  addOrDelete(set, celli, add);
76  }
77  }
78  }
79 
80  // Handle all selection.
81  if (option_ == ALL)
82  {
83  // Count number of selected faces per cell.
84 
85  Map<label> facesPerCell(loadedSet.size());
86 
87  forAllConstIter(faceSet, loadedSet, iter)
88  {
89  const label facei = iter.key();
90  const label own = mesh_.faceOwner()[facei];
91 
92  Map<label>::iterator fndOwn = facesPerCell.find(own);
93 
94  if (fndOwn == facesPerCell.end())
95  {
96  facesPerCell.insert(own, 1);
97  }
98  else
99  {
100  fndOwn()++;
101  }
102 
103  if (mesh_.isInternalFace(facei))
104  {
105  label nei = mesh_.faceNeighbour()[facei];
106 
107  Map<label>::iterator fndNei = facesPerCell.find(nei);
108 
109  if (fndNei == facesPerCell.end())
110  {
111  facesPerCell.insert(nei, 1);
112  }
113  else
114  {
115  fndNei()++;
116  }
117  }
118  }
119 
120  // Include cells that are referenced as many times as they have faces
121  // -> all faces in set.
122  forAllConstIter(Map<label>, facesPerCell, iter)
123  {
124  const label celli = iter.key();
125 
126  if (iter() == mesh_.cells()[celli].size())
127  {
128  addOrDelete(set, celli, add);
129  }
130  }
131  }
132 }
133 
134 
135 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
136 
138 (
139  const polyMesh& mesh,
140  const word& setName,
141  const faceAction option
142 )
143 :
145  setName_(setName),
146  option_(option)
147 {}
148 
149 
151 (
152  const polyMesh& mesh,
153  const dictionary& dict
154 )
155 :
157  setName_(dict.lookup("set")),
158  option_(faceActionNames_.read(dict.lookup("option")))
159 {}
160 
161 
162 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
163 
165 {}
166 
167 
168 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
169 
171 (
172  const topoSetSource::setAction action,
173  topoSet& set
174 ) const
175 {
176  if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
177  {
178  Info<< " Adding cells according to faceSet " << setName_
179  << " ..." << endl;
180 
181  combine(set, true);
182  }
183  else if (action == topoSetSource::DELETE)
184  {
185  Info<< " Removing cells according to faceSet " << setName_
186  << " ..." << endl;
187 
188  combine(set, false);
189  }
190 }
191 
192 
193 // ************************************************************************* //
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:476
Macros for easy insertion into run-time selection tables.
friend class iterator
Declare friendship with the iterator.
Definition: HashTable.H:194
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:55
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
A topoSetSource to select cells based on usage in faces.
Definition: faceToCell.H:52
virtual void applyToSet(const topoSetSource::setAction action, topoSet &) const
Definition: faceToCell.C:171
faceToCell(const polyMesh &mesh, const word &setName, const faceAction option)
Construct from components.
Definition: faceToCell.C:138
virtual ~faceToCell()
Destructor.
Definition: faceToCell.C:164
static const NamedEnum< faceAction, 4 > faceActionNames_
Names of the valid options.
Definition: faceToCell.H:67
faceAction
Enumeration defining the valid options.
Definition: faceToCell.H:59
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
virtual const labelList & faceOwner() const
Return face owner.
Definition: polyMesh.C:1357
virtual const labelList & faceNeighbour() const
Return face neighbour.
Definition: polyMesh.C:1363
bool isInternalFace(const label faceIndex) const
Return true if given face label is internal to the mesh.
const cellList & cells() const
Base class of a source for a topoSet.
Definition: topoSetSource.H:64
void addOrDelete(topoSet &set, const label celli, const bool) const
Add (if bool) celli to set or delete celli from set.
Definition: topoSetSource.C:87
setAction
Enumeration defining the valid actions.
Definition: topoSetSource.H:83
const polyMesh & mesh_
Definition: topoSetSource.H:99
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:61
A class for handling words, derived from string.
Definition: word.H:62
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
Namespace for OpenFOAM.
bool read(const char *, int32_t &)
Definition: int32IO.C:85
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
addToRunTimeSelectionTable(polyPatch, mergedCyclicPolyPatch, word)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
messageStream Info
defineTypeNameAndDebug(combustionModel, 0)
void add(LagrangianPatchField< typename typeOfSum< Type1, Type2 >::type > &f, const LagrangianPatchField< Type1 > &f1, const LagrangianPatchField< Type2 > &f2)
treeBoundBox combine(const treeBoundBox &a, const treeBoundBox &b)
Definition: patchToPatch.C:78
dictionary dict