1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | cfMesh: A library for mesh generation
5 \\ / A nd | Copyright held by the original author
7 -------------------------------------------------------------------------------
9 This file is part of cfMesh.
11 cfMesh 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 3 of the License, or (at your
14 option) any later version.
16 cfMesh 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 cfMesh. If not, see <http://www.gnu.org/licenses/>.
25 Calulate the face centres and areas.
27 Calculate the centre by breaking the face into triangles using the face
28 centre and area-weighted averaging their centres. This method copes with
31 \*---------------------------------------------------------------------------*/
33 #include "polyMeshGenAddressing.H"
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
42 void polyMeshGenAddressing::calcFaceCentresAndAreas() const
44 if( faceCentresPtr_ || faceAreasPtr_ )
46 FatalErrorIn("polyMeshGenAddressing::calcFaceCentresAndAreas() const")
47 << "Face centres or face areas already calculated"
51 const pointFieldPMG& points = mesh_.points();
52 const faceListPMG& faces = mesh_.faces();
54 faceCentresPtr_ = new vectorField(faces.size());
55 vectorField& fCtrs = *faceCentresPtr_;
57 faceAreasPtr_ = new vectorField(faces.size());
58 vectorField& fAreas = *faceAreasPtr_;
60 makeFaceCentresAndAreas(points, fCtrs, fAreas);
63 void polyMeshGenAddressing::makeFaceCentresAndAreas
65 const pointFieldPMG& p,
70 const faceListPMG& fs = mesh_.faces();
71 const label nFaces = fs.size();
74 # pragma omp parallel for if( nFaces > 1000 )
76 for(label faceI=0;faceI<nFaces;++faceI)
78 const face& f = fs[faceI];
79 label nPoints = f.size();
81 // If the face is a triangle, do a direct calculation for efficiency
82 // and to avoid round-off error-related problems
85 fCtrs[faceI] = (1.0/3.0)*(p[f[0]] + p[f[1]] + p[f[2]]);
86 fAreas[faceI] = 0.5*((p[f[1]] - p[f[0]])^(p[f[2]] - p[f[0]]));
90 vector sumN = vector::zero;
92 vector sumAc = vector::zero;
94 point fCentre = p[f[0]];
95 for(label pi=1;pi<nPoints;++pi)
102 for(label pi=0;pi<nPoints;++pi)
104 const point& nextPoint = p[f.nextLabel(pi)];
106 vector c = p[f[pi]] + nextPoint + fCentre;
107 vector n = (nextPoint - p[f[pi]])^(fCentre - p[f[pi]]);
115 fCtrs[faceI] = (1.0/3.0)*sumAc/(sumA + VSMALL);
116 fAreas[faceI] = 0.5*sumN;
121 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
123 const vectorField& polyMeshGenAddressing::faceCentres() const
125 if( !faceCentresPtr_ )
128 if( omp_in_parallel() )
131 "const vectorField& polyMeshGenAddressing::faceCentres() const"
132 ) << "Calculating addressing inside a parallel region."
133 << " This is not thread safe" << exit(FatalError);
136 calcFaceCentresAndAreas();
139 return *faceCentresPtr_;
142 const vectorField& polyMeshGenAddressing::faceAreas() const
147 if( omp_in_parallel() )
150 "const vectorField& polyMeshGenAddressing::faceAreas() const"
151 ) << "Calculating addressing inside a parallel region."
152 << " This is not thread safe" << exit(FatalError);
155 calcFaceCentresAndAreas();
158 return *faceAreasPtr_;
161 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
163 } // End namespace Foam
165 // ************************************************************************* //