PatchToolsGatherAndMerge.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2012-2017 OpenFOAM Foundation
9  Copyright (C) 2019-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "PatchTools.H"
30 #include "polyMesh.H"
31 #include "globalMeshData.H"
32 #include "mergePoints.H"
33 
34 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
35 
36 template<class FaceList, class PointField>
38 (
39  const scalar mergeDist,
41  Field
42  <
44  >& mergedPoints,
45  List
46  <
48  >& mergedFaces,
49  labelList& pointMergeMap
50 )
51 {
52  typedef typename PrimitivePatch<FaceList,PointField>::face_type FaceType;
53  typedef typename PrimitivePatch<FaceList,PointField>::point_type PointType;
54 
55  // Collect points from all processors
56  labelList pointSizes;
57  {
58  const globalIndex gi(p.points().size());
59 
60  gi.gather(p.points(), mergedPoints);
61 
62  pointSizes = gi.sizes();
63  }
64 
65  // Collect faces from all processors and renumber using sizes of
66  // gathered points
67  {
68  List<List<FaceType>> gatheredFaces(Pstream::nProcs());
69  gatheredFaces[Pstream::myProcNo()] = p;
70  Pstream::gatherList(gatheredFaces);
71 
72  if (Pstream::master())
73  {
74  mergedFaces = static_cast<const List<FaceType>&>
75  (
76  ListListOps::combineOffset<List<FaceType>>
77  (
78  gatheredFaces,
79  pointSizes,
82  )
83  );
84  }
85  }
86 
87  if (Pstream::master())
88  {
89  Field<PointType> newPoints;
90  labelList oldToNew;
91 
92  bool hasMerged = mergePoints
93  (
94  mergedPoints,
95  mergeDist,
96  false, // verbosity
97  oldToNew,
98  newPoints
99  );
100 
101  if (hasMerged)
102  {
103  // Store point mapping
104  pointMergeMap.transfer(oldToNew);
105 
106  // Copy points
107  mergedPoints.transfer(newPoints);
108 
109  // Relabel faces
110  for (auto& f : mergedFaces)
111  {
112  inplaceRenumber(pointMergeMap, f);
113  }
114  }
115  }
116 }
117 
118 
119 template<class FaceList>
121 (
122  const polyMesh& mesh,
123  const FaceList& localFaces,
124  const labelList& meshPoints,
125  const Map<label>& meshPointMap,
126 
127  labelList& pointToGlobal,
128  labelList& uniqueMeshPointLabels,
129  autoPtr<globalIndex>& globalPointsPtr,
130  autoPtr<globalIndex>& globalFacesPtr,
132  pointField& mergedPoints
133 )
134 {
135  typedef typename FaceList::value_type FaceType;
136 
137  if (Pstream::parRun())
138  {
139  // Renumber the setPatch points/faces into unique points
140  globalPointsPtr = mesh.globalData().mergePoints
141  (
142  meshPoints,
143  meshPointMap,
144  pointToGlobal,
145  uniqueMeshPointLabels
146  );
147 
148  globalFacesPtr.reset(new globalIndex(localFaces.size()));
149 
150  if (Pstream::master())
151  {
152  // Get renumbered local data
153  pointField myPoints(mesh.points(), uniqueMeshPointLabels);
154  List<FaceType> myFaces(localFaces);
155  for (auto& f : myFaces)
156  {
157  inplaceRenumber(pointToGlobal, f);
158  }
159 
160 
161  mergedFaces.setSize(globalFacesPtr().size());
162  mergedPoints.setSize(globalPointsPtr().size());
163 
164  // Insert master data first
165  label pOffset = globalPointsPtr().offset(Pstream::masterNo());
166  SubList<point>(mergedPoints, myPoints.size(), pOffset) = myPoints;
167 
168  label fOffset = globalFacesPtr().offset(Pstream::masterNo());
169  SubList<FaceType>(mergedFaces, myFaces.size(), fOffset) = myFaces;
170 
171 
172  // Receive slave ones
173  for (const int slave : Pstream::subProcs())
174  {
175  IPstream fromSlave(Pstream::commsTypes::scheduled, slave);
176 
177  pointField slavePoints(fromSlave);
178  List<FaceType> slaveFaces(fromSlave);
179 
180  label pOffset = globalPointsPtr().offset(slave);
181  SubList<point>(mergedPoints, slavePoints.size(), pOffset) =
182  slavePoints;
183 
184  label fOffset = globalFacesPtr().offset(slave);
185  SubList<FaceType>(mergedFaces, slaveFaces.size(), fOffset) =
186  slaveFaces;
187  }
188  }
189  else
190  {
191  // Get renumbered local data
192  pointField myPoints(mesh.points(), uniqueMeshPointLabels);
193  List<FaceType> myFaces(localFaces);
194  for (auto& f : myFaces)
195  {
196  inplaceRenumber(pointToGlobal, f);
197  }
198 
199  // Construct processor stream with estimate of size. Could
200  // be improved.
201  OPstream toMaster
202  (
203  Pstream::commsTypes::scheduled,
204  Pstream::masterNo(),
205  myPoints.byteSize() + 4*sizeof(label)*myFaces.size()
206  );
207  toMaster << myPoints << myFaces;
208  }
209  }
210  else
211  {
212  pointToGlobal = identity(meshPoints.size());
213  uniqueMeshPointLabels = pointToGlobal;
214 
215  globalPointsPtr.reset(new globalIndex(meshPoints.size()));
216  globalFacesPtr.reset(new globalIndex(localFaces.size()));
217 
218  mergedFaces = localFaces;
219  mergedPoints = pointField(mesh.points(), meshPoints);
220  }
221 }
222 
223 
224 // ************************************************************************* //
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::globalIndex::gather
static void gather(const labelUList &offsets, const label comm, const ProcIDsContainer &procIDs, const UList< Type > &fld, List< Type > &allFld, const int tag=UPstream::msgType(), const Pstream::commsTypes=Pstream::commsTypes::nonBlocking)
Collect data in processor order on master (== procIDs[0]).
Definition: globalIndexTemplates.C:75
Foam::accessOp
Definition: UList.H:668
PatchTools.H
Foam::OPstream
Output inter-processor communications stream.
Definition: OPstream.H:53
globalMeshData.H
Foam::PatchTools::gatherAndMerge
static void gatherAndMerge(const scalar mergeDist, const PrimitivePatch< FaceList, PointField > &p, Field< typename PrimitivePatch< FaceList, PointField >::point_type > &mergedPoints, List< typename PrimitivePatch< FaceList, PointField >::face_type > &mergedFaces, labelList &pointMergeMap)
Gather points and faces onto master and merge into single patch.
Definition: PatchToolsGatherAndMerge.C:38
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:54
Foam::Map< label >
polyMesh.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::List::setSize
void setSize(const label n)
Alias for resize()
Definition: List.H:222
Foam::PrimitivePatch::point_type
std::remove_reference< PointField >::type::value_type point_type
The point type.
Definition: PrimitivePatch.H:94
Foam::List::transfer
void transfer(List< T > &list)
Definition: List.C:456
Foam::offsetOp
Offset operator for ListListOps::combineOffset()
Definition: ListListOps.H:101
Foam::ListListOps::inplaceRenumber
void inplaceRenumber(const labelUList &oldToNew, IntListType &lists)
Inplace renumber the values (not the indices) of a list of lists.
Definition: ensightOutput.H:89
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::globalIndex
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:68
Foam::mergePoints
label mergePoints(const PointList &points, const scalar mergeTol, const bool verbose, labelList &pointMap, typename PointList::const_reference origin=PointList::value_type::zero)
Sorts and merges points. All points closer than/equal mergeTol get merged.
Foam::autoPtr::reset
void reset(autoPtr< T > &&other) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:117
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
f
labelList f(nPoints)
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::identity
labelList identity(const label len, label start=0)
Create identity map of the given length with (map[i] == i)
Definition: labelList.C:38
mergePoints.H
Merge points. See below.
Foam::PrimitivePatch::face_type
std::remove_reference< FaceList >::type::value_type face_type
The face type.
Definition: PrimitivePatch.H:90
Foam::IPstream
Input inter-processor communications stream.
Definition: IPstream.H:53
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatch.H:79