1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 Calulate the face centres and areas.
28 Calculate the centre by breaking the face into triangles using the face
29 centre and area-weighted averaging their centres. This method copes with
32 \*---------------------------------------------------------------------------*/
34 #include "primitiveMesh.H"
37 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
39 void Foam::primitiveMesh::calcFaceCentresAndAreas() const
43 Pout<< "primitiveMesh::calcFaceCentresAndAreas() : "
44 << "Calculating face centres and face areas"
48 // It is an error to attempt to recalculate faceCentres
49 // if the pointer is already set
50 if (faceCentresPtr_ || faceAreasPtr_)
52 FatalErrorIn("primitiveMesh::calcFaceCentresAndAreas() const")
53 << "Face centres or face areas already calculated"
57 faceCentresPtr_ = new vectorField(nFaces());
58 vectorField& fCtrs = *faceCentresPtr_;
60 faceAreasPtr_ = new vectorField(nFaces());
61 vectorField& fAreas = *faceAreasPtr_;
63 makeFaceCentresAndAreas(points(), fCtrs, fAreas);
67 Pout<< "primitiveMesh::calcFaceCentresAndAreas() : "
68 << "Finished calculating face centres and face areas"
74 void Foam::primitiveMesh::makeFaceCentresAndAreas
81 const faceList& fs = faces();
85 const labelList& f = fs[facei];
86 label nPoints = f.size();
88 // If the face is a triangle, do a direct calculation for efficiency
89 // and to avoid round-off error-related problems
92 fCtrs[facei] = (1.0/3.0)*(p[f[0]] + p[f[1]] + p[f[2]]);
93 fAreas[facei] = 0.5*((p[f[1]] - p[f[0]])^(p[f[2]] - p[f[0]]));
97 vector sumN = vector::zero;
99 vector sumAc = vector::zero;
101 point fCentre = p[f[0]];
102 for (label pi = 1; pi < nPoints; pi++)
109 for (label pi = 0; pi < nPoints; pi++)
111 const point& nextPoint = p[f[(pi + 1) % nPoints]];
113 vector c = p[f[pi]] + nextPoint + fCentre;
114 vector n = (nextPoint - p[f[pi]])^(fCentre - p[f[pi]]);
122 fCtrs[facei] = (1.0/3.0)*sumAc/(sumA + VSMALL);
123 fAreas[facei] = 0.5*sumN;
129 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
131 const Foam::vectorField& Foam::primitiveMesh::faceCentres() const
133 if (!faceCentresPtr_)
135 calcFaceCentresAndAreas();
138 return *faceCentresPtr_;
142 const Foam::vectorField& Foam::primitiveMesh::faceAreas() const
146 calcFaceCentresAndAreas();
149 return *faceAreasPtr_;
153 // ************************************************************************* //