fvMeshDistribute.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::fvMeshDistribute
26 
27 Description
28  Sends/receives parts of mesh+fvfields to neighbouring processors.
29  Used in load balancing.
30 
31  Input is per local cell the processor it should move to. Moves meshes
32  and volFields/surfaceFields and returns map which can be used to
33  distribute other.
34 
35  Notes:
36  - does not handle cyclics. Will probably handle separated proc patches.
37  - if all cells move off processor also all its processor patches will
38  get deleted so comms might be screwed up (since e.g. globalMeshData
39  expects procPatches on all)
40  - initial mesh has to have procPatches last and all normal patches common
41  to all processors and in the same order. This is checked.
42  - faces are matched topologically but points on the faces are not. So
43  expect problems -on separated patches (cyclics?) -on zero sized processor
44  edges.
45 
46 SourceFiles
47  fvMeshDistribute.C
48  fvMeshDistributeTemplates.C
49 
50 \*---------------------------------------------------------------------------*/
51 
52 #ifndef fvMeshDistribute_H
53 #define fvMeshDistribute_H
54 
55 #include "fvMeshSubset.H"
56 #include "FieldField.H"
57 
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59 
60 namespace Foam
61 {
62 
63 // Forward declaration of classes
64 class mapAddedPolyMesh;
65 class polyDistributionMap;
66 
67 /*---------------------------------------------------------------------------*\
68  Class fvMeshDistribute Declaration
69 \*---------------------------------------------------------------------------*/
70 
71 class fvMeshDistribute
72 {
73  // Private Data
74 
75  //- Underlying fvMesh
76  fvMesh& mesh_;
77 
78 
79  // Private Member Functions
80 
81  static void inplaceRenumberWithFlip
82  (
83  const labelUList& oldToNew,
84  const bool oldToNewHasFlip,
85  const bool lstHasFlip,
86  labelUList& lst
87  );
88 
89  //- Find indices with value
90  static labelList select
91  (
92  const bool selectEqual,
93  const labelList& values,
94  const label value
95  );
96 
97  wordList fieldNames(const word& typeName, label& nFields) const;
98 
99  //- Check all procs have same names and in exactly same order
100  // and return the number of fields
101  static label checkEqualWordList(const word& typeName, const wordList&);
102 
103  //- Merge wordlists over all processors
104  static wordList mergeWordList(const wordList&);
105 
106 
107  // Patch handling
108 
109  //- Find internal patch to put exposed internal faces into
110  // Used when fields are present and require mapping
111  label findInternalPatch() const;
112 
113  //- Find non-empty patch to put exposed internal faces into
114  // Used when no fields require mapping
115  label findNonEmptyPatch() const;
116 
117  //- Save boundary fields
118  template<class Type, class Mesh>
119  void saveBoundaryFields
120  (
122  ) const;
123 
124  //- Map boundary fields
125  template<class Type, class Mesh>
126  void mapBoundaryFields
127  (
128  const polyTopoChangeMap& map,
130  );
131 
132  //- Set value of exposed patch faces
133  template<class Type>
134  void initMapExposedFaces(PtrList<Field<Type>>& iflds) const;
135 
136  //- Set value of exposed patch faces
137  template<class Type>
138  void mapExposedFaces
139  (
140  const polyTopoChangeMap& map,
141  const PtrList<Field<Type>>& oldFlds
142  );
143 
144  //- Correct coupled patch fields
145  template<class GeoField>
146  void correctCoupledPatchFields();
147 
148  //- Delete all processor patches. Move any processor faces into
149  // destinationPatch
150  autoPtr<polyTopoChangeMap> deleteProcPatches
151  (
152  const label destinationPatch
153  );
154 
155  //- Repatch the mesh. This is only necessary for the proc
156  // boundary faces. newPatchID is over all boundary faces: -1 or
157  // new patchID. constructFaceMap is being adapted for the
158  // possible new face position (since proc faces get automatically
159  // matched)
161  (
162  const labelList& newPatchID,
163  labelListList& constructFaceMap
164  );
165 
166  //- Merge any local points that were remotely coupled.
167  // constructPointMap is adapted for the new point labels.
168  //
169  // Detect shared points. Need processor patches to be present.
170  // Background: when adding bits of mesh one can get points which
171  // share the same position but are only detectable to be
172  // topologically the same point when doing parallel analysis. This
173  // routine will merge those points.
174  autoPtr<polyTopoChangeMap> mergeSharedPoints
175  (
176  const labelList& pointToGlobalMaster,
177  labelListList& constructPointMap
178  );
179 
180 
181  // Coupling information
182 
183  //- Construct the local environment of all boundary faces.
184  void getCouplingData
185  (
186  const labelList& distribution,
187  labelList& sourceFace,
188  labelList& sourceProc,
189  labelList& sourcePatch,
190  labelList& sourceNbrPatch,
191  labelList& sourceNewProc,
192  labelList& sourcePointMaster
193  ) const;
194 
195  // Subset the neighbourCell/neighbourProc fields
196  static void subsetCouplingData
197  (
198  const fvMesh& mesh,
199  const labelList& pointMap,
200  const labelList& faceMap,
201  const labelList& cellMap,
202 
203  const labelList& oldDistribution,
204  const labelList& oldFaceOwner,
205  const labelList& oldFaceNeighbour,
206  const label oldInternalFaces,
207 
208  const labelList& sourceFace,
209  const labelList& sourceProc,
210  const labelList& sourcePatch,
211  const labelList& sourceNbrPatch,
212  const labelList& sourceNewProc,
213  const labelList& sourcePointMaster,
214 
215  labelList& subFace,
216  labelList& subProc,
217  labelList& subPatch,
218  labelList& subNbrPatch,
219  labelList& subNewProc,
220  labelList& subPointMaster
221  );
222 
223  //- Find cells on mesh whose faceID/procID match the neighbour
224  // cell/proc of domainMesh. Store the matching face.
225  static void findCouples
226  (
227  const primitiveMesh&,
228  const labelList& sourceFace,
229  const labelList& sourceProc,
230  const labelList& sourcePatch,
231 
232  const label domain,
233  const primitiveMesh& domainMesh,
234  const labelList& domainFace,
235  const labelList& domainProc,
236  const labelList& domainPatch,
237 
238  labelList& masterCoupledFaces,
239  labelList& slaveCoupledFaces
240  );
241 
242  //- Map data on boundary faces to new mesh (resulting from adding
243  // two meshes)
244  static labelList mapBoundaryData
245  (
246  const primitiveMesh& mesh, // mesh after adding
247  const mapAddedPolyMesh& map,
248  const labelList& boundaryData0, // mesh before adding
249  const label nInternalFaces1,
250  const labelList& boundaryData1 // added mesh
251  );
252 
253  //- Map point data to new mesh (resulting from adding two meshes)
254  static labelList mapPointData
255  (
256  const primitiveMesh& mesh, // mesh after adding
257  const mapAddedPolyMesh& map,
258  const labelList& boundaryData0, // on mesh before adding
259  const labelList& boundaryData1 // on added mesh
260  );
261 
262  // Other
263 
264  //- Remove cells. Add all exposed faces to patch oldInternalPatchi
265  autoPtr<polyTopoChangeMap> doRemoveCells
266  (
267  const labelList& cellsToRemove,
268  const label oldInternalPatchi
269  );
270 
271  //- Add processor patches. Changes mesh and returns per neighbour
272  // proc the processor patchID.
273  void addProcPatches
274  (
275  const labelList& nbrProc, // Processor that neighbour
276  // is now on
277  const labelList& referPatchID, // Original patch ID (or -1)
278  const labelList& referNbrPatchID, // Original neighbour patch
279  // ID (or -1)
280  List<Map<label>>& procPatchID
281  );
282 
283  //- Add non-conformal processor cyclic patches. Changes the mesh.
284  void addNccProcPatches();
285 
286  //- Get boundary faces to be repatched. Is -1 or new patchID
287  static labelList getBoundaryPatch
288  (
289  const labelList& neighbourNewProc, // new processor per b. face
290  const labelList& referPatchID, // -1 or original patch
291  const List<Map<label>>& procPatchID // patchID
292  );
293 
294  //- Send mesh and coupling data.
295  static void sendMesh
296  (
297  const label domain,
298  const fvMesh& mesh,
299  const wordList& pointZoneNames,
300  const wordList& facesZoneNames,
301  const wordList& cellZoneNames,
302  const labelList& sourceFace,
303  const labelList& sourceProc,
304  const labelList& sourcePatch,
305  const labelList& sourceNbrPatch,
306  const labelList& sourceNewProc,
307  const labelList& sourcePointMaster,
308  Ostream& toDomain
309  );
310 
311  //- Send subset of fields
312  template<class GeoField>
313  static void sendFields
314  (
315  const label domain,
316  const wordList& fieldNames,
317  const fvMeshSubset&,
318  Ostream& toNbr
319  );
320 
321  //- Receive mesh. Opposite of sendMesh
322  static autoPtr<fvMesh> receiveMesh
323  (
324  const label domain,
325  const wordList& pointZoneNames,
326  const wordList& facesZoneNames,
327  const wordList& cellZoneNames,
328  const Time& runTime,
329  labelList& domainSourceFace,
330  labelList& domainSourceProc,
331  labelList& domainSourcePatch,
332  labelList& domainSourceNbrPatch,
333  labelList& domainSourceNewProc,
334  labelList& domainSourcePointMaster,
335  Istream& fromNbr
336  );
337 
338  //- Receive fields. Opposite of sendFields
339  template<class GeoField>
340  static void receiveFields
341  (
342  const label domain,
343  const wordList& fieldNames,
344  typename GeoField::Mesh&,
346  const dictionary& fieldDicts
347  );
348 
349 
350 public:
351 
352  ClassName("fvMeshDistribute");
353 
354 
355  // Constructors
356 
357  //- Construct from mesh
358  fvMeshDistribute(fvMesh& mesh);
359 
360  //- Disallow default bitwise copy construction
361  fvMeshDistribute(const fvMeshDistribute&) = delete;
362 
363 
364  // Member Functions
365 
366  //- Helper function: count cells per processor in wanted distribution
367  static labelList countCells(const labelList&);
368 
369  //- Send cells to neighbours according to distribution
370  // (for every cell the new proc)
372 
373  // Debugging
374 
375  //- Print some info on coupling data
376  static void printCoupleInfo
377  (
378  const primitiveMesh&,
379  const labelList&,
380  const labelList&,
381  const labelList&,
382  const labelList&
383  );
384 
385  //- Print some field info
386  template<class GeoField>
387  static void printFieldInfo(const fvMesh&);
388 
389  //- Print some info on mesh.
390  static void printMeshInfo(const fvMesh&);
391 
392 
393  // Member Operators
394 
395  //- Disallow default bitwise assignment
396  void operator=(const fvMeshDistribute&) = delete;
397 };
398 
399 
400 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
401 
402 } // End namespace Foam
403 
404 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
405 
406 #ifdef NoRepository
407  #include "fvMeshDistributeTemplates.C"
408 #endif
409 
410 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
411 
412 #endif
413 
414 // ************************************************************************* //
Generic field type.
Definition: FieldField.H:77
Pre-declare SubField and related Field type.
Definition: Field.H:83
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
Base class for statistical distributions.
Definition: distribution.H:76
Sends/receives parts of mesh+fvfields to neighbouring processors. Used in load balancing.
ClassName("fvMeshDistribute")
static void printCoupleInfo(const primitiveMesh &, const labelList &, const labelList &, const labelList &, const labelList &)
Print some info on coupling data.
fvMeshDistribute(fvMesh &mesh)
Construct from mesh.
void operator=(const fvMeshDistribute &)=delete
Disallow default bitwise assignment.
autoPtr< polyDistributionMap > distribute(const labelList &dist)
Send cells to neighbours according to distribution.
static void printMeshInfo(const fvMesh &)
Print some info on mesh.
static labelList countCells(const labelList &)
Helper function: count cells per processor in wanted distribution.
static void printFieldInfo(const fvMesh &)
Print some field info.
Post-processing mesh subset tool. Given the original mesh and the list of selected cells,...
Definition: fvMeshSubset.H:74
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:99
Class containing mesh-to-mesh mapping information after a mesh addition where we add a mesh ('added m...
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Cell-face mesh analysis engine.
Definition: primitiveMesh.H:75
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)