fvMeshToolsProcAddr.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) 2015-2022 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28#include "fvMeshTools.H"
29#include "fileOperation.H"
30#include "IndirectList.H"
31#include "labelRange.H"
33#include "OSspecific.H"
34
35// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
36
37namespace Foam
38{
39
40// Create a reconstruct map.
41// The baseMeshPtr is non-null (and probably has cells) on the master
42// is ignored elsewhere.
43//
44// The incomming faceProcAddressing is assumed to have flip addressing.
46(
47 const fvMesh& mesh,
48 const autoPtr<fvMesh>& baseMeshPtr,
49 const labelList& cellProcAddressing,
50 const labelList& faceProcAddressing,
51 const labelList& pointProcAddressing,
52 const labelList& boundaryProcAddressing
53)
54{
55 const label nOldPoints = mesh.nPoints();
56 const label nOldFaces = mesh.nFaces();
57 const label nOldCells = mesh.nCells();
58
59 const polyBoundaryMesh& pbm = mesh.boundaryMesh();
60
61 labelList oldPatchStarts(pbm.size());
62 labelList oldPatchNumPoints(pbm.size());
63 forAll(pbm, patchi)
64 {
65 oldPatchStarts[patchi] = pbm[patchi].start();
66 oldPatchNumPoints[patchi] = pbm[patchi].nPoints();
67 }
68
69 // Patches: purge -1 entries
70 labelList patchProcAddressing
71 (
73 (
74 boundaryProcAddressing,
76 )
77 );
78
79
80 labelListList cellSubMap(Pstream::nProcs());
81 cellSubMap[Pstream::masterNo()] = identity(nOldCells);
82
83 labelListList faceSubMap(Pstream::nProcs());
84 faceSubMap[Pstream::masterNo()] = identity(nOldFaces);
85
86 labelListList pointSubMap(Pstream::nProcs());
87 pointSubMap[Pstream::masterNo()] = identity(nOldPoints);
88
89 labelListList patchSubMap(Pstream::nProcs());
90 patchSubMap[Pstream::masterNo()] = patchProcAddressing;
91
92
93 // Gather addressing on master
94 labelListList cellAddressing(Pstream::nProcs());
95 cellAddressing[Pstream::myProcNo()] = cellProcAddressing;
96 Pstream::gatherList(cellAddressing);
97
98 labelListList faceAddressing(Pstream::nProcs());
99 faceAddressing[Pstream::myProcNo()] = faceProcAddressing;
100 Pstream::gatherList(faceAddressing);
101
102 labelListList pointAddressing(Pstream::nProcs());
103 pointAddressing[Pstream::myProcNo()] = pointProcAddressing;
104 Pstream::gatherList(pointAddressing);
105
106 labelListList patchAddressing(Pstream::nProcs());
107 patchAddressing[Pstream::myProcNo()] = patchProcAddressing;
108 Pstream::gatherList(patchAddressing);
109
110
111 // NB: can only have a reconstruct on master!
112 if (Pstream::master() && baseMeshPtr && baseMeshPtr->nCells())
113 {
114 const fvMesh& baseMesh = *baseMeshPtr;
115
116 const label nNewPoints = baseMesh.nPoints();
117 const label nNewFaces = baseMesh.nFaces();
118 const label nNewCells = baseMesh.nCells();
119 const label nNewPatches = baseMesh.boundaryMesh().size();
120
121 mapDistribute cellMap
122 (
123 nNewCells,
124 std::move(cellSubMap),
125 std::move(cellAddressing)
126 );
127
129 (
130 nNewFaces,
131 std::move(faceSubMap),
132 std::move(faceAddressing),
133 false, // subHasFlip
134 true // constructHasFlip
135 );
136
137 mapDistribute pointMap
138 (
139 nNewPoints,
140 std::move(pointSubMap),
141 std::move(pointAddressing)
142 );
143
144 mapDistribute patchMap
145 (
146 nNewPatches,
147 std::move(patchSubMap),
148 std::move(patchAddressing)
149 );
150
152 (
153 nOldPoints,
154 nOldFaces,
155 nOldCells,
156 std::move(oldPatchStarts),
157 std::move(oldPatchNumPoints),
158 std::move(pointMap),
159 std::move(faceMap),
160 std::move(cellMap),
161 std::move(patchMap)
162 );
163 }
164 else
165 {
166 // Zero-sized mesh (eg, processor mesh)
167
168 mapDistribute cellMap
169 (
170 0, // nNewCells
171 std::move(cellSubMap),
172 labelListList(Pstream::nProcs()) // constructMap
173 );
174
176 (
177 0, // nNewFaces
178 std::move(faceSubMap),
179 labelListList(Pstream::nProcs()), // constructMap
180 false, // subHasFlip
181 true // constructHasFlip
182 );
183
184 mapDistribute pointMap
185 (
186 0, // nNewPoints
187 std::move(pointSubMap),
188 labelListList(Pstream::nProcs()) // constructMap
189 );
190
191 mapDistribute patchMap
192 (
193 0, // nNewPatches
194 std::move(patchSubMap),
195 labelListList(Pstream::nProcs()) // constructMap
196 );
197
199 (
200 nOldPoints,
201 nOldFaces,
202 nOldCells,
203 std::move(oldPatchStarts),
204 std::move(oldPatchNumPoints),
205 std::move(pointMap),
206 std::move(faceMap),
207 std::move(cellMap),
208 std::move(patchMap)
209 );
210 }
211}
212
213} // End namespace Foam
214
215
216// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
217
220(
221 const fvMesh& mesh,
222 const autoPtr<fvMesh>& baseMeshPtr
223)
224{
225 // Processor-local reading
226 IOobject ioAddr
227 (
228 "procAddressing",
231 mesh.thisDb(),
234 false // no register
235 );
236
237 //if (ioAddr.typeHeaderOk<labelIOList>(true))
238 //{
239 // Pout<< "Reading addressing from " << io.name() << " at "
240 // << mesh.facesInstance() << nl << endl;
241 // mapDistributePolyMesh distMap = IOmapDistributePolyMesh(ioAddr);
242 // return autoPtr<mapDistributePolyMesh>::New(std::move(distMap));
243 //}
244 //else
245
246 {
247 Info<< "Reading (cell|face|point|boundary)ProcAddressing from "
248 << mesh.facesInstance().c_str() << '/'
250
251 ioAddr.rename("cellProcAddressing");
252 labelIOList cellProcAddressing(ioAddr, Zero);
253
254 ioAddr.rename("faceProcAddressing");
255 labelIOList faceProcAddressing(ioAddr, Zero);
256
257 ioAddr.rename("pointProcAddressing");
258 labelIOList pointProcAddressing(ioAddr, Zero);
259
260 ioAddr.rename("boundaryProcAddressing");
261 labelIOList boundaryProcAddressing(ioAddr, Zero);
262
263 if
264 (
265 mesh.nCells() != cellProcAddressing.size()
266 || mesh.nPoints() != pointProcAddressing.size()
267 || mesh.nFaces() != faceProcAddressing.size()
268 || mesh.boundaryMesh().size() != boundaryProcAddressing.size()
269 )
270 {
272 << "Read addressing inconsistent with mesh sizes" << nl
273 << "cells:" << mesh.nCells()
274 << " addressing:" << cellProcAddressing.objectRelPath()
275 << " size:" << cellProcAddressing.size() << nl
276 << "faces:" << mesh.nFaces()
277 << " addressing:" << faceProcAddressing.objectRelPath()
278 << " size:" << faceProcAddressing.size() << nl
279 << "points:" << mesh.nPoints()
280 << " addressing:" << pointProcAddressing.objectRelPath()
281 << " size:" << pointProcAddressing.size()
282 << "patches:" << mesh.boundaryMesh().size()
283 << " addressing:" << boundaryProcAddressing.objectRelPath()
284 << " size:" << boundaryProcAddressing.size()
285 << exit(FatalError);
286 }
287
289 (
290 mesh,
291 baseMeshPtr,
292 cellProcAddressing,
293 faceProcAddressing,
294 pointProcAddressing,
295 boundaryProcAddressing
296 );
297 }
298}
299
300
302(
303 const fvMesh& mesh,
304 const mapDistributePolyMesh& map,
305 const bool decompose,
306 autoPtr<fileOperation>&& writeHandler
307)
308{
309 Info<< "Writing ("
310 << (decompose ? "decompose" : "reconstruct")
311 << ") procAddressing files to "
312 << mesh.facesInstance().c_str() << '/'
314
315 // Processor-local outputs for components
316 // NB: the full "procAddressing" output is presumed to already have
317 // been done independently (as a registered object)
318 IOobject ioAddr
319 (
320 "procAddressing",
323 mesh.thisDb(),
326 false // no register
327 );
328
329 // cellProcAddressing (polyMesh)
330 ioAddr.rename("cellProcAddressing");
331 labelIOList cellMap(ioAddr, Zero);
332
333 // faceProcAddressing (polyMesh)
334 ioAddr.rename("faceProcAddressing");
335 labelIOList faceMap(ioAddr, Zero);
336
337 // pointProcAddressing (polyMesh)
338 ioAddr.rename("pointProcAddressing");
339 labelIOList pointMap(ioAddr, Zero);
340
341 // boundaryProcAddressing (polyMesh)
342 ioAddr.rename("boundaryProcAddressing");
343 labelIOList patchMap(ioAddr, Zero);
344
345
346 if (decompose)
347 {
348 // Decompose
349 // - forward map: [undecomposed] -> [decomposed]
350
351 cellMap = identity(map.nOldCells());
352 map.distributeCellData(cellMap);
353
354 faceMap = identity(map.nOldFaces());
355 {
356 const mapDistribute& faceDistMap = map.faceMap();
357
358 if (faceDistMap.subHasFlip() || faceDistMap.constructHasFlip())
359 {
360 // Offset by 1
361 faceMap = faceMap + 1;
362 }
363
364 faceDistMap.mapDistributeBase::distribute
365 (
367 faceMap,
368 flipLabelOp() // Apply face flips
369 );
370 }
371
372 pointMap = identity(map.nOldPoints());
373 map.distributePointData(pointMap);
374
375 patchMap = identity(map.oldPatchSizes().size());
376 map.patchMap().mapDistributeBase::distribute
377 (
379 label(-1), // nullValue for new patches...
380 patchMap,
381 flipOp() // negate op
382 );
383 }
384 else
385 {
386 // Reconstruct
387 // - reverse map: [undecomposed] <- [decomposed]
388
389 cellMap = identity(mesh.nCells());
390 map.cellMap().reverseDistribute(map.nOldCells(), cellMap);
391
393 {
394 const mapDistribute& faceDistMap = map.faceMap();
395
396 if (faceDistMap.subHasFlip() || faceDistMap.constructHasFlip())
397 {
398 // Offset by 1
399 faceMap = faceMap + 1;
400 }
401
402 faceDistMap.mapDistributeBase::reverseDistribute
403 (
405 map.nOldFaces(),
406 faceMap,
407 flipLabelOp() // Apply face flips
408 );
409 }
410
411 pointMap = identity(mesh.nPoints());
412 map.pointMap().reverseDistribute(map.nOldPoints(), pointMap);
413
414 patchMap = identity(mesh.boundaryMesh().size());
415 map.patchMap().mapDistributeBase::reverseDistribute
416 (
418 map.oldPatchSizes().size(),
419 label(-1), // nullValue for unmapped patches...
420 patchMap
421 );
422 }
423
424 autoPtr<fileOperation> defaultHandler;
425 if (writeHandler)
426 {
427 defaultHandler = fileHandler(std::move(writeHandler));
428 }
429
430 const bool cellOk = cellMap.write();
431 const bool faceOk = faceMap.write();
432 const bool pointOk = pointMap.write();
433 const bool patchOk = patchMap.write();
434
435 if (defaultHandler)
436 {
437 writeHandler = fileHandler(std::move(defaultHandler));
438 }
439
440 if (!cellOk || !faceOk || !pointOk || !patchOk)
441 {
443 << "Failed to write some of "
444 << cellMap.objectRelPath() << ", "
445 << faceMap.objectRelPath() << ", "
446 << pointMap.objectRelPath() << ", "
447 << patchMap.objectRelPath() << endl;
448 }
449}
450
451
452// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
fileName objectRelPath() const
The object path relative to the root.
Definition: IOobject.C:558
virtual void rename(const word &newName)
Rename the object.
Definition: IOobject.H:497
A List with indirect addressing.
Definition: IndirectList.H:119
label nProcs() const noexcept
Number of ranks associated with PstreamBuffers.
static void gatherList(const List< commsStruct > &comms, List< T > &values, const int tag, const label comm)
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
@ nonBlocking
"nonBlocking"
static constexpr int masterNo() noexcept
Process index of the master (always 0)
Definition: UPstream.H:451
label size() const noexcept
The number of elements in the list.
Definition: UPtrListI.H:106
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
static void writeProcAddressing(const fvMesh &procMesh, const mapDistributePolyMesh &map, const bool decompose, autoPtr< fileOperation > &&writeHandler)
static autoPtr< mapDistributePolyMesh > readProcAddressing(const fvMesh &procMesh, const autoPtr< fvMesh > &baseMeshPtr)
Read procAddressing components (reconstructing)
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:91
virtual const objectRegistry & thisDb() const
Return the object registry - resolve conflict polyMesh/lduMesh.
Definition: fvMesh.H:302
bool constructHasFlip() const noexcept
Does constructMap include a sign.
bool subHasFlip() const noexcept
Does subMap include a sign.
Class containing mesh-to-mesh mapping information after a mesh distribution where we send parts of me...
const mapDistribute & patchMap() const noexcept
Patch distribute map.
void distributeCellData(List< T > &values) const
Distribute list of cell data.
label nOldCells() const noexcept
Number of cells in mesh before distribution.
label nOldFaces() const noexcept
Number of faces in mesh before distribution.
const labelList & oldPatchSizes() const noexcept
List of the old patch sizes.
void distributePointData(List< T > &values) const
Distribute list of point data.
const mapDistribute & cellMap() const noexcept
Cell distribute map.
const mapDistribute & pointMap() const noexcept
Point distribute map.
const mapDistribute & faceMap() const noexcept
Face distribute map.
label nOldPoints() const noexcept
Number of points in mesh before distribution.
Class containing processor-to-processor mapping information.
void reverseDistribute(const label constructSize, List< T > &fld, const bool dummyTransform=true, const int tag=UPstream::msgType()) const
Reverse distribute data using default commsType.
A polyBoundaryMesh is a polyPatch list with additional search methods and registered IO.
label start() const noexcept
The start label of boundary faces in the polyMesh face list.
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:866
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:456
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:324
label nPoints() const noexcept
Number of mesh points.
label nCells() const noexcept
Number of mesh cells.
label nFaces() const noexcept
Number of mesh faces.
int myProcNo() const noexcept
Return processor number.
virtual bool write(const bool valid=true) const
Write using setting from DB.
splitCell * master() const
Definition: splitCell.H:113
dynamicFvMesh & mesh
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
#define WarningInFunction
Report a warning using Foam::Warning.
Namespace for OpenFOAM.
const fileOperation & fileHandler()
Get current file handler.
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i)
Definition: labelList.C:38
messageStream Info
Information stream (stdout output on master, null elsewhere)
static mapDistributePolyMesh createReconstructMap(const faMesh &mesh, const autoPtr< faMesh > &baseMeshPtr, const labelUList &faceProcAddr, const labelUList &edgeProcAddr, const labelUList &pointProcAddr, const labelUList &boundaryProcAddr)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
List< labelList > labelListList
A List of labelList.
Definition: labelList.H:56
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
Unary predicate for greater-equal 0 (int values)
Definition: IntRange.H:130
Negate integer values.
Definition: flipOp.H:90
Functor to negate primitives. Dummy for most other types.
Definition: flipOp.H:69