1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "regionSplit.H"
28 #include "cyclicPolyPatch.H"
29 #include "processorPolyPatch.H"
30 #include "globalIndex.H"
31 #include "syncTools.H"
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 defineTypeNameAndDebug(regionSplit, 0);
42 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
44 // Handle (non-processor) coupled faces.
45 void Foam::regionSplit::transferCoupledFaceRegion
48 const label otherFaceI,
50 labelList& faceRegion,
51 DynamicList<label>& newChangedFaces
54 if (faceRegion[faceI] >= 0)
56 if (faceRegion[otherFaceI] == -1)
58 faceRegion[otherFaceI] = faceRegion[faceI];
59 newChangedFaces.append(otherFaceI);
61 else if (faceRegion[otherFaceI] == -2)
63 // otherFaceI blocked but faceI is not. Is illegal for coupled
64 // faces, not for explicit connections.
66 else if (faceRegion[otherFaceI] != faceRegion[faceI])
70 "regionSplit::transferCoupledFaceRegion"
71 "(const label, const label, labelList&, labelList&) const"
72 ) << "Problem : coupled face " << faceI
73 << " on patch " << mesh_.boundaryMesh().whichPatch(faceI)
74 << " has region " << faceRegion[faceI]
75 << " but coupled face " << otherFaceI
76 << " has region " << faceRegion[otherFaceI]
78 << "Is your blocked faces specification"
79 << " synchronized across coupled boundaries?"
83 else if (faceRegion[faceI] == -1)
85 if (faceRegion[otherFaceI] >= 0)
87 faceRegion[faceI] = faceRegion[otherFaceI];
88 newChangedFaces.append(faceI);
90 else if (faceRegion[otherFaceI] == -2)
92 // otherFaceI blocked but faceI is not. Is illegal for coupled
93 // faces, not for explicit connections.
99 void Foam::regionSplit::fillSeedMask
101 const List<labelPair>& explicitConnections,
102 labelList& cellRegion,
103 labelList& faceRegion,
104 const label seedCellID,
105 const label markValue
109 cellRegion[seedCellID] = markValue;
112 // Collect faces on seed cell
113 const cell& cFaces = mesh_.cells()[seedCellID];
117 labelList changedFaces(cFaces.size());
121 label faceI = cFaces[i];
123 if (faceRegion[faceI] == -1)
125 faceRegion[faceI] = markValue;
126 changedFaces[nFaces++] = faceI;
129 changedFaces.setSize(nFaces);
132 // Loop over changed faces. MeshWave in small.
134 while (changedFaces.size())
138 // Pout<< "regionSplit::fillSeedMask : changedFaces:"
139 // << changedFaces.size() << endl;
142 DynamicList<label> changedCells(changedFaces.size());
144 forAll(changedFaces, i)
146 label faceI = changedFaces[i];
148 label own = mesh_.faceOwner()[faceI];
150 if (cellRegion[own] == -1)
152 cellRegion[own] = markValue;
153 changedCells.append(own);
156 if (mesh_.isInternalFace(faceI))
158 label nei = mesh_.faceNeighbour()[faceI];
160 if (cellRegion[nei] == -1)
162 cellRegion[nei] = markValue;
163 changedCells.append(nei);
171 // Pout<< "regionSplit::fillSeedMask : changedCells:"
172 // << changedCells.size() << endl;
175 // Loop over changedCells and collect faces
176 DynamicList<label> newChangedFaces(changedCells.size());
178 forAll(changedCells, i)
180 label cellI = changedCells[i];
182 const cell& cFaces = mesh_.cells()[cellI];
184 forAll(cFaces, cFaceI)
186 label faceI = cFaces[cFaceI];
188 if (faceRegion[faceI] == -1)
190 faceRegion[faceI] = markValue;
191 newChangedFaces.append(faceI);
199 // Pout<< "regionSplit::fillSeedMask : changedFaces before sync:"
200 // << changedFaces.size() << endl;
204 // Check for changes to any locally coupled face.
205 // Global connections are done later.
207 const polyBoundaryMesh& patches = mesh_.boundaryMesh();
209 forAll(patches, patchI)
211 const polyPatch& pp = patches[patchI];
213 if (isA<cyclicPolyPatch>(pp))
215 label faceI = pp.start();
217 label halfSz = pp.size()/2;
219 for (label i = 0; i < halfSz; i++)
221 label otherFaceI = refCast<const cyclicPolyPatch>(pp)
222 .transformGlobalFace(faceI);
224 transferCoupledFaceRegion
236 forAll(explicitConnections, i)
238 transferCoupledFaceRegion
240 explicitConnections[i][0],
241 explicitConnections[i][1],
249 // Pout<< "regionSplit::fillSeedMask : changedFaces after sync:"
250 // << newChangedFaces.size() << endl;
253 changedFaces.transfer(newChangedFaces);
258 Foam::label Foam::regionSplit::calcRegionSplit
260 const boolList& blockedFace,
261 const List<labelPair>& explicitConnections,
263 labelList& cellRegion
268 if (blockedFace.size())
270 // Check that blockedFace is synced.
271 boolList syncBlockedFace(blockedFace);
272 syncTools::swapFaceList(mesh_, syncBlockedFace, false);
274 forAll(syncBlockedFace, faceI)
276 if (syncBlockedFace[faceI] != blockedFace[faceI])
280 "regionSplit::calcRegionSplit(..)"
281 ) << "Face " << faceI << " not synchronised. My value:"
282 << blockedFace[faceI] << " coupled value:"
283 << syncBlockedFace[faceI]
284 << abort(FatalError);
293 labelList faceRegion(mesh_.nFaces(), -1);
295 if (blockedFace.size())
297 forAll(blockedFace, faceI)
299 if (blockedFace[faceI])
301 faceRegion[faceI] = -2;
307 // Assign local regions
308 // ~~~~~~~~~~~~~~~~~~~~
310 // Start with region 0
313 label unsetCellI = 0;
317 // Find first unset cell
319 for (; unsetCellI < mesh_.nCells(); unsetCellI++)
321 if (cellRegion[unsetCellI] == -1)
327 if (unsetCellI >= mesh_.nCells())
341 // Current unsetCell has now been handled. Go to next region.
350 forAll(cellRegion, cellI)
352 if (cellRegion[cellI] < 0)
354 FatalErrorIn("regionSplit::calcRegionSplit(..)")
355 << "cell:" << cellI << " region:" << cellRegion[cellI]
356 << abort(FatalError);
360 forAll(faceRegion, faceI)
362 if (faceRegion[faceI] == -1)
364 FatalErrorIn("regionSplit::calcRegionSplit(..)")
365 << "face:" << faceI << " region:" << faceRegion[faceI]
366 << abort(FatalError);
373 // Assign global regions
374 // ~~~~~~~~~~~~~~~~~~~~~
375 // Offset local regions to create unique global regions.
377 globalIndex globalRegions(nRegions);
380 // Merge global regions
381 // ~~~~~~~~~~~~~~~~~~~~
382 // Regions across non-blocked proc patches get merged.
383 // This will set merged global regions to be the min of both.
384 // (this will create gaps in the global region list so they will get
387 // Map from global to merged global
388 labelList mergedGlobal(identity(globalRegions.size()));
391 // See if any regions get merged. Only nessecary for parallel
392 while (Pstream::parRun())
396 Pout<< nl << "-- Starting Iteration --" << endl;
399 const polyBoundaryMesh& patches = mesh_.boundaryMesh();
401 // Send global regions across (or -2 if blocked face)
402 forAll(patches, patchI)
404 const polyPatch& pp = patches[patchI];
406 if (isA<processorPolyPatch>(pp))
408 labelList myGlobalRegions(pp.size());
410 label faceI = pp.start();
414 if (faceRegion[faceI] < 0)
416 myGlobalRegions[i] = faceRegion[faceI];
420 myGlobalRegions[i] = mergedGlobal
421 [globalRegions.toGlobal(faceRegion[faceI])];
430 refCast<const processorPolyPatch>(pp).neighbProcNo()
433 toProcNbr << myGlobalRegions;
438 // Receive global regions
442 forAll(patches, patchI)
444 const polyPatch& pp = patches[patchI];
446 if (isA<processorPolyPatch>(pp))
448 const processorPolyPatch& procPp =
449 refCast<const processorPolyPatch>(pp);
451 IPstream fromProcNbr(Pstream::blocking, procPp.neighbProcNo());
453 labelList nbrRegions(fromProcNbr);
456 // Compare with my regions to see which get merged.
458 label faceI = pp.start();
464 faceRegion[faceI] < 0
468 if (faceRegion[faceI] != nbrRegions[i])
470 FatalErrorIn("regionSplit::calcRegionSplit(..)")
471 << "On patch:" << pp.name()
473 << " my local region:" << faceRegion[faceI]
474 << " neighbouring region:"
475 << nbrRegions[i] << nl
476 << "Maybe your blockedFaces are not"
477 << " synchronized across coupled faces?"
478 << abort(FatalError);
483 label uncompactGlobal =
484 globalRegions.toGlobal(faceRegion[faceI]);
486 label myGlobal = mergedGlobal[uncompactGlobal];
488 if (myGlobal != nbrRegions[i])
490 label minRegion = min(myGlobal, nbrRegions[i]);
494 Pout<< "Merging region " << myGlobal
495 << " (on proc " << Pstream::myProcNo()
496 << ") and region " << nbrRegions[i]
497 << " (on proc " << procPp.neighbProcNo()
498 << ") into region " << minRegion << endl;
501 mergedGlobal[uncompactGlobal] = minRegion;
502 mergedGlobal[myGlobal] = minRegion;
503 mergedGlobal[nbrRegions[i]] = minRegion;
515 reduce(nMerged, sumOp<label>());
519 Pout<< "nMerged:" << nMerged << endl;
527 // Merge the compacted regions.
528 Pstream::listCombineGather(mergedGlobal, minEqOp<label>());
529 Pstream::listCombineScatter(mergedGlobal);
533 // Compact global regions
534 // ~~~~~~~~~~~~~~~~~~~~~~
536 // All procs will have the same global mergedGlobal region.
537 // There might be gaps in it however so compact.
539 labelList mergedToCompacted(globalRegions.size(), -1);
543 forAll(mergedGlobal, i)
545 label merged = mergedGlobal[i];
547 if (mergedToCompacted[merged] == -1)
549 mergedToCompacted[merged] = compactI++;
555 Pout<< "Compacted down to " << compactI << " regions." << endl;
558 // Renumber cellRegion to be global regions
559 forAll(cellRegion, cellI)
561 label region = cellRegion[cellI];
565 label merged = mergedGlobal[globalRegions.toGlobal(region)];
567 cellRegion[cellI] = mergedToCompacted[merged];
575 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
577 Foam::regionSplit::regionSplit(const polyMesh& mesh)
579 labelList(mesh.nCells(), -1),
581 nRegions_(calcRegionSplit(boolList(0, false), List<labelPair>(0), *this))
585 Foam::regionSplit::regionSplit
587 const polyMesh& mesh,
588 const boolList& blockedFace
591 labelList(mesh.nCells(), -1),
593 nRegions_(calcRegionSplit(blockedFace, List<labelPair>(0), *this))
597 Foam::regionSplit::regionSplit
599 const polyMesh& mesh,
600 const boolList& blockedFace,
601 const List<labelPair>& explicitConnections
604 labelList(mesh.nCells(), -1),
606 nRegions_(calcRegionSplit(blockedFace, explicitConnections, *this))
610 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
613 // ************************************************************************* //