cellMatcher.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) 2019 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 "cellMatcher.H"
30 
31 #include "primitiveMesh.H"
32 #include "Map.H"
33 #include "faceList.H"
34 #include "labelList.H"
35 #include "ListOps.H"
36 
37 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
38 
40 (
41  const label vertPerCell,
42  const label facePerCell,
43  const label maxVertPerFace,
44  const word& cellModelName
45 )
46 :
47  localPoint_(100),
48  localFaces_(facePerCell),
49  faceSize_(facePerCell, -1),
50  pointMap_(vertPerCell),
51  faceMap_(facePerCell),
52  edgeFaces_(2*vertPerCell*vertPerCell),
53  pointFaceIndex_(vertPerCell),
54  vertLabels_(vertPerCell),
55  faceLabels_(facePerCell),
56  cellModelName_(cellModelName),
57  cellModelPtr_(nullptr)
58 {
59  for (face& f : localFaces_)
60  {
61  f.setSize(maxVertPerFace);
62  }
63 
64  for (labelList& faceIndices : pointFaceIndex_)
65  {
66  faceIndices.setSize(facePerCell);
67  }
68 }
69 
70 
71 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
72 
74 (
75  const faceList& faces,
76  const labelList& myFaces
77 )
78 {
79  // Clear map from global to cell numbering
80  localPoint_.clear();
81 
82  // Renumber face vertices and insert directly into localFaces_
83  label newVertI = 0;
84  forAll(myFaces, myFacei)
85  {
86  const label facei = myFaces[myFacei];
87 
88  const face& f = faces[facei];
89  face& localFace = localFaces_[myFacei];
90 
91  // Size of localFace
92  faceSize_[myFacei] = f.size();
93 
94  forAll(f, localVertI)
95  {
96  const label vertI = f[localVertI];
97 
98  const auto iter = localPoint_.cfind(vertI);
99  if (iter.found())
100  {
101  // Reuse local vertex number.
102  localFace[localVertI] = iter.val();
103  }
104  else
105  {
106  // Not found. Assign local vertex number.
107 
108  if (newVertI >= pointMap_.size())
109  {
110  // Illegal face: more unique vertices than vertPerCell
111  return -1;
112  }
113 
114  localFace[localVertI] = newVertI;
115  localPoint_.insert(vertI, newVertI);
116  newVertI++;
117  }
118  }
119 
120  // Create face from localvertex labels
121  faceMap_[myFacei] = facei;
122  }
123 
124  // Create local to global vertex mapping
125  forAllConstIters(localPoint_, iter)
126  {
127  pointMap_[iter.val()] = iter.key();
128  }
129 
131  //write(Info);
132 
133  return newVertI;
134 }
135 
136 
137 void Foam::cellMatcher::calcEdgeAddressing(const label numVert)
138 {
139  edgeFaces_ = -1;
140 
141  forAll(localFaces_, localFacei)
142  {
143  const face& f = localFaces_[localFacei];
144 
145  label prevVertI = faceSize_[localFacei] - 1;
146  //forAll(f, fp)
147  for
148  (
149  label fp = 0;
150  fp < faceSize_[localFacei];
151  fp++
152  )
153  {
154  label start = f[prevVertI];
155  label end = f[fp];
156 
157  label key1 = edgeKey(numVert, start, end);
158  label key2 = edgeKey(numVert, end, start);
159 
160  if (edgeFaces_[key1] == -1)
161  {
162  // Entry key1 unoccupied. Store both permutations.
163  edgeFaces_[key1] = localFacei;
164  edgeFaces_[key2] = localFacei;
165  }
166  else if (edgeFaces_[key1+1] == -1)
167  {
168  // Entry key1+1 unoccupied
169  edgeFaces_[key1+1] = localFacei;
170  edgeFaces_[key2+1] = localFacei;
171  }
172  else
173  {
175  << "edgeFaces_ full at entry:" << key1
176  << " for edge " << start << " " << end
177  << abort(FatalError);
178  }
179 
180  prevVertI = fp;
181  }
182  }
183 }
184 
185 
187 {
188  // Fill pointFaceIndex_ with -1
189  for (labelList& faceIndices : pointFaceIndex_)
190  {
191  faceIndices = -1;
192  }
193 
194  forAll(localFaces_, localFacei)
195  {
196  const face& f = localFaces_[localFacei];
197 
198  for
199  (
200  label fp = 0;
201  fp < faceSize_[localFacei];
202  ++fp
203  )
204  {
205  const label vert = f[fp];
206  pointFaceIndex_[vert][localFacei] = fp;
207  }
208  }
209 }
210 
211 
213 (
214  const label numVert,
215  const label v0,
216  const label v1,
217  const label localFacei
218 ) const
219 {
220  const label key = edgeKey(numVert, v0, v1);
221 
222  if (edgeFaces_[key] == localFacei)
223  {
224  return edgeFaces_[key+1];
225  }
226  else if (edgeFaces_[key+1] == localFacei)
227  {
228  return edgeFaces_[key];
229  }
230 
232  << "edgeFaces_ does not contain:" << localFacei
233  << " for edge " << v0 << " " << v1 << " at key " << key
234  << " edgeFaces_[key, key+1]:" << edgeFaces_[key]
235  << " , " << edgeFaces_[key+1]
236  << abort(FatalError);
237 
238  return -1;
239 }
240 
241 
243 {
244  os << "Faces:" << endl;
245 
246  forAll(localFaces_, facei)
247  {
248  os << " ";
249 
250  for (label fp = 0; fp < faceSize_[facei]; fp++)
251  {
252  os << ' ' << localFaces_[facei][fp];
253  }
254  os << nl;
255  }
256 
257  os << "Face map : " << faceMap_ << nl;
258  os << "Point map : " << pointMap_ << endl;
259 }
260 
261 
262 // ************************************************************************* //
Foam::cellMatcher::cellMatcher
cellMatcher(const cellMatcher &)=delete
No copy construct.
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::cellMatcher::write
void write(Ostream &os) const
Definition: cellMatcher.C:242
Foam::cellMatcher::localFaces_
faceList localFaces_
Faces using local vertex numbering.
Definition: cellMatcher.H:123
Foam::cellMatcher::calcLocalFaces
label calcLocalFaces(const faceList &faces, const labelList &myFaces)
Calculates localFaces. Returns number of local vertices (or -1.
Definition: cellMatcher.C:74
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
primitiveMesh.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::cellMatcher::calcEdgeAddressing
void calcEdgeAddressing(const label numVert)
Fill edge (start, end) to face number.
Definition: cellMatcher.C:137
faceList.H
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::cellMatcher::edgeFaces_
labelList edgeFaces_
Map from 'edge' to neighbouring faces.
Definition: cellMatcher.H:135
Map.H
Foam::cellMatcher::calcPointFaceIndex
void calcPointFaceIndex()
Fill vertex/face to index in face data structure.
Definition: cellMatcher.C:186
labelList.H
Foam::FatalError
error FatalError
os
OBJstream os(runTime.globalPath()/outputName)
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam::cellMatcher::edgeKey
static label edgeKey(const label numVert, const label v0, const label v1)
Given start and end of edge generate unique key.
Definition: cellMatcherI.H:97
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::cellMatcher::otherFace
label otherFace(const label numVert, const label v0, const label v1, const label localFacei) const
Given start,end of edge lookup both faces sharing it and return.
Definition: cellMatcher.C:213
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
f
labelList f(nPoints)
Foam::List< label >
cellMatcher.H
Foam::cellMatcher::faceSize_
labelList faceSize_
Number of vertices per face in localFaces_.
Definition: cellMatcher.H:126
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:72
ListOps.H
Various functions to operate on Lists.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56