BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / src / OpenFOAM / meshes / meshShapes / cellMatcher / cellMatcher.C
blobf94aef37f998cb1cc4336ddc86aa05f8beb38647
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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.
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.
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "cellMatcher.H"
28 #include "primitiveMesh.H"
29 #include "Map.H"
30 #include "faceList.H"
31 #include "labelList.H"
32 #include "ListOps.H"
35 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
37 Foam::cellMatcher::cellMatcher
39     const label vertPerCell,
40     const label facePerCell,
41     const label maxVertPerFace,
42     const word& cellModelName
45     localPoint_(100),
46     localFaces_(facePerCell),
47     faceSize_(facePerCell, -1),
48     pointMap_(vertPerCell),
49     faceMap_(facePerCell),
50     edgeFaces_(2*vertPerCell*vertPerCell),
51     pointFaceIndex_(vertPerCell),
52     vertLabels_(vertPerCell),
53     faceLabels_(facePerCell),
54     cellModelName_(cellModelName),
55     cellModelPtr_(NULL)
57     forAll(localFaces_, faceI)
58     {
59         face& f = localFaces_[faceI];
61         f.setSize(maxVertPerFace);
62     }
64     forAll(pointFaceIndex_, vertI)
65     {
66         pointFaceIndex_[vertI].setSize(facePerCell);
67     }
71 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
73 // Create localFaces_ , pointMap_ , faceMap_
74 Foam::label Foam::cellMatcher::calcLocalFaces
76     const faceList& faces,
77     const labelList& myFaces
80     // Clear map from global to cell numbering
81     localPoint_.clear();
83     // Renumber face vertices and insert directly into localFaces_
84     label newVertI = 0;
85     forAll(myFaces, myFaceI)
86     {
87         label faceI = myFaces[myFaceI];
89         const face& f = faces[faceI];
90         face& localFace = localFaces_[myFaceI];
92         // Size of localFace
93         faceSize_[myFaceI] = f.size();
95         forAll(f, localVertI)
96         {
97             label vertI = f[localVertI];
99             Map<label>::iterator iter = localPoint_.find(vertI);
100             if (iter == localPoint_.end())
101             {
102                 // Not found. Assign local vertex number.
104                 if (newVertI >= pointMap_.size())
105                 {
106                     // Illegal face: more unique vertices than vertPerCell
107                     return -1;
108                 }
110                 localFace[localVertI] = newVertI;
111                 localPoint_.insert(vertI, newVertI);
112                 newVertI++;
113             }
114             else
115             {
116                 // Reuse local vertex number.
117                 localFace[localVertI] = *iter;
118             }
119         }
121         // Create face from localvertex labels
122         faceMap_[myFaceI] = faceI;
123     }
125     // Create local to global vertex mapping
126     forAllConstIter(Map<label>, localPoint_, iter)
127     {
128         const label fp = iter();
129         pointMap_[fp] = iter.key();
130     }
132     ////debug
133     //write(Info);
135     return newVertI;
139 // Create edgeFaces_ : map from edge to two localFaces for single cell.
140 void Foam::cellMatcher::calcEdgeAddressing(const label numVert)
142     edgeFaces_ = -1;
144     forAll(localFaces_, localFaceI)
145     {
146         const face& f = localFaces_[localFaceI];
148         label prevVertI = faceSize_[localFaceI] - 1;
149         //forAll(f, fp)
150         for
151         (
152             label fp = 0;
153             fp < faceSize_[localFaceI];
154             fp++
155         )
156         {
157             label start = f[prevVertI];
158             label end = f[fp];
160             label key1 = edgeKey(numVert, start, end);
161             label key2 = edgeKey(numVert, end, start);
163             if (edgeFaces_[key1] == -1)
164             {
165                 // Entry key1 unoccupied. Store both permutations.
166                 edgeFaces_[key1] = localFaceI;
167                 edgeFaces_[key2] = localFaceI;
168             }
169             else if (edgeFaces_[key1+1] == -1)
170             {
171                 // Entry key1+1 unoccupied
172                 edgeFaces_[key1+1] = localFaceI;
173                 edgeFaces_[key2+1] = localFaceI;
174             }
175             else
176             {
177                 FatalErrorIn
178                 (
179                     "calcEdgeAddressing"
180                     "(const faceList&, const label)"
181                 )   << "edgeFaces_ full at entry:" << key1
182                     << " for edge " << start << " " << end
183                     << abort(FatalError);
184             }
186             prevVertI = fp;
187         }
188     }
192 // Create pointFaceIndex_ : map from vertI, faceI to index of vertI on faceI.
193 void Foam::cellMatcher::calcPointFaceIndex()
195     // Fill pointFaceIndex_ with -1
196     forAll(pointFaceIndex_, i)
197     {
198         labelList& faceIndices = pointFaceIndex_[i];
200         faceIndices = -1;
201     }
203     forAll(localFaces_, localFaceI)
204     {
205         const face& f = localFaces_[localFaceI];
207         for
208         (
209             label fp = 0;
210             fp < faceSize_[localFaceI];
211             fp++
212         )
213         {
214             label vert = f[fp];
215             pointFaceIndex_[vert][localFaceI] = fp;
216         }
217     }
221 // Given edge(v0,v1) and (local)faceI return the other face
222 Foam::label Foam::cellMatcher::otherFace
224     const label numVert,
225     const label v0,
226     const label v1,
227     const label localFaceI
228 ) const
230     label key = edgeKey(numVert, v0, v1);
232     if (edgeFaces_[key] == localFaceI)
233     {
234         return edgeFaces_[key+1];
235     }
236     else if (edgeFaces_[key+1] == localFaceI)
237     {
238         return edgeFaces_[key];
239     }
240     else
241     {
242         FatalErrorIn
243         (
244             "otherFace"
245             "(const label, const labelList&, const label, const label, "
246             "const label)"
247         )   << "edgeFaces_ does not contain:" << localFaceI
248             << " for edge " << v0 << " " << v1 << " at key " << key
249             << " edgeFaces_[key, key+1]:" <<  edgeFaces_[key]
250             << " , " << edgeFaces_[key+1]
251             << abort(FatalError);
253         return -1;
254     }
258 void Foam::cellMatcher::write(Foam::Ostream& os) const
260     os  << "Faces:" << endl;
262     forAll(localFaces_, faceI)
263     {
264         os  << "    ";
266         for (label fp = 0; fp < faceSize_[faceI]; fp++)
267         {
268             os  << ' ' << localFaces_[faceI][fp];
269         }
270         os  << endl;
271     }
273     os  <<  "Face map  : " << faceMap_ << endl;
274     os  <<  "Point map : " << pointMap_ << endl;
278 // ************************************************************************* //