OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

directionInfo.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 
00026 \*---------------------------------------------------------------------------*/
00027 
00028 #include "directionInfo.H"
00029 #include "hexMatcher.H"
00030 #include "meshTools.H"
00031 #include "polyMesh.H"
00032 
00033 
00034 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00035 
00036 bool Foam::directionInfo::equal(const edge& e, const label v0, const label v1)
00037 {
00038     return
00039         (e.start() == v0 && e.end() == v1)
00040      || (e.start() == v1 && e.end() == v0);
00041 }
00042 
00043 
00044 Foam::point Foam::directionInfo::eMid
00045 (
00046     const primitiveMesh& mesh,
00047     const label edgeI
00048 )
00049 {
00050     const edge& e = mesh.edges()[edgeI];
00051 
00052     return
00053         0.5
00054       * (mesh.points()[e.start()] + mesh.points()[e.end()]);
00055 }
00056 
00057 
00058 // Find edge among edgeLabels that uses v0 and v1
00059 Foam::label Foam::directionInfo::findEdge
00060 (
00061     const primitiveMesh& mesh,
00062     const labelList& edgeLabels,
00063     const label v1,
00064     const label v0
00065 )
00066 {
00067     forAll(edgeLabels, edgeLabelI)
00068     {
00069         label edgeI = edgeLabels[edgeLabelI];
00070 
00071         const edge& e = mesh.edges()[edgeI];
00072 
00073         if (equal(e, v0, v1))
00074         {
00075             return edgeI;
00076         }
00077     }
00078 
00079     FatalErrorIn("directionInfo::findEdge")
00080         << "Cannot find an edge among " << edgeLabels << endl
00081         << "that uses vertices " << v0
00082         << " and " << v1
00083         << abort(FatalError);
00084 
00085     return -1;
00086 }
00087 
00088 
00089 Foam::label Foam::directionInfo::lowest
00090 (
00091     const label size,
00092     const label a,
00093     const label b
00094 )
00095 {
00096     // Get next point
00097     label a1 = (a + 1) % size;
00098 
00099     if (a1 == b)
00100     {
00101         return a;
00102     }
00103     else
00104     {
00105         label b1 = (b + 1) % size;
00106 
00107         if (b1 != a)
00108         {
00109             FatalErrorIn("directionInfo::lowest")
00110                 << "Problem : a:" << a << " b:" << b << " size:" << size
00111                 << abort(FatalError);
00112         }
00113 
00114         return b;
00115     }
00116 }
00117 
00118 
00119 // Have edge on hex cell. Find corresponding edge on face. Return -1 if none
00120 // found.
00121 Foam::label Foam::directionInfo::edgeToFaceIndex
00122 (
00123     const primitiveMesh& mesh,
00124     const label cellI,
00125     const label faceI,
00126     const label edgeI
00127 )
00128 {
00129     if ((edgeI < 0) || (edgeI >= mesh.nEdges()))
00130     {
00131         FatalErrorIn("directionInfo::edgeToFaceIndex")
00132             << "Illegal edge label:" << edgeI
00133             << " when projecting cut edge from cell " << cellI
00134             << " to face " << faceI
00135             << abort(FatalError);
00136     }
00137 
00138     const edge& e = mesh.edges()[edgeI];
00139 
00140     const face& f = mesh.faces()[faceI];
00141 
00142     // edgeI is either
00143     // - in faceI. Convert into index in face.
00144     // - connected (but not in) to face. Return -1.
00145     // - in face opposite faceI. Convert into index in face.
00146 
00147     label fpA = findIndex(f, e.start());
00148     label fpB = findIndex(f, e.end());
00149 
00150     if (fpA != -1)
00151     {
00152         if (fpB != -1)
00153         {
00154             return lowest(f.size(), fpA, fpB);
00155         }
00156         else
00157         {
00158             // e.start() in face, e.end() not
00159             return -1;
00160         }
00161     }
00162     else
00163     {
00164         if (fpB != -1)
00165         {
00166             // e.end() in face, e.start() not
00167             return -1;
00168         }
00169         else
00170         {
00171             // Both not in face.
00172             // e is on opposite face. Determine corresponding edge on this face:
00173             // - determine two faces using edge (one is the opposite face, 
00174             //   one is 'side' face
00175             // - walk on both these faces to opposite edge
00176             // - check if this opposite edge is on faceI
00177 
00178             label f0I, f1I;
00179 
00180             meshTools::getEdgeFaces(mesh, cellI, edgeI, f0I, f1I);
00181 
00182             // Walk to opposite edge on face f0
00183             label edge0I =
00184                 meshTools::walkFace(mesh, f0I, edgeI, e.start(), 2);
00185 
00186             // Check if edge on faceI.
00187 
00188             const edge& e0 = mesh.edges()[edge0I];
00189 
00190             fpA = findIndex(f, e0.start());
00191             fpB = findIndex(f, e0.end());
00192 
00193             if ((fpA != -1) && (fpB != -1))
00194             {
00195                 return lowest(f.size(), fpA, fpB);
00196             }
00197 
00198             // Face0 is doesn't have an edge on faceI (so must be the opposite
00199             // face) so try face1.
00200 
00201             // Walk to opposite edge on face f1
00202             label edge1I =
00203                 meshTools::walkFace(mesh, f1I, edgeI, e.start(), 2);
00204 
00205             // Check if edge on faceI.
00206             const edge& e1 = mesh.edges()[edge1I];
00207 
00208             fpA = findIndex(f, e1.start());
00209             fpB = findIndex(f, e1.end());
00210 
00211             if ((fpA != -1) && (fpB != -1))
00212             {
00213                 return lowest(f.size(), fpA, fpB);
00214             }
00215 
00216             FatalErrorIn("directionInfo::edgeToFaceIndex")
00217                 << "Found connected faces " << mesh.faces()[f0I] << " and "
00218                 << mesh.faces()[f1I] << " sharing edge " << edgeI << endl
00219                 << "But none seems to be connected to face " << faceI
00220                 << " vertices:" << f
00221                 << abort(FatalError);
00222 
00223             return -1;
00224         }
00225     }
00226 }
00227 
00228 
00229 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00230 
00231 // Update this cell with neighbouring face information
00232 bool Foam::directionInfo::updateCell
00233 (
00234     const polyMesh& mesh,
00235     const label thisCellI,
00236     const label neighbourFaceI,
00237     const directionInfo& neighbourInfo,
00238     const scalar        // tol
00239 )
00240 {
00241     if (index_ >= -2)
00242     {
00243         // Already determined.
00244         return false;
00245     }
00246 
00247     if (hexMatcher().isA(mesh, thisCellI))
00248     {
00249         const face& f = mesh.faces()[neighbourFaceI];
00250 
00251         if (neighbourInfo.index() == -2)
00252         {
00253             // Geometric information from neighbour
00254             index_ = -2;
00255         }
00256         else if (neighbourInfo.index() == -1)
00257         {
00258             // Cut tangential to face. Take any edge connected to face
00259             // but not used in face.
00260 
00261             // Get first edge on face.
00262             label edgeI = mesh.faceEdges()[neighbourFaceI][0];
00263 
00264             const edge& e = mesh.edges()[edgeI];
00265 
00266             // Find face connected to face through edgeI and on same cell.
00267             label faceI = 
00268                 meshTools::otherFace
00269                 (
00270                     mesh,
00271                     thisCellI,
00272                     neighbourFaceI,
00273                     edgeI
00274                 );
00275 
00276             // Find edge on faceI which is connected to e.start() but not edgeI.
00277             index_ =
00278                 meshTools::otherEdge
00279                 (
00280                     mesh,
00281                     mesh.faceEdges()[faceI],
00282                     edgeI,
00283                     e.start()
00284                 );
00285         }
00286         else
00287         {
00288             // Index is a vertex on the face. Convert to mesh edge.
00289 
00290             // Get mesh edge between f[index_] and f[index_+1]
00291             label v0 = f[neighbourInfo.index()];
00292             label v1 = f[(neighbourInfo.index() + 1) % f.size()];
00293 
00294             index_ = findEdge(mesh, mesh.faceEdges()[neighbourFaceI], v0, v1);
00295         }
00296     }
00297     else
00298     {
00299         // Not a hex so mark this as geometric.
00300         index_ = -2;
00301     }
00302 
00303 
00304     n_ = neighbourInfo.n();
00305 
00306     return true;
00307 }    
00308 
00309 
00310 // Update this face with neighbouring cell information
00311 bool Foam::directionInfo::updateFace
00312 (
00313     const polyMesh& mesh,
00314     const label thisFaceI,
00315     const label neighbourCellI,
00316     const directionInfo& neighbourInfo,
00317     const scalar    // tol
00318 )
00319 {
00320     // Handle special cases first
00321 
00322     if (index_ >= -2)
00323     {
00324         // Already determined
00325         return false;
00326     }
00327 
00328     // Handle normal cases where topological or geometrical info comes from
00329     // neighbouring cell
00330 
00331     if (neighbourInfo.index() >= 0)
00332     {
00333         // Neighbour has topological direction (and hence is hex). Find cut
00334         // edge on face.
00335         index_ =
00336             edgeToFaceIndex
00337             (
00338                 mesh,
00339                 neighbourCellI,
00340                 thisFaceI,
00341                 neighbourInfo.index()
00342             );
00343     }
00344     else
00345     {
00346         // Neighbour has geometric information. Use.
00347         index_ = -2;
00348     }
00349 
00350 
00351     n_ = neighbourInfo.n();
00352 
00353     return true;
00354 }    
00355 
00356 
00357 // Merge this with information on same face
00358 bool Foam::directionInfo::updateFace
00359 (
00360     const polyMesh& mesh,
00361     const label,    // thisFaceI
00362     const directionInfo& neighbourInfo,
00363     const scalar    // tol
00364 )
00365 {
00366     if (index_ >= -2)
00367     {
00368         // Already visited.
00369         return false;
00370     }
00371     else
00372     {
00373         index_ = neighbourInfo.index();
00374 
00375         n_ = neighbourInfo.n();
00376 
00377         return true;
00378     }
00379 }    
00380 
00381 
00382 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00383 
00384 Foam::Ostream& Foam::operator<<
00385 (
00386     Foam::Ostream& os,
00387     const Foam::directionInfo& wDist
00388 )
00389 {
00390     return os << wDist.index_ << wDist.n_;
00391 }
00392 
00393 
00394 Foam::Istream& Foam::operator>>(Foam::Istream& is, Foam::directionInfo& wDist)
00395 {
00396     return is >> wDist.index_ >> wDist.n_;
00397 }
00398 
00399 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd