Added faceCentres member function.
[OpenFOAM-1.6.x.git] / src / OpenFOAM / meshes / primitiveMesh / PrimitivePatch / PrimitivePatchMeshData.C
blobcd05f3f0e1fea2a63e4e9d83d01b6c1025535e25
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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
25 \*---------------------------------------------------------------------------*/
27 #include "PrimitivePatch.H"
28 #include "Map.H"
30 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
32 template
34     class Face,
35     template<class> class FaceList,
36     class PointField,
37     class PointType
39 void
40 Foam::PrimitivePatch<Face, FaceList, PointField, PointType>::
41 calcMeshData() const
43     if (debug)
44     {
45         Pout<< "PrimitivePatch<Face, FaceList, PointField, PointType>::"
46                "calcMeshData() : "
47                "calculating mesh data in PrimitivePatch"
48             << endl;
49     }
51     // It is considered an error to attempt to recalculate meshPoints
52     // if they have already been calculated.
53     if (meshPointsPtr_ || localFacesPtr_)
54     {
55         FatalErrorIn
56         (
57             "PrimitivePatch<Face, FaceList, PointField, PointType>::"
58             "calcMeshData()"
59         )   << "meshPointsPtr_ or localFacesPtr_already allocated"
60             << abort(FatalError);
61     }
63     // Create a map for marking points.  Estimated size is 4 times the
64     // number of faces in the patch
65     Map<label> markedPoints(4*this->size());
68     // Important:
69     // ~~~~~~~~~~
70     // In <= 1.5 the meshPoints would be in increasing order but this gives
71     // problems in processor point synchronisation where we have to find out
72     // how the opposite side would have allocated points.
74     ////- 1.5 code:
75     //// if the point is used, set the mark to 1
76     //forAll(*this, facei)
77     //{
78     //    const Face& curPoints = this->operator[](facei);
79     //
80     //    forAll(curPoints, pointi)
81     //    {
82     //        markedPoints.insert(curPoints[pointi], -1);
83     //    }
84     //}
85     //
86     //// Create the storage and store the meshPoints.  Mesh points are
87     //// the ones marked by the usage loop above
88     //meshPointsPtr_ = new labelList(markedPoints.toc());
89     //labelList& pointPatch = *meshPointsPtr_;
90     //
91     //// Sort the list to preserve compatibility with the old ordering
92     //sort(pointPatch);
93     //
94     //// For every point in map give it its label in mesh points
95     //forAll(pointPatch, pointi)
96     //{
97     //    markedPoints.find(pointPatch[pointi])() = pointi;
98     //}
100     //- Unsorted version:
101     DynamicList<label> meshPoints(2*this->size());
102     forAll(*this, facei)
103     {
104         const Face& curPoints = this->operator[](facei);
106         forAll(curPoints, pointi)
107         {
108             if (markedPoints.insert(curPoints[pointi], meshPoints.size()))
109             {
110                 meshPoints.append(curPoints[pointi]);
111             }
112         }
113     }
114     // Transfer to straight list (reuses storage)
115     meshPointsPtr_ = new labelList(meshPoints, true);
118     // Create local faces. Note that we start off from copy of original face
119     // list (even though vertices are overwritten below). This is done so
120     // additional data gets copied (e.g. region number of labelledTri)
121     localFacesPtr_ = new List<Face>(*this);
122     List<Face>& lf = *localFacesPtr_;
124     forAll(*this, facei)
125     {
126         const Face& curFace = this->operator[](facei);
127         lf[facei].setSize(curFace.size());
129         forAll(curFace, labelI)
130         {
131             lf[facei][labelI] = markedPoints.find(curFace[labelI])();
132         }
133     }
135     if (debug)
136     {
137         Pout<< "PrimitivePatch<Face, FaceList, PointField, PointType>::"
138                "calcMeshData() : "
139                "finished calculating mesh data in PrimitivePatch"
140             << endl;
141     }
145 template
147     class Face,
148     template<class> class FaceList,
149     class PointField,
150     class PointType
152 void
153 Foam::PrimitivePatch<Face, FaceList, PointField, PointType>::
154 calcMeshPointMap() const
156     if (debug)
157     {
158         Pout<< "PrimitivePatch<Face, FaceList, PointField, PointType>::"
159                "calcMeshPointMap() : "
160                "calculating mesh point map in PrimitivePatch"
161             << endl;
162     }
164     // It is considered an error to attempt to recalculate meshPoints
165     // if they have already been calculated.
166     if (meshPointMapPtr_)
167     {
168         FatalErrorIn
169         (
170             "PrimitivePatch<Face, FaceList, PointField, PointType>::"
171             "calcMeshPointMap()"
172         )   << "meshPointMapPtr_ already allocated"
173             << abort(FatalError);
174     }
176     const labelList& mp = meshPoints();
178     meshPointMapPtr_ = new Map<label>(2*mp.size());
179     Map<label>& mpMap = *meshPointMapPtr_;
181     forAll(mp, i)
182     {
183         mpMap.insert(mp[i], i);
184     }
186     if (debug)
187     {
188         Pout<< "PrimitivePatch<Face, FaceList, PointField, PointType>::"
189                "calcMeshPointMap() : "
190                "finished calculating mesh point map in PrimitivePatch"
191             << endl;
192     }
196 template
198     class Face,
199     template<class> class FaceList,
200     class PointField,
201     class PointType
203 void
204 Foam::PrimitivePatch<Face, FaceList, PointField, PointType>::
205 calcLocalPoints() const
207     if (debug)
208     {
209         Pout<< "PrimitivePatch<Face, FaceList, PointField, PointType>::"
210                "calcLocalPoints() : "
211                "calculating localPoints in PrimitivePatch"
212             << endl;
213     }
215     // It is considered an error to attempt to recalculate localPoints
216     // if they have already been calculated.
217     if (localPointsPtr_)
218     {
219         FatalErrorIn
220         (
221             "PrimitivePatch<Face, FaceList, PointField, PointType>::"
222             "calcLocalPoints()"
223         )   << "localPointsPtr_already allocated"
224             << abort(FatalError);
225     }
227     const labelList& meshPts = meshPoints();
229     localPointsPtr_ = new Field<PointType>(meshPts.size());
231     Field<PointType>& locPts = *localPointsPtr_;
233     forAll(meshPts, pointi)
234     {
235         locPts[pointi] = points_[meshPts[pointi]];
236     }
238     if (debug)
239     {
240         Pout<< "PrimitivePatch<Face, FaceList, PointField, PointType>::"
241             << "calcLocalPoints() : "
242             << "finished calculating localPoints in PrimitivePatch"
243             << endl;
244     }
248 template
250     class Face,
251     template<class> class FaceList,
252     class PointField,
253     class PointType
255 void
256 Foam::PrimitivePatch<Face, FaceList, PointField, PointType>::
257 calcPointNormals() const
259     if (debug)
260     {
261         Pout<< "PrimitivePatch<Face, FaceList, PointField, PointType>::"
262                "calcPointNormals() : "
263                "calculating pointNormals in PrimitivePatch"
264             << endl;
265     }
267     // It is considered an error to attempt to recalculate pointNormals
268     // if they have already been calculated.
269     if (pointNormalsPtr_)
270     {
271         FatalErrorIn
272         (
273             "PrimitivePatch<Face, FaceList, PointField, PointType>::"
274             "calcPointNormals()"
275         )   << "pointNormalsPtr_already allocated"
276             << abort(FatalError);
277     }
279     const Field<PointType>& faceUnitNormals = faceNormals();
281     const labelListList& pf = pointFaces();
283     pointNormalsPtr_ = new Field<PointType>
284     (
285         meshPoints().size(),
286         PointType::zero
287     );
289     Field<PointType>& n = *pointNormalsPtr_;
291     forAll(pf, pointi)
292     {
293         PointType& curNormal = n[pointi];
295         const labelList& curFaces = pf[pointi];
297         forAll(curFaces, facei)
298         {
299             curNormal += faceUnitNormals[curFaces[facei]];
300         }
302         curNormal /= mag(curNormal) + VSMALL;
303     }
305     if (debug)
306     {
307         Pout<< "PrimitivePatch<Face, FaceList, PointField, PointType>::"
308                "calcPointNormals() : "
309                "finished calculating pointNormals in PrimitivePatch"
310             << endl;
311     }
315 template
317     class Face,
318     template<class> class FaceList,
319     class PointField,
320     class PointType
322 void
323 Foam::PrimitivePatch<Face, FaceList, PointField, PointType>::
324 calcFaceCentres() const
326     if (debug)
327     {
328         Pout<< "PrimitivePatch<Face, FaceList, PointField, PointType>::"
329                "calcFaceCentres() : "
330                "calculating faceCentres in PrimitivePatch"
331             << endl;
332     }
334     // It is considered an error to attempt to recalculate faceCentres
335     // if they have already been calculated.
336     if (faceCentresPtr_)
337     {
338         FatalErrorIn
339         (
340             "PrimitivePatch<Face, FaceList, PointField, PointType>::"
341             "calcFaceCentres()"
342         )   << "faceCentresPtr_already allocated"
343             << abort(FatalError);
344     }
346     faceCentresPtr_ = new Field<PointType>(this->size());
348     Field<PointType>& c = *faceCentresPtr_;
350     forAll(c, facei)
351     {
352         c[facei] = this->operator[](facei).centre(points_);
353     }
355     if (debug)
356     {
357         Pout<< "PrimitivePatch<Face, FaceList, PointField, PointType>::"
358                "calcFaceCentres() : "
359                "finished calculating faceCentres in PrimitivePatch"
360             << endl;
361     }
365 template
367     class Face,
368     template<class> class FaceList,
369     class PointField,
370     class PointType
372 void
373 Foam::PrimitivePatch<Face, FaceList, PointField, PointType>::
374 calcFaceNormals() const
376     if (debug)
377     {
378         Pout<< "PrimitivePatch<Face, FaceList, PointField, PointType>::"
379                "calcFaceNormals() : "
380                "calculating faceNormals in PrimitivePatch"
381             << endl;
382     }
384     // It is considered an error to attempt to recalculate faceNormals
385     // if they have already been calculated.
386     if (faceNormalsPtr_)
387     {
388         FatalErrorIn
389         (
390             "PrimitivePatch<Face, FaceList, PointField, PointType>::"
391             "calcFaceNormals()"
392         )   << "faceNormalsPtr_already allocated"
393             << abort(FatalError);
394     }
396     faceNormalsPtr_ = new Field<PointType>(this->size());
398     Field<PointType>& n = *faceNormalsPtr_;
400     forAll(n, facei)
401     {
402         n[facei] = this->operator[](facei).normal(points_);
403         n[facei] /= mag(n[facei]) + VSMALL;
404     }
406     if (debug)
407     {
408         Pout<< "PrimitivePatch<Face, FaceList, PointField, PointType>::"
409                "calcFaceNormals() : "
410                "finished calculating faceNormals in PrimitivePatch"
411             << endl;
412     }
416 // ************************************************************************* //