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-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
36template<class FaceList, class PointField>
38(
39 const scalar mergeDist,
41 Field
42 <
44 >& mergedPoints,
45 List
46 <
48 >& mergedFaces,
49 labelList& pointMergeMap,
50 const bool useLocal
51)
52{
53 typedef typename PrimitivePatch<FaceList,PointField>::face_type FaceType;
54
55 // Faces from all ranks
56 const globalIndex faceAddr(pp.size(), globalIndex::gatherOnly{});
57
58 // Points from all ranks
59 const globalIndex pointAddr
60 (
61 (useLocal ? pp.localPoints().size() : pp.points().size()),
63 );
64
65 if (useLocal)
66 {
67 faceAddr.gather(pp.localFaces(), mergedFaces);
68 pointAddr.gather(pp.localPoints(), mergedPoints);
69 }
70 else
71 {
72 faceAddr.gather(pp, mergedFaces);
73 pointAddr.gather(pp.points(), mergedPoints);
74 }
75
76 // Relabel faces according to global point offsets
77 for (const label proci : faceAddr.subProcs())
78 {
79 SubList<FaceType> slot(mergedFaces, faceAddr.range(proci));
80
81 for (auto& f : slot)
82 {
83 pointAddr.inplaceToGlobal(proci, f);
84 }
85 }
86
87
88 // Merging points
89 label nPointsChanged(0);
90
91 labelList boundaryPoints;
92
93 if (Pstream::parRun())
94 {
95 const globalIndex localPointAddr
96 (
97 pp.localPoints().size(),
99 );
100
101 const globalIndex bndPointAddr
102 (
103 pp.boundaryPoints().size(),
105 );
106
107 bndPointAddr.gather(pp.boundaryPoints(), boundaryPoints);
108
109 // Relabel according to global point offsets
110 for (const label proci : localPointAddr.subProcs())
111 {
112 SubList<label> slot(boundaryPoints, bndPointAddr.range(proci));
113 localPointAddr.inplaceToGlobal(proci, slot);
114 }
115 }
116
117
119 {
120 labelList pointToUnique;
121
122 nPointsChanged = Foam::inplaceMergePoints
123 (
124 mergedPoints,
125 boundaryPoints, // selection of points to merge
126 mergeDist,
127 false, // verbose = false
128 pointToUnique
129 );
130
131 if (nPointsChanged)
132 {
133 // Renumber faces to use unique point numbers
134 for (auto& f : mergedFaces)
135 {
136 inplaceRenumber(pointToUnique, f);
137 }
138
139 // Store point mapping
140 if (notNull(pointMergeMap))
141 {
142 pointMergeMap.transfer(pointToUnique);
143 }
144 }
145 }
146
147 if (!nPointsChanged && notNull(pointMergeMap))
148 {
149 // Safety
150 pointMergeMap = identity(mergedPoints.size());
151 }
152}
153
154
155template<class FaceList>
157(
158 const polyMesh& mesh,
159 const FaceList& localFaces,
160 const labelList& meshPoints,
161 const Map<label>& meshPointMap,
162
163 labelList& pointToGlobal,
164 labelList& uniqueMeshPointLabels,
165 autoPtr<globalIndex>& globalPointsPtr,
166 autoPtr<globalIndex>& globalFacesPtr,
168 pointField& mergedPoints
169)
170{
171 typedef typename FaceList::value_type FaceType;
172
173 if (Pstream::parRun())
174 {
175 // Renumber the points/faces into unique points
176 globalPointsPtr = mesh.globalData().mergePoints
177 (
178 meshPoints,
179 meshPointMap,
180 pointToGlobal,
181 uniqueMeshPointLabels
182 );
183
184 globalFacesPtr.reset(new globalIndex(localFaces.size()));
185
186 // Renumber faces locally
187 List<FaceType> myFaces(localFaces);
188 for (auto& f : myFaces)
189 {
190 inplaceRenumber(pointToGlobal, f);
191 }
192
193 // Can also use
194 // UIndirectList<point>(mesh.points(), uniqueMeshPointLabels)
195 // but favour communication over local memory use
196 globalPointsPtr().gather
197 (
198 pointField(mesh.points(), uniqueMeshPointLabels),
199 mergedPoints
200 );
201 globalFacesPtr().gather(myFaces, mergedFaces);
202 }
203 else
204 {
205 pointToGlobal = identity(meshPoints.size());
206 uniqueMeshPointLabels = pointToGlobal;
207
208 globalPointsPtr.reset(new globalIndex(meshPoints.size()));
209 globalFacesPtr.reset(new globalIndex(localFaces.size()));
210
211 mergedFaces = localFaces;
212 mergedPoints = pointField(mesh.points(), meshPoints);
213 }
214}
215
216
217// ************************************************************************* //
Generic templated field type.
Definition: Field.H:82
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
void transfer(List< T > &list)
Definition: List.C:447
A HashTable to objects of type <T> with a label key.
Definition: Map.H:60
static void gatherAndMerge(const scalar mergeDist, const PrimitivePatch< FaceList, PointField > &pp, Field< typename PrimitivePatch< FaceList, PointField >::point_type > &mergedPoints, List< typename PrimitivePatch< FaceList, PointField >::face_type > &mergedFaces, labelList &pointMergeMap, const bool useLocal=false)
Gather points and faces onto master and merge into single patch.
A list of faces which address into the list of points.
const Field< point_type > & localPoints() const
Return pointField of points in patch.
std::remove_reference< PointField >::type::value_type point_type
The point type.
const Field< point_type > & points() const noexcept
Return reference to global points.
const labelList & boundaryPoints() const
Return list of boundary points, address into LOCAL point list.
std::remove_reference< FaceList >::type::value_type face_type
The face type.
const List< face_type > & localFaces() const
Return patch faces addressing into local point list.
A List obtained as a section of another List.
Definition: SubList.H:70
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
void reset(autoPtr< T > &&other) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:117
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:68
labelRange range() const
Return start/size range of local processor data.
Definition: globalIndexI.H:232
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 UPstream::commsTypes=UPstream::commsTypes::nonBlocking)
Collect data in processor order on master (== procIDs[0]).
void inplaceToGlobal(labelUList &labels) const
From local to global index (inplace)
Definition: globalIndexI.H:303
labelRange subProcs() const noexcept
Range of process indices for addressed sub-offsets (processes)
Definition: globalIndexI.H:159
autoPtr< globalIndex > mergePoints(labelList &pointToGlobal, labelList &uniquePoints) const
Helper for merging (collocated!) mesh point data.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
const globalMeshData & globalData() const
Return parallel info.
Definition: polyMesh.C:1310
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1083
splitCell * master() const
Definition: splitCell.H:113
dynamicFvMesh & mesh
Geometric merging of points. See below.
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i)
Definition: labelList.C:38
void inplaceRenumber(const labelUList &oldToNew, IntListType &input)
Inplace renumber the values (not the indices) of a list.
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
bool notNull(const T *ptr)
True if ptr is not a pointer (of type T) to the nullObject.
Definition: nullObject.H:207
label inplaceMergePoints(PointList &points, const scalar mergeTol, const bool verbose, labelList &pointToUnique)
labelList f(nPoints)