FreeFOAM The Cross-Platform CFD Toolkit
walkPatch.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 "walkPatch.H"
29 #include <OpenFOAM/ListOps.H>
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
34 
35 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36 
37 // Get other face using v0, v1 (in localFaces numbering). Or -1.
38 Foam::label Foam::walkPatch::getNeighbour
39 (
40  const label faceI,
41  const label fp,
42  const label v0,
43  const label v1
44 ) const
45 {
46  const labelList& fEdges = pp_.faceEdges()[faceI];
47 
48  const edgeList& edges = pp_.edges();
49 
50 
51  label nbrEdgeI = -1;
52 
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]];
56 
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];
68 
69  const edge& e = edges[edgeI];
70 
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;
79 
80  break;
81  }
82  }
83  }
84 
85 
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  }
92 
93 
94  // Get neighbouring face.
95 
96  const labelList& eFaces = pp_.edgeFaces()[nbrEdgeI];
97 
98  if (eFaces.size() == 1)
99  {
100  return -1;
101  }
102  else if (eFaces.size() == 2)
103  {
104  label nbrFaceI = eFaces[0];
105 
106  if (nbrFaceI == faceI)
107  {
108  nbrFaceI = eFaces[1];
109  }
110 
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  }
122 }
123 
124 
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
128 (
129  const labelList& changedFaces,
130  const labelList& enterVerts,
131 
132  labelList& nbrFaces,
133  labelList& nbrEnterVerts
134 )
135 {
136  nbrFaces.setSize(pp_.size());
137  nbrEnterVerts.setSize(pp_.size());
138  label changedI = 0;
139 
140  forAll(changedFaces, i)
141  {
142  label faceI = changedFaces[i];
143  label enterVertI = enterVerts[i];
144 
145  if (!visited_[faceI])
146  {
147  // Do this face
148  visited_[faceI] = true;
149  visitOrder_.append(faceI);
150 
151  const face& f = pp_.localFaces()[faceI];
152 
153  label fp = findIndex(f, enterVertI);
154 
155  indexInFace_.append(fp);
156 
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]);
162 
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  }
174 
175  fp = fp1;
176  }
177  }
178  }
179 
180  nbrFaces.setSize(changedI);
181  nbrEnterVerts.setSize(changedI);
182 }
183 
184 
185 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
186 
187 // Construct from components
188 Foam::walkPatch::walkPatch
189 (
190  const primitivePatch& pp,
191  const labelList& faceZone,
192  const bool reverse,
193  const label faceI,
194  const label enterVertI,
195  boolList& visited
196 )
197 :
198  pp_(pp),
199  faceZone_(faceZone),
200  reverse_(reverse),
201  visited_(visited),
202  visitOrder_(pp.size()),
203  indexInFace_(pp.size())
204 {
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);
209 
210  while (true)
211  {
212  labelList nbrFaces;
213  labelList nbrEnterVerts;
214 
215  faceToFace
216  (
217  changedFaces,
218  enterVerts,
219 
220  nbrFaces,
221  nbrEnterVerts
222  );
223 
224 
225  if (nbrFaces.empty())
226  {
227  break;
228  }
229 
230  changedFaces = nbrFaces;
231  enterVerts = nbrEnterVerts;
232  }
233 
234  visitOrder_.shrink();
235  indexInFace_.shrink();
236 }
237 
238 
239 // ************************ vim: set sw=4 sts=4 et: ************************ //