initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / meshes / meshShapes / cellMatcher / cellMatcher.C
blobdffd5db8bb6b6b599bb8c50a1f56a5bd52b938f4
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     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, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
27 \*---------------------------------------------------------------------------*/
29 #include "cellMatcher.H"
31 #include "primitiveMesh.H"
32 #include "Map.H"
33 #include "faceList.H"
34 #include "labelList.H"
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 Foam::labelList Foam::cellMatcher::makeIdentity(const label nElems)
40     labelList result(nElems);
42     forAll(result, elemI)
43     {
44         result[elemI] = elemI;
45     }
46     return result;
50 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
52 // Construct from components
53 Foam::cellMatcher::cellMatcher
55     const label vertPerCell,
56     const label facePerCell,
57     const label maxVertPerFace,
58     const word& cellModelName
61     localPoint_(100),
62     localFaces_(facePerCell),
63     faceSize_(facePerCell, -1),
64     pointMap_(vertPerCell),
65     faceMap_(facePerCell),
66     edgeFaces_(2*vertPerCell*vertPerCell),
67     pointFaceIndex_(vertPerCell),
68     vertLabels_(vertPerCell),
69     faceLabels_(facePerCell),
70     cellModelName_(cellModelName),
71     cellModelPtr_(NULL)
73     forAll(localFaces_, faceI)
74     {
75         face& f = localFaces_[faceI];
77         f.setSize(maxVertPerFace);
78     }
80     forAll(pointFaceIndex_, vertI)
81     {
82         pointFaceIndex_[vertI].setSize(facePerCell);
83     }
87 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
89 // Create localFaces_ , pointMap_ , faceMap_
90 Foam::label Foam::cellMatcher::calcLocalFaces
92     const faceList& faces,
93     const labelList& myFaces
96     // Clear map from global to cell numbering
97     localPoint_.clear();
99     // Renumber face vertices and insert directly into localFaces_
100     label newVertI = 0;
101     forAll(myFaces, myFaceI)
102     {
103         label faceI = myFaces[myFaceI];
105         const face& f = faces[faceI];
106         face& localFace = localFaces_[myFaceI];
108         // Size of localFace
109         faceSize_[myFaceI] = f.size();
111         forAll(f, localVertI)
112         {
113             label vertI = f[localVertI];
115             Map<label>::iterator iter = localPoint_.find(vertI);
116             if (iter == localPoint_.end())
117             {
118                 // Not found. Assign local vertex number.
120                 if (newVertI >= pointMap_.size())
121                 {
122                     // Illegal face: more unique vertices than vertPerCell
123                     return -1;
124                 }
126                 localFace[localVertI] = newVertI;
127                 localPoint_.insert(vertI, newVertI);
128                 newVertI++;
129             }
130             else
131             {
132                 // Reuse local vertex number.
133                 localFace[localVertI] = *iter;
134             }
135         }
137         // Create face from localvertex labels
138         faceMap_[myFaceI] = faceI;
139     }
141     // Create local to global vertex mapping
142     for
143     (
144         Map<label>::iterator iter = localPoint_.begin();
145         iter != localPoint_.end();
146         ++iter
147     )
148     {
149         label fp = iter();
150         pointMap_[fp] = iter.key();
151     }
153     ////debug
154     //write(Info);
156     return newVertI;
160 // Create edgeFaces_ : map from edge to two localFaces for single cell.
161 void Foam::cellMatcher::calcEdgeAddressing(const label numVert)
163     edgeFaces_ = -1;
165     forAll(localFaces_, localFaceI)
166     {
167         const face& f = localFaces_[localFaceI];
169         label prevVertI = faceSize_[localFaceI] - 1;
170         //forAll(f, fp)
171         for
172         (
173             label fp = 0;
174             fp < faceSize_[localFaceI];
175             fp++
176         )
177         {
178             label start = f[prevVertI];
179             label end = f[fp];
180             
181             label key1 = edgeKey(numVert, start, end);
182             label key2 = edgeKey(numVert, end, start);
184             if (edgeFaces_[key1] == -1)
185             {
186                 // Entry key1 unoccupied. Store both permutations.
187                 edgeFaces_[key1] = localFaceI;
188                 edgeFaces_[key2] = localFaceI;
189             }
190             else if (edgeFaces_[key1+1] == -1)
191             {
192                 // Entry key1+1 unoccupied
193                 edgeFaces_[key1+1] = localFaceI;
194                 edgeFaces_[key2+1] = localFaceI;
195             }
196             else
197             {
198                 FatalErrorIn
199                 (
200                     "calcEdgeAddressing"
201                     "(const faceList&, const label)"
202                 )   << "edgeFaces_ full at entry:" << key1
203                     << " for edge " << start << " " << end
204                     << abort(FatalError);
205             }
207             prevVertI = fp;
208         }
209     }
213 // Create pointFaceIndex_ : map from vertI, faceI to index of vertI on faceI.
214 void Foam::cellMatcher::calcPointFaceIndex()
216     // Fill pointFaceIndex_ with -1
217     forAll(pointFaceIndex_, i)
218     {
219         labelList& faceIndices = pointFaceIndex_[i];
221         faceIndices = -1;
222     }
224     forAll(localFaces_, localFaceI)
225     {
226         const face& f = localFaces_[localFaceI];
228         for
229         (
230             label fp = 0;
231             fp < faceSize_[localFaceI];
232             fp++
233         )
234         {
235             label vert = f[fp];
236             pointFaceIndex_[vert][localFaceI] = fp;
237         }
238     }
242 // Given edge(v0,v1) and (local)faceI return the other face
243 Foam::label Foam::cellMatcher::otherFace
245     const label numVert,
246     const label v0,
247     const label v1,
248     const label localFaceI
249 ) const
251     label key = edgeKey(numVert, v0, v1);
253     if (edgeFaces_[key] == localFaceI)
254     {
255         return edgeFaces_[key+1];
256     }
257     else if (edgeFaces_[key+1] == localFaceI)
258     {
259         return edgeFaces_[key];
260     }
261     else
262     {
263         FatalErrorIn
264         (
265             "otherFace"
266             "(const label, const labelList&, const label, const label, "
267             "const label)"
268         )   << "edgeFaces_ does not contain:" << localFaceI
269             << " for edge " << v0 << " " << v1 << " at key " << key
270             << " edgeFaces_[key, key+1]:" <<  edgeFaces_[key]
271             << " , " << edgeFaces_[key+1]
272             << abort(FatalError);
274         return -1;
275     }
279 void Foam::cellMatcher::write(Foam::Ostream& os) const
281     os  << "Faces:" << endl;
283     forAll(localFaces_, faceI)
284     {
285         os  << "    ";
287         for(label fp = 0; fp < faceSize_[faceI]; fp++)
288         {
289             os  << ' ' << localFaces_[faceI][fp];
290         }
291         os  << endl;
292     }
294     os  <<  "Face map  : " << faceMap_ << endl;
295     os  <<  "Point map : " << pointMap_ << endl;
299 // ************************************************************************* //