Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / mesh / cfMesh / utilities / meshes / polyMeshGenModifier / polyMeshGenModifierAddBufferCells.C
blob26e9322da1c01b61546e1ce3d0a1719468d1c929
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | cfMesh: A library for mesh generation
4    \\    /   O peration     |
5     \\  /    A nd           | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6      \\/     M anipulation  | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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/>.
24 Description
26 \*---------------------------------------------------------------------------*/
28 #include "polyMeshGenModifier.H"
29 #include "polyMeshGenAddressing.H"
30 #include "demandDrivenData.H"
31 #include "labelledPoint.H"
32 #include "helperFunctions.H"
34 // #define DEBUGSearch
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 namespace Foam
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 void polyMeshGenModifier::addBufferCells()
45     if( !Pstream::parRun() )
46         return;
48     Info << "Adding buffer layers" << endl;
50     const labelList& owner = mesh_.owner();
51     pointFieldPMG& points = mesh_.points();
52     faceListPMG& faces = facesAccess();
53     const cellListPMG& cells = mesh_.cells();
54     const PtrList<processorBoundaryPatch>& procBoundaries = mesh_.procBoundaries();
55     const polyMeshGenAddressing& addressing = mesh_.addressingData();
56     const labelLongList& globalPointLabel = addressing.globalPointLabel();
57     const Map<label>& globalToLocal = addressing.globalToLocalPointAddressing();
59     //- receive vertices
60     forAll(procBoundaries, patchI)
61     {
62         labelHashSet pointsToSend;
64         label faceI = procBoundaries[patchI].patchStart();
65         const label end = faceI + procBoundaries[patchI].patchSize();
66         for(;faceI<end;++faceI)
67         {
68             const cell& c = cells[owner[faceI]];
69             forAll(c, fI)
70             {
71                 const face& f = faces[c[fI]];
73                 forAll(f, pI)
74                     pointsToSend.insert(f[pI]);
75             }
76         }
78         faceI = 0;
79         List<labelledPoint> ptsToSend(pointsToSend.size());
80         forAllConstIter(labelHashSet, pointsToSend, it)
81             ptsToSend[faceI++] =
82             labelledPoint
83             (
84                 globalPointLabel[it.key()],
85                 points[it.key()]
86             );
88         OPstream toOtherProc
89         (
90             Pstream::blocking,
91             procBoundaries[patchI].neiProcNo()
92         );
94         toOtherProc << ptsToSend;
95     }
97     Map<label> globalToLocalReceived;
98     forAll(procBoundaries, patchI)
99     {
100         List<labelledPoint> receivedPoints;
101         IPstream fromOtherProc
102         (
103             IPstream::blocking,
104             procBoundaries[patchI].neiProcNo()
105         );
107         fromOtherProc >> receivedPoints;
109         forAll(receivedPoints, i)
110         {
111             if( globalToLocal.found(receivedPoints[i].pointLabel()) )
112                 continue;
113             if( globalToLocalReceived.found(receivedPoints[i].pointLabel()) )
114                 continue;
116             globalToLocalReceived.insert
117             (
118                 receivedPoints[i].pointLabel(),
119                 points.size()
120             );
121             points.append(receivedPoints[i].coordinates());
122         }
123     }
125     //- send cells to other processors
126     forAll(procBoundaries, patchI)
127     {
128         labelHashSet cellsToSend;
130         label faceI = procBoundaries[patchI].patchStart();
131         const label end = faceI + procBoundaries[patchI].patchSize();
132         for(;faceI<end;++faceI)
133             cellsToSend.insert(owner[faceI]);
135         labelLongList flattenedCells;
136         forAllConstIter(labelHashSet, cellsToSend, it)
137         {
138             const cell& c = cells[it.key()];
140             //- the number of faces in the cell
141             flattenedCells.append(c.size());
143             //- add faces
144             forAll(c, fI)
145             {
146                 const face& f = faces[c[fI]];
148                 //- the number of vertices in the face
149                 flattenedCells.append(f.size());
150                 forAll(f, pI)
151                     flattenedCells.append(globalPointLabel[f[pI]]);
152             }
153         }
155         OPstream toOtherProc
156         (
157             Pstream::blocking,
158             procBoundaries[patchI].neiProcNo()
159         );
161         toOtherProc << flattenedCells;
162     }
164     forAll(procBoundaries, patchI)
165     {
166         word subsetName = "processor_";
167         subsetName += help::scalarToText(procBoundaries[patchI].neiProcNo());
168         const label subsetID = mesh_.addCellSubset(subsetName);
170         labelList receivedCells;
172         IPstream fromOtherProc
173         (
174             Pstream::blocking,
175             procBoundaries[patchI].neiProcNo()
176         );
178         fromOtherProc >> receivedCells;
180         label counter(0);
181         while( counter < receivedCells.size() )
182         {
183             faceList c(receivedCells[counter++]);
184             forAll(c, fI)
185             {
186                 c[fI].setSize(receivedCells[counter++]);
187                 forAll(c[fI], pI)
188                 {
189                     const label gpI = receivedCells[counter++];
191                     if( globalToLocal.found(gpI) )
192                     {
193                         c[fI][pI] = globalToLocal[gpI];
194                     }
195                     else
196                     {
197                         c[fI][pI] = globalToLocalReceived[gpI];
198                     }
199                 }
200             }
202             mesh_.addCellToSubset(subsetID, cells.size());
203             addCell(c);
204         }
205     }
207     mesh_.clearOut();
209     Info << "Finished adding buffer layers" << endl;
212 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
214 } // End namespace Foam
216 // ************************************************************************* //