cellEdgeAddressing.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) 2022-2024 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 "cellEdgeAddressing.H"
27 #include "polyDistributionMap.H"
28 #include "polyTopoChangeMap.H"
29 #include "polyMeshMap.H"
30 #include "HashList.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // An edge hash functor that is much faster than Hash<edge>, but is also more
38 // prone to collisions
40 {
41  unsigned operator()(const edge& e)
42  {
43  return e[0]*e[1];
44  }
45 };
46 
47 // An invalid null edge for unset entries in the edge map
48 template<>
50 
51 // Workspace for addressing calculations
53 {
55 
57 
59  :
60  edgeToCei(0),
61  cfei0()
62  {
63  resizeAndClear(6, 12);
64  }
65 
66  void resizeAndClear(const label nCellFaces, const label nCellEdges)
67  {
68  edgeToCei.resizeAndClear(nCellEdges*6);
69 
70  cfei0.resize(nCellFaces);
71  cfei0 = -1;
72  }
73 }
75 
76 }
77 
78 
79 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
80 
82 (
83  const cell& c,
84  const faceList& fs,
85  const bool cOwnsFirst
86 )
87 {
88  // Allocate and initialise the addressing
89  cfiAndFeiToCei_.resize(UIndirectList<face>(fs, c));
90  ceiToCfiAndFei_ =
92  (
93  cfiAndFeiToCei_.m().size()/2,
94  Pair<labelPair>(labelPair(-1, -1), labelPair(-1, -1))
95  );
96 
97  // Resize and reset the workspace
98  workspace_.resizeAndClear(c.size(), ceiToCfiAndFei_.size());
99 
100  // Construct a map to enumerate the cell edges
101  {
102  label cellEdgei = 0;
103 
104  forAll(c, cfi)
105  {
106  const face& f = fs[c[cfi]];
107 
108  forAll(f, fei)
109  {
110  cellEdgei +=
111  workspace_.edgeToCei.insert(f.faceEdge(fei), cellEdgei);
112  }
113  }
114  }
115 
116  // Copy data out of the map into the addressing
117  forAll(c, cfi)
118  {
119  const face& f = fs[c[cfi]];
120 
121  cfiAndFeiToCei_[cfi] = -1;
122 
123  forAll(f, fei)
124  {
125  const label cei = workspace_.edgeToCei[f.faceEdge(fei)];
126 
127  cfiAndFeiToCei_[cfi][fei] = cei;
128  ceiToCfiAndFei_[cei]
129  [
130  ceiToCfiAndFei_[cei][0] != labelPair(-1, -1)
131  ] = {cfi, fei};
132  }
133  }
134 
135  // Allocate and initialise the face ownership
136  cOwns_ = boolList(c.size(), false);
137  cOwns_[0] = cOwnsFirst;
138  workspace_.cfei0[0] = 0;
139 
140  // Walk around the cell, comparing edges to determine face ownership
141  {
142  label cfi = 0, fei = 0;
143 
144  do
145  {
146  // Get the adjacent face and face-edge (given subscript j)
147  const label cei = cfiAndFeiToCei_[cfi][fei];
148  const labelPair cfiAndFei(labelPair(cfi, fei));
149  const labelPair& cfjAndFej =
150  ceiToCfiAndFei_[cei][ceiToCfiAndFei_[cei][0] == cfiAndFei];
151  const label cfj = cfjAndFej[0], fej = cfjAndFej[1];
152 
153  // If the adjacent face has not been visited then set its ownership
154  // and its starting face edge and move forwards into it
155  if (workspace_.cfei0[cfj] == -1)
156  {
157  // If the face-edges point in different directions then the
158  // faces have the same owner. If they point in the same
159  // direction then they have different owners.
160  const label sign =
162  (
163  fs[c[cfi]].faceEdge(fei),
164  fs[c[cfj]].faceEdge(fej)
165  );
166 
167  cOwns_[cfj] = sign < 0 ? cOwns_[cfi] : !cOwns_[cfi];
168 
169  workspace_.cfei0[cfj] = fej;
170  cfi = cfj;
171  fei = (fej + 1) % fs[c[cfj]].size();
172  }
173 
174  // If the adjacent face has been visited, and we are not back at
175  // the starting edge of this face, then move to the next edge of
176  // this face
177  else if (fei != workspace_.cfei0[cfi])
178  {
179  fei = (fei + 1) % fs[c[cfi]].size();
180  }
181 
182  // If we are back at the first edge then this face is complete, so
183  // move backwards into the adjacent face
184  else // if (fei == workspace_.cfei0[cfi])
185  {
186  cfi = cfj;
187  fei = fej;
188  }
189  }
190  while (cfi != 0 || fei != 0);
191  }
192 }
193 
194 
196 :
198  <
199  polyMesh,
202  >(mesh),
203  list_(mesh.nCells())
204 {}
205 
206 
207 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
208 
210 {
211  return true;
212 }
213 
214 
216 {
217  // We could be more selective here and try to keep addressing for cells
218  // that haven't changed
219 
220  list_.clear();
221  list_.resize(map.mesh().nCells());
222 }
223 
224 
226 {
227  // We could be more selective here and try to keep addressing for cells
228  // that haven't changed
229 
230  list_.clear();
231  list_.resize(map.mesh().nCells());
232 }
233 
234 
236 {
237  list_.clear();
238  list_.resize(map.mesh().nCells());
239 }
240 
241 
242 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
void resize(const label mRows)
Reset size of CompactListList.
Templated abstract base-class for demand-driven mesh objects used to automate their allocation to the...
void resize(const label)
Alter the addressed list size.
Definition: DynamicListI.H:216
HashList class. Like HashTable, but much less dynamic memory-y. Should be faster for small sets of no...
Definition: HashList.H:60
static const Key nullKey
Null key value for unset elements in the list.
Definition: HashList.H:66
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: Pair.H:65
const UList< T > & m() const
Return the packed matrix of data.
A List with indirect addressing.
Definition: UIndirectList.H:60
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
cellEdgeAddressingData(const cell &c, const faceList &fs, const bool cOwnsFirst)
Construct from a cell and its faces.
virtual bool movePoints()
Update following mesh motion.
cellEdgeAddressingList(const polyMesh &mesh)
Construct for a mesh.
virtual void mapMesh(const polyMeshMap &map)
Update following mapping.
virtual void distribute(const polyDistributionMap &map)
Update following mesh distribution.
virtual void topoChange(const polyTopoChangeMap &map)
Update following topology change.
A cell is defined as a list of faces with extra functionality.
Definition: cell.H:60
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:61
static int compare(const edge &, const edge &)
Compare edges.
Definition: edgeI.H:36
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:76
Class containing mesh-to-mesh mapping information after a mesh distribution where we send parts of me...
const polyMesh & mesh() const
Return polyMesh.
Class containing mesh-to-mesh mapping information.
Definition: polyMeshMap.H:51
const polyMesh & mesh() const
Return polyMesh.
Definition: polyMeshMap.H:75
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
const polyMesh & mesh() const
Return polyMesh.
label nCells() const
const dimensionedScalar c
Speed of light in a vacuum.
Namespace for OpenFOAM.
Pair< label > labelPair
Label pair.
Definition: labelPair.H:48
const doubleScalar e
Definition: doubleScalar.H:106
dimensionedScalar sign(const dimensionedScalar &ds)
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
List< bool > boolList
Bool container classes.
Definition: boolList.H:50
struct Foam::cellEdgeAddressingWorkspace workspace_
static const label labelMax
Definition: label.H:62
labelList f(nPoints)
unsigned operator()(const edge &e)
HashList< label, edge, QuickHashEdge > edgeToCei
void resizeAndClear(const label nCellFaces, const label nCellEdges)