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

fvMeshSubset.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 Description
00026     Post-processing mesh subset tool.  Given the original mesh and the
00027     list of selected cells, it creates the mesh consisting only of the
00028     desired cells, with the mapping list for points, faces, and cells.
00029 
00030     MJ 23/03/05 on coupled faces change the patch of the face to the
00031     oldInternalFaces patch.
00032 
00033 \*---------------------------------------------------------------------------*/
00034 
00035 #include "fvMeshSubset.H"
00036 #include "boolList.H"
00037 #include "Pstream.H"
00038 #include "emptyPolyPatch.H"
00039 #include "demandDrivenData.H"
00040 #include "cyclicPolyPatch.H"
00041 
00042 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00043 
00044 namespace Foam
00045 {
00046 
00047 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00048 
00049 bool Foam::fvMeshSubset::checkCellSubset() const
00050 {
00051     if (fvMeshSubsetPtr_.empty())
00052     {
00053         FatalErrorIn("bool fvMeshSubset::checkCellSubset() const")
00054             << "Mesh subset not set.  Please set the cell map using "
00055             << "void setCellSubset(const labelHashSet& cellsToSubset)" << endl
00056             << "before attempting to access subset data"
00057             << abort(FatalError);
00058 
00059         return false;
00060     }
00061     else
00062     {
00063         return true;
00064     }
00065 }
00066 
00067 
00068 void Foam::fvMeshSubset::markPoints
00069 (
00070     const labelList& curPoints,
00071     Map<label>& pointMap
00072 )
00073 {
00074     forAll (curPoints, pointI)
00075     {
00076         // Note: insert will only insert if not yet there.
00077         pointMap.insert(curPoints[pointI], 0);
00078     }
00079 }
00080 
00081 
00082 void Foam::fvMeshSubset::markPoints
00083 (
00084     const labelList& curPoints,
00085     labelList& pointMap
00086 )
00087 {
00088     forAll (curPoints, pointI)
00089     {
00090         pointMap[curPoints[pointI]] = 0;
00091     }
00092 }
00093 
00094 
00095 // Synchronize nCellsUsingFace on both sides of coupled patches. Marks
00096 // faces that become 'uncoupled' with 3.
00097 void Foam::fvMeshSubset::doCoupledPatches
00098 (
00099     const bool syncPar,
00100     labelList& nCellsUsingFace
00101 ) const
00102 {
00103     const polyBoundaryMesh& oldPatches = baseMesh().boundaryMesh();
00104 
00105     label nUncoupled = 0;
00106 
00107     if (syncPar && Pstream::parRun())
00108     {
00109         // Send face usage across processor patches
00110         forAll (oldPatches, oldPatchI)
00111         {
00112             const polyPatch& pp = oldPatches[oldPatchI];
00113 
00114             if (typeid(pp) == typeid(processorPolyPatch))
00115             {
00116                 const processorPolyPatch& procPatch =
00117                     refCast<const processorPolyPatch>(pp);
00118 
00119                 OPstream toNeighbour
00120                 (
00121                     Pstream::blocking,
00122                     procPatch.neighbProcNo()
00123                 );
00124 
00125                 toNeighbour
00126                     << SubList<label>(nCellsUsingFace, pp.size(), pp.start());
00127             }
00128         }
00129 
00130 
00131         // Receive face usage count and check for faces that become uncoupled.
00132         forAll (oldPatches, oldPatchI)
00133         {
00134             const polyPatch& pp = oldPatches[oldPatchI];
00135 
00136             if (typeid(pp) == typeid(processorPolyPatch))
00137             {
00138                 const processorPolyPatch& procPatch =
00139                     refCast<const processorPolyPatch>(pp);
00140 
00141                 IPstream fromNeighbour
00142                 (
00143                     Pstream::blocking,
00144                     procPatch.neighbProcNo()
00145                 );
00146 
00147                 labelList nbrCellsUsingFace(fromNeighbour);
00148 
00149                 // Combine with this side.
00150 
00151                 forAll (pp, i)
00152                 {
00153                     if
00154                     (
00155                         nCellsUsingFace[pp.start()+i] == 1
00156                      && nbrCellsUsingFace[i] == 0
00157                     )
00158                     {
00159                         // Face's neighbour is no longer there. Mark face off
00160                         // as coupled
00161                         nCellsUsingFace[pp.start()+i] = 3;
00162                         nUncoupled++;
00163                     }
00164                 }
00165             }
00166         }
00167     }
00168 
00169     // Do same for cyclics.
00170     forAll (oldPatches, oldPatchI)
00171     {
00172         const polyPatch& pp = oldPatches[oldPatchI];
00173 
00174         if (typeid(pp) == typeid(cyclicPolyPatch))
00175         {
00176             const cyclicPolyPatch& cycPatch =
00177                 refCast<const cyclicPolyPatch>(pp);
00178 
00179             forAll (cycPatch, i)
00180             {
00181                 label thisFaceI = cycPatch.start() + i;
00182                 label otherFaceI = cycPatch.transformGlobalFace(thisFaceI);
00183 
00184                 if
00185                 (
00186                     nCellsUsingFace[thisFaceI] == 1
00187                  && nCellsUsingFace[otherFaceI] == 0
00188                 )
00189                 {
00190                     nCellsUsingFace[thisFaceI] = 3;
00191                     nUncoupled++;
00192                 }
00193             }
00194         }
00195     }
00196 
00197     if (syncPar)
00198     {
00199         reduce(nUncoupled, sumOp<label>());
00200     }
00201 
00202     if (nUncoupled > 0)
00203     {
00204         Info<< "Uncoupled " << nUncoupled << " faces on coupled patches. "
00205             << "(processorPolyPatch, cyclicPolyPatch)" << nl
00206             << "You might need to run couplePatches to restore the patch face"
00207             << " ordering." << nl << endl;
00208     }
00209 }
00210 
00211 
00212 labelList Foam::fvMeshSubset::subset
00213 (
00214     const label nElems,
00215     const labelList& selectedElements,
00216     const labelList& subsetMap
00217 )
00218 {
00219     // Mark selected elements.
00220     boolList selected(nElems, false);
00221     forAll(selectedElements, i)
00222     {
00223         selected[selectedElements[i]] = true;
00224     }
00225 
00226     // Count subset of selected elements
00227     label n = 0;
00228     forAll(subsetMap, i)
00229     {
00230         if (selected[subsetMap[i]])
00231         {
00232             n++;
00233         }
00234     }
00235 
00236     // Collect selected elements
00237     labelList subsettedElements(n);
00238     n = 0;
00239 
00240     forAll(subsetMap, i)
00241     {
00242         if (selected[subsetMap[i]])
00243         {
00244             subsettedElements[n++] = i;
00245         }
00246     }
00247 
00248     return subsettedElements;
00249 }
00250 
00251 
00252 void Foam::fvMeshSubset::subsetZones()
00253 {
00254     // Keep all zones, even if zero size.
00255 
00256     const pointZoneMesh& pointZones = baseMesh().pointZones();
00257 
00258     // PointZones
00259     List<pointZone*> pZonePtrs(pointZones.size());
00260 
00261     forAll(pointZones, i)
00262     {
00263         const pointZone& pz = pointZones[i];
00264 
00265         pZonePtrs[i] = new pointZone
00266         (
00267             pz.name(),
00268             subset(baseMesh().nPoints(), pz, pointMap()),
00269             i,
00270             fvMeshSubsetPtr_().pointZones()
00271         );
00272     }
00273 
00274 
00275     // FaceZones
00276 
00277     const faceZoneMesh& faceZones = baseMesh().faceZones();
00278 
00279 
00280     // Do we need to remove zones where the side we're interested in
00281     // no longer exists? Guess not.
00282     List<faceZone*> fZonePtrs(faceZones.size());
00283 
00284     forAll(faceZones, i)
00285     {
00286         const faceZone& fz = faceZones[i];
00287 
00288         // Create list of mesh faces part of the new zone
00289         labelList subAddressing
00290         (
00291             subset
00292             (
00293                 baseMesh().nFaces(),
00294                 fz,
00295                 faceMap()
00296             )
00297         );
00298 
00299         // Flipmap for all mesh faces
00300         boolList fullFlipStatus(baseMesh().nFaces(), false);
00301         forAll(fz, j)
00302         {
00303             fullFlipStatus[fz[j]] = fz.flipMap()[j];
00304         }
00305         // Extract sub part
00306         boolList subFlipStatus(subAddressing.size(), false);
00307         forAll(subAddressing, j)
00308         {
00309             subFlipStatus[j] = fullFlipStatus[faceMap()[subAddressing[j]]];
00310         }
00311 
00312         fZonePtrs[i] = new faceZone
00313         (
00314             fz.name(),
00315             subAddressing,
00316             subFlipStatus,
00317             i,
00318             fvMeshSubsetPtr_().faceZones()
00319         );
00320     }
00321 
00322 
00323     const cellZoneMesh& cellZones = baseMesh().cellZones();
00324 
00325     List<cellZone*> cZonePtrs(cellZones.size());
00326 
00327     forAll(cellZones, i)
00328     {
00329         const cellZone& cz = cellZones[i];
00330 
00331         cZonePtrs[i] = new cellZone
00332         (
00333             cz.name(),
00334             subset(baseMesh().nCells(), cz, cellMap()),
00335             i,
00336             fvMeshSubsetPtr_().cellZones()
00337         );
00338     }
00339 
00340 
00341     // Add the zones
00342     fvMeshSubsetPtr_().addZones(pZonePtrs, fZonePtrs, cZonePtrs);
00343 }
00344 
00345 
00346 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00347 
00348 // Construct from components
00349 Foam::fvMeshSubset::fvMeshSubset(const fvMesh& baseMesh)
00350 :
00351     baseMesh_(baseMesh),
00352     fvMeshSubsetPtr_(NULL),
00353     pointMap_(0),
00354     faceMap_(0),
00355     cellMap_(0),
00356     patchMap_(0)
00357 {}
00358 
00359 
00360 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00361 
00362 void Foam::fvMeshSubset::setCellSubset
00363 (
00364     const labelHashSet& globalCellMap,
00365     const label patchID
00366 )
00367 {
00368     // Initial check on patches before doing anything time consuming.
00369     const polyBoundaryMesh& oldPatches = baseMesh().boundaryMesh();
00370     const cellList& oldCells = baseMesh().cells();
00371     const faceList& oldFaces = baseMesh().faces();
00372     const pointField& oldPoints = baseMesh().points();
00373     const labelList& oldOwner = baseMesh().faceOwner();
00374     const labelList& oldNeighbour = baseMesh().faceNeighbour();
00375 
00376     label wantedPatchID = patchID;
00377 
00378     if (wantedPatchID == -1)
00379     {
00380         // No explicit patch specified. Put in oldInternalFaces patch.
00381         // Check if patch with this name already exists.
00382         wantedPatchID = oldPatches.findPatchID("oldInternalFaces");
00383     }
00384     else if (wantedPatchID < 0 || wantedPatchID >= oldPatches.size())
00385     {
00386         FatalErrorIn
00387         (
00388             "fvMeshSubset::setCellSubset(const labelHashSet&"
00389             ", const label patchID)"
00390         )   << "Non-existing patch index " << wantedPatchID << endl
00391             << "Should be between 0 and " << oldPatches.size()-1
00392             << abort(FatalError);
00393     }
00394 
00395 
00396     cellMap_ = globalCellMap.toc();
00397 
00398     // Sort the cell map in the ascending order
00399     sort(cellMap_);
00400 
00401     // Approximate sizing parameters for face and point lists
00402     const label avgNFacesPerCell = 6;
00403     const label avgNPointsPerFace = 4;
00404 
00405 
00406     label nCellsInSet = cellMap_.size();
00407 
00408     // Mark all used faces
00409 
00410     Map<label> facesToSubset(avgNFacesPerCell*nCellsInSet);
00411 
00412     forAll (cellMap_, cellI)
00413     {
00414         // Mark all faces from the cell
00415         const labelList& curFaces = oldCells[cellMap_[cellI]];
00416 
00417         forAll (curFaces, faceI)
00418         {
00419             if (!facesToSubset.found(curFaces[faceI]))
00420             {
00421                 facesToSubset.insert(curFaces[faceI], 1);
00422             }
00423             else
00424             {
00425                 facesToSubset[curFaces[faceI]]++;
00426             }
00427         }
00428     }
00429 
00430     // Mark all used points and make a global-to-local face map
00431     Map<label> globalFaceMap(facesToSubset.size());
00432 
00433     // Make a global-to-local point map
00434     Map<label> globalPointMap(avgNPointsPerFace*facesToSubset.size());
00435 
00436     // This is done in two goes, so that the boundary faces are last
00437     // in the list.  Because of this, I need to create the face map
00438     // along the way rather than just grab the table of contents.
00439     labelList facesToc = facesToSubset.toc();
00440     sort(facesToc);
00441     faceMap_.setSize(facesToc.size());
00442 
00443     // 1. Get all faces that will be internal to the submesh.
00444     forAll (facesToc, faceI)
00445     {
00446         if (facesToSubset[facesToc[faceI]] == 2)
00447         {
00448             // Mark face and increment number of points in set
00449             faceMap_[globalFaceMap.size()] = facesToc[faceI];
00450             globalFaceMap.insert(facesToc[faceI], globalFaceMap.size());
00451 
00452             // Mark all points from the face
00453             markPoints(oldFaces[facesToc[faceI]], globalPointMap);
00454         }
00455     }
00456 
00457     // These are all the internal faces in the mesh.
00458     label nInternalFaces = globalFaceMap.size();
00459 
00460 
00461     // Where to insert old internal faces.
00462     label oldPatchStart = labelMax;
00463     if (wantedPatchID != -1)
00464     {
00465         oldPatchStart = oldPatches[wantedPatchID].start();
00466     }
00467 
00468 
00469     label faceI = 0;
00470 
00471     // 2. Boundary faces up to where we want to insert old internal faces
00472     for (; faceI< facesToc.size(); faceI++)
00473     {
00474         if (facesToc[faceI] >= oldPatchStart)
00475         {
00476             break;
00477         }
00478         if
00479         (
00480             !baseMesh().isInternalFace(facesToc[faceI])
00481          && facesToSubset[facesToc[faceI]] == 1
00482         )
00483         {
00484             // Mark face and increment number of points in set
00485             faceMap_[globalFaceMap.size()] = facesToc[faceI];
00486             globalFaceMap.insert(facesToc[faceI], globalFaceMap.size());
00487 
00488             // Mark all points from the face
00489             markPoints(oldFaces[facesToc[faceI]], globalPointMap);
00490         }
00491     }
00492 
00493     // 3. old internal faces
00494     forAll(facesToc, intFaceI)
00495     {
00496         if
00497         (
00498             baseMesh().isInternalFace(facesToc[intFaceI])
00499          && facesToSubset[facesToc[intFaceI]] == 1
00500         )
00501         {
00502             // Mark face and increment number of points in set
00503             faceMap_[globalFaceMap.size()] = facesToc[intFaceI];
00504             globalFaceMap.insert(facesToc[intFaceI], globalFaceMap.size());
00505 
00506             // Mark all points from the face
00507             markPoints(oldFaces[facesToc[intFaceI]], globalPointMap);
00508         }
00509     }
00510 
00511     // 4. Remaining boundary faces
00512     for (; faceI< facesToc.size(); faceI++)
00513     {
00514         if
00515         (
00516             !baseMesh().isInternalFace(facesToc[faceI])
00517          && facesToSubset[facesToc[faceI]] == 1
00518         )
00519         {
00520             // Mark face and increment number of points in set
00521             faceMap_[globalFaceMap.size()] = facesToc[faceI];
00522             globalFaceMap.insert(facesToc[faceI], globalFaceMap.size());
00523 
00524             // Mark all points from the face
00525             markPoints(oldFaces[facesToc[faceI]], globalPointMap);
00526         }
00527     }
00528 
00529 
00530 
00531     // Grab the points map
00532     pointMap_ = globalPointMap.toc();
00533     sort(pointMap_);
00534 
00535     forAll (pointMap_, pointI)
00536     {
00537         globalPointMap[pointMap_[pointI]] = pointI;
00538     }
00539 
00540     Pout << "Number of cells in new mesh: " << nCellsInSet << endl;
00541     Pout << "Number of faces in new mesh: " << globalFaceMap.size() << endl;
00542     Pout << "Number of points in new mesh: " << globalPointMap.size() << endl;
00543 
00544     // Make a new mesh
00545     pointField newPoints(globalPointMap.size());
00546 
00547     label nNewPoints = 0;
00548 
00549     forAll (pointMap_, pointI)
00550     {
00551         newPoints[nNewPoints] = oldPoints[pointMap_[pointI]];
00552         nNewPoints++;
00553     }
00554 
00555     faceList newFaces(globalFaceMap.size());
00556 
00557     label nNewFaces = 0;
00558 
00559     // Make internal faces
00560     for (label faceI = 0; faceI < nInternalFaces; faceI++)
00561     {
00562         const face& oldF = oldFaces[faceMap_[faceI]];
00563 
00564         face newF(oldF.size());
00565 
00566         forAll (newF, i)
00567         {
00568             newF[i] = globalPointMap[oldF[i]];
00569         }
00570 
00571         newFaces[nNewFaces] = newF;
00572         nNewFaces++;
00573     }
00574 
00575     // Make boundary faces
00576 
00577     label nbSize = oldPatches.size();
00578     label oldInternalPatchID  = -1;
00579 
00580     if (wantedPatchID == -1)
00581     {
00582         // Create 'oldInternalFaces' patch at the end
00583         // and put all exposed internal faces in there.
00584         oldInternalPatchID = nbSize;
00585         nbSize++;
00586 
00587     }
00588     else
00589     {
00590         oldInternalPatchID = wantedPatchID;
00591     }
00592 
00593 
00594     // Grad size and start of each patch on the fly.  Because of the
00595     // structure of the underlying mesh, the patches will appear in the
00596     // ascending order
00597     labelList boundaryPatchSizes(nbSize, 0);
00598 
00599     // Assign boundary faces. Visited in order of faceMap_.
00600     for (label faceI = nInternalFaces; faceI < faceMap_.size(); faceI++)
00601     {
00602         label oldFaceI = faceMap_[faceI];
00603 
00604         face oldF = oldFaces[oldFaceI];
00605 
00606         // Turn the faces as necessary to point outwards
00607         if (baseMesh().isInternalFace(oldFaceI))
00608         {
00609             // Internal face. Possibly turned the wrong way round
00610             if
00611             (
00612                 !globalCellMap.found(oldOwner[oldFaceI])
00613              && globalCellMap.found(oldNeighbour[oldFaceI])
00614             )
00615             {
00616                 oldF = oldFaces[oldFaceI].reverseFace();
00617             }
00618 
00619             // Update count for patch
00620             boundaryPatchSizes[oldInternalPatchID]++;
00621         }
00622         else
00623         {
00624             // Boundary face. Increment the appropriate patch
00625             label patchOfFace = oldPatches.whichPatch(oldFaceI);
00626 
00627             // Update count for patch
00628             boundaryPatchSizes[patchOfFace]++;
00629         }
00630 
00631         face newF(oldF.size());
00632 
00633         forAll (newF, i)
00634         {
00635             newF[i] = globalPointMap[oldF[i]];
00636         }
00637 
00638         newFaces[nNewFaces] = newF;
00639         nNewFaces++;
00640     }
00641 
00642 
00643 
00644     // Create cells
00645     cellList newCells(nCellsInSet);
00646 
00647     label nNewCells = 0;
00648 
00649     forAll (cellMap_, cellI)
00650     {
00651         const labelList& oldC = oldCells[cellMap_[cellI]];
00652 
00653         labelList newC(oldC.size());
00654 
00655         forAll (newC, i)
00656         {
00657             newC[i] = globalFaceMap[oldC[i]];
00658         }
00659 
00660         newCells[nNewCells] = cell(newC);
00661         nNewCells++;
00662     }
00663 
00664 
00665     // Make a new mesh
00666     fvMeshSubsetPtr_.reset
00667     (
00668         new fvMesh
00669         (
00670             IOobject
00671             (
00672                 baseMesh().name() + "SubSet",
00673                 baseMesh().time().timeName(),
00674                 baseMesh().time(),
00675                 IOobject::NO_READ,
00676                 IOobject::NO_WRITE
00677             ),
00678             xferMove(newPoints),
00679             xferMove(newFaces),
00680             xferMove(newCells)
00681         )
00682     );
00683 
00684 
00685     // Add old patches
00686     List<polyPatch*> newBoundary(nbSize);
00687     patchMap_.setSize(nbSize);
00688     label nNewPatches = 0;
00689     label patchStart = nInternalFaces;
00690 
00691     forAll(oldPatches, patchI)
00692     {
00693         if (boundaryPatchSizes[patchI] > 0)
00694         {
00695             // Patch still exists. Add it
00696             newBoundary[nNewPatches] = oldPatches[patchI].clone
00697             (
00698                 fvMeshSubsetPtr_().boundaryMesh(),
00699                 nNewPatches,
00700                 boundaryPatchSizes[patchI],
00701                 patchStart
00702             ).ptr();
00703 
00704             patchStart += boundaryPatchSizes[patchI];
00705             patchMap_[nNewPatches] = patchI;
00706             nNewPatches++;
00707         }
00708     }
00709 
00710     if (wantedPatchID == -1)
00711     {
00712         // Newly created patch so is at end. Check if any faces in it.
00713         if (boundaryPatchSizes[oldInternalPatchID] > 0)
00714         {
00715             newBoundary[nNewPatches] = new emptyPolyPatch
00716             (
00717                 "oldInternalFaces",
00718                 boundaryPatchSizes[oldInternalPatchID],
00719                 patchStart,
00720                 nNewPatches,
00721                 fvMeshSubsetPtr_().boundaryMesh()
00722             );
00723 
00724             // The index for the first patch is -1 as it originates from
00725             // the internal faces
00726             patchMap_[nNewPatches] = -1;
00727             nNewPatches++;
00728         }
00729     }
00730 
00731     // Reset the patch lists
00732     newBoundary.setSize(nNewPatches);
00733     patchMap_.setSize(nNewPatches);
00734 
00735     // Add the fvPatches
00736     fvMeshSubsetPtr_().addFvPatches(newBoundary);
00737 
00738     // Subset and add any zones
00739     subsetZones();
00740 }
00741 
00742 
00743 void Foam::fvMeshSubset::setLargeCellSubset
00744 (
00745     const labelList& region,
00746     const label currentRegion,
00747     const label patchID,
00748     const bool syncPar
00749 )
00750 {
00751     const cellList& oldCells = baseMesh().cells();
00752     const faceList& oldFaces = baseMesh().faces();
00753     const pointField& oldPoints = baseMesh().points();
00754     const labelList& oldOwner = baseMesh().faceOwner();
00755     const labelList& oldNeighbour = baseMesh().faceNeighbour();
00756     const polyBoundaryMesh& oldPatches = baseMesh().boundaryMesh();
00757     const label oldNInternalFaces = baseMesh().nInternalFaces();
00758 
00759     // Initial checks
00760 
00761     if (region.size() != oldCells.size())
00762     {
00763         FatalErrorIn
00764         (
00765             "fvMeshSubset::setCellSubset(const labelList&"
00766             ", const label, const label, const bool)"
00767         )   << "Size of region " << region.size()
00768             << " is not equal to number of cells in mesh " << oldCells.size()
00769             << abort(FatalError);
00770     }
00771 
00772 
00773     label wantedPatchID = patchID;
00774 
00775     if (wantedPatchID == -1)
00776     {
00777         // No explicit patch specified. Put in oldInternalFaces patch.
00778         // Check if patch with this name already exists.
00779         wantedPatchID = oldPatches.findPatchID("oldInternalFaces");
00780     }
00781     else if (wantedPatchID < 0 || wantedPatchID >= oldPatches.size())
00782     {
00783         FatalErrorIn
00784         (
00785             "fvMeshSubset::setCellSubset(const labelList&"
00786             ", const label, const label, const bool)"
00787         )   << "Non-existing patch index " << wantedPatchID << endl
00788             << "Should be between 0 and " << oldPatches.size()-1
00789             << abort(FatalError);
00790     }
00791 
00792 
00793     // Get the cells for the current region.
00794     cellMap_.setSize(oldCells.size());
00795     label nCellsInSet = 0;
00796 
00797     forAll (region, oldCellI)
00798     {
00799         if (region[oldCellI] == currentRegion)
00800         {
00801             cellMap_[nCellsInSet++] = oldCellI;
00802         }
00803     }
00804     cellMap_.setSize(nCellsInSet);
00805 
00806 
00807     // Mark all used faces. Count number of cells using them
00808     // 0: face not used anymore
00809     // 1: face used by one cell, face becomes/stays boundary face
00810     // 2: face still used and remains internal face
00811     // 3: face coupled and used by one cell only (so should become normal,
00812     //    non-coupled patch face)
00813     //
00814     // Note that this is not really nessecary - but means we can size things
00815     // correctly. Also makes handling coupled faces much easier.
00816 
00817     labelList nCellsUsingFace(oldFaces.size(), 0);
00818 
00819     label nFacesInSet = 0;
00820     forAll (oldFaces, oldFaceI)
00821     {
00822         bool faceUsed = false;
00823 
00824         if (region[oldOwner[oldFaceI]] == currentRegion)
00825         {
00826             nCellsUsingFace[oldFaceI]++;
00827             faceUsed = true;
00828         }
00829 
00830         if
00831         (
00832             baseMesh().isInternalFace(oldFaceI)
00833          && (region[oldNeighbour[oldFaceI]] == currentRegion)
00834         )
00835         {
00836             nCellsUsingFace[oldFaceI]++;
00837             faceUsed = true;
00838         }
00839 
00840         if (faceUsed)
00841         {
00842             nFacesInSet++;
00843         }
00844     }
00845     faceMap_.setSize(nFacesInSet);
00846 
00847     // Handle coupled faces. Modifies patch faces to be uncoupled to 3.
00848     doCoupledPatches(syncPar, nCellsUsingFace);
00849 
00850 
00851     // See which patch to use for exposed internal faces.
00852     label oldInternalPatchID = 0;
00853 
00854     // Insert faces before which patch
00855     label nextPatchID = oldPatches.size();
00856 
00857     // old to new patches
00858     labelList globalPatchMap(oldPatches.size());
00859 
00860     // New patch size
00861     label nbSize = oldPatches.size();
00862 
00863     if (wantedPatchID == -1)
00864     {
00865         // Create 'oldInternalFaces' patch at the end (or before
00866         // processorPatches)
00867         // and put all exposed internal faces in there.
00868 
00869         forAll(oldPatches, patchI)
00870         {
00871             if (isA<processorPolyPatch>(oldPatches[patchI]))
00872             {
00873                 nextPatchID = patchI;
00874                 break;
00875             }
00876             oldInternalPatchID++;
00877         }
00878 
00879         nbSize++;
00880 
00881         // adapt old to new patches for inserted patch
00882         for (label oldPatchI = 0; oldPatchI < nextPatchID; oldPatchI++)
00883         {
00884             globalPatchMap[oldPatchI] = oldPatchI;
00885         }
00886         for
00887         (
00888             label oldPatchI = nextPatchID;
00889             oldPatchI < oldPatches.size();
00890             oldPatchI++
00891         )
00892         {
00893             globalPatchMap[oldPatchI] = oldPatchI+1;
00894         }
00895     }
00896     else
00897     {
00898         oldInternalPatchID = wantedPatchID;
00899         nextPatchID = wantedPatchID+1;
00900 
00901         // old to new patches
00902         globalPatchMap = identity(oldPatches.size());
00903     }
00904 
00905     labelList boundaryPatchSizes(nbSize, 0);
00906 
00907 
00908     // Make a global-to-local point map
00909     labelList globalPointMap(oldPoints.size(), -1);
00910 
00911     labelList globalFaceMap(oldFaces.size(), -1);
00912     label faceI = 0;
00913 
00914     // 1. Pick up all preserved internal faces.
00915     for (label oldFaceI = 0; oldFaceI < oldNInternalFaces; oldFaceI++)
00916     {
00917         if (nCellsUsingFace[oldFaceI] == 2)
00918         {
00919             globalFaceMap[oldFaceI] = faceI;
00920             faceMap_[faceI++] = oldFaceI;
00921 
00922             // Mark all points from the face
00923             markPoints(oldFaces[oldFaceI], globalPointMap);
00924         }
00925     }
00926 
00927     // These are all the internal faces in the mesh.
00928     label nInternalFaces = faceI;
00929 
00930     // 2. Boundary faces up to where we want to insert old internal faces
00931     for
00932     (
00933         label oldPatchI = 0;
00934         oldPatchI < oldPatches.size()
00935      && oldPatchI < nextPatchID;
00936         oldPatchI++
00937     )
00938     {
00939         const polyPatch& oldPatch = oldPatches[oldPatchI];
00940 
00941         label oldFaceI = oldPatch.start();
00942 
00943         forAll (oldPatch, i)
00944         {
00945             if (nCellsUsingFace[oldFaceI] == 1)
00946             {
00947                 // Boundary face is kept.
00948 
00949                 // Mark face and increment number of points in set
00950                 globalFaceMap[oldFaceI] = faceI;
00951                 faceMap_[faceI++] = oldFaceI;
00952 
00953                 // Mark all points from the face
00954                 markPoints(oldFaces[oldFaceI], globalPointMap);
00955 
00956                 // Increment number of patch faces
00957                 boundaryPatchSizes[globalPatchMap[oldPatchI]]++;
00958             }
00959             oldFaceI++;
00960         }
00961     }
00962 
00963     // 3a. old internal faces that have become exposed.
00964     for (label oldFaceI = 0; oldFaceI < oldNInternalFaces; oldFaceI++)
00965     {
00966         if (nCellsUsingFace[oldFaceI] == 1)
00967         {
00968             globalFaceMap[oldFaceI] = faceI;
00969             faceMap_[faceI++] = oldFaceI;
00970 
00971             // Mark all points from the face
00972             markPoints(oldFaces[oldFaceI], globalPointMap);
00973 
00974             // Increment number of patch faces
00975             boundaryPatchSizes[oldInternalPatchID]++;
00976         }
00977     }
00978 
00979     // 3b. coupled patch faces that have become uncoupled.
00980     for
00981     (
00982         label oldFaceI = oldNInternalFaces;
00983         oldFaceI < oldFaces.size();
00984         oldFaceI++
00985     )
00986     {
00987         if (nCellsUsingFace[oldFaceI] == 3)
00988         {
00989             globalFaceMap[oldFaceI] = faceI;
00990             faceMap_[faceI++] = oldFaceI;
00991 
00992             // Mark all points from the face
00993             markPoints(oldFaces[oldFaceI], globalPointMap);
00994 
00995             // Increment number of patch faces
00996             boundaryPatchSizes[oldInternalPatchID]++;
00997         }
00998     }
00999 
01000     // 4. Remaining boundary faces
01001     for
01002     (
01003         label oldPatchI = nextPatchID;
01004         oldPatchI < oldPatches.size();
01005         oldPatchI++
01006     )
01007     {
01008         const polyPatch& oldPatch = oldPatches[oldPatchI];
01009 
01010         label oldFaceI = oldPatch.start();
01011 
01012         forAll (oldPatch, i)
01013         {
01014             if (nCellsUsingFace[oldFaceI] == 1)
01015             {
01016                 // Boundary face is kept.
01017 
01018                 // Mark face and increment number of points in set
01019                 globalFaceMap[oldFaceI] = faceI;
01020                 faceMap_[faceI++] = oldFaceI;
01021 
01022                 // Mark all points from the face
01023                 markPoints(oldFaces[oldFaceI], globalPointMap);
01024 
01025                 // Increment number of patch faces
01026                 boundaryPatchSizes[globalPatchMap[oldPatchI]]++;
01027             }
01028             oldFaceI++;
01029         }
01030     }
01031 
01032     if (faceI != nFacesInSet)
01033     {
01034         FatalErrorIn
01035         (
01036             "fvMeshSubset::setCellSubset(const labelList&"
01037             ", const label, const label, const bool)"
01038         )   << "Problem" << abort(FatalError);
01039     }
01040 
01041 
01042     // Grab the points map
01043     label nPointsInSet = 0;
01044 
01045     forAll(globalPointMap, pointI)
01046     {
01047         if (globalPointMap[pointI] != -1)
01048         {
01049             nPointsInSet++;
01050         }
01051     }
01052     pointMap_.setSize(nPointsInSet);
01053 
01054     nPointsInSet = 0;
01055 
01056     forAll(globalPointMap, pointI)
01057     {
01058         if (globalPointMap[pointI] != -1)
01059         {
01060             pointMap_[nPointsInSet] = pointI;
01061             globalPointMap[pointI] = nPointsInSet;
01062             nPointsInSet++;
01063         }
01064     }
01065 
01066     //Pout<< "Number of cells in new mesh : " << cellMap_.size() << endl;
01067     //Pout<< "Number of faces in new mesh : " << faceMap_.size() << endl;
01068     //Pout<< "Number of points in new mesh: " << pointMap_.size() << endl;
01069 
01070     // Make a new mesh
01071     pointField newPoints(pointMap_.size());
01072 
01073     label nNewPoints = 0;
01074 
01075     forAll (pointMap_, pointI)
01076     {
01077         newPoints[nNewPoints] = oldPoints[pointMap_[pointI]];
01078         nNewPoints++;
01079     }
01080 
01081     faceList newFaces(faceMap_.size());
01082 
01083     label nNewFaces = 0;
01084 
01085     // Make internal faces
01086     for (label faceI = 0; faceI < nInternalFaces; faceI++)
01087     {
01088         const face& oldF = oldFaces[faceMap_[faceI]];
01089 
01090         face newF(oldF.size());
01091 
01092         forAll (newF, i)
01093         {
01094             newF[i] = globalPointMap[oldF[i]];
01095         }
01096 
01097         newFaces[nNewFaces] = newF;
01098         nNewFaces++;
01099     }
01100 
01101 
01102     // Make boundary faces. (different from internal since might need to be
01103     // flipped)
01104     for (label faceI = nInternalFaces; faceI < faceMap_.size(); faceI++)
01105     {
01106         label oldFaceI = faceMap_[faceI];
01107 
01108         face oldF = oldFaces[oldFaceI];
01109 
01110         // Turn the faces as necessary to point outwards
01111         if (baseMesh().isInternalFace(oldFaceI))
01112         {
01113             // Was internal face. Possibly turned the wrong way round
01114             if
01115             (
01116                 region[oldOwner[oldFaceI]] != currentRegion
01117              && region[oldNeighbour[oldFaceI]] == currentRegion
01118             )
01119             {
01120                 oldF = oldFaces[oldFaceI].reverseFace();
01121             }
01122         }
01123 
01124         // Relabel vertices of the (possibly turned) face.
01125         face newF(oldF.size());
01126 
01127         forAll (newF, i)
01128         {
01129             newF[i] = globalPointMap[oldF[i]];
01130         }
01131 
01132         newFaces[nNewFaces] = newF;
01133         nNewFaces++;
01134     }
01135 
01136 
01137 
01138     // Create cells
01139     cellList newCells(nCellsInSet);
01140 
01141     label nNewCells = 0;
01142 
01143     forAll (cellMap_, cellI)
01144     {
01145         const labelList& oldC = oldCells[cellMap_[cellI]];
01146 
01147         labelList newC(oldC.size());
01148 
01149         forAll (newC, i)
01150         {
01151             newC[i] = globalFaceMap[oldC[i]];
01152         }
01153 
01154         newCells[nNewCells] = cell(newC);
01155         nNewCells++;
01156     }
01157 
01158 
01159     // Make a new mesh
01160     // Note that mesh gets registered with same name as original mesh. This is
01161     // not proper but cannot be avoided since otherwise surfaceInterpolation
01162     // cannot find its fvSchemes (it will try to read e.g.
01163     // system/region0SubSet/fvSchemes)
01164     fvMeshSubsetPtr_.reset
01165     (
01166         new fvMesh
01167         (
01168             IOobject
01169             (
01170                 baseMesh().name(),
01171                 baseMesh().time().timeName(),
01172                 baseMesh().time(),
01173                 IOobject::NO_READ,
01174                 IOobject::NO_WRITE
01175             ),
01176             xferMove(newPoints),
01177             xferMove(newFaces),
01178             xferMove(newCells),
01179             syncPar           // parallel synchronisation
01180         )
01181     );
01182 
01183     // Add old patches
01184     List<polyPatch*> newBoundary(nbSize);
01185     patchMap_.setSize(nbSize);
01186     label nNewPatches = 0;
01187     label patchStart = nInternalFaces;
01188 
01189 
01190     // For parallel: only remove patch if none of the processors has it.
01191     // This only gets done for patches before the one being inserted
01192     // (so patches < nextPatchID)
01193 
01194     // Get sum of patch sizes. Zero if patch can be deleted.
01195     labelList globalPatchSizes(boundaryPatchSizes);
01196     globalPatchSizes.setSize(nextPatchID);
01197 
01198     if (syncPar && Pstream::parRun())
01199     {
01200         // Get patch names (up to nextPatchID)
01201         List<wordList> patchNames(Pstream::nProcs());
01202         patchNames[Pstream::myProcNo()] = oldPatches.names();
01203         patchNames[Pstream::myProcNo()].setSize(nextPatchID);
01204         Pstream::gatherList(patchNames);
01205         Pstream::scatterList(patchNames);
01206 
01207         // Get patch sizes (up to nextPatchID).
01208         // Note that up to nextPatchID the globalPatchMap is an identity so
01209         // no need to index through that.
01210         Pstream::listCombineGather(globalPatchSizes, plusEqOp<label>());
01211         Pstream::listCombineScatter(globalPatchSizes);
01212 
01213         // Now all processors have all the patchnames.
01214         // Decide: if all processors have the same patch names and size is zero
01215         // everywhere remove the patch.
01216         bool samePatches = true;
01217 
01218         for (label procI = 1; procI < patchNames.size(); procI++)
01219         {
01220             if (patchNames[procI] != patchNames[0])
01221             {
01222                 samePatches = false;
01223                 break;
01224             }
01225         }
01226 
01227         if (!samePatches)
01228         {
01229             // Patchnames not sync on all processors so disable removal of
01230             // zero sized patches.
01231             globalPatchSizes = labelMax;
01232         }
01233     }
01234 
01235 
01236     // Old patches
01237 
01238     for
01239     (
01240         label oldPatchI = 0;
01241         oldPatchI < oldPatches.size()
01242      && oldPatchI < nextPatchID;
01243         oldPatchI++
01244     )
01245     {
01246         label newSize = boundaryPatchSizes[globalPatchMap[oldPatchI]];
01247 
01248         // Clone (even if 0 size)
01249         newBoundary[nNewPatches] = oldPatches[oldPatchI].clone
01250         (
01251             fvMeshSubsetPtr_().boundaryMesh(),
01252             nNewPatches,
01253             newSize,
01254             patchStart
01255         ).ptr();
01256 
01257         patchStart += newSize;
01258         patchMap_[nNewPatches] = oldPatchI;    // compact patchMap
01259         nNewPatches++;
01260     }
01261 
01262     // Inserted patch
01263 
01264     if (wantedPatchID == -1)
01265     {
01266         label oldInternalSize = boundaryPatchSizes[oldInternalPatchID];
01267 
01268         if (syncPar)
01269         {
01270             reduce(oldInternalSize, sumOp<label>());
01271         }
01272 
01273         // Newly created patch so is at end. Check if any faces in it.
01274         if (oldInternalSize > 0)
01275         {
01276             newBoundary[nNewPatches] = new emptyPolyPatch
01277             (
01278                 "oldInternalFaces",
01279                 boundaryPatchSizes[oldInternalPatchID],
01280                 patchStart,
01281                 nNewPatches,
01282                 fvMeshSubsetPtr_().boundaryMesh()
01283             );
01284 
01285             //Pout<< "    oldInternalFaces : "
01286             //    << boundaryPatchSizes[oldInternalPatchID] << endl;
01287 
01288             // The index for the first patch is -1 as it originates from
01289             // the internal faces
01290             patchStart += boundaryPatchSizes[oldInternalPatchID];
01291             patchMap_[nNewPatches] = -1;
01292             nNewPatches++;
01293         }
01294     }
01295 
01296     // Old patches
01297 
01298     for
01299     (
01300         label oldPatchI = nextPatchID;
01301         oldPatchI < oldPatches.size();
01302         oldPatchI++
01303     )
01304     {
01305         label newSize = boundaryPatchSizes[globalPatchMap[oldPatchI]];
01306 
01307         // Patch still exists. Add it
01308         newBoundary[nNewPatches] = oldPatches[oldPatchI].clone
01309         (
01310             fvMeshSubsetPtr_().boundaryMesh(),
01311             nNewPatches,
01312             newSize,
01313             patchStart
01314         ).ptr();
01315 
01316         //Pout<< "    " << oldPatches[oldPatchI].name() << " : "
01317         //    << newSize << endl;
01318 
01319         patchStart += newSize;
01320         patchMap_[nNewPatches] = oldPatchI;    // compact patchMap
01321         nNewPatches++;
01322     }
01323 
01324 
01325     // Reset the patch lists
01326     newBoundary.setSize(nNewPatches);
01327     patchMap_.setSize(nNewPatches);
01328 
01329 
01330     // Add the fvPatches
01331     fvMeshSubsetPtr_().addFvPatches(newBoundary, syncPar);
01332 
01333     // Subset and add any zones
01334     subsetZones();
01335 }
01336 
01337 
01338 void Foam::fvMeshSubset::setLargeCellSubset
01339 (
01340     const labelHashSet& globalCellMap,
01341     const label patchID,
01342     const bool syncPar
01343 )
01344 {
01345     labelList region(baseMesh().nCells(), 0);
01346 
01347     forAllConstIter (labelHashSet, globalCellMap, iter)
01348     {
01349         region[iter.key()] = 1;
01350     }
01351     setLargeCellSubset(region, 1, patchID, syncPar);
01352 }
01353 
01354 
01355 const fvMesh& Foam::fvMeshSubset::subMesh() const
01356 {
01357     checkCellSubset();
01358 
01359     return fvMeshSubsetPtr_();
01360 }
01361 
01362 
01363 fvMesh& Foam::fvMeshSubset::subMesh()
01364 {
01365     checkCellSubset();
01366 
01367     return fvMeshSubsetPtr_();
01368 }
01369 
01370 
01371 const labelList& Foam::fvMeshSubset::pointMap() const
01372 {
01373     checkCellSubset();
01374 
01375     return pointMap_;
01376 }
01377 
01378 
01379 const labelList& Foam::fvMeshSubset::faceMap() const
01380 {
01381     checkCellSubset();
01382 
01383     return faceMap_;
01384 }
01385 
01386 
01387 const labelList& Foam::fvMeshSubset::cellMap() const
01388 {
01389     checkCellSubset();
01390 
01391     return cellMap_;
01392 }
01393 
01394 
01395 const labelList& Foam::fvMeshSubset::patchMap() const
01396 {
01397     checkCellSubset();
01398 
01399     return patchMap_;
01400 }
01401 
01402 
01403 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
01404 
01405 } // End namespace Foam
01406 
01407 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd