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

hexCellLooper.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 #include "hexCellLooper.H"
00028 #include "cellFeatures.H"
00029 #include "polyMesh.H"
00030 #include "cellModeller.H"
00031 #include "mathematicalConstants.H"
00032 #include "plane.H"
00033 #include "ListOps.H"
00034 #include "meshTools.H"
00035 #include "OFstream.H"
00036 
00037 #include "addToRunTimeSelectionTable.H"
00038 
00039 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00040 
00041 namespace Foam
00042 {
00043 
00044 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00045 
00046 defineTypeNameAndDebug(hexCellLooper, 0);
00047 
00048 addToRunTimeSelectionTable(cellLooper, hexCellLooper, word);
00049 
00050 
00051 }
00052 
00053 
00054 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00055 
00056 // Starting from cut edge start walking.
00057 bool Foam::hexCellLooper::walkHex
00058 (
00059     const label cellI,
00060     const label startFaceI,
00061     const label startEdgeI,
00062 
00063     labelList& loop,
00064     scalarField& loopWeights
00065 ) const
00066 {
00067     label faceI = startFaceI;
00068 
00069     label edgeI = startEdgeI;
00070 
00071     label cutI = 0;
00072 
00073     do
00074     {
00075         if (debug & 2)
00076         {
00077             Pout<< "    walkHex : inserting cut onto edge:" << edgeI
00078                 << " vertices:" << mesh().edges()[edgeI] << endl;
00079         }
00080 
00081         // Store cut through edge. For now cut edges halfway.
00082         loop[cutI] = edgeToEVert(edgeI);
00083         loopWeights[cutI] = 0.5;
00084         cutI++;
00085 
00086         faceI = meshTools::otherFace(mesh(), cellI, faceI, edgeI);
00087 
00088         const edge& e = mesh().edges()[edgeI];
00089 
00090         // Walk two edges further
00091         edgeI = meshTools::walkFace(mesh(), faceI, edgeI, e.end(), 2);
00092 
00093         if (edgeI == startEdgeI)
00094         {
00095             break;
00096         }
00097     }
00098     while (true);
00099 
00100     // Checks.
00101     if (cutI > 4)
00102     {
00103         Pout<< "hexCellLooper::walkHex" << "Problem : cell:" << cellI
00104             << " collected loop:";
00105         writeCuts(Pout, loop, loopWeights);
00106         Pout<< "loopWeights:" << loopWeights << endl;
00107 
00108         return false;
00109     }
00110     else
00111     {
00112         return true;
00113     }
00114 }
00115 
00116 
00117 
00118 void Foam::hexCellLooper::makeFace
00119 (
00120     const labelList& loop,
00121     const scalarField& loopWeights,
00122 
00123     labelList& faceVerts,
00124     pointField& facePoints
00125 ) const
00126 {
00127     facePoints.setSize(loop.size());
00128     faceVerts.setSize(loop.size());
00129 
00130     forAll(loop, cutI)
00131     {
00132         label cut = loop[cutI];
00133 
00134         if (isEdge(cut))
00135         {
00136             label edgeI = getEdge(cut);
00137 
00138             const edge& e = mesh().edges()[edgeI];
00139 
00140             const point& v0 = mesh().points()[e.start()];
00141             const point& v1 = mesh().points()[e.end()];
00142 
00143             facePoints[cutI] =
00144                 loopWeights[cutI]*v1 + (1.0-loopWeights[cutI])*v0;
00145         }
00146         else
00147         {
00148             label vertI = getVertex(cut);
00149 
00150             facePoints[cutI] = mesh().points()[vertI];
00151         }
00152 
00153         faceVerts[cutI] = cutI;
00154     }
00155 }
00156 
00157 
00158 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00159 
00160 // Construct from components
00161 Foam::hexCellLooper::hexCellLooper(const polyMesh& mesh)
00162 :
00163     geomCellLooper(mesh),
00164     hex_(*(cellModeller::lookup("hex")))
00165 {}
00166 
00167 
00168 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00169 
00170 Foam::hexCellLooper::~hexCellLooper()
00171 {}
00172 
00173 
00174 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00175 
00176 bool Foam::hexCellLooper::cut
00177 (
00178     const vector& refDir,
00179     const label cellI,
00180     const boolList& vertIsCut,
00181     const boolList& edgeIsCut,
00182     const scalarField& edgeWeight,
00183 
00184     labelList& loop,
00185     scalarField& loopWeights
00186 ) const
00187 {
00188     bool success = false;
00189 
00190     if (mesh().cellShapes()[cellI].model() == hex_)
00191     {
00192         // Get starting edge. Note: should be compatible with way refDir is
00193         // determined.
00194         label edgeI = meshTools::cutDirToEdge(mesh(), cellI, refDir);
00195 
00196         // Get any face using edge
00197         label face0;
00198         label face1;
00199         meshTools::getEdgeFaces(mesh(), cellI, edgeI, face0, face1);
00200 
00201         // Walk circumference of hex, cutting edges only
00202         loop.setSize(4);
00203         loopWeights.setSize(4);
00204 
00205         success = walkHex(cellI, face0, edgeI, loop, loopWeights);
00206     }
00207     else
00208     {
00209         success = geomCellLooper::cut
00210         (
00211             refDir,
00212             cellI,
00213             vertIsCut,
00214             edgeIsCut,
00215             edgeWeight,
00216 
00217             loop,
00218             loopWeights
00219         );
00220     }
00221 
00222     if (debug)
00223     {
00224         if (loop.empty())
00225         {
00226             WarningIn("hexCellLooper")
00227                 << "could not cut cell " << cellI << endl;
00228 
00229             fileName cutsFile("hexCellLooper_" + name(cellI) + ".obj");
00230 
00231             Pout<< "hexCellLooper : writing cell to " << cutsFile << endl;
00232 
00233             OFstream cutsStream(cutsFile);
00234 
00235             meshTools::writeOBJ
00236             (
00237                 cutsStream,
00238                 mesh().cells(),
00239                 mesh().faces(),
00240                 mesh().points(),
00241                 labelList(1, cellI)
00242             );
00243 
00244             return false;
00245         }
00246 
00247         // Check for duplicate cuts.
00248         labelHashSet loopSet(loop.size());
00249 
00250         forAll(loop, elemI)
00251         {
00252             label elem = loop[elemI];
00253 
00254             if (loopSet.found(elem))
00255             {
00256                 FatalErrorIn("hexCellLooper::walkHex") << " duplicate cut"
00257                     << abort(FatalError);
00258             }
00259             loopSet.insert(elem);
00260         }
00261 
00262 
00263         face faceVerts(loop.size());
00264         pointField facePoints(loop.size());
00265 
00266         makeFace(loop, loopWeights, faceVerts, facePoints);
00267 
00268         if ((faceVerts.mag(facePoints) < SMALL) || (loop.size() < 3))
00269         {
00270             FatalErrorIn("hexCellLooper::walkHex") << "Face:" << faceVerts
00271                 << " on points:" << facePoints << endl
00272                 << UIndirectList<point>(facePoints, faceVerts)()
00273                 << abort(FatalError);
00274         }
00275     }
00276     return success;
00277 }
00278 
00279 
00280 // No shortcuts for cutting with arbitrary plane.
00281 bool Foam::hexCellLooper::cut
00282 (
00283     const plane& cutPlane,
00284     const label cellI,
00285     const boolList& vertIsCut,
00286     const boolList& edgeIsCut,
00287     const scalarField& edgeWeight,
00288 
00289     labelList& loop,
00290     scalarField& loopWeights
00291 ) const
00292 {
00293     return
00294         geomCellLooper::cut
00295         (
00296             cutPlane,
00297             cellI,
00298             vertIsCut,
00299             edgeIsCut,
00300             edgeWeight,
00301 
00302             loop,
00303             loopWeights
00304         );
00305 }
00306 
00307 
00308 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd