manifoldCellsMeshObject.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) 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 
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34  defineTypeName(manifoldCellsMeshObject);
35 }
36 
37 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
38 
39 namespace Foam
40 {
41 
42 // Change entries in labelList
43 static inline void replaceAll
44 (
45  const label oldVal,
46  const label newVal,
47  labelUList& list
48 )
49 {
50  for (label& val : list)
51  {
52  if (val == oldVal)
53  {
54  val = newVal;
55  }
56  }
57 }
58 
59 } // End namespace Foam
60 
61 
62 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
63 
64 Foam::refPtr<Foam::cellList> Foam::manifoldCellsMeshObject::filter
65 (
66  const polyMesh& mesh,
67  label& nCellsCorrected
68 )
69 {
70  const auto& oldCells = mesh.cells();
71 
72  // Start with copy of existing cell list
73  auto tnewCells = refPtr<cellList>::New(oldCells);
74  auto& newCells = tnewCells.ref();
75 
76  DynamicList<label> removed;
77 
78  nCellsCorrected = 0;
79  forAll(oldCells, celli)
80  {
81  const auto& oldCFaces = oldCells[celli];
82  auto& newCFaces = newCells[celli];
83 
84  removed.clear();
85 
86  forAll(oldCFaces, cFacei)
87  {
88  const label facei = oldCFaces[cFacei];
89  const label masteri = newCFaces[cFacei];
90 
91  const face& f = mesh.faces()[facei];
92 
93  forAll(oldCFaces, cFacej)
94  {
95  const label facej = oldCFaces[cFacej];
96  const label masterj = newCFaces[cFacej];
97 
98  if (facej != facei)
99  {
100  if (face::sameVertices(f, mesh.faces()[facej]))
101  {
102  //Pout<< "Same face:" << facei
103  // << " master:" << masteri
104  // << " verts:" << f << nl
105  // << " :" << facej
106  // << " master:" << masterj
107  // << " verts:" << mesh.faces()[facej]
108  // << endl;
109 
110  if (masteri < masterj)
111  {
112  replaceAll(masterj, masteri, newCFaces);
113  removed.append(masterj);
114  }
115  else if (masterj < masteri)
116  {
117  replaceAll(masteri, masterj, newCFaces);
118  removed.append(masteri);
119  }
120  }
121  }
122  }
123  }
124 
125  if (removed.size())
126  {
127  // Compact out removed faces
128  label newi = 0;
129  for (const label facei : oldCFaces)
130  {
131  if (!removed.found(facei))
132  {
133  newCFaces[newi++] = facei;
134  }
135  }
136  newCFaces.resize(newi);
137  ++nCellsCorrected;
138  }
139  }
140 
141  // Not needed (locally)
142  if (nCellsCorrected == 0)
143  {
144  // Just use the existing mesh
145  tnewCells.cref(mesh.cells());
146  }
147 
148  // Number of cells corrected (globally)
149  reduce(nCellsCorrected, sumOp<label>());
150 
151  return tnewCells;
152 }
153 
154 
155 Foam::refPtr<Foam::cellList> Foam::manifoldCellsMeshObject::filter
156 (
157  const polyMesh& mesh
158 )
159 {
160  label count = 0;
161  return filter(mesh, count);
162 }
163 
164 
165 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
166 
167 Foam::manifoldCellsMeshObject::manifoldCellsMeshObject(const polyMesh& mesh)
168 :
170  cellsPtr_(nullptr),
171  nCorrected_(-1)
172 {}
173 
174 
175 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
176 
178 {
179  if (nCorrected_ < 0)
180  {
181  cellsPtr_ = filter(mesh(), nCorrected_);
182  }
183 
184  return (nCorrected_ > 0);
185 }
186 
187 
189 {
190  if (nCorrected_ < 0)
191  {
192  cellsPtr_ = filter(mesh(), nCorrected_);
193  }
194 
195  return (cellsPtr_ ? cellsPtr_.cref() : mesh().cells());
196 }
197 
198 
199 void Foam::manifoldCellsMeshObject::updateMesh(const mapPolyMesh&)
200 {
201  cellsPtr_.reset(nullptr);
202  nCorrected_ = -1;
203 }
204 
205 
206 // ************************************************************************* //
static refPtr< T > New(Args &&... args)
Construct refPtr with forwarding arguments.
Definition: refPtr.H:187
const cellList & cells() const
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
defineTypeName(manifoldCellsMeshObject)
Templated abstract base-class for optional mesh objects used to automate their allocation to the mesh...
Definition: MeshObject.H:85
Provides cell-to-faces (&#39;cells()&#39;) with duplicate faces removed.
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
dynamicFvMesh & mesh
const polyMesh & mesh() const noexcept
Reference to the mesh.
Definition: MeshObject.H:157
virtual void updateMesh(const mapPolyMesh &)
Mesh changes.
virtual const faceList & faces() const
Return raw faces.
Definition: polyMesh.C:1103
labelList f(nPoints)
bool manifold() const
True if any manifold cells detected (globally) Triggers demand-driven filtering if required...
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.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
static bool sameVertices(const face &a, const face &b)
True if the faces have all the same vertices.
Definition: face.C:374
const cellList & cells() const
Return the (optionally compacted) cell list Triggers demand-driven filtering if required.
Namespace for OpenFOAM.
static void replaceAll(const label oldVal, const label newVal, labelUList &list)