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

meshRefinementBaffles.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 "meshRefinement.H"
00028 #include "fvMesh.H"
00029 #include "syncTools.H"
00030 #include "Time.H"
00031 #include "refinementSurfaces.H"
00032 #include "pointSet.H"
00033 #include "faceSet.H"
00034 #include "indirectPrimitivePatch.H"
00035 #include "polyTopoChange.H"
00036 #include "meshTools.H"
00037 #include "polyModifyFace.H"
00038 #include "polyModifyCell.H"
00039 #include "polyAddFace.H"
00040 #include "polyRemoveFace.H"
00041 #include "polyAddPoint.H"
00042 #include "localPointRegion.H"
00043 #include "duplicatePoints.H"
00044 #include "OFstream.H"
00045 #include "regionSplit.H"
00046 #include "removeCells.H"
00047 
00048 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00049 
00050 // Repatches external face or creates baffle for internal face
00051 // with user specified patches (might be different for both sides).
00052 // Returns label of added face.
00053 Foam::label Foam::meshRefinement::createBaffle
00054 (
00055     const label faceI,
00056     const label ownPatch,
00057     const label neiPatch,
00058     polyTopoChange& meshMod
00059 ) const
00060 {
00061     const face& f = mesh_.faces()[faceI];
00062     label zoneID = mesh_.faceZones().whichZone(faceI);
00063     bool zoneFlip = false;
00064 
00065     if (zoneID >= 0)
00066     {
00067         const faceZone& fZone = mesh_.faceZones()[zoneID];
00068         zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
00069     }
00070 
00071     meshMod.setAction
00072     (
00073         polyModifyFace
00074         (
00075             f,                          // modified face
00076             faceI,                      // label of face
00077             mesh_.faceOwner()[faceI],   // owner
00078             -1,                         // neighbour
00079             false,                      // face flip
00080             ownPatch,                   // patch for face
00081             false,                      // remove from zone
00082             zoneID,                     // zone for face
00083             zoneFlip                    // face flip in zone
00084         )
00085     );
00086 
00087 
00088     label dupFaceI = -1;
00089 
00090     if (mesh_.isInternalFace(faceI))
00091     {
00092         if (neiPatch == -1)
00093         {
00094             FatalErrorIn
00095             (
00096                 "meshRefinement::createBaffle"
00097                 "(const label, const label, const label, polyTopoChange&)"
00098             )   << "No neighbour patch for internal face " << faceI
00099                 << " fc:" << mesh_.faceCentres()[faceI]
00100                 << " ownPatch:" << ownPatch << abort(FatalError);
00101         }
00102 
00103         bool reverseFlip = false;
00104         if (zoneID >= 0)
00105         {
00106             reverseFlip = !zoneFlip;
00107         }
00108 
00109         dupFaceI = meshMod.setAction
00110         (
00111             polyAddFace
00112             (
00113                 f.reverseFace(),            // modified face
00114                 mesh_.faceNeighbour()[faceI],// owner
00115                 -1,                         // neighbour
00116                 -1,                         // masterPointID
00117                 -1,                         // masterEdgeID
00118                 faceI,                      // masterFaceID,
00119                 true,                       // face flip
00120                 neiPatch,                   // patch for face
00121                 zoneID,                     // zone for face
00122                 reverseFlip                 // face flip in zone
00123             )
00124         );
00125     }
00126     return dupFaceI;
00127 }
00128 
00129 
00130 // Get an estimate for the patch the internal face should get. Bit heuristic.
00131 Foam::label Foam::meshRefinement::getBafflePatch
00132 (
00133     const labelList& facePatch,
00134     const label faceI
00135 ) const
00136 {
00137     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00138 
00139     // Loop over face points
00140     // for each point check all faces patch IDs
00141     // as soon as an ID >= 0 is found, break and assign that ID
00142     // to the current face.
00143     // Check first for real patch (so proper surface intersection and then
00144     // in facePatch array for patches to block off faces
00145 
00146     forAll(mesh_.faces()[faceI], fp)
00147     {
00148         label pointI = mesh_.faces()[faceI][fp];
00149 
00150         forAll(mesh_.pointFaces()[pointI], pf)
00151         {
00152             label pFaceI = mesh_.pointFaces()[pointI][pf];
00153 
00154             label patchI = patches.whichPatch(pFaceI);
00155 
00156             if (patchI != -1 && !patches[patchI].coupled())
00157             {
00158                 return patchI;
00159             }
00160             else if (facePatch[pFaceI] != -1)
00161             {
00162                 return facePatch[pFaceI];
00163             }
00164         }
00165     }
00166 
00167     // Loop over owner and neighbour cells, looking for the first face with a
00168     // valid patch number
00169     const cell& ownFaces = mesh_.cells()[mesh_.faceOwner()[faceI]];
00170 
00171     forAll(ownFaces, i)
00172     {
00173         label cFaceI = ownFaces[i];
00174 
00175         label patchI = patches.whichPatch(cFaceI);
00176 
00177         if (patchI != -1 && !patches[patchI].coupled())
00178         {
00179             return patchI;
00180         }
00181         else if (facePatch[cFaceI] != -1)
00182         {
00183             return facePatch[cFaceI];
00184         }
00185     }
00186 
00187     if (mesh_.isInternalFace(faceI))
00188     {
00189         const cell& neiFaces = mesh_.cells()[mesh_.faceNeighbour()[faceI]];
00190 
00191         forAll(neiFaces, i)
00192         {
00193             label cFaceI = neiFaces[i];
00194 
00195             label patchI = patches.whichPatch(cFaceI);
00196 
00197             if (patchI != -1 && !patches[patchI].coupled())
00198             {
00199                 return patchI;
00200             }
00201             else if (facePatch[cFaceI] != -1)
00202             {
00203                 return facePatch[cFaceI];
00204             }
00205         }
00206     }
00207 
00208     WarningIn
00209     (
00210         "meshRefinement::getBafflePatch(const labelList&, const label)"
00211     )   << "Could not find boundary face neighbouring internal face "
00212         << faceI << " with face centre " << mesh_.faceCentres()[faceI]
00213         << nl
00214         << "Using arbitrary patch " << 0 << " instead." << endl;
00215 
00216     return 0;
00217 }
00218 
00219 
00220 // Determine patches for baffles.
00221 void Foam::meshRefinement::getBafflePatches
00222 (
00223     const labelList& globalToPatch,
00224     const labelList& neiLevel,
00225     const pointField& neiCc,
00226 
00227     labelList& ownPatch,
00228     labelList& neiPatch
00229 ) const
00230 {
00231     autoPtr<OFstream> str;
00232     label vertI = 0;
00233     if (debug&OBJINTERSECTIONS)
00234     {
00235         str.reset
00236         (
00237             new OFstream
00238             (
00239                 mesh_.time().path()/timeName()/"intersections.obj"
00240             )
00241         );
00242 
00243         Pout<< "getBafflePatches : Writing surface intersections to file "
00244             << str().name() << nl << endl;
00245     }
00246 
00247     const pointField& cellCentres = mesh_.cellCentres();
00248 
00249     // Build list of surfaces that are not to be baffled.
00250     const wordList& cellZoneNames = surfaces_.cellZoneNames();
00251 
00252     labelList surfacesToBaffle(cellZoneNames.size());
00253     label baffleI = 0;
00254     forAll(cellZoneNames, surfI)
00255     {
00256         if (cellZoneNames[surfI].size())
00257         {
00258             if (debug)
00259             {
00260                 Pout<< "getBafflePatches : Not baffling surface "
00261                     << surfaces_.names()[surfI] << endl;
00262             }
00263         }
00264         else
00265         {
00266             surfacesToBaffle[baffleI++] = surfI;
00267         }
00268     }
00269     surfacesToBaffle.setSize(baffleI);
00270 
00271     ownPatch.setSize(mesh_.nFaces());
00272     ownPatch = -1;
00273     neiPatch.setSize(mesh_.nFaces());
00274     neiPatch = -1;
00275 
00276 
00277     // Collect candidate faces
00278     // ~~~~~~~~~~~~~~~~~~~~~~~
00279 
00280     labelList testFaces(intersectedFaces());
00281 
00282     // Collect segments
00283     // ~~~~~~~~~~~~~~~~
00284 
00285     pointField start(testFaces.size());
00286     pointField end(testFaces.size());
00287 
00288     forAll(testFaces, i)
00289     {
00290         label faceI = testFaces[i];
00291 
00292         label own = mesh_.faceOwner()[faceI];
00293 
00294         if (mesh_.isInternalFace(faceI))
00295         {
00296             start[i] = cellCentres[own];
00297             end[i] = cellCentres[mesh_.faceNeighbour()[faceI]];
00298         }
00299         else
00300         {
00301             start[i] = cellCentres[own];
00302             end[i] = neiCc[faceI-mesh_.nInternalFaces()];
00303         }
00304     }
00305 
00306 
00307     // Do test for intersections
00308     // ~~~~~~~~~~~~~~~~~~~~~~~~~
00309     labelList surface1;
00310     List<pointIndexHit> hit1;
00311     labelList region1;
00312     labelList surface2;
00313     List<pointIndexHit> hit2;
00314     labelList region2;
00315     surfaces_.findNearestIntersection
00316     (
00317         surfacesToBaffle,
00318         start,
00319         end,
00320 
00321         surface1,
00322         hit1,
00323         region1,
00324 
00325         surface2,
00326         hit2,
00327         region2
00328     );
00329 
00330     forAll(testFaces, i)
00331     {
00332         label faceI = testFaces[i];
00333 
00334         if (hit1[i].hit() && hit2[i].hit())
00335         {
00336             if (str.valid())
00337             {
00338                 meshTools::writeOBJ(str(), start[i]);
00339                 vertI++;
00340                 meshTools::writeOBJ(str(), hit1[i].rawPoint());
00341                 vertI++;
00342                 meshTools::writeOBJ(str(), hit2[i].rawPoint());
00343                 vertI++;
00344                 meshTools::writeOBJ(str(), end[i]);
00345                 vertI++;
00346                 str()<< "l " << vertI-3 << ' ' << vertI-2 << nl;
00347                 str()<< "l " << vertI-2 << ' ' << vertI-1 << nl;
00348                 str()<< "l " << vertI-1 << ' ' << vertI << nl;
00349             }
00350 
00351             // Pick up the patches
00352             ownPatch[faceI] = globalToPatch
00353             [
00354                 surfaces_.globalRegion(surface1[i], region1[i])
00355             ];
00356             neiPatch[faceI] = globalToPatch
00357             [
00358                 surfaces_.globalRegion(surface2[i], region2[i])
00359             ];
00360 
00361             if (ownPatch[faceI] == -1 || neiPatch[faceI] == -1)
00362             {
00363                 FatalErrorIn("getBafflePatches(..)")
00364                     << "problem." << abort(FatalError);
00365             }
00366         }
00367     }
00368 
00369     // No need to parallel sync since intersection data (surfaceIndex_ etc.)
00370     // already guaranteed to be synced...
00371     // However:
00372     // - owncc and neicc are reversed on different procs so might pick
00373     //   up different regions reversed? No problem. Neighbour on one processor
00374     //   might not be owner on the other processor but the neighbour is
00375     //   not used when creating baffles from proc faces.
00376     // - tolerances issues occasionally crop up.
00377     syncTools::syncFaceList(mesh_, ownPatch, maxEqOp<label>(), false);
00378     syncTools::syncFaceList(mesh_, neiPatch, maxEqOp<label>(), false);
00379 }
00380 
00381 
00382 // Create baffle for every face where ownPatch != -1
00383 Foam::autoPtr<Foam::mapPolyMesh> Foam::meshRefinement::createBaffles
00384 (
00385     const labelList& ownPatch,
00386     const labelList& neiPatch
00387 )
00388 {
00389     if
00390     (
00391         ownPatch.size() != mesh_.nFaces()
00392      || neiPatch.size() != mesh_.nFaces()
00393     )
00394     {
00395         FatalErrorIn
00396         (
00397             "meshRefinement::createBaffles"
00398             "(const labelList&, const labelList&)"
00399         )   << "Illegal size :"
00400             << " ownPatch:" << ownPatch.size()
00401             << " neiPatch:" << neiPatch.size()
00402             << ". Should be number of faces:" << mesh_.nFaces()
00403             << abort(FatalError);
00404     }
00405 
00406     if (debug)
00407     {
00408         labelList syncedOwnPatch(ownPatch);
00409         syncTools::syncFaceList(mesh_, syncedOwnPatch, maxEqOp<label>(), false);
00410         labelList syncedNeiPatch(neiPatch);
00411         syncTools::syncFaceList(mesh_, syncedNeiPatch, maxEqOp<label>(), false);
00412 
00413         forAll(syncedOwnPatch, faceI)
00414         {
00415             if
00416             (
00417                 (ownPatch[faceI] == -1 && syncedOwnPatch[faceI] != -1)
00418              || (neiPatch[faceI] == -1 && syncedNeiPatch[faceI] != -1)
00419             )
00420             {
00421                 FatalErrorIn
00422                 (
00423                     "meshRefinement::createBaffles"
00424                     "(const labelList&, const labelList&)"
00425                 )   << "Non synchronised at face:" << faceI
00426                     << " on patch:" << mesh_.boundaryMesh().whichPatch(faceI)
00427                     << " fc:" << mesh_.faceCentres()[faceI] << endl
00428                     << "ownPatch:" << ownPatch[faceI]
00429                     << " syncedOwnPatch:" << syncedOwnPatch[faceI]
00430                     << " neiPatch:" << neiPatch[faceI]
00431                     << " syncedNeiPatch:" << syncedNeiPatch[faceI]
00432                     << abort(FatalError);
00433             }
00434         }
00435     }
00436 
00437     // Topochange container
00438     polyTopoChange meshMod(mesh_);
00439 
00440     label nBaffles = 0;
00441 
00442     forAll(ownPatch, faceI)
00443     {
00444         if (ownPatch[faceI] != -1)
00445         {
00446             // Create baffle or repatch face. Return label of inserted baffle
00447             // face.
00448             createBaffle
00449             (
00450                 faceI,
00451                 ownPatch[faceI],   // owner side patch
00452                 neiPatch[faceI],   // neighbour side patch
00453                 meshMod
00454             );
00455             nBaffles++;
00456         }
00457     }
00458 
00459     // Change the mesh (no inflation, parallel sync)
00460     autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh_, false, true);
00461 
00462     // Update fields
00463     mesh_.updateMesh(map);
00464 
00465     // Move mesh if in inflation mode
00466     if (map().hasMotionPoints())
00467     {
00468         mesh_.movePoints(map().preMotionPoints());
00469     }
00470     else
00471     {
00472         // Delete mesh volumes.
00473         mesh_.clearOut();
00474     }
00475 
00476     if (overwrite())
00477     {
00478         mesh_.setInstance(oldInstance());
00479     }
00480 
00481     //- Redo the intersections on the newly create baffle faces. Note that
00482     //  this changes also the cell centre positions.
00483     faceSet baffledFacesSet(mesh_, "baffledFacesSet", 2*nBaffles);
00484 
00485     const labelList& reverseFaceMap = map().reverseFaceMap();
00486     const labelList& faceMap = map().faceMap();
00487 
00488     // Pick up owner side of baffle
00489     forAll(ownPatch, oldFaceI)
00490     {
00491         label faceI = reverseFaceMap[oldFaceI];
00492 
00493         if (ownPatch[oldFaceI] != -1 && faceI >= 0)
00494         {
00495             const cell& ownFaces = mesh_.cells()[mesh_.faceOwner()[faceI]];
00496 
00497             forAll(ownFaces, i)
00498             {
00499                 baffledFacesSet.insert(ownFaces[i]);
00500             }
00501         }
00502     }
00503     // Pick up neighbour side of baffle (added faces)
00504     forAll(faceMap, faceI)
00505     {
00506         label oldFaceI = faceMap[faceI];
00507 
00508         if (oldFaceI >= 0 && reverseFaceMap[oldFaceI] != faceI)
00509         {
00510             const cell& ownFaces = mesh_.cells()[mesh_.faceOwner()[faceI]];
00511 
00512             forAll(ownFaces, i)
00513             {
00514                 baffledFacesSet.insert(ownFaces[i]);
00515             }
00516         }
00517     }
00518     baffledFacesSet.sync(mesh_);
00519 
00520     updateMesh(map, baffledFacesSet.toc());
00521 
00522     return map;
00523 }
00524 
00525 
00526 // Return a list of coupled face pairs, i.e. faces that use the same vertices.
00527 // (this information is recalculated instead of maintained since would be too
00528 // hard across splitMeshRegions).
00529 Foam::List<Foam::labelPair> Foam::meshRefinement::getDuplicateFaces
00530 (
00531     const labelList& testFaces
00532 ) const
00533 {
00534     labelList duplicateFace
00535     (
00536         localPointRegion::findDuplicateFaces
00537         (
00538             mesh_,
00539             testFaces
00540         )
00541     );
00542 
00543     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00544 
00545     // Convert into list of coupled face pairs (mesh face labels).
00546     List<labelPair> duplicateFaces(testFaces.size());
00547     label dupI = 0;
00548 
00549     forAll(duplicateFace, i)
00550     {
00551         label otherFaceI = duplicateFace[i];
00552 
00553         if (otherFaceI != -1 && i < otherFaceI)
00554         {
00555             label meshFace0 = testFaces[i];
00556             label patch0 = patches.whichPatch(meshFace0);
00557             label meshFace1 = testFaces[otherFaceI];
00558             label patch1 = patches.whichPatch(meshFace1);
00559 
00560             if
00561             (
00562                 (patch0 != -1 && isA<processorPolyPatch>(patches[patch0]))
00563              || (patch1 != -1 && isA<processorPolyPatch>(patches[patch1]))
00564             )
00565             {
00566                 FatalErrorIn
00567                 (
00568                     "meshRefinement::getDuplicateFaces"
00569                     "(const bool, const labelList&)"
00570                 )   << "One of two duplicate faces is on"
00571                     << " processorPolyPatch."
00572                     << "This is not allowed." << nl
00573                     << "Face:" << meshFace0
00574                     << " is on patch:" << patches[patch0].name()
00575                     << nl
00576                     << "Face:" << meshFace1
00577                     << " is on patch:" << patches[patch1].name()
00578                     << abort(FatalError);
00579             }
00580 
00581             duplicateFaces[dupI++] = labelPair(meshFace0, meshFace1);
00582         }
00583     }
00584     duplicateFaces.setSize(dupI);
00585 
00586     Info<< "getDuplicateFaces : found " << returnReduce(dupI, sumOp<label>())
00587         << " pairs of duplicate faces." << nl << endl;
00588 
00589 
00590     if (debug)
00591     {
00592         faceSet duplicateFaceSet(mesh_, "duplicateFaces", 2*dupI);
00593 
00594         forAll(duplicateFaces, i)
00595         {
00596             duplicateFaceSet.insert(duplicateFaces[i][0]);
00597             duplicateFaceSet.insert(duplicateFaces[i][1]);
00598         }
00599         Pout<< "Writing duplicate faces (baffles) to faceSet "
00600             << duplicateFaceSet.name() << nl << endl;
00601         duplicateFaceSet.write();
00602     }
00603 
00604     return duplicateFaces;
00605 }
00606 
00607 
00608 // Extract those baffles (duplicate) faces that are on the edge of a baffle
00609 // region. These are candidates for merging.
00610 // Done by counting the number of baffles faces per mesh edge. If edge
00611 // has 2 boundary faces and both are baffle faces it is the edge of a baffle
00612 // region.
00613 Foam::List<Foam::labelPair> Foam::meshRefinement::filterDuplicateFaces
00614 (
00615     const List<labelPair>& couples
00616 ) const
00617 {
00618     // Construct addressing engine for all duplicate faces (only one
00619     // for each pair)
00620 
00621     // Duplicate faces in mesh labels (first face of each pair only)
00622     // (reused later on to mark off filtered couples. see below)
00623     labelList duplicateFaces(couples.size());
00624 
00625 
00626     // All duplicate faces on edge of the patch are to be merged.
00627     // So we count for all edges of duplicate faces how many duplicate
00628     // faces use them.
00629     labelList nBafflesPerEdge(mesh_.nEdges(), 0);
00630 
00631 
00632 
00633     // Count number of boundary faces per edge
00634     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00635 
00636     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00637 
00638     forAll(patches, patchI)
00639     {
00640         const polyPatch& pp = patches[patchI];
00641 
00642         // Count number of boundary faces. Discard coupled boundary faces.
00643         if (!pp.coupled())
00644         {
00645             label faceI = pp.start();
00646 
00647             forAll(pp, i)
00648             {
00649                 const labelList& fEdges = mesh_.faceEdges(faceI);
00650 
00651                 forAll(fEdges, fEdgeI)
00652                 {
00653                     nBafflesPerEdge[fEdges[fEdgeI]]++;
00654                 }
00655                 faceI++;
00656             }
00657         }
00658     }
00659 
00660 
00661     DynamicList<label> fe0;
00662     DynamicList<label> fe1;
00663 
00664 
00665     // Count number of duplicate boundary faces per edge
00666     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00667 
00668     forAll(couples, i)
00669     {
00670         const labelList& fEdges0 = mesh_.faceEdges(couples[i].first(), fe0);
00671 
00672         forAll(fEdges0, fEdgeI)
00673         {
00674             nBafflesPerEdge[fEdges0[fEdgeI]] += 1000000;
00675         }
00676 
00677         const labelList& fEdges1 = mesh_.faceEdges(couples[i].second(), fe1);
00678 
00679         forAll(fEdges1, fEdgeI)
00680         {
00681             nBafflesPerEdge[fEdges1[fEdgeI]] += 1000000;
00682         }
00683     }
00684 
00685     // Add nBaffles on shared edges
00686     syncTools::syncEdgeList
00687     (
00688         mesh_,
00689         nBafflesPerEdge,
00690         plusEqOp<label>(),  // in-place add
00691         0,                  // initial value
00692         false               // no separation
00693     );
00694 
00695 
00696     // Baffles which are not next to other boundaries and baffles will have
00697     // value 2*1000000+2*1
00698 
00699     List<labelPair> filteredCouples(couples.size());
00700     label filterI = 0;
00701 
00702     forAll(couples, i)
00703     {
00704         const labelPair& couple = couples[i];
00705 
00706         if
00707         (
00708             patches.whichPatch(couple.first())
00709          == patches.whichPatch(couple.second())
00710         )
00711         {
00712             const labelList& fEdges = mesh_.faceEdges(couples[i].first());
00713 
00714             forAll(fEdges, fEdgeI)
00715             {
00716                 label edgeI = fEdges[fEdgeI];
00717 
00718                 if (nBafflesPerEdge[edgeI] == 2*1000000+2*1)
00719                 {
00720                     filteredCouples[filterI++] = couples[i];
00721                     break;
00722                 }
00723             }
00724         }
00725     }
00726     filteredCouples.setSize(filterI);
00727 
00728     //Info<< "filterDuplicateFaces : from "
00729     //    << returnReduce(couples.size(), sumOp<label>())
00730     //    << " down to "
00731     //    << returnReduce(filteredCouples.size(), sumOp<label>())
00732     //    << " baffles." << nl << endl;
00733 
00734     return filteredCouples;
00735 }
00736 
00737 
00738 // Merge baffles. Gets pairs of faces.
00739 Foam::autoPtr<Foam::mapPolyMesh> Foam::meshRefinement::mergeBaffles
00740 (
00741     const List<labelPair>& couples
00742 )
00743 {
00744     // Mesh change engine
00745     polyTopoChange meshMod(mesh_);
00746 
00747     const faceList& faces = mesh_.faces();
00748     const labelList& faceOwner = mesh_.faceOwner();
00749     const faceZoneMesh& faceZones = mesh_.faceZones();
00750 
00751     forAll(couples, i)
00752     {
00753         label face0 = couples[i].first();
00754         label face1 = couples[i].second();
00755 
00756         // face1 < 0 signals a coupled face that has been converted to baffle.
00757 
00758         label own0 = faceOwner[face0];
00759         label own1 = faceOwner[face1];
00760 
00761         if (face1 < 0 || own0 < own1)
00762         {
00763             // Use face0 as the new internal face.
00764             label zoneID = faceZones.whichZone(face0);
00765             bool zoneFlip = false;
00766 
00767             if (zoneID >= 0)
00768             {
00769                 const faceZone& fZone = faceZones[zoneID];
00770                 zoneFlip = fZone.flipMap()[fZone.whichFace(face0)];
00771             }
00772 
00773             label nei = (face1 < 0 ? -1 : own1);
00774 
00775             meshMod.setAction(polyRemoveFace(face1));
00776             meshMod.setAction
00777             (
00778                 polyModifyFace
00779                 (
00780                     faces[face0],           // modified face
00781                     face0,                  // label of face being modified
00782                     own0,                   // owner
00783                     nei,                    // neighbour
00784                     false,                  // face flip
00785                     -1,                     // patch for face
00786                     false,                  // remove from zone
00787                     zoneID,                 // zone for face
00788                     zoneFlip                // face flip in zone
00789                 )
00790             );
00791         }
00792         else
00793         {
00794             // Use face1 as the new internal face.
00795             label zoneID = faceZones.whichZone(face1);
00796             bool zoneFlip = false;
00797 
00798             if (zoneID >= 0)
00799             {
00800                 const faceZone& fZone = faceZones[zoneID];
00801                 zoneFlip = fZone.flipMap()[fZone.whichFace(face1)];
00802             }
00803 
00804             meshMod.setAction(polyRemoveFace(face0));
00805             meshMod.setAction
00806             (
00807                 polyModifyFace
00808                 (
00809                     faces[face1],           // modified face
00810                     face1,                  // label of face being modified
00811                     own1,                   // owner
00812                     own0,                   // neighbour
00813                     false,                  // face flip
00814                     -1,                     // patch for face
00815                     false,                  // remove from zone
00816                     zoneID,                 // zone for face
00817                     zoneFlip                // face flip in zone
00818                 )
00819             );
00820         }
00821     }
00822 
00823     // Change the mesh (no inflation)
00824     autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh_, false, true);
00825 
00826     // Update fields
00827     mesh_.updateMesh(map);
00828 
00829     // Move mesh (since morphing does not do this)
00830     if (map().hasMotionPoints())
00831     {
00832         mesh_.movePoints(map().preMotionPoints());
00833     }
00834     else
00835     {
00836         // Delete mesh volumes.
00837         mesh_.clearOut();
00838     }
00839 
00840     if (overwrite())
00841     {
00842         mesh_.setInstance(oldInstance());
00843     }
00844 
00845     // Update intersections. Recalculate intersections on merged faces since
00846     // this seems to give problems? Note: should not be nessecary since
00847     // baffles preserve intersections from when they were created.
00848     labelList newExposedFaces(2*couples.size());
00849     label newI = 0;
00850 
00851     forAll(couples, i)
00852     {
00853         label newFace0 = map().reverseFaceMap()[couples[i].first()];
00854         if (newFace0 != -1)
00855         {
00856             newExposedFaces[newI++] = newFace0;
00857         }
00858 
00859         label newFace1 = map().reverseFaceMap()[couples[i].second()];
00860         if (newFace1 != -1)
00861         {
00862             newExposedFaces[newI++] = newFace1;
00863         }
00864     }
00865     newExposedFaces.setSize(newI);
00866     updateMesh(map, newExposedFaces);
00867 
00868     return map;
00869 }
00870 
00871 
00872 // Finds region per cell for cells inside closed named surfaces
00873 void Foam::meshRefinement::findCellZoneGeometric
00874 (
00875     const labelList& closedNamedSurfaces,   // indices of closed surfaces
00876     labelList& namedSurfaceIndex,           // per face index of named surface
00877     const labelList& surfaceToCellZone,     // cell zone index per surface
00878 
00879     labelList& cellToZone
00880 ) const
00881 {
00882     const pointField& cellCentres = mesh_.cellCentres();
00883     const labelList& faceOwner = mesh_.faceOwner();
00884     const labelList& faceNeighbour = mesh_.faceNeighbour();
00885 
00886     // Check if cell centre is inside
00887     labelList insideSurfaces;
00888     surfaces_.findInside
00889     (
00890         closedNamedSurfaces,
00891         cellCentres,
00892         insideSurfaces
00893     );
00894 
00895     forAll(insideSurfaces, cellI)
00896     {
00897         if (cellToZone[cellI] == -2)
00898         {
00899             label surfI = insideSurfaces[cellI];
00900 
00901             if (surfI != -1)
00902             {
00903                 cellToZone[cellI] = surfaceToCellZone[surfI];
00904             }
00905         }
00906     }
00907 
00908 
00909     // Some cells with cell centres close to surface might have
00910     // had been put into wrong surface. Recheck with perturbed cell centre.
00911 
00912 
00913     // 1. Collect points
00914 
00915     // Count points to test.
00916     label nCandidates = 0;
00917     forAll(namedSurfaceIndex, faceI)
00918     {
00919         label surfI = namedSurfaceIndex[faceI];
00920 
00921         if (surfI != -1)
00922         {
00923             if (mesh_.isInternalFace(faceI))
00924             {
00925                 nCandidates += 2;
00926             }
00927             else
00928             {
00929                 nCandidates += 1;
00930             }
00931         }
00932     }
00933 
00934     // Collect points.
00935     pointField candidatePoints(nCandidates);
00936     nCandidates = 0;
00937     forAll(namedSurfaceIndex, faceI)
00938     {
00939         label surfI = namedSurfaceIndex[faceI];
00940 
00941         if (surfI != -1)
00942         {
00943             label own = faceOwner[faceI];
00944             const point& ownCc = cellCentres[own];
00945 
00946             if (mesh_.isInternalFace(faceI))
00947             {
00948                 label nei = faceNeighbour[faceI];
00949                 const point& neiCc = cellCentres[nei];
00950                 // Perturbed cc
00951                 const vector d = 1E-4*(neiCc - ownCc);
00952                 candidatePoints[nCandidates++] = ownCc-d;
00953                 candidatePoints[nCandidates++] = neiCc+d;
00954             }
00955             else
00956             {
00957                 const point& neiFc = mesh_.faceCentres()[faceI];
00958                 // Perturbed cc
00959                 const vector d = 1E-4*(neiFc - ownCc);
00960                 candidatePoints[nCandidates++] = ownCc-d;
00961             }
00962         }
00963     }
00964 
00965 
00966     // 2. Test points for inside
00967 
00968     surfaces_.findInside
00969     (
00970         closedNamedSurfaces,
00971         candidatePoints,
00972         insideSurfaces
00973     );
00974 
00975 
00976     // 3. Update zone information
00977 
00978     nCandidates = 0;
00979     forAll(namedSurfaceIndex, faceI)
00980     {
00981         label surfI = namedSurfaceIndex[faceI];
00982 
00983         if (surfI != -1)
00984         {
00985             label own = faceOwner[faceI];
00986 
00987             if (mesh_.isInternalFace(faceI))
00988             {
00989                 label ownSurfI = insideSurfaces[nCandidates++];
00990                 if (ownSurfI != -1)
00991                 {
00992                     cellToZone[own] = surfaceToCellZone[ownSurfI];
00993                 }
00994 
00995                 label neiSurfI = insideSurfaces[nCandidates++];
00996                 if (neiSurfI != -1)
00997                 {
00998                     label nei = faceNeighbour[faceI];
00999 
01000                     cellToZone[nei] = surfaceToCellZone[neiSurfI];
01001                 }
01002             }
01003             else
01004             {
01005                 label ownSurfI = insideSurfaces[nCandidates++];
01006                 if (ownSurfI != -1)
01007                 {
01008                     cellToZone[own] = surfaceToCellZone[ownSurfI];
01009                 }
01010             }
01011         }
01012     }
01013 
01014 
01015     // Adapt the namedSurfaceIndex
01016     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
01017     // for if any cells were not completely covered.
01018 
01019     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
01020     {
01021         label ownZone = cellToZone[mesh_.faceOwner()[faceI]];
01022         label neiZone = cellToZone[mesh_.faceNeighbour()[faceI]];
01023 
01024         if (namedSurfaceIndex[faceI] == -1 && (ownZone != neiZone))
01025         {
01026             // Give face the zone of max cell zone
01027             namedSurfaceIndex[faceI] = findIndex
01028             (
01029                 surfaceToCellZone,
01030                 max(ownZone, neiZone)
01031             );
01032         }
01033     }
01034 
01035     labelList neiCellZone(mesh_.nFaces()-mesh_.nInternalFaces());
01036     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
01037 
01038     forAll(patches, patchI)
01039     {
01040         const polyPatch& pp = patches[patchI];
01041 
01042         if (pp.coupled())
01043         {
01044             forAll(pp, i)
01045             {
01046                 label faceI = pp.start()+i;
01047                 label ownZone = cellToZone[mesh_.faceOwner()[faceI]];
01048                 neiCellZone[faceI-mesh_.nInternalFaces()] = ownZone;
01049             }
01050         }
01051     }
01052     syncTools::swapBoundaryFaceList(mesh_, neiCellZone, false);
01053 
01054     forAll(patches, patchI)
01055     {
01056         const polyPatch& pp = patches[patchI];
01057 
01058         if (pp.coupled())
01059         {
01060             forAll(pp, i)
01061             {
01062                 label faceI = pp.start()+i;
01063                 label ownZone = cellToZone[mesh_.faceOwner()[faceI]];
01064                 label neiZone = neiCellZone[faceI-mesh_.nInternalFaces()];
01065 
01066                 if (namedSurfaceIndex[faceI] == -1 && (ownZone != neiZone))
01067                 {
01068                     // Give face the max cell zone
01069                     namedSurfaceIndex[faceI] = findIndex
01070                     (
01071                         surfaceToCellZone,
01072                         max(ownZone, neiZone)
01073                     );
01074                 }
01075             }
01076         }
01077     }
01078 
01079     // Sync
01080     syncTools::syncFaceList
01081     (
01082         mesh_,
01083         namedSurfaceIndex,
01084         maxEqOp<label>(),
01085         false
01086     );
01087 }
01088 
01089 
01090 bool Foam::meshRefinement::calcRegionToZone
01091 (
01092     const label surfZoneI,
01093     const label ownRegion,
01094     const label neiRegion,
01095 
01096     labelList& regionToCellZone
01097 ) const
01098 {
01099     bool changed = false;
01100 
01101     // Check whether inbetween different regions
01102     if (ownRegion != neiRegion)
01103     {
01104         // Jump. Change one of the sides to my type.
01105 
01106         // 1. Interface between my type and unset region.
01107         // Set region to keepRegion
01108 
01109         if (regionToCellZone[ownRegion] == -2)
01110         {
01111             if (regionToCellZone[neiRegion] == surfZoneI)
01112             {
01113                 // Face between unset and my region. Put unset
01114                 // region into keepRegion
01115                 regionToCellZone[ownRegion] = -1;
01116                 changed = true;
01117             }
01118             else if (regionToCellZone[neiRegion] != -2)
01119             {
01120                 // Face between unset and other region.
01121                 // Put unset region into my region
01122                 regionToCellZone[ownRegion] = surfZoneI;
01123                 changed = true;
01124             }
01125         }
01126         else if (regionToCellZone[neiRegion] == -2)
01127         {
01128             if (regionToCellZone[ownRegion] == surfZoneI)
01129             {
01130                 // Face between unset and my region. Put unset
01131                 // region into keepRegion
01132                 regionToCellZone[neiRegion] = -1;
01133                 changed = true;
01134             }
01135             else if (regionToCellZone[ownRegion] != -2)
01136             {
01137                 // Face between unset and other region.
01138                 // Put unset region into my region
01139                 regionToCellZone[neiRegion] = surfZoneI;
01140                 changed = true;
01141             }
01142         }
01143     }
01144     return changed;
01145 }
01146 
01147 
01148 // Finds region per cell. Assumes:
01149 // - region containing keepPoint does not go into a cellZone
01150 // - all other regions can be found by crossing faces marked in
01151 //   namedSurfaceIndex.
01152 void Foam::meshRefinement::findCellZoneTopo
01153 (
01154     const point& keepPoint,
01155     const labelList& namedSurfaceIndex,
01156     const labelList& surfaceToCellZone,
01157     labelList& cellToZone
01158 ) const
01159 {
01160     // Analyse regions. Reuse regionsplit
01161     boolList blockedFace(mesh_.nFaces());
01162 
01163     forAll(namedSurfaceIndex, faceI)
01164     {
01165         if (namedSurfaceIndex[faceI] == -1)
01166         {
01167             blockedFace[faceI] = false;
01168         }
01169         else
01170         {
01171             blockedFace[faceI] = true;
01172         }
01173     }
01174     // No need to sync since namedSurfaceIndex already is synced
01175 
01176     // Set region per cell based on walking
01177     regionSplit cellRegion(mesh_, blockedFace);
01178     blockedFace.clear();
01179 
01180     // Per mesh region the zone the cell should be put in.
01181     // -2   : not analysed yet
01182     // -1   : keepPoint region. Not put into any cellzone.
01183     // >= 0 : index of cellZone
01184     labelList regionToCellZone(cellRegion.nRegions(), -2);
01185 
01186     // See which cells already are set in the cellToZone (from geometric
01187     // searching) and use these to take over their zones.
01188     // Note: could be improved to count number of cells per region.
01189     forAll(cellToZone, cellI)
01190     {
01191         if (cellToZone[cellI] != -2)
01192         {
01193             regionToCellZone[cellRegion[cellI]] = cellToZone[cellI];
01194         }
01195     }
01196 
01197 
01198 
01199     // Find the region containing the keepPoint
01200     label keepRegionI = -1;
01201 
01202     label cellI = mesh_.findCell(keepPoint);
01203 
01204     if (cellI != -1)
01205     {
01206         keepRegionI = cellRegion[cellI];
01207     }
01208     reduce(keepRegionI, maxOp<label>());
01209 
01210     Info<< "Found point " << keepPoint << " in cell " << cellI
01211         << " in global region " << keepRegionI
01212         << " out of " << cellRegion.nRegions() << " regions." << endl;
01213 
01214     if (keepRegionI == -1)
01215     {
01216         FatalErrorIn
01217         (
01218             "meshRefinement::findCellZoneTopo"
01219             "(const point&, const labelList&, const labelList&, labelList&)"
01220         )   << "Point " << keepPoint
01221             << " is not inside the mesh." << nl
01222             << "Bounding box of the mesh:" << mesh_.globalData().bb()
01223             << exit(FatalError);
01224     }
01225 
01226     // Mark default region with zone -1.
01227     if (regionToCellZone[keepRegionI] == -2)
01228     {
01229         regionToCellZone[keepRegionI] = -1;
01230     }
01231 
01232 
01233     // Find correspondence between cell zone and surface
01234     // by changing cell zone every time we cross a surface.
01235     while (true)
01236     {
01237         // Synchronise regionToCellZone.
01238         // Note:
01239         // - region numbers are identical on all processors
01240         // - keepRegion is identical ,,
01241         // - cellZones are identical ,,
01242         // This done at top of loop to account for geometric matching
01243         // not being synchronised.
01244         Pstream::listCombineGather(regionToCellZone, maxEqOp<label>());
01245         Pstream::listCombineScatter(regionToCellZone);
01246 
01247 
01248         bool changed = false;
01249 
01250         // Internal faces
01251 
01252         for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
01253         {
01254             label surfI = namedSurfaceIndex[faceI];
01255 
01256             if (surfI != -1)
01257             {
01258                 // Calculate region to zone from cellRegions on either side
01259                 // of internal face.
01260                 bool changedCell = calcRegionToZone
01261                 (
01262                     surfaceToCellZone[surfI],
01263                     cellRegion[mesh_.faceOwner()[faceI]],
01264                     cellRegion[mesh_.faceNeighbour()[faceI]],
01265                     regionToCellZone
01266                 );
01267 
01268                 changed = changed | changedCell;
01269             }
01270         }
01271 
01272         // Boundary faces
01273 
01274         const polyBoundaryMesh& patches = mesh_.boundaryMesh();
01275 
01276         // Get coupled neighbour cellRegion
01277         labelList neiCellRegion(mesh_.nFaces()-mesh_.nInternalFaces());
01278         forAll(patches, patchI)
01279         {
01280             const polyPatch& pp = patches[patchI];
01281 
01282             if (pp.coupled())
01283             {
01284                 forAll(pp, i)
01285                 {
01286                     label faceI = pp.start()+i;
01287                     neiCellRegion[faceI-mesh_.nInternalFaces()] =
01288                         cellRegion[mesh_.faceOwner()[faceI]];
01289                 }
01290             }
01291         }
01292         syncTools::swapBoundaryFaceList(mesh_, neiCellRegion, false);
01293 
01294         // Calculate region to zone from cellRegions on either side of coupled
01295         // face.
01296         forAll(patches, patchI)
01297         {
01298             const polyPatch& pp = patches[patchI];
01299 
01300             if (pp.coupled())
01301             {
01302                 forAll(pp, i)
01303                 {
01304                     label faceI = pp.start()+i;
01305 
01306                     label surfI = namedSurfaceIndex[faceI];
01307 
01308                     if (surfI != -1)
01309                     {
01310                         bool changedCell = calcRegionToZone
01311                         (
01312                             surfaceToCellZone[surfI],
01313                             cellRegion[mesh_.faceOwner()[faceI]],
01314                             neiCellRegion[faceI-mesh_.nInternalFaces()],
01315                             regionToCellZone
01316                         );
01317 
01318                         changed = changed | changedCell;
01319                     }
01320                 }
01321             }
01322         }
01323 
01324         if (!returnReduce(changed, orOp<bool>()))
01325         {
01326             break;
01327         }
01328     }
01329 
01330 
01331     forAll(regionToCellZone, regionI)
01332     {
01333         label zoneI = regionToCellZone[regionI];
01334 
01335         if (zoneI ==  -2)
01336         {
01337             FatalErrorIn
01338             (
01339                 "meshRefinement::findCellZoneTopo"
01340                 "(const point&, const labelList&, const labelList&, labelList&)"
01341             )   << "For region " << regionI << " haven't set cell zone."
01342                 << exit(FatalError);
01343         }
01344     }
01345 
01346     if (debug)
01347     {
01348         forAll(regionToCellZone, regionI)
01349         {
01350             Pout<< "Region " << regionI
01351                 << " becomes cellZone:" << regionToCellZone[regionI]
01352                 << endl;
01353         }
01354     }
01355 
01356     // Rework into cellToZone
01357     forAll(cellToZone, cellI)
01358     {
01359         cellToZone[cellI] = regionToCellZone[cellRegion[cellI]];
01360     }
01361 }
01362 
01363 
01364 // Make namedSurfaceIndex consistent with cellToZone - clear out any blocked
01365 // faces inbetween same cell zone.
01366 void Foam::meshRefinement::makeConsistentFaceIndex
01367 (
01368     const labelList& cellToZone,
01369     labelList& namedSurfaceIndex
01370 ) const
01371 {
01372     const labelList& faceOwner = mesh_.faceOwner();
01373     const labelList& faceNeighbour = mesh_.faceNeighbour();
01374 
01375     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
01376     {
01377         label ownZone = cellToZone[faceOwner[faceI]];
01378         label neiZone = cellToZone[faceNeighbour[faceI]];
01379 
01380         if (ownZone == neiZone && namedSurfaceIndex[faceI] != -1)
01381         {
01382             namedSurfaceIndex[faceI] = -1;
01383         }
01384         else if (ownZone != neiZone && namedSurfaceIndex[faceI] == -1)
01385         {
01386             FatalErrorIn("meshRefinement::zonify()")
01387                 << "Different cell zones on either side of face " << faceI
01388                 << " at " << mesh_.faceCentres()[faceI]
01389                 << " but face not marked with a surface."
01390                 << abort(FatalError);
01391         }
01392     }
01393 
01394     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
01395 
01396     // Get coupled neighbour cellZone
01397     labelList neiCellZone(mesh_.nFaces()-mesh_.nInternalFaces());
01398     forAll(patches, patchI)
01399     {
01400         const polyPatch& pp = patches[patchI];
01401 
01402         if (pp.coupled())
01403         {
01404             forAll(pp, i)
01405             {
01406                 label faceI = pp.start()+i;
01407                 neiCellZone[faceI-mesh_.nInternalFaces()] =
01408                     cellToZone[mesh_.faceOwner()[faceI]];
01409             }
01410         }
01411     }
01412     syncTools::swapBoundaryFaceList(mesh_, neiCellZone, false);
01413 
01414     // Use coupled cellZone to do check
01415     forAll(patches, patchI)
01416     {
01417         const polyPatch& pp = patches[patchI];
01418 
01419         if (pp.coupled())
01420         {
01421             forAll(pp, i)
01422             {
01423                 label faceI = pp.start()+i;
01424 
01425                 label ownZone = cellToZone[faceOwner[faceI]];
01426                 label neiZone = neiCellZone[faceI-mesh_.nInternalFaces()];
01427 
01428                 if (ownZone == neiZone && namedSurfaceIndex[faceI] != -1)
01429                 {
01430                     namedSurfaceIndex[faceI] = -1;
01431                 }
01432                 else if (ownZone != neiZone && namedSurfaceIndex[faceI] == -1)
01433                 {
01434                     FatalErrorIn("meshRefinement::zonify()")
01435                         << "Different cell zones on either side of face "
01436                         << faceI << " at " << mesh_.faceCentres()[faceI]
01437                         << " but face not marked with a surface."
01438                         << abort(FatalError);
01439                 }
01440             }
01441         }
01442     }
01443 }
01444 
01445 
01446 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
01447 
01448 // Split off unreachable areas of mesh.
01449 void Foam::meshRefinement::baffleAndSplitMesh
01450 (
01451     const bool handleSnapProblems,
01452     const bool removeEdgeConnectedCells,
01453     const scalarField& perpendicularAngle,
01454     const bool mergeFreeStanding,
01455     const dictionary& motionDict,
01456     Time& runTime,
01457     const labelList& globalToPatch,
01458     const point& keepPoint
01459 )
01460 {
01461     // Introduce baffles
01462     // ~~~~~~~~~~~~~~~~~
01463 
01464     // Split the mesh along internal faces wherever there is a pierce between
01465     // two cell centres.
01466 
01467     Info<< "Introducing baffles for "
01468         << returnReduce(countHits(), sumOp<label>())
01469         << " faces that are intersected by the surface." << nl << endl;
01470 
01471     // Swap neighbouring cell centres and cell level
01472     labelList neiLevel(mesh_.nFaces()-mesh_.nInternalFaces());
01473     pointField neiCc(mesh_.nFaces()-mesh_.nInternalFaces());
01474     calcNeighbourData(neiLevel, neiCc);
01475 
01476     labelList ownPatch, neiPatch;
01477     getBafflePatches
01478     (
01479         globalToPatch,
01480         neiLevel,
01481         neiCc,
01482 
01483         ownPatch,
01484         neiPatch
01485     );
01486 
01487     if (debug)
01488     {
01489         runTime++;
01490     }
01491 
01492     createBaffles(ownPatch, neiPatch);
01493 
01494     if (debug)
01495     {
01496         // Debug:test all is still synced across proc patches
01497         checkData();
01498     }
01499 
01500     Info<< "Created baffles in = "
01501         << runTime.cpuTimeIncrement() << " s\n" << nl << endl;
01502 
01503     printMeshInfo(debug, "After introducing baffles");
01504 
01505     if (debug)
01506     {
01507         Pout<< "Writing baffled mesh to time " << timeName()
01508             << endl;
01509         write(debug, runTime.path()/"baffles");
01510         Pout<< "Dumped debug data in = "
01511             << runTime.cpuTimeIncrement() << " s\n" << nl << endl;
01512     }
01513 
01514 
01515     // Introduce baffles to delete problem cells
01516     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01517 
01518     // Create some additional baffles where we want surface cells removed.
01519 
01520     if (handleSnapProblems)
01521     {
01522         Info<< nl
01523             << "Introducing baffles to block off problem cells" << nl
01524             << "----------------------------------------------" << nl
01525             << endl;
01526 
01527         labelList facePatch
01528         (
01529             markFacesOnProblemCells
01530             (
01531                 motionDict,
01532                 removeEdgeConnectedCells,
01533                 perpendicularAngle,
01534                 globalToPatch
01535             )
01536             //markFacesOnProblemCellsGeometric(motionDict)
01537         );
01538         Info<< "Analyzed problem cells in = "
01539             << runTime.cpuTimeIncrement() << " s\n" << nl << endl;
01540 
01541         if (debug)
01542         {
01543             faceSet problemTopo(mesh_, "problemFacesTopo", 100);
01544 
01545             const labelList facePatchTopo
01546             (
01547                 markFacesOnProblemCells
01548                 (
01549                     motionDict,
01550                     removeEdgeConnectedCells,
01551                     perpendicularAngle,
01552                     globalToPatch
01553                 )
01554             );
01555             forAll(facePatchTopo, faceI)
01556             {
01557                 if (facePatchTopo[faceI] != -1)
01558                 {
01559                     problemTopo.insert(faceI);
01560                 }
01561             }
01562             Pout<< "Dumping " << problemTopo.size()
01563                 << " problem faces to " << problemTopo.objectPath() << endl;
01564             problemTopo.write();
01565         }
01566 
01567         Info<< "Introducing baffles to delete problem cells." << nl << endl;
01568 
01569         if (debug)
01570         {
01571             runTime++;
01572         }
01573 
01574         // Create baffles with same owner and neighbour for now.
01575         createBaffles(facePatch, facePatch);
01576 
01577         if (debug)
01578         {
01579             // Debug:test all is still synced across proc patches
01580             checkData();
01581         }
01582         Info<< "Created baffles in = "
01583             << runTime.cpuTimeIncrement() << " s\n" << nl << endl;
01584 
01585         printMeshInfo(debug, "After introducing baffles");
01586 
01587         if (debug)
01588         {
01589             Pout<< "Writing extra baffled mesh to time "
01590                 << timeName() << endl;
01591             write(debug, runTime.path()/"extraBaffles");
01592             Pout<< "Dumped debug data in = "
01593                 << runTime.cpuTimeIncrement() << " s\n" << nl << endl;
01594         }
01595     }
01596 
01597 
01598     // Select part of mesh
01599     // ~~~~~~~~~~~~~~~~~~~
01600 
01601     Info<< nl
01602         << "Remove unreachable sections of mesh" << nl
01603         << "-----------------------------------" << nl
01604         << endl;
01605 
01606     if (debug)
01607     {
01608         runTime++;
01609     }
01610 
01611     splitMeshRegions(keepPoint);
01612 
01613     if (debug)
01614     {
01615         // Debug:test all is still synced across proc patches
01616         checkData();
01617     }
01618     Info<< "Split mesh in = "
01619         << runTime.cpuTimeIncrement() << " s\n" << nl << endl;
01620 
01621     printMeshInfo(debug, "After subsetting");
01622 
01623     if (debug)
01624     {
01625         Pout<< "Writing subsetted mesh to time " << timeName()
01626             << endl;
01627         write(debug, runTime.path()/timeName());
01628         Pout<< "Dumped debug data in = "
01629             << runTime.cpuTimeIncrement() << " s\n" << nl << endl;
01630     }
01631 
01632 
01633     // Merge baffles
01634     // ~~~~~~~~~~~~~
01635 
01636     if (mergeFreeStanding)
01637     {
01638         Info<< nl
01639             << "Merge free-standing baffles" << nl
01640             << "---------------------------" << nl
01641             << endl;
01642 
01643         if (debug)
01644         {
01645             runTime++;
01646         }
01647 
01648         // List of pairs of freestanding baffle faces.
01649         List<labelPair> couples
01650         (
01651             filterDuplicateFaces    // filter out freestanding baffles
01652             (
01653                 getDuplicateFaces   // get all baffles
01654                 (
01655                     identity(mesh_.nFaces()-mesh_.nInternalFaces())
01656                    +mesh_.nInternalFaces()
01657                 )
01658             )
01659         );
01660 
01661         label nCouples = couples.size();
01662         reduce(nCouples, sumOp<label>());
01663 
01664         Info<< "Detected free-standing baffles : " << nCouples << endl;
01665 
01666         if (nCouples > 0)
01667         {
01668             // Actually merge baffles. Note: not exactly parallellized. Should
01669             // convert baffle faces into processor faces if they resulted
01670             // from them.
01671             mergeBaffles(couples);
01672 
01673             if (debug)
01674             {
01675                 // Debug:test all is still synced across proc patches
01676                 checkData();
01677             }
01678         }
01679         Info<< "Merged free-standing baffles in = "
01680             << runTime.cpuTimeIncrement() << " s\n" << nl << endl;
01681     }
01682 }
01683 
01684 
01685 // Split off (with optional buffer layers) unreachable areas of mesh.
01686 Foam::autoPtr<Foam::mapPolyMesh> Foam::meshRefinement::splitMesh
01687 (
01688     const label nBufferLayers,
01689     const labelList& globalToPatch,
01690     const point& keepPoint
01691 )
01692 {
01693     // Determine patches to put intersections into
01694     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01695 
01696     // Swap neighbouring cell centres and cell level
01697     labelList neiLevel(mesh_.nFaces()-mesh_.nInternalFaces());
01698     pointField neiCc(mesh_.nFaces()-mesh_.nInternalFaces());
01699     calcNeighbourData(neiLevel, neiCc);
01700 
01701     labelList ownPatch, neiPatch;
01702     getBafflePatches
01703     (
01704         globalToPatch,
01705         neiLevel,
01706         neiCc,
01707 
01708         ownPatch,
01709         neiPatch
01710     );
01711 
01712     // Analyse regions. Reuse regionsplit
01713     boolList blockedFace(mesh_.nFaces(), false);
01714 
01715     forAll(ownPatch, faceI)
01716     {
01717         if (ownPatch[faceI] != -1 || neiPatch[faceI] != -1)
01718         {
01719             blockedFace[faceI] = true;
01720         }
01721     }
01722     syncTools::syncFaceList(mesh_, blockedFace, orEqOp<bool>(), false);
01723 
01724     // Set region per cell based on walking
01725     regionSplit cellRegion(mesh_, blockedFace);
01726     blockedFace.clear();
01727 
01728     // Find the region containing the keepPoint
01729     label keepRegionI = -1;
01730 
01731     label cellI = mesh_.findCell(keepPoint);
01732 
01733     if (cellI != -1)
01734     {
01735         keepRegionI = cellRegion[cellI];
01736     }
01737     reduce(keepRegionI, maxOp<label>());
01738 
01739     Info<< "Found point " << keepPoint << " in cell " << cellI
01740         << " in global region " << keepRegionI
01741         << " out of " << cellRegion.nRegions() << " regions." << endl;
01742 
01743     if (keepRegionI == -1)
01744     {
01745         FatalErrorIn
01746         (
01747             "meshRefinement::splitMesh"
01748             "(const label, const labelList&, const point&)"
01749         )   << "Point " << keepPoint
01750             << " is not inside the mesh." << nl
01751             << "Bounding box of the mesh:" << mesh_.globalData().bb()
01752             << exit(FatalError);
01753     }
01754 
01755 
01756     // Walk out nBufferlayers from region split
01757     // (modifies cellRegion, ownPatch)
01758     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01759     // Takes over face patch onto points and then back to faces and cells
01760     // (so cell-face-point walk)
01761 
01762     const labelList& faceOwner = mesh_.faceOwner();
01763     const labelList& faceNeighbour = mesh_.faceNeighbour();
01764 
01765     // Patch for exposed faces for lack of anything sensible.
01766     label defaultPatch = 0;
01767     if (globalToPatch.size())
01768     {
01769         defaultPatch = globalToPatch[0];
01770     }
01771 
01772     for (label i = 0; i < nBufferLayers; i++)
01773     {
01774         // 1. From cells (via faces) to points
01775 
01776         labelList pointBaffle(mesh_.nPoints(), -1);
01777 
01778         forAll(faceNeighbour, faceI)
01779         {
01780             const face& f = mesh_.faces()[faceI];
01781 
01782             label ownRegion = cellRegion[faceOwner[faceI]];
01783             label neiRegion = cellRegion[faceNeighbour[faceI]];
01784 
01785             if (ownRegion == keepRegionI && neiRegion != keepRegionI)
01786             {
01787                 // Note max(..) since possibly regionSplit might have split
01788                 // off extra unreachable parts of mesh. Note: or can this only
01789                 // happen for boundary faces?
01790                 forAll(f, fp)
01791                 {
01792                     pointBaffle[f[fp]] = max(defaultPatch, ownPatch[faceI]);
01793                 }
01794             }
01795             else if (ownRegion != keepRegionI && neiRegion == keepRegionI)
01796             {
01797                 label newPatchI = neiPatch[faceI];
01798                 if (newPatchI == -1)
01799                 {
01800                     newPatchI = max(defaultPatch, ownPatch[faceI]);
01801                 }
01802                 forAll(f, fp)
01803                 {
01804                     pointBaffle[f[fp]] = newPatchI;
01805                 }
01806             }
01807         }
01808         for
01809         (
01810             label faceI = mesh_.nInternalFaces();
01811             faceI < mesh_.nFaces();
01812             faceI++
01813         )
01814         {
01815             const face& f = mesh_.faces()[faceI];
01816 
01817             label ownRegion = cellRegion[faceOwner[faceI]];
01818 
01819             if (ownRegion == keepRegionI)
01820             {
01821                 forAll(f, fp)
01822                 {
01823                     pointBaffle[f[fp]] = max(defaultPatch, ownPatch[faceI]);
01824                 }
01825             }
01826         }
01827 
01828         // Sync
01829         syncTools::syncPointList
01830         (
01831             mesh_,
01832             pointBaffle,
01833             maxEqOp<label>(),
01834             -1,                 // null value
01835             false               // no separation
01836         );
01837 
01838 
01839         // 2. From points back to faces
01840 
01841         const labelListList& pointFaces = mesh_.pointFaces();
01842 
01843         forAll(pointFaces, pointI)
01844         {
01845             if (pointBaffle[pointI] != -1)
01846             {
01847                 const labelList& pFaces = pointFaces[pointI];
01848 
01849                 forAll(pFaces, pFaceI)
01850                 {
01851                     label faceI = pFaces[pFaceI];
01852 
01853                     if (ownPatch[faceI] == -1)
01854                     {
01855                         ownPatch[faceI] = pointBaffle[pointI];
01856                     }
01857                 }
01858             }
01859         }
01860         syncTools::syncFaceList(mesh_, ownPatch, maxEqOp<label>(), false);
01861 
01862 
01863         // 3. From faces to cells (cellRegion) and back to faces (ownPatch)
01864 
01865         labelList newOwnPatch(ownPatch);
01866 
01867         forAll(ownPatch, faceI)
01868         {
01869             if (ownPatch[faceI] != -1)
01870             {
01871                 label own = faceOwner[faceI];
01872 
01873                 if (cellRegion[own] != keepRegionI)
01874                 {
01875                     cellRegion[own] = keepRegionI;
01876 
01877                     const cell& ownFaces = mesh_.cells()[own];
01878                     forAll(ownFaces, j)
01879                     {
01880                         if (ownPatch[ownFaces[j]] == -1)
01881                         {
01882                             newOwnPatch[ownFaces[j]] = ownPatch[faceI];
01883                         }
01884                     }
01885                 }
01886                 if (mesh_.isInternalFace(faceI))
01887                 {
01888                     label nei = faceNeighbour[faceI];
01889 
01890                     if (cellRegion[nei] != keepRegionI)
01891                     {
01892                         cellRegion[nei] = keepRegionI;
01893 
01894                         const cell& neiFaces = mesh_.cells()[nei];
01895                         forAll(neiFaces, j)
01896                         {
01897                             if (ownPatch[neiFaces[j]] == -1)
01898                             {
01899                                 newOwnPatch[neiFaces[j]] = ownPatch[faceI];
01900                             }
01901                         }
01902                     }
01903                 }
01904             }
01905         }
01906 
01907         ownPatch.transfer(newOwnPatch);
01908 
01909         syncTools::syncFaceList(mesh_, ownPatch, maxEqOp<label>(), false);
01910     }
01911 
01912 
01913 
01914     // Subset
01915     // ~~~~~~
01916 
01917     // Get cells to remove
01918     DynamicList<label> cellsToRemove(mesh_.nCells());
01919     forAll(cellRegion, cellI)
01920     {
01921         if (cellRegion[cellI] != keepRegionI)
01922         {
01923             cellsToRemove.append(cellI);
01924         }
01925     }
01926     cellsToRemove.shrink();
01927 
01928     label nCellsToKeep = mesh_.nCells() - cellsToRemove.size();
01929     reduce(nCellsToKeep, sumOp<label>());
01930 
01931     Info<< "Keeping all cells in region " << keepRegionI
01932         << " containing point " << keepPoint << endl
01933         << "Selected for keeping : " << nCellsToKeep
01934         << " cells." << endl;
01935 
01936 
01937     // Remove cells
01938     removeCells cellRemover(mesh_);
01939 
01940     // Pick up patches for exposed faces
01941     labelList exposedFaces(cellRemover.getExposedFaces(cellsToRemove));
01942     labelList exposedPatches(exposedFaces.size());
01943 
01944     forAll(exposedFaces, i)
01945     {
01946         label faceI = exposedFaces[i];
01947 
01948         if (ownPatch[faceI] != -1)
01949         {
01950             exposedPatches[i] = ownPatch[faceI];
01951         }
01952         else
01953         {
01954             WarningIn("meshRefinement::splitMesh(..)")
01955                 << "For exposed face " << faceI
01956                 << " fc:" << mesh_.faceCentres()[faceI]
01957                 << " found no patch." << endl
01958                 << "    Taking patch " << defaultPatch
01959                 << " instead." << endl;
01960             exposedPatches[i] = defaultPatch;
01961         }
01962     }
01963 
01964     return doRemoveCells
01965     (
01966         cellsToRemove,
01967         exposedFaces,
01968         exposedPatches,
01969         cellRemover
01970     );
01971 }
01972 
01973 
01974 // Find boundary points that connect to more than one cell region and
01975 // split them.
01976 Foam::autoPtr<Foam::mapPolyMesh> Foam::meshRefinement::dupNonManifoldPoints()
01977 {
01978     // Topochange container
01979     polyTopoChange meshMod(mesh_);
01980 
01981 
01982     // Analyse which points need to be duplicated
01983     localPointRegion regionSide(mesh_);
01984 
01985     label nNonManifPoints = returnReduce
01986     (
01987         regionSide.meshPointMap().size(),
01988         sumOp<label>()
01989     );
01990 
01991     Info<< "dupNonManifoldPoints : Found : " << nNonManifPoints
01992         << " non-manifold points (out of "
01993         << mesh_.globalData().nTotalPoints()
01994         << ')' << endl;
01995 
01996     // Topo change engine
01997     duplicatePoints pointDuplicator(mesh_);
01998 
01999     // Insert changes into meshMod
02000     pointDuplicator.setRefinement(regionSide, meshMod);
02001 
02002     // Change the mesh (no inflation, parallel sync)
02003     autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh_, false, true);
02004 
02005     // Update fields
02006     mesh_.updateMesh(map);
02007 
02008     // Move mesh if in inflation mode
02009     if (map().hasMotionPoints())
02010     {
02011         mesh_.movePoints(map().preMotionPoints());
02012     }
02013     else
02014     {
02015         // Delete mesh volumes.
02016         mesh_.clearOut();
02017     }
02018 
02019     if (overwrite())
02020     {
02021         mesh_.setInstance(oldInstance());
02022     }
02023 
02024     // Update intersections. Is mapping only (no faces created, positions stay
02025     // same) so no need to recalculate intersections.
02026     updateMesh(map, labelList(0));
02027 
02028     return map;
02029 }
02030 
02031 
02032 // Zoning
02033 Foam::autoPtr<Foam::mapPolyMesh> Foam::meshRefinement::zonify
02034 (
02035     const point& keepPoint
02036 )
02037 {
02038     const wordList& cellZoneNames = surfaces_.cellZoneNames();
02039     const wordList& faceZoneNames = surfaces_.faceZoneNames();
02040 
02041     labelList namedSurfaces(surfaces_.getNamedSurfaces());
02042 
02043     boolList isNamedSurface(cellZoneNames.size(), false);
02044 
02045     forAll(namedSurfaces, i)
02046     {
02047         label surfI = namedSurfaces[i];
02048 
02049         isNamedSurface[surfI] = true;
02050 
02051         Info<< "Surface : " << surfaces_.names()[surfI] << nl
02052             << "    faceZone : " << faceZoneNames[surfI] << nl
02053             << "    cellZone : " << cellZoneNames[surfI] << endl;
02054     }
02055 
02056 
02057     // Add zones to mesh
02058 
02059     labelList surfaceToFaceZone(faceZoneNames.size(), -1);
02060     {
02061         faceZoneMesh& faceZones = mesh_.faceZones();
02062 
02063         forAll(namedSurfaces, i)
02064         {
02065             label surfI = namedSurfaces[i];
02066 
02067             label zoneI = faceZones.findZoneID(faceZoneNames[surfI]);
02068 
02069             if (zoneI == -1)
02070             {
02071                 zoneI = faceZones.size();
02072                 faceZones.setSize(zoneI+1);
02073                 faceZones.set
02074                 (
02075                     zoneI,
02076                     new faceZone
02077                     (
02078                         faceZoneNames[surfI],   //name
02079                         labelList(0),           //addressing
02080                         boolList(0),            //flipmap
02081                         zoneI,                  //index
02082                         faceZones               //faceZoneMesh
02083                     )
02084                 );
02085             }
02086 
02087             if (debug)
02088             {
02089                 Pout<< "Faces on " << surfaces_.names()[surfI]
02090                     << " will go into faceZone " << zoneI << endl;
02091             }
02092             surfaceToFaceZone[surfI] = zoneI;
02093         }
02094 
02095         // Check they are synced
02096         List<wordList> allFaceZones(Pstream::nProcs());
02097         allFaceZones[Pstream::myProcNo()] = faceZones.names();
02098         Pstream::gatherList(allFaceZones);
02099         Pstream::scatterList(allFaceZones);
02100 
02101         for (label procI = 1; procI < allFaceZones.size(); procI++)
02102         {
02103             if (allFaceZones[procI] != allFaceZones[0])
02104             {
02105                 FatalErrorIn
02106                 (
02107                     "meshRefinement::zonify"
02108                     "(const label, const point&)"
02109                 )   << "Zones not synchronised among processors." << nl
02110                     << " Processor0 has faceZones:" << allFaceZones[0]
02111                     << " , processor" << procI
02112                     << " has faceZones:" << allFaceZones[procI]
02113                     << exit(FatalError);
02114             }
02115         }
02116     }
02117 
02118     labelList surfaceToCellZone(cellZoneNames.size(), -1);
02119     {
02120         cellZoneMesh& cellZones = mesh_.cellZones();
02121 
02122         forAll(namedSurfaces, i)
02123         {
02124             label surfI = namedSurfaces[i];
02125 
02126             label zoneI = cellZones.findZoneID(cellZoneNames[surfI]);
02127 
02128             if (zoneI == -1)
02129             {
02130                 zoneI = cellZones.size();
02131                 cellZones.setSize(zoneI+1);
02132                 cellZones.set
02133                 (
02134                     zoneI,
02135                     new cellZone
02136                     (
02137                         cellZoneNames[surfI],   //name
02138                         labelList(0),           //addressing
02139                         zoneI,                  //index
02140                         cellZones               //cellZoneMesh
02141                     )
02142                 );
02143             }
02144 
02145             if (debug)
02146             {
02147                 Pout<< "Cells inside " << surfaces_.names()[surfI]
02148                     << " will go into cellZone " << zoneI << endl;
02149             }
02150             surfaceToCellZone[surfI] = zoneI;
02151         }
02152 
02153         // Check they are synced
02154         List<wordList> allCellZones(Pstream::nProcs());
02155         allCellZones[Pstream::myProcNo()] = cellZones.names();
02156         Pstream::gatherList(allCellZones);
02157         Pstream::scatterList(allCellZones);
02158 
02159         for (label procI = 1; procI < allCellZones.size(); procI++)
02160         {
02161             if (allCellZones[procI] != allCellZones[0])
02162             {
02163                 FatalErrorIn
02164                 (
02165                     "meshRefinement::zonify"
02166                     "(const label, const point&)"
02167                 )   << "Zones not synchronised among processors." << nl
02168                     << " Processor0 has cellZones:" << allCellZones[0]
02169                     << " , processor" << procI
02170                     << " has cellZones:" << allCellZones[procI]
02171                     << exit(FatalError);
02172             }
02173         }
02174     }
02175 
02176 
02177 
02178     const pointField& cellCentres = mesh_.cellCentres();
02179     const labelList& faceOwner = mesh_.faceOwner();
02180     const labelList& faceNeighbour = mesh_.faceNeighbour();
02181     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
02182 
02183 
02184     // Mark faces intersecting zoned surfaces
02185     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
02186 
02187 
02188     // Like surfaceIndex_ but only for named surfaces.
02189     labelList namedSurfaceIndex(mesh_.nFaces(), -1);
02190 
02191     {
02192         // Statistics: number of faces per faceZone
02193         labelList nSurfFaces(faceZoneNames.size(), 0);
02194 
02195         // Swap neighbouring cell centres and cell level
02196         labelList neiLevel(mesh_.nFaces()-mesh_.nInternalFaces());
02197         pointField neiCc(mesh_.nFaces()-mesh_.nInternalFaces());
02198         calcNeighbourData(neiLevel, neiCc);
02199 
02200         // Note: for all internal faces? internal + coupled?
02201         // Since zonify is run after baffling the surfaceIndex_ on baffles is
02202         // not synchronised across both baffle faces. Fortunately we don't
02203         // do zonify baffle faces anyway (they are normal boundary faces).
02204 
02205         // Collect candidate faces
02206         // ~~~~~~~~~~~~~~~~~~~~~~~
02207 
02208         labelList testFaces(intersectedFaces());
02209 
02210         // Collect segments
02211         // ~~~~~~~~~~~~~~~~
02212 
02213         pointField start(testFaces.size());
02214         pointField end(testFaces.size());
02215 
02216         forAll(testFaces, i)
02217         {
02218             label faceI = testFaces[i];
02219 
02220             if (mesh_.isInternalFace(faceI))
02221             {
02222                 start[i] = cellCentres[faceOwner[faceI]];
02223                 end[i] = cellCentres[faceNeighbour[faceI]];
02224             }
02225             else
02226             {
02227                 start[i] = cellCentres[faceOwner[faceI]];
02228                 end[i] = neiCc[faceI-mesh_.nInternalFaces()];
02229             }
02230         }
02231 
02232 
02233         // Do test for intersections
02234         // ~~~~~~~~~~~~~~~~~~~~~~~~~
02235         // Note that we intersect all intersected faces again. Could reuse
02236         // the information already in surfaceIndex_.
02237 
02238         labelList surface1;
02239         labelList surface2;
02240         {
02241             List<pointIndexHit> hit1;
02242             labelList region1;
02243             List<pointIndexHit> hit2;
02244             labelList region2;
02245             surfaces_.findNearestIntersection
02246             (
02247                 namedSurfaces,
02248                 start,
02249                 end,
02250 
02251                 surface1,
02252                 hit1,
02253                 region1,
02254                 surface2,
02255                 hit2,
02256                 region2
02257             );
02258         }
02259 
02260         forAll(testFaces, i)
02261         {
02262             label faceI = testFaces[i];
02263 
02264             if (surface1[i] != -1)
02265             {
02266                 // If both hit should probably choose nearest. For later.
02267                 namedSurfaceIndex[faceI] = surface1[i];
02268                 nSurfFaces[surface1[i]]++;
02269             }
02270             else if (surface2[i] != -1)
02271             {
02272                 namedSurfaceIndex[faceI] = surface2[i];
02273                 nSurfFaces[surface2[i]]++;
02274             }
02275         }
02276 
02277 
02278         // surfaceIndex migh have different surfaces on both sides if
02279         // there happen to be a (obviously thin) surface with different
02280         // regions between the cell centres. If one is on a named surface
02281         // and the other is not this might give problems so sync.
02282         syncTools::syncFaceList
02283         (
02284             mesh_,
02285             namedSurfaceIndex,
02286             maxEqOp<label>(),
02287             false
02288         );
02289 
02290         // Print a bit
02291         if (debug)
02292         {
02293             forAll(nSurfFaces, surfI)
02294             {
02295                 Pout<< "Surface:"
02296                     << surfaces_.names()[surfI]
02297                     << "  nZoneFaces:" << nSurfFaces[surfI] << nl;
02298             }
02299             Pout<< endl;
02300         }
02301     }
02302 
02303 
02304     // Put the cells into the correct zone
02305     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
02306 
02307     // Closed surfaces with cellZone specified.
02308     labelList closedNamedSurfaces(surfaces_.getClosedNamedSurfaces());
02309 
02310     // Zone per cell:
02311     // -2 : unset
02312     // -1 : not in any zone
02313     // >=0: zoneID
02314     labelList cellToZone(mesh_.nCells(), -2);
02315 
02316 
02317     // Set using geometric test
02318     // ~~~~~~~~~~~~~~~~~~~~~~~~
02319 
02320     if (closedNamedSurfaces.size())
02321     {
02322         findCellZoneGeometric
02323         (
02324             closedNamedSurfaces,   // indices of closed surfaces
02325             namedSurfaceIndex,     // per face index of named surface
02326             surfaceToCellZone,     // cell zone index per surface
02327             cellToZone
02328         );
02329     }
02330 
02331     // Set using walking
02332     // ~~~~~~~~~~~~~~~~~
02333 
02334     //if (returnReduce(nSet, sumOp<label>()) < mesh_.globalData().nTotalCells())
02335     {
02336         // Topological walk
02337         findCellZoneTopo
02338         (
02339             keepPoint,
02340             namedSurfaceIndex,
02341             surfaceToCellZone,
02342             cellToZone
02343         );
02344     }
02345 
02346 
02348     //makeConsistentFaceIndex(cellToZone, namedSurfaceIndex);
02349 
02350 
02351     // Topochange container
02352     polyTopoChange meshMod(mesh_);
02353 
02354 
02355     // Put the faces into the correct zone
02356     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
02357 
02358     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
02359     {
02360         label surfI = namedSurfaceIndex[faceI];
02361 
02362         if (surfI != -1)
02363         {
02364             // Orient face zone to have slave cells in max cell zone.
02365             label ownZone = cellToZone[faceOwner[faceI]];
02366             label neiZone = cellToZone[faceNeighbour[faceI]];
02367 
02368             bool flip;
02369             if (ownZone == max(ownZone, neiZone))
02370             {
02371                 flip = false;
02372             }
02373             else
02374             {
02375                 flip = true;
02376             }
02377 
02378             meshMod.setAction
02379             (
02380                 polyModifyFace
02381                 (
02382                     mesh_.faces()[faceI],           // modified face
02383                     faceI,                          // label of face
02384                     faceOwner[faceI],               // owner
02385                     faceNeighbour[faceI],           // neighbour
02386                     false,                          // face flip
02387                     -1,                             // patch for face
02388                     false,                          // remove from zone
02389                     surfaceToFaceZone[surfI],       // zone for face
02390                     flip                            // face flip in zone
02391                 )
02392             );
02393         }
02394     }
02395 
02396     // Get coupled neighbour cellZone. Set to -1 on non-coupled patches.
02397     labelList neiCellZone(mesh_.nFaces()-mesh_.nInternalFaces(), -1);
02398     forAll(patches, patchI)
02399     {
02400         const polyPatch& pp = patches[patchI];
02401 
02402         if (pp.coupled())
02403         {
02404             forAll(pp, i)
02405             {
02406                 label faceI = pp.start()+i;
02407                 neiCellZone[faceI-mesh_.nInternalFaces()] =
02408                     cellToZone[mesh_.faceOwner()[faceI]];
02409             }
02410         }
02411     }
02412     syncTools::swapBoundaryFaceList(mesh_, neiCellZone, false);
02413 
02414     // Set owner as no-flip
02415     forAll(patches, patchI)
02416     {
02417         const polyPatch& pp = patches[patchI];
02418 
02419         label faceI = pp.start();
02420 
02421         forAll(pp, i)
02422         {
02423             label surfI = namedSurfaceIndex[faceI];
02424 
02425             if (surfI != -1)
02426             {
02427                 label ownZone = cellToZone[faceOwner[faceI]];
02428                 label neiZone = neiCellZone[faceI-mesh_.nInternalFaces()];
02429 
02430                 bool flip;
02431                 if (ownZone == max(ownZone, neiZone))
02432                 {
02433                     flip = false;
02434                 }
02435                 else
02436                 {
02437                     flip = true;
02438                 }
02439 
02440                 meshMod.setAction
02441                 (
02442                     polyModifyFace
02443                     (
02444                         mesh_.faces()[faceI],           // modified face
02445                         faceI,                          // label of face
02446                         faceOwner[faceI],               // owner
02447                         -1,                             // neighbour
02448                         false,                          // face flip
02449                         patchI,                         // patch for face
02450                         false,                          // remove from zone
02451                         surfaceToFaceZone[surfI],       // zone for face
02452                         flip                            // face flip in zone
02453                     )
02454                 );
02455             }
02456             faceI++;
02457         }
02458     }
02459 
02460 
02461     // Put the cells into the correct zone
02462     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
02463 
02464     forAll(cellToZone, cellI)
02465     {
02466         label zoneI = cellToZone[cellI];
02467 
02468         if (zoneI >= 0)
02469         {
02470             meshMod.setAction
02471             (
02472                 polyModifyCell
02473                 (
02474                     cellI,
02475                     false,          // removeFromZone
02476                     zoneI
02477                 )
02478             );
02479         }
02480     }
02481 
02482     // Change the mesh (no inflation, parallel sync)
02483     autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh_, false, true);
02484 
02485     // Update fields
02486     mesh_.updateMesh(map);
02487 
02488     // Move mesh if in inflation mode
02489     if (map().hasMotionPoints())
02490     {
02491         mesh_.movePoints(map().preMotionPoints());
02492     }
02493     else
02494     {
02495         // Delete mesh volumes.
02496         mesh_.clearOut();
02497     }
02498 
02499     if (overwrite())
02500     {
02501         mesh_.setInstance(oldInstance());
02502     }
02503 
02504     // None of the faces has changed, only the zones. Still...
02505     updateMesh(map, labelList());
02506 
02507     return map;
02508 }
02509 
02510 
02511 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd