Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / foam / meshes / primitiveMesh / primitivePatch / walkPatch.C
blob566784d7c6abd12673656cd8d060f63fda63fef7
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Description
26 \*---------------------------------------------------------------------------*/
28 #include "walkPatch.H"
29 #include "ListOps.H"
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 defineTypeNameAndDebug(Foam::walkPatch, 0);
35 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
37 // Get other face using v0, v1 (in localFaces numbering). Or -1.
38 Foam::label Foam::walkPatch::getNeighbour
40     const label faceI,
41     const label fp,
42     const label v0,
43     const label v1
44 ) const
46     const labelList& fEdges = pp_.faceEdges()[faceI];
48     const edgeList& edges = pp_.edges();
51     label nbrEdgeI = -1;
53     // Shortcut: maybe faceEdges are sorted(?) in which case fEdges[fp] is
54     // edge between v0 and v1.
55     const edge& e = edges[fEdges[fp]];
57     if ((e[0] == v0 && e[1] == v1) || (e[0] == v1 && e[1] == v0))
58     {
59         // Correct edge.
60         nbrEdgeI = fEdges[fp];
61     }
62     else
63     {
64         // Loop over all faceEdges.
65         forAll(fEdges, i)
66         {
67             label edgeI = fEdges[i];
69             const edge& e = edges[edgeI];
71             if
72             (
73                 (e[0] == v0 && e[1] == v1)
74              || (e[0] == v1 && e[1] == v0)
75             )
76             {
77                 // Found edge on face which uses v0, v1.
78                 nbrEdgeI = edgeI;
80                 break;
81             }
82         }
83     }
86     if (nbrEdgeI == -1)
87     {
88         FatalErrorIn("getNeighbour")
89             << "Did not find edge on face " << faceI << " that uses vertices"
90             << v0 << " and " << v1 << abort(FatalError);
91     }
94     // Get neighbouring face.
96     const labelList& eFaces = pp_.edgeFaces()[nbrEdgeI];
98     if (eFaces.size() == 1)
99     {
100         return -1;
101     }
102     else if (eFaces.size() == 2)
103     {
104         label nbrFaceI = eFaces[0];
106         if (nbrFaceI == faceI)
107         {
108             nbrFaceI = eFaces[1];
109         }
111         return nbrFaceI;
112     }
113     else
114     {
115         FatalErrorIn("getNeighbour")
116             << "Illegal surface on patch. Face " << faceI
117             << " at vertices " << v0 << ',' << v1
118             << " has fewer than 1 or more than 2 neighbours"
119             << abort(FatalError);
120         return -1;
121     }
125 // Gets labels of changed faces and enterVertices on faces.
126 // Returns labels of faces changed and enterVertices on them.
127 void Foam::walkPatch::faceToFace
129     const labelList& changedFaces,
130     const labelList& enterVerts,
132     labelList& nbrFaces,
133     labelList& nbrEnterVerts
136     nbrFaces.setSize(pp_.size());
137     nbrEnterVerts.setSize(pp_.size());
138     label changedI = 0;
140     forAll(changedFaces, i)
141     {
142         label faceI = changedFaces[i];
143         label enterVertI = enterVerts[i];
145         if (!visited_[faceI])
146         {
147             // Do this face
148             visited_[faceI] = true;
149             visitOrder_.append(faceI);
151             const face& f = pp_.localFaces()[faceI];
153             label fp = findIndex(f, enterVertI);
155             indexInFace_.append(fp);
157             // Visit neighbouring faces in order, starting at fp.
158             for (label i = 0; i < f.size(); i++)
159             {
160                 label fp1 = reverse_ ? f.rcIndex(fp) : f.fcIndex(fp);
161                 label nbr = getNeighbour(faceI, fp, f[fp], f[fp1]);
163                 if
164                 (
165                     nbr != -1
166                  && !visited_[nbr]
167                  && faceZone_[nbr] == faceZone_[faceI]
168                 )
169                 {
170                     nbrFaces[changedI] = nbr;
171                     nbrEnterVerts[changedI] = f[fp];
172                     changedI++;
173                 }
175                 fp = fp1;
176             }
177         }
178     }
180     nbrFaces.setSize(changedI);
181     nbrEnterVerts.setSize(changedI);
185 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
187 // Construct from components
188 Foam::walkPatch::walkPatch
190     const primitivePatch& pp,
191     const labelList& faceZone,
192     const bool reverse,
193     const label faceI,
194     const label enterVertI,
195     boolList& visited
198     pp_(pp),
199     faceZone_(faceZone),
200     reverse_(reverse),
201     visited_(visited),
202     visitOrder_(pp.size()),
203     indexInFace_(pp.size())
205     // List of faces that have been visited in the current iteration.
206     labelList changedFaces(1, faceI);
207     // Corresponding list of entry vertices
208     labelList enterVerts(1, enterVertI);
210     while (true)
211     {
212         labelList nbrFaces;
213         labelList nbrEnterVerts;
215         faceToFace
216         (
217             changedFaces,
218             enterVerts,
220             nbrFaces,
221             nbrEnterVerts
222         );
225         if (nbrFaces.empty())
226         {
227             break;
228         }
230         changedFaces = nbrFaces;
231         enterVerts = nbrEnterVerts;
232     }
234     visitOrder_.shrink();
235     indexInFace_.shrink();
239 // ************************************************************************* //