OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

regionSplit.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "regionSplit.H"
00028 #include "cyclicPolyPatch.H"
00029 #include "processorPolyPatch.H"
00030 #include "globalIndex.H"
00031 #include "syncTools.H"
00032 
00033 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037 
00038 defineTypeNameAndDebug(regionSplit, 0);
00039 
00040 }
00041 
00042 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00043 
00044 // Handle (non-processor) coupled faces.
00045 void Foam::regionSplit::transferCoupledFaceRegion
00046 (
00047     const label faceI,
00048     const label otherFaceI,
00049 
00050     labelList& faceRegion,
00051     DynamicList<label>& newChangedFaces
00052 ) const
00053 {
00054     if (faceRegion[faceI] >= 0)
00055     {
00056         if (faceRegion[otherFaceI] == -1)
00057         {
00058             faceRegion[otherFaceI] = faceRegion[faceI];
00059             newChangedFaces.append(otherFaceI);
00060         }
00061         else if (faceRegion[otherFaceI] == -2)
00062         {
00063             // otherFaceI blocked but faceI is not. Is illegal for coupled
00064             // faces, not for explicit connections.
00065         }
00066         else if (faceRegion[otherFaceI] != faceRegion[faceI])
00067         {
00068             FatalErrorIn
00069             (
00070                   "regionSplit::transferCoupledFaceRegion"
00071                   "(const label, const label, labelList&, labelList&) const"
00072               )   << "Problem : coupled face " << faceI
00073                   << " on patch " << mesh_.boundaryMesh().whichPatch(faceI)
00074                   << " has region " << faceRegion[faceI]
00075                   << " but coupled face " << otherFaceI
00076                   << " has region " << faceRegion[otherFaceI]
00077                   << endl
00078                   << "Is your blocked faces specification"
00079                   << " synchronized across coupled boundaries?"
00080                   << abort(FatalError);
00081         }
00082     }
00083     else if (faceRegion[faceI] == -1)
00084     {
00085         if (faceRegion[otherFaceI] >= 0)
00086         {
00087             faceRegion[faceI] = faceRegion[otherFaceI];
00088             newChangedFaces.append(faceI);
00089         }
00090         else if (faceRegion[otherFaceI] == -2)
00091         {
00092             // otherFaceI blocked but faceI is not. Is illegal for coupled
00093             // faces, not for explicit connections.
00094         }
00095     }
00096 }
00097 
00098 
00099 void Foam::regionSplit::fillSeedMask
00100 (
00101     const List<labelPair>& explicitConnections,
00102     labelList& cellRegion,
00103     labelList& faceRegion,
00104     const label seedCellID,
00105     const label markValue
00106 ) const
00107 {
00108     // Do seed cell
00109     cellRegion[seedCellID] = markValue;
00110 
00111 
00112     // Collect faces on seed cell
00113     const cell& cFaces = mesh_.cells()[seedCellID];
00114 
00115     label nFaces = 0;
00116 
00117     labelList changedFaces(cFaces.size());
00118 
00119     forAll(cFaces, i)
00120     {
00121         label faceI = cFaces[i];
00122 
00123         if (faceRegion[faceI] == -1)
00124         {
00125             faceRegion[faceI] = markValue;
00126             changedFaces[nFaces++] = faceI;
00127         }
00128     }
00129     changedFaces.setSize(nFaces);
00130 
00131 
00132     // Loop over changed faces. MeshWave in small.
00133 
00134     while (changedFaces.size())
00135     {
00136         //if (debug)
00137         //{
00138         //    Pout<< "regionSplit::fillSeedMask : changedFaces:"
00139         //        << changedFaces.size() << endl;
00140         //}
00141 
00142         DynamicList<label> changedCells(changedFaces.size());
00143 
00144         forAll(changedFaces, i)
00145         {
00146             label faceI = changedFaces[i];
00147 
00148             label own = mesh_.faceOwner()[faceI];
00149 
00150             if (cellRegion[own] == -1)
00151             {
00152                 cellRegion[own] = markValue;
00153                 changedCells.append(own);
00154             }
00155 
00156             if (mesh_.isInternalFace(faceI))
00157             {
00158                 label nei = mesh_.faceNeighbour()[faceI];
00159 
00160                 if (cellRegion[nei] == -1)
00161                 {
00162                     cellRegion[nei] = markValue;
00163                     changedCells.append(nei);
00164                 }
00165             }
00166         }
00167 
00168 
00169         //if (debug)
00170         //{
00171         //    Pout<< "regionSplit::fillSeedMask : changedCells:"
00172         //        << changedCells.size() << endl;
00173         //}
00174 
00175         // Loop over changedCells and collect faces
00176         DynamicList<label> newChangedFaces(changedCells.size());
00177 
00178         forAll(changedCells, i)
00179         {
00180             label cellI = changedCells[i];
00181 
00182             const cell& cFaces = mesh_.cells()[cellI];
00183 
00184             forAll(cFaces, cFaceI)
00185             {
00186                 label faceI = cFaces[cFaceI];
00187 
00188                 if (faceRegion[faceI] == -1)
00189                 {
00190                     faceRegion[faceI] = markValue;
00191                     newChangedFaces.append(faceI);
00192                 }
00193             }
00194         }
00195 
00196 
00197         //if (debug)
00198         //{
00199         //    Pout<< "regionSplit::fillSeedMask : changedFaces before sync:"
00200         //        << changedFaces.size() << endl;
00201         //}
00202 
00203 
00204         // Check for changes to any locally coupled face.
00205         // Global connections are done later.
00206 
00207         const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00208 
00209         forAll(patches, patchI)
00210         {
00211             const polyPatch& pp = patches[patchI];
00212 
00213             if (isA<cyclicPolyPatch>(pp))
00214             {
00215                 label faceI = pp.start();
00216 
00217                 label halfSz = pp.size()/2;
00218 
00219                 for (label i = 0; i < halfSz; i++)
00220                 {
00221                     label otherFaceI = refCast<const cyclicPolyPatch>(pp)
00222                         .transformGlobalFace(faceI);
00223 
00224                     transferCoupledFaceRegion
00225                     (
00226                         faceI,
00227                         otherFaceI,
00228                         faceRegion,
00229                         newChangedFaces
00230                     );
00231 
00232                     faceI++;
00233                 }
00234             }
00235         }
00236         forAll(explicitConnections, i)
00237         {
00238             transferCoupledFaceRegion
00239             (
00240                 explicitConnections[i][0],
00241                 explicitConnections[i][1],
00242                 faceRegion,
00243                 newChangedFaces
00244             );
00245         }
00246 
00247         //if (debug)
00248         //{
00249         //    Pout<< "regionSplit::fillSeedMask : changedFaces after sync:"
00250         //        << newChangedFaces.size() << endl;
00251         //}
00252 
00253         changedFaces.transfer(newChangedFaces);
00254     }
00255 }
00256 
00257 
00258 Foam::label Foam::regionSplit::calcRegionSplit
00259 (
00260     const boolList& blockedFace,
00261     const List<labelPair>& explicitConnections,
00262 
00263     labelList& cellRegion
00264 ) const
00265 {
00266     if (debug)
00267     {
00268         if (blockedFace.size())
00269         {
00270             // Check that blockedFace is synced.
00271             boolList syncBlockedFace(blockedFace);
00272             syncTools::swapFaceList(mesh_, syncBlockedFace, false);
00273 
00274             forAll(syncBlockedFace, faceI)
00275             {
00276                 if (syncBlockedFace[faceI] != blockedFace[faceI])
00277                 {
00278                     FatalErrorIn
00279                     (
00280                         "regionSplit::calcRegionSplit(..)"
00281                     )   << "Face " << faceI << " not synchronised. My value:"
00282                         << blockedFace[faceI] << "  coupled value:"
00283                         << syncBlockedFace[faceI]
00284                         << abort(FatalError);
00285                 }
00286             }
00287         }
00288     }
00289 
00290     // Region per face.
00291     // -1 unassigned
00292     // -2 blocked
00293     labelList faceRegion(mesh_.nFaces(), -1);
00294 
00295     if (blockedFace.size())
00296     {
00297         forAll(blockedFace, faceI)
00298         {
00299             if (blockedFace[faceI])
00300             {
00301                 faceRegion[faceI] = -2;
00302             }
00303         }
00304     }
00305 
00306 
00307     // Assign local regions
00308     // ~~~~~~~~~~~~~~~~~~~~
00309 
00310     // Start with region 0
00311     label nRegions = 0;
00312 
00313     label unsetCellI = 0;
00314 
00315     do
00316     {
00317         // Find first unset cell
00318 
00319         for (; unsetCellI < mesh_.nCells(); unsetCellI++)
00320         {
00321             if (cellRegion[unsetCellI] == -1)
00322             {
00323                 break;
00324             }
00325         }
00326 
00327         if (unsetCellI >= mesh_.nCells())
00328         {
00329             break;
00330         }
00331 
00332         fillSeedMask
00333         (
00334             explicitConnections,
00335             cellRegion,
00336             faceRegion,
00337             unsetCellI,
00338             nRegions
00339         );
00340 
00341         // Current unsetCell has now been handled. Go to next region.
00342         nRegions++;
00343         unsetCellI++;
00344     }
00345     while(true);
00346 
00347 
00348     if (debug)
00349     {
00350         forAll(cellRegion, cellI)
00351         {
00352             if (cellRegion[cellI] < 0)
00353             {
00354                 FatalErrorIn("regionSplit::calcRegionSplit(..)")
00355                     << "cell:" << cellI << " region:" << cellRegion[cellI]
00356                     << abort(FatalError);
00357             }
00358         }
00359 
00360         forAll(faceRegion, faceI)
00361         {
00362             if (faceRegion[faceI] == -1)
00363             {
00364                 FatalErrorIn("regionSplit::calcRegionSplit(..)")
00365                     << "face:" << faceI << " region:" << faceRegion[faceI]
00366                     << abort(FatalError);
00367             }
00368         }
00369     }
00370 
00371 
00372 
00373     // Assign global regions
00374     // ~~~~~~~~~~~~~~~~~~~~~
00375     // Offset local regions to create unique global regions.
00376 
00377     globalIndex globalRegions(nRegions);
00378 
00379 
00380     // Merge global regions
00381     // ~~~~~~~~~~~~~~~~~~~~
00382     // Regions across non-blocked proc patches get merged.
00383     // This will set merged global regions to be the min of both.
00384     // (this will create gaps in the global region list so they will get
00385     // merged later on)
00386 
00387     // Map from global to merged global
00388     labelList mergedGlobal(identity(globalRegions.size()));
00389 
00390 
00391     // See if any regions get merged. Only nessecary for parallel
00392     while (Pstream::parRun())
00393     {
00394         if (debug)
00395         {
00396             Pout<< nl << "-- Starting Iteration --" << endl;
00397         }
00398 
00399         const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00400 
00401         // Send global regions across (or -2 if blocked face)
00402         forAll(patches, patchI)
00403         {
00404             const polyPatch& pp = patches[patchI];
00405 
00406             if (isA<processorPolyPatch>(pp))
00407             {
00408                 labelList myGlobalRegions(pp.size());
00409 
00410                 label faceI = pp.start();
00411 
00412                 forAll(pp, i)
00413                 {
00414                     if (faceRegion[faceI] < 0)
00415                     {
00416                         myGlobalRegions[i] = faceRegion[faceI];
00417                     }
00418                     else
00419                     {
00420                         myGlobalRegions[i] = mergedGlobal
00421                         [globalRegions.toGlobal(faceRegion[faceI])];
00422                     }
00423 
00424                     faceI++;
00425                 }
00426 
00427                 OPstream toProcNbr
00428                 (
00429                     Pstream::blocking,
00430                     refCast<const processorPolyPatch>(pp).neighbProcNo()
00431                 );
00432 
00433                 toProcNbr << myGlobalRegions;
00434             }
00435         }
00436 
00437 
00438         // Receive global regions
00439 
00440         label nMerged = 0;
00441 
00442         forAll(patches, patchI)
00443         {
00444             const polyPatch& pp = patches[patchI];
00445 
00446             if (isA<processorPolyPatch>(pp))
00447             {
00448                 const processorPolyPatch& procPp =
00449                     refCast<const processorPolyPatch>(pp);
00450 
00451                 IPstream fromProcNbr(Pstream::blocking, procPp.neighbProcNo());
00452 
00453                 labelList nbrRegions(fromProcNbr);
00454 
00455 
00456                 // Compare with my regions to see which get merged.
00457 
00458                 label faceI = pp.start();
00459 
00460                 forAll(pp, i)
00461                 {
00462                     if
00463                     (
00464                         faceRegion[faceI] < 0
00465                      || nbrRegions[i] < 0
00466                     )
00467                     {
00468                         if (faceRegion[faceI] != nbrRegions[i])
00469                         {
00470                             FatalErrorIn("regionSplit::calcRegionSplit(..)")
00471                                 << "On patch:" << pp.name()
00472                                 << " face:" << faceI
00473                                 << " my local region:" << faceRegion[faceI]
00474                                 << " neighbouring region:"
00475                                 << nbrRegions[i] << nl
00476                                 << "Maybe your blockedFaces are not"
00477                                 << " synchronized across coupled faces?"
00478                                 << abort(FatalError);
00479                         }
00480                     }
00481                     else
00482                     {
00483                         label uncompactGlobal =
00484                             globalRegions.toGlobal(faceRegion[faceI]);
00485 
00486                         label myGlobal = mergedGlobal[uncompactGlobal];
00487 
00488                         if (myGlobal != nbrRegions[i])
00489                         {
00490                             label minRegion = min(myGlobal, nbrRegions[i]);
00491 
00492                             if (debug)
00493                             {
00494                                 Pout<< "Merging region " << myGlobal
00495                                     << " (on proc " << Pstream::myProcNo()
00496                                     << ") and region " << nbrRegions[i]
00497                                     << " (on proc " << procPp.neighbProcNo()
00498                                     << ") into region " << minRegion << endl;
00499                             }
00500 
00501                             mergedGlobal[uncompactGlobal] = minRegion;
00502                             mergedGlobal[myGlobal] = minRegion;
00503                             mergedGlobal[nbrRegions[i]] = minRegion;
00504 
00505                             nMerged++;
00506                         }
00507                     }
00508 
00509                     faceI++;
00510                 }
00511             }
00512         }
00513 
00514 
00515         reduce(nMerged, sumOp<label>());
00516 
00517         if (debug)
00518         {
00519             Pout<< "nMerged:" << nMerged << endl;
00520         }
00521 
00522         if (nMerged == 0)
00523         {
00524             break;
00525         }
00526 
00527         // Merge the compacted regions.
00528         Pstream::listCombineGather(mergedGlobal, minEqOp<label>());
00529         Pstream::listCombineScatter(mergedGlobal);
00530     }
00531 
00532 
00533     // Compact global regions
00534     // ~~~~~~~~~~~~~~~~~~~~~~
00535 
00536     // All procs will have the same global mergedGlobal region.
00537     // There might be gaps in it however so compact.
00538 
00539     labelList mergedToCompacted(globalRegions.size(), -1);
00540 
00541     label compactI = 0;
00542 
00543     forAll(mergedGlobal, i)
00544     {
00545         label merged = mergedGlobal[i];
00546 
00547         if (mergedToCompacted[merged] == -1)
00548         {
00549             mergedToCompacted[merged] = compactI++;
00550         }
00551     }
00552 
00553     if (debug)
00554     {
00555         Pout<< "Compacted down to " << compactI << " regions." << endl;
00556     }
00557 
00558     // Renumber cellRegion to be global regions
00559     forAll(cellRegion, cellI)
00560     {
00561         label region = cellRegion[cellI];
00562 
00563         if (region >= 0)
00564         {
00565             label merged = mergedGlobal[globalRegions.toGlobal(region)];
00566 
00567             cellRegion[cellI] = mergedToCompacted[merged];
00568         }
00569     }
00570 
00571     return compactI;
00572 }
00573 
00574 
00575 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00576 
00577 Foam::regionSplit::regionSplit(const polyMesh& mesh)
00578 :
00579     labelList(mesh.nCells(), -1),
00580     mesh_(mesh),
00581     nRegions_(calcRegionSplit(boolList(0, false), List<labelPair>(0), *this))
00582 {}
00583 
00584 
00585 Foam::regionSplit::regionSplit
00586 (
00587     const polyMesh& mesh,
00588     const boolList& blockedFace
00589 )
00590 :
00591     labelList(mesh.nCells(), -1),
00592     mesh_(mesh),
00593     nRegions_(calcRegionSplit(blockedFace, List<labelPair>(0), *this))
00594 {}
00595 
00596 
00597 Foam::regionSplit::regionSplit
00598 (
00599     const polyMesh& mesh,
00600     const boolList& blockedFace,
00601     const List<labelPair>& explicitConnections
00602 )
00603 :
00604     labelList(mesh.nCells(), -1),
00605     mesh_(mesh),
00606     nRegions_(calcRegionSplit(blockedFace, explicitConnections, *this))
00607 {}
00608 
00609 
00610 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00611 
00612 
00613 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd