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

primitiveMeshFaceCentresAndAreas.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     Calulate the face centres and areas.
00027 
00028     Calculate the centre by breaking the face into triangles using the face
00029     centre and area-weighted averaging their centres.  This method copes with
00030     small face-concavity.
00031 
00032 \*---------------------------------------------------------------------------*/
00033 
00034 #include "primitiveMesh.H"
00035 
00036 
00037 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00038 
00039 void Foam::primitiveMesh::calcFaceCentresAndAreas() const
00040 {
00041     if (debug)
00042     {
00043         Pout<< "primitiveMesh::calcFaceCentresAndAreas() : "
00044             << "Calculating face centres and face areas"
00045             << endl;
00046     }
00047 
00048     // It is an error to attempt to recalculate faceCentres
00049     // if the pointer is already set
00050     if (faceCentresPtr_ || faceAreasPtr_)
00051     {
00052         FatalErrorIn("primitiveMesh::calcFaceCentresAndAreas() const")
00053             << "Face centres or face areas already calculated"
00054             << abort(FatalError);
00055     }
00056 
00057     faceCentresPtr_ = new vectorField(nFaces());
00058     vectorField& fCtrs = *faceCentresPtr_;
00059 
00060     faceAreasPtr_ = new vectorField(nFaces());
00061     vectorField& fAreas = *faceAreasPtr_;
00062 
00063     makeFaceCentresAndAreas(points(), fCtrs, fAreas);
00064 
00065     if (debug)
00066     {
00067         Pout<< "primitiveMesh::calcFaceCentresAndAreas() : "
00068             << "Finished calculating face centres and face areas"
00069             << endl;
00070     }
00071 }
00072 
00073 
00074 void Foam::primitiveMesh::makeFaceCentresAndAreas
00075 (
00076     const pointField& p,
00077     vectorField& fCtrs,
00078     vectorField& fAreas
00079 ) const
00080 {
00081     const faceList& fs = faces();
00082 
00083     forAll (fs, facei)
00084     {
00085         const labelList& f = fs[facei];
00086         label nPoints = f.size();
00087 
00088         // If the face is a triangle, do a direct calculation for efficiency
00089         // and to avoid round-off error-related problems
00090         if (nPoints == 3)
00091         {
00092             fCtrs[facei] = (1.0/3.0)*(p[f[0]] + p[f[1]] + p[f[2]]);
00093             fAreas[facei] = 0.5*((p[f[1]] - p[f[0]])^(p[f[2]] - p[f[0]]));
00094         }
00095         else
00096         {
00097             vector sumN = vector::zero;
00098             scalar sumA = 0.0;
00099             vector sumAc = vector::zero;
00100 
00101             point fCentre = p[f[0]];
00102             for (label pi = 1; pi < nPoints; pi++)
00103             {
00104                 fCentre += p[f[pi]];
00105             }
00106 
00107             fCentre /= nPoints;
00108 
00109             for (label pi = 0; pi < nPoints; pi++)
00110             {
00111                 const point& nextPoint = p[f[(pi + 1) % nPoints]];
00112 
00113                 vector c = p[f[pi]] + nextPoint + fCentre;
00114                 vector n = (nextPoint - p[f[pi]])^(fCentre - p[f[pi]]);
00115                 scalar a = mag(n);
00116 
00117                 sumN += n;
00118                 sumA += a;
00119                 sumAc += a*c;
00120             }
00121 
00122             fCtrs[facei] = (1.0/3.0)*sumAc/(sumA + VSMALL);
00123             fAreas[facei] = 0.5*sumN;
00124         }
00125     }
00126 }
00127 
00128 
00129 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00130 
00131 const Foam::vectorField& Foam::primitiveMesh::faceCentres() const
00132 {
00133     if (!faceCentresPtr_)
00134     {
00135         calcFaceCentresAndAreas();
00136     }
00137 
00138     return *faceCentresPtr_;
00139 }
00140 
00141 
00142 const Foam::vectorField& Foam::primitiveMesh::faceAreas() const
00143 {
00144     if (!faceAreasPtr_)
00145     {
00146         calcFaceCentresAndAreas();
00147     }
00148 
00149     return *faceAreasPtr_;
00150 }
00151 
00152 
00153 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd