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

twoDPointCorrector.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 Description
00026     Class applies a two-dimensional correction to mesh motion point field.
00027     The correction guarantees that the mesh does not get twisted during motion
00028     and thus introduce a third dimension into a 2-D problem.
00029 
00030 \*---------------------------------------------------------------------------*/
00031 
00032 #include "twoDPointCorrector.H"
00033 #include "polyMesh.H"
00034 #include "wedgePolyPatch.H"
00035 #include "emptyPolyPatch.H"
00036 
00037 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00038 
00039 namespace Foam
00040 {
00041 
00042 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00043 
00044 const scalar twoDPointCorrector::edgeOrthogonalityTol = 1.0 - 1e-4;
00045 
00046 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00047 
00048 void twoDPointCorrector::calcAddressing() const
00049 {
00050     // Find geometry normal
00051     planeNormalPtr_ = new vector(0, 0, 0);
00052     vector& pn = *planeNormalPtr_;
00053 
00054     bool isWedge = false;
00055 
00056     // Algorithm:
00057     // Attempt to find wedge patch and work out the normal from it.
00058     // If not found, find an empty patch with faces in it and use the
00059     // normal of the first face.  If neither is found, declare an
00060     // error.
00061 
00062     // Try and find a wedge patch
00063     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00064 
00065     forAll (patches, patchI)
00066     {
00067         if (isA<wedgePolyPatch>(patches[patchI]))
00068         {
00069             isWedge = true;
00070 
00071             pn = refCast<const wedgePolyPatch>(patches[patchI]).centreNormal();
00072 
00073             if (polyMesh::debug)
00074             {
00075                 Pout << "Found normal from wedge patch " << patchI;
00076             }
00077 
00078             break;
00079         }
00080     }
00081 
00082     // Try to find an empty patch with faces
00083     if (!isWedge)
00084     {
00085         forAll (patches, patchI)
00086         {
00087             if (isA<emptyPolyPatch>(patches[patchI]) && patches[patchI].size())
00088             {
00089                 pn = patches[patchI].faceAreas()[0];
00090 
00091                 if (polyMesh::debug)
00092                 {
00093                     Pout << "Found normal from empty patch " << patchI;
00094                 }
00095 
00096                 break;
00097             }
00098         }
00099     }
00100 
00101 
00102     if (mag(pn) < VSMALL)
00103     {
00104         FatalErrorIn
00105         (
00106             "twoDPointCorrector::twoDPointCorrector(const polyMesh& mesh, "
00107             "const vector& n)"
00108         )   << "Cannot determine normal vector from patches."
00109             << abort(FatalError);
00110     }
00111     else
00112     {
00113         pn /= mag(pn);
00114     }
00115 
00116     if (polyMesh::debug)
00117     {
00118         Pout << " twoDPointCorrector normal: " << pn << endl;
00119     }
00120 
00121     // Select edges to be included in check.
00122     normalEdgeIndicesPtr_ = new labelList(mesh_.nEdges());
00123     labelList& neIndices = *normalEdgeIndicesPtr_;
00124 
00125     const edgeList& meshEdges = mesh_.edges();
00126 
00127     const pointField& meshPoints = mesh_.points();
00128 
00129     label nNormalEdges = 0;
00130 
00131     forAll (meshEdges, edgeI)
00132     {
00133         vector edgeVector =
00134             meshEdges[edgeI].vec(meshPoints)/
00135             (meshEdges[edgeI].mag(meshPoints) + VSMALL);
00136 
00137         if (mag(edgeVector & pn) > edgeOrthogonalityTol)
00138         {
00139             // this edge is normal to the plane. Add it to the list
00140             neIndices[nNormalEdges++] = edgeI;
00141         }
00142     }
00143 
00144     neIndices.setSize(nNormalEdges);
00145 
00146     // Construction check: number of points in a read 2-D or wedge geometry
00147     // should be odd and the number of edges normal to the plane should be
00148     // exactly half the number of points
00149     if (!isWedge)
00150     {
00151         if (meshPoints.size() % 2 != 0)
00152         {
00153             WarningIn
00154             (
00155                 "twoDPointCorrector::twoDPointCorrector("
00156                 "const polyMesh& mesh, const vector& n)"
00157             )   << "the number of vertices in the geometry "
00158                 << "is odd - this should not be the case for a 2-D case. "
00159                 << "Please check the geometry."
00160                 << endl;
00161         }
00162 
00163         if (2*nNormalEdges != meshPoints.size())
00164         {
00165             WarningIn
00166             (
00167                 "twoDPointCorrector::twoDPointCorrector("
00168                 "const polyMesh& mesh, const vector& n)"
00169             )   << "The number of points in the mesh is "
00170                 << "not equal to twice the number of edges normal to the plane "
00171                 << "- this may be OK only for wedge geometries.\n"
00172                 << "    Please check the geometry or adjust "
00173                 << "the orthogonality tolerance.\n" << endl
00174                 << "Number of normal edges: " << nNormalEdges
00175                 << " number of points: " << meshPoints.size()
00176                 << endl;
00177         }
00178     }
00179 }
00180 
00181 
00182 void twoDPointCorrector::clearAddressing() const
00183 {
00184     deleteDemandDrivenData(planeNormalPtr_);
00185     deleteDemandDrivenData(normalEdgeIndicesPtr_);
00186 }
00187 
00188 
00189 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00190 
00191 twoDPointCorrector::twoDPointCorrector(const polyMesh& mesh)
00192 :
00193     mesh_(mesh),
00194     required_(mesh_.nGeometricD() == 2),
00195     planeNormalPtr_(NULL),
00196     normalEdgeIndicesPtr_(NULL)
00197 {}
00198 
00199 
00200 
00201 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00202 
00203 Foam::twoDPointCorrector::~twoDPointCorrector()
00204 {
00205     clearAddressing();
00206 }
00207 
00208 
00209 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00210 
00211 direction twoDPointCorrector::normalDir() const
00212 {
00213     const vector& pn = planeNormal();
00214 
00215     if (mag(pn.x()) >= edgeOrthogonalityTol)
00216     {
00217         return vector::X;
00218     }
00219     else if (mag(pn.y()) >= edgeOrthogonalityTol)
00220     {
00221         return vector::Y;
00222     }
00223     else if (mag(pn.z()) >= edgeOrthogonalityTol)
00224     {
00225         return vector::Z;
00226     }
00227     else
00228     {
00229         FatalErrorIn("direction twoDPointCorrector::normalDir() const")
00230             << "Plane normal not aligned with the coordinate system" << nl
00231             << "    pn = " << pn
00232             << abort(FatalError);
00233 
00234         return vector::Z;
00235     }
00236 }
00237 
00238 
00239 // Return plane normal
00240 const vector& twoDPointCorrector::planeNormal() const
00241 {
00242     if (!planeNormalPtr_)
00243     {
00244         calcAddressing();
00245     }
00246 
00247     return *planeNormalPtr_;
00248 }
00249 
00250 
00251 // Return indices of normal edges.
00252 const labelList& twoDPointCorrector::normalEdgeIndices() const
00253 {
00254     if (!normalEdgeIndicesPtr_)
00255     {
00256         calcAddressing();
00257     }
00258 
00259     return *normalEdgeIndicesPtr_;
00260 }
00261 
00262 
00263 void twoDPointCorrector::correctPoints(pointField& p) const
00264 {
00265     if (!required_) return;
00266 
00267     // Algorithm:
00268     // Loop through all edges. Calculate the average point position A for
00269     // the front and the back. Correct the position of point P (in two planes)
00270     // such that vectors AP and planeNormal are parallel
00271 
00272     // Get reference to edges
00273     const edgeList&  meshEdges = mesh_.edges();
00274 
00275     const labelList& neIndices = normalEdgeIndices();
00276     const vector& pn = planeNormal();
00277 
00278     forAll (neIndices, edgeI)
00279     {
00280         point& pStart = p[meshEdges[neIndices[edgeI]].start()];
00281 
00282         point& pEnd = p[meshEdges[neIndices[edgeI]].end()];
00283 
00284         // calculate average point position
00285         const point A = 0.5*(pStart + pEnd);
00286 
00287         // correct point locations
00288         pStart = A + pn*(pn & (pStart - A));
00289         pEnd = A + pn*(pn & (pEnd - A));
00290     }
00291 }
00292 
00293 
00294 void twoDPointCorrector::updateMesh()
00295 {
00296     clearAddressing();
00297 }
00298 
00299 
00300 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00301 
00302 } // End namespace Foam
00303 
00304 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd