FreeFOAM The Cross-Platform CFD Toolkit
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 | Copyright (C) 1991-2010 OpenCFD Ltd.
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 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include <OpenFOAM/cellMatcher.H>
29 
30 #include <OpenFOAM/primitiveMesh.H>
31 #include <OpenFOAM/Map.H>
32 #include <OpenFOAM/faceList.H>
33 #include <OpenFOAM/labelList.H>
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
38 {
39  labelList result(nElems);
40 
41  forAll(result, elemI)
42  {
43  result[elemI] = elemI;
44  }
45  return result;
46 }
47 
48 
49 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
50 
51 // Construct from components
52 Foam::cellMatcher::cellMatcher
53 (
54  const label vertPerCell,
55  const label facePerCell,
56  const label maxVertPerFace,
57  const word& cellModelName
58 )
59 :
60  localPoint_(100),
61  localFaces_(facePerCell),
62  faceSize_(facePerCell, -1),
63  pointMap_(vertPerCell),
64  faceMap_(facePerCell),
65  edgeFaces_(2*vertPerCell*vertPerCell),
66  pointFaceIndex_(vertPerCell),
67  vertLabels_(vertPerCell),
68  faceLabels_(facePerCell),
69  cellModelName_(cellModelName),
70  cellModelPtr_(NULL)
71 {
72  forAll(localFaces_, faceI)
73  {
74  face& f = localFaces_[faceI];
75 
76  f.setSize(maxVertPerFace);
77  }
78 
79  forAll(pointFaceIndex_, vertI)
80  {
81  pointFaceIndex_[vertI].setSize(facePerCell);
82  }
83 }
84 
85 
86 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
87 
88 // Create localFaces_ , pointMap_ , faceMap_
90 (
91  const faceList& faces,
92  const labelList& myFaces
93 )
94 {
95  // Clear map from global to cell numbering
96  localPoint_.clear();
97 
98  // Renumber face vertices and insert directly into localFaces_
99  label newVertI = 0;
100  forAll(myFaces, myFaceI)
101  {
102  label faceI = myFaces[myFaceI];
103 
104  const face& f = faces[faceI];
105  face& localFace = localFaces_[myFaceI];
106 
107  // Size of localFace
108  faceSize_[myFaceI] = f.size();
109 
110  forAll(f, localVertI)
111  {
112  label vertI = f[localVertI];
113 
114  Map<label>::iterator iter = localPoint_.find(vertI);
115  if (iter == localPoint_.end())
116  {
117  // Not found. Assign local vertex number.
118 
119  if (newVertI >= pointMap_.size())
120  {
121  // Illegal face: more unique vertices than vertPerCell
122  return -1;
123  }
124 
125  localFace[localVertI] = newVertI;
126  localPoint_.insert(vertI, newVertI);
127  newVertI++;
128  }
129  else
130  {
131  // Reuse local vertex number.
132  localFace[localVertI] = *iter;
133  }
134  }
135 
136  // Create face from localvertex labels
137  faceMap_[myFaceI] = faceI;
138  }
139 
140  // Create local to global vertex mapping
141  for
142  (
143  Map<label>::iterator iter = localPoint_.begin();
144  iter != localPoint_.end();
145  ++iter
146  )
147  {
148  label fp = iter();
149  pointMap_[fp] = iter.key();
150  }
151 
153  //write(Info);
154 
155  return newVertI;
156 }
157 
158 
159 // Create edgeFaces_ : map from edge to two localFaces for single cell.
160 void Foam::cellMatcher::calcEdgeAddressing(const label numVert)
161 {
162  edgeFaces_ = -1;
163 
164  forAll(localFaces_, localFaceI)
165  {
166  const face& f = localFaces_[localFaceI];
167 
168  label prevVertI = faceSize_[localFaceI] - 1;
169  //forAll(f, fp)
170  for
171  (
172  label fp = 0;
173  fp < faceSize_[localFaceI];
174  fp++
175  )
176  {
177  label start = f[prevVertI];
178  label end = f[fp];
179 
180  label key1 = edgeKey(numVert, start, end);
181  label key2 = edgeKey(numVert, end, start);
182 
183  if (edgeFaces_[key1] == -1)
184  {
185  // Entry key1 unoccupied. Store both permutations.
186  edgeFaces_[key1] = localFaceI;
187  edgeFaces_[key2] = localFaceI;
188  }
189  else if (edgeFaces_[key1+1] == -1)
190  {
191  // Entry key1+1 unoccupied
192  edgeFaces_[key1+1] = localFaceI;
193  edgeFaces_[key2+1] = localFaceI;
194  }
195  else
196  {
198  (
199  "calcEdgeAddressing"
200  "(const faceList&, const label)"
201  ) << "edgeFaces_ full at entry:" << key1
202  << " for edge " << start << " " << end
203  << abort(FatalError);
204  }
205 
206  prevVertI = fp;
207  }
208  }
209 }
210 
211 
212 // Create pointFaceIndex_ : map from vertI, faceI to index of vertI on faceI.
214 {
215  // Fill pointFaceIndex_ with -1
216  forAll(pointFaceIndex_, i)
217  {
218  labelList& faceIndices = pointFaceIndex_[i];
219 
220  faceIndices = -1;
221  }
222 
223  forAll(localFaces_, localFaceI)
224  {
225  const face& f = localFaces_[localFaceI];
226 
227  for
228  (
229  label fp = 0;
230  fp < faceSize_[localFaceI];
231  fp++
232  )
233  {
234  label vert = f[fp];
235  pointFaceIndex_[vert][localFaceI] = fp;
236  }
237  }
238 }
239 
240 
241 // Given edge(v0,v1) and (local)faceI return the other face
243 (
244  const label numVert,
245  const label v0,
246  const label v1,
247  const label localFaceI
248 ) const
249 {
250  label key = edgeKey(numVert, v0, v1);
251 
252  if (edgeFaces_[key] == localFaceI)
253  {
254  return edgeFaces_[key+1];
255  }
256  else if (edgeFaces_[key+1] == localFaceI)
257  {
258  return edgeFaces_[key];
259  }
260  else
261  {
263  (
264  "otherFace"
265  "(const label, const labelList&, const label, const label, "
266  "const label)"
267  ) << "edgeFaces_ does not contain:" << localFaceI
268  << " for edge " << v0 << " " << v1 << " at key " << key
269  << " edgeFaces_[key, key+1]:" << edgeFaces_[key]
270  << " , " << edgeFaces_[key+1]
271  << abort(FatalError);
272 
273  return -1;
274  }
275 }
276 
277 
279 {
280  os << "Faces:" << endl;
281 
282  forAll(localFaces_, faceI)
283  {
284  os << " ";
285 
286  for(label fp = 0; fp < faceSize_[faceI]; fp++)
287  {
288  os << ' ' << localFaces_[faceI][fp];
289  }
290  os << endl;
291  }
292 
293  os << "Face map : " << faceMap_ << endl;
294  os << "Point map : " << pointMap_ << endl;
295 }
296 
297 
298 // ************************ vim: set sw=4 sts=4 et: ************************ //