fvMeshDistribute.H
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) 2011-2018 OpenFOAM Foundation
9  Copyright (C) 2016-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 Class
28  Foam::fvMeshDistribute
29 
30 Description
31  Sends/receives parts of mesh+fvfields to neighbouring processors.
32  Used in load balancing.
33 
34  Input is per local cell the processor it should move to. Moves meshes
35  and volFields/surfaceFields and returns map which can be used to
36  distribute other.
37 
38  Notes:
39  - does not handle cyclics. Will probably handle separated proc patches.
40  - if all cells move off processor also all its processor patches will
41  get deleted so comms might be screwed up (since e.g. globalMeshData
42  expects procPatches on all)
43  - initial mesh has to have procPatches last and all normal patches common
44  to all processors and in the same order. This is checked.
45  - faces are matched topologically but points on the faces are not. So
46  expect problems -on separated patches (cyclics?) -on zero sized processor
47  edges.
48 
49 SourceFiles
50  fvMeshDistribute.C
51  fvMeshDistributeTemplates.C
52 
53 \*---------------------------------------------------------------------------*/
54 
55 #ifndef fvMeshDistribute_H
56 #define fvMeshDistribute_H
57 
58 #include "Field.H"
59 #include "fvMeshSubset.H"
60 
61 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
62 
63 namespace Foam
64 {
65 
66 // Forward Declarations
67 class mapAddedPolyMesh;
68 class mapDistributePolyMesh;
69 
70 /*---------------------------------------------------------------------------*\
71  Class fvMeshDistribute Declaration
72 \*---------------------------------------------------------------------------*/
73 
74 class fvMeshDistribute
75 {
76  // Private data
77 
78  //- Underlying fvMesh
79  fvMesh& mesh_;
80 
81  //- Absolute merging tolerance (constructing meshes gets done using
82  // geometric matching)
83  const scalar mergeTol_;
84 
85 
86  // Private Member Functions
87 
88  static void inplaceRenumberWithFlip
89  (
90  const labelUList& oldToNew,
91  const bool oldToNewHasFlip,
92  const bool lstHasFlip,
93  labelUList& lst
94  );
95 
96  //- Find indices with value
97  static labelList select
98  (
99  const bool selectEqual,
100  const labelList& values,
101  const label value
102  );
103 
104  //- Merge wordlists over all processors
105  static wordList mergeWordList(const wordList&);
106 
107 
108  // Patch handling
109 
110  //- Find patch to put exposed faces into.
111  label findNonEmptyPatch() const;
112 
113  //- Save boundary fields
114  template<class T, class Mesh>
115  void saveBoundaryFields
116  (
118  ) const;
119 
120  //- Map boundary fields
121  template<class T, class Mesh>
122  void mapBoundaryFields
123  (
124  const mapPolyMesh& map,
125  const PtrList<FieldField<fvsPatchField, T>>& oldBflds
126  );
127 
128  //- Save internal fields of surfaceFields
129  template<class T>
130  void saveInternalFields(PtrList<Field<T>>& iflds) const;
131 
132  //- Set value of patch faces resulting from internal faces
133  template<class T>
134  void mapExposedFaces
135  (
136  const mapPolyMesh& map,
137  const PtrList<Field<T>>& oldFlds
138  );
139 
140  //- Init patch fields of certain type
141  template<class GeoField, class PatchFieldType>
142  void initPatchFields
143  (
144  const typename GeoField::value_type& initVal
145  );
146 
147  //- Call correctBoundaryConditions on fields
148  template<class GeoField>
149  void correctBoundaryConditions();
150 
151  //- Delete all processor patches. Move any processor faces into
152  // patchi.
153  autoPtr<mapPolyMesh> deleteProcPatches(const label patchi);
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)
160  autoPtr<mapPolyMesh> repatch
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  autoPtr<mapPolyMesh> mergeSharedPoints
169  (
170  const labelList& pointToGlobalMaster,
171  labelListList& constructPointMap
172  );
173 
174 
175  // Coupling information
176 
177  //- Construct the local environment of all boundary faces.
178  void getCouplingData
179  (
180  const labelList& distribution,
181  labelList& sourceFace,
182  labelList& sourceProc,
183  labelList& sourcePatch,
184  labelList& sourceNewProc,
185  labelList& sourcePointMaster
186  ) const;
187 
188  // Subset the neighbourCell/neighbourProc fields
189  static void subsetCouplingData
190  (
191  const fvMesh& mesh,
192  const labelList& pointMap,
193  const labelList& faceMap,
194  const labelList& cellMap,
195 
196  const labelList& oldDistribution,
197  const labelList& oldFaceOwner,
198  const labelList& oldFaceNeighbour,
199  const label oldInternalFaces,
200 
201  const labelList& sourceFace,
202  const labelList& sourceProc,
203  const labelList& sourcePatch,
204  const labelList& sourceNewProc,
205  const labelList& sourcePointMaster,
206 
207  labelList& subFace,
208  labelList& subProc,
209  labelList& subPatch,
210  labelList& subNewProc,
211  labelList& subPointMaster
212  );
213 
214  //- Find cells on mesh whose faceID/procID match the neighbour
215  // cell/proc of domainMesh. Store the matching face.
216  static void findCouples
217  (
218  const primitiveMesh&,
219  const labelList& sourceFace,
220  const labelList& sourceProc,
221  const labelList& sourcePatch,
222 
223  const label domain,
224  const primitiveMesh& domainMesh,
225  const labelList& domainFace,
226  const labelList& domainProc,
227  const labelList& domainPatch,
228 
229  labelList& masterCoupledFaces,
230  labelList& slaveCoupledFaces
231  );
232 
233  //- Map data on boundary faces to new mesh (resulting from adding
234  // two meshes)
235  static labelList mapBoundaryData
236  (
237  const primitiveMesh& mesh, // mesh after adding
238  const mapAddedPolyMesh& map,
239  const labelList& boundaryData0, // mesh before adding
240  const label nInternalFaces1,
241  const labelList& boundaryData1 // added mesh
242  );
243 
244  //- Map point data to new mesh (resulting from adding two meshes)
245  static labelList mapPointData
246  (
247  const primitiveMesh& mesh, // mesh after adding
248  const mapAddedPolyMesh& map,
249  const labelList& boundaryData0, // on mesh before adding
250  const labelList& boundaryData1 // on added mesh
251  );
252 
253  // Other
254 
255  //- Remove cells. Add all exposed faces to patch oldInternalPatchi
256  autoPtr<mapPolyMesh> doRemoveCells
257  (
258  const labelList& cellsToRemove,
259  const label oldInternalPatchi
260  );
261 
262  //- Add processor patches. Changes mesh and returns per neighbour
263  // proc the processor patchID.
264  void addProcPatches
265  (
266  const labelList&, // processor that neighbour is now on
267  const labelList&, // -1 or patch that face originated from
268  List<Map<label>>& procPatchID
269  );
270 
271  //- Get boundary faces to be repatched. Is -1 or new patchID
272  static labelList getBoundaryPatch
273  (
274  const labelList& neighbourNewProc, // new processor per b. face
275  const labelList& referPatchID, // -1 or original patch
276  const List<Map<label>>& procPatchID // patchID
277  );
278 
279  //- Send mesh and coupling data.
280  static void sendMesh
281  (
282  const label domain,
283  const fvMesh& mesh,
284  const wordList& pointZoneNames,
285  const wordList& facesZoneNames,
286  const wordList& cellZoneNames,
287  const labelList& sourceFace,
288  const labelList& sourceProc,
289  const labelList& sourcePatch,
290  const labelList& sourceNewProc,
291  const labelList& sourcePointMaster,
292  Ostream& toDomain
293  );
294 
295  //- Get sorted names of GeoField, optionally test
296  //- that all procs have the same names.
297  template<class GeoField>
298  static void getFieldNames
299  (
300  const fvMesh& mesh,
301  HashTable<wordList>& allFieldNames,
302  const bool syncPar = true
303  );
304 
305  //- Send subset of fields
306  template<class GeoField>
307  static void sendFields
308  (
309  const label domain,
310  const HashTable<wordList>& allFieldNames,
311  const fvMeshSubset& subsetter,
312  Ostream&
313  );
314 
315  //- Receive mesh. Opposite of sendMesh
316  static autoPtr<fvMesh> receiveMesh
317  (
318  const label domain,
319  const wordList& pointZoneNames,
320  const wordList& facesZoneNames,
321  const wordList& cellZoneNames,
322  const Time& runTime,
323  labelList& domainSourceFace,
324  labelList& domainSourceProc,
325  labelList& domainSourcePatch,
326  labelList& domainSourceNewProc,
327  labelList& domainSourcePointMaster,
328  Istream& fromNbr
329  );
330 
331  //- Receive fields. Opposite of sendFields
332  template<class GeoField>
333  static void receiveFields
334  (
335  const label domain,
336  const HashTable<wordList>& allFieldNames,
337  fvMesh& mesh,
339  const dictionary& allFieldsDict
340  );
341 
342  //- No copy construct
343  fvMeshDistribute(const fvMeshDistribute&) = delete;
344 
345  //- No copy assignment
346  void operator=(const fvMeshDistribute&) = delete;
347 
348 public:
349 
350  ClassName("fvMeshDistribute");
351 
352 
353  // Constructors
354 
355  //- Construct from mesh and absolute merge tolerance
356  fvMeshDistribute(fvMesh& mesh, const scalar mergeTol);
357 
358 
359  // Member Functions
360 
361  //- Helper function: count cells per processor in wanted distribution
362  static labelList countCells(const labelList&);
363 
364  //- Send cells to neighbours according to distribution
365  // (for every cell the new proc)
367 
368  // Debugging
369 
370  //- Print some info on coupling data
371  static void printCoupleInfo
372  (
373  const primitiveMesh&,
374  const labelList&,
375  const labelList&,
376  const labelList&,
377  const labelList&
378  );
379 
380  //- Print some field info
381  template<class GeoField>
382  static void printFieldInfo(const fvMesh&);
383 
384  //- Print some info on mesh.
385  static void printMeshInfo(const fvMesh&);
386 
387  //- Generate a test field on faces
389 
390  //- Check whether field consistent with face orientation
391  static void testField(const surfaceScalarField&);
392 };
393 
394 
395 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
396 
397 } // End namespace Foam
398 
399 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
400 
401 #ifdef NoRepository
402  #include "fvMeshDistributeTemplates.C"
403 #endif
404 
405 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
406 
407 #endif
408 
409 // ************************************************************************* //
fvMeshDistributeTemplates.C
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::fvMeshDistribute::countCells
static labelList countCells(const labelList &)
Helper function: count cells per processor in wanted distribution.
Definition: fvMeshDistribute.C:1690
Foam::distribution
Accumulating histogram of values. Specified bin resolution automatic generation of bins.
Definition: distribution.H:61
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::faceMap
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Definition: blockMeshMergeTopological.C:94
Foam::FieldField
A field of fields is a PtrList of fields with reference counting.
Definition: FieldField.H:53
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::fvMeshSubset
Given the original mesh and the list of selected cells, it creates the mesh consisting only of the de...
Definition: fvMeshSubset.H:73
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
fvMeshSubset.H
Foam::fvMeshDistribute::printFieldInfo
static void printFieldInfo(const fvMesh &)
Print some field info.
Definition: fvMeshDistributeTemplates.C:34
Foam::Map< label >
Foam::fvMeshDistribute::ClassName
ClassName("fvMeshDistribute")
Foam::fvMeshDistribute::generateTestField
static tmp< surfaceScalarField > generateTestField(const fvMesh &)
Generate a test field on faces.
Definition: fvMeshDistribute.C:390
Foam::Field< T >
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Field.H
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:62
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:84
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::HashTable< wordList >
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::fvMeshDistribute::printMeshInfo
static void printMeshInfo(const fvMesh &)
Print some info on mesh.
Definition: fvMeshDistribute.C:214
Foam::List< label >
Foam::fvMeshDistribute::printCoupleInfo
static void printCoupleInfo(const primitiveMesh &, const labelList &, const labelList &, const labelList &, const labelList &)
Print some info on coupling data.
Definition: fvMeshDistribute.C:274
Foam::UList< label >
Foam::fvMeshDistribute::testField
static void testField(const surfaceScalarField &)
Check whether field consistent with face orientation.
Definition: fvMeshDistribute.C:440
Foam::fvMeshDistribute::distribute
autoPtr< mapDistributePolyMesh > distribute(const labelList &dist)
Send cells to neighbours according to distribution.
Definition: fvMeshDistribute.C:1714
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:160
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::mapAddedPolyMesh
Class containing mesh-to-mesh mapping information after a mesh addition where we add a mesh ('added m...
Definition: mapAddedPolyMesh.H:58
Foam::GeometricField< scalar, fvsPatchField, surfaceMesh >
Foam::fvMeshDistribute
Sends/receives parts of mesh+fvfields to neighbouring processors. Used in load balancing.
Definition: fvMeshDistribute.H:73
fields
multivariateSurfaceInterpolationScheme< scalar >::fieldTable fields
Definition: createFields.H:97
Foam::primitiveMesh
Cell-face mesh analysis engine.
Definition: primitiveMesh.H:78