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

hexRef8.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 "hexRef8.H"
00028 
00029 #include "polyMesh.H"
00030 #include "polyTopoChange.H"
00031 #include "meshTools.H"
00032 #include "polyAddFace.H"
00033 #include "polyAddPoint.H"
00034 #include "polyAddCell.H"
00035 #include "polyModifyFace.H"
00036 #include "syncTools.H"
00037 #include "faceSet.H"
00038 #include "cellSet.H"
00039 #include "pointSet.H"
00040 #include "OFstream.H"
00041 #include "Time.H"
00042 #include "FaceCellWave.H"
00043 #include "mapDistributePolyMesh.H"
00044 #include "refinementData.H"
00045 #include "refinementDistanceData.H"
00046 
00047 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00048 
00049 namespace Foam
00050 {
00051     defineTypeNameAndDebug(hexRef8, 0);
00052 
00053     //- Reduction class. If x and y are not equal assign value.
00054     template<int value>
00055     class ifEqEqOp
00056     {
00057         public:
00058         void operator()(label& x, const label& y) const
00059         {
00060             x = (x==y) ? x : value;
00061         }
00062     };
00063 }
00064 
00065 
00066 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00067 
00068 void Foam::hexRef8::reorder
00069 (
00070     const labelList& map,
00071     const label len,
00072     const label null,
00073     labelList& elems
00074 )
00075 {
00076     labelList newElems(len, null);
00077 
00078     forAll(elems, i)
00079     {
00080         label newI = map[i];
00081 
00082         if (newI >= len)
00083         {
00084             FatalErrorIn("hexRef8::reorder(..)") << abort(FatalError);
00085         }
00086 
00087         if (newI >= 0)
00088         {
00089             newElems[newI] = elems[i];
00090         }
00091     }
00092 
00093     elems.transfer(newElems);
00094 }
00095 
00096 
00097 void Foam::hexRef8::getFaceInfo
00098 (
00099     const label faceI,
00100     label& patchID,
00101     label& zoneID,
00102     label& zoneFlip
00103 ) const
00104 {
00105     patchID = -1;
00106 
00107     if (!mesh_.isInternalFace(faceI))
00108     {
00109         patchID = mesh_.boundaryMesh().whichPatch(faceI);
00110     }
00111 
00112     zoneID = mesh_.faceZones().whichZone(faceI);
00113 
00114     zoneFlip = false;
00115 
00116     if (zoneID >= 0)
00117     {
00118         const faceZone& fZone = mesh_.faceZones()[zoneID];
00119 
00120         zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
00121     }
00122 }
00123 
00124 
00125 // Adds a face on top of existing faceI.
00126 Foam::label Foam::hexRef8::addFace
00127 (
00128     polyTopoChange& meshMod,
00129     const label faceI,
00130     const face& newFace,
00131     const label own,
00132     const label nei
00133 ) const
00134 {
00135     label patchID, zoneID, zoneFlip;
00136 
00137     getFaceInfo(faceI, patchID, zoneID, zoneFlip);
00138 
00139     label newFaceI = -1;
00140 
00141     if ((nei == -1) || (own < nei))
00142     {
00143         // Ordering ok.
00144         newFaceI = meshMod.setAction
00145         (
00146             polyAddFace
00147             (
00148                 newFace,                    // face
00149                 own,                        // owner
00150                 nei,                        // neighbour
00151                 -1,                         // master point
00152                 -1,                         // master edge
00153                 faceI,                      // master face for addition
00154                 false,                      // flux flip
00155                 patchID,                    // patch for face
00156                 zoneID,                     // zone for face
00157                 zoneFlip                    // face zone flip
00158             )
00159         );
00160     }
00161     else
00162     {
00163         // Reverse owner/neighbour
00164         newFaceI = meshMod.setAction
00165         (
00166             polyAddFace
00167             (
00168                 newFace.reverseFace(),      // face
00169                 nei,                        // owner
00170                 own,                        // neighbour
00171                 -1,                         // master point
00172                 -1,                         // master edge
00173                 faceI,                      // master face for addition
00174                 false,                      // flux flip
00175                 patchID,                    // patch for face
00176                 zoneID,                     // zone for face
00177                 zoneFlip                    // face zone flip
00178             )
00179         );
00180     }
00181     return newFaceI;
00182 }
00183 
00184 
00185 // Adds an internal face from an edge. Assumes orientation correct.
00186 // Problem is that the face is between four new vertices. So what do we provide
00187 // as master? The only existing mesh item we have is the edge we have split.
00188 // Have to be careful in only using it if it has internal faces since otherwise
00189 // polyMeshMorph will complain (because it cannot generate a sensible mapping
00190 // for the face)
00191 Foam::label Foam::hexRef8::addInternalFace
00192 (
00193     polyTopoChange& meshMod,
00194     const label meshFaceI,
00195     const label meshPointI,
00196     const face& newFace,
00197     const label own,
00198     const label nei
00199 ) const
00200 {
00201     if (mesh_.isInternalFace(meshFaceI))
00202     {
00203         return meshMod.setAction
00204         (
00205             polyAddFace
00206             (
00207                 newFace,                    // face
00208                 own,                        // owner
00209                 nei,                        // neighbour
00210                 -1,                         // master point
00211                 -1,                         // master edge
00212                 meshFaceI,                  // master face for addition
00213                 false,                      // flux flip
00214                 -1,                         // patch for face
00215                 -1,                         // zone for face
00216                 false                       // face zone flip
00217             )
00218         );
00219     }
00220     else
00221     {
00222         // Two choices:
00223         // - append (i.e. create out of nothing - will not be mapped)
00224         //   problem: field does not get mapped.
00225         // - inflate from point.
00226         //   problem: does interpolative mapping which constructs full
00227         //   volPointInterpolation!
00228 
00229         // For now create out of nothing
00230 
00231         return meshMod.setAction
00232         (
00233             polyAddFace
00234             (
00235                 newFace,                    // face
00236                 own,                        // owner
00237                 nei,                        // neighbour
00238                 -1,                         // master point
00239                 -1,                         // master edge
00240                 -1,                         // master face for addition
00241                 false,                      // flux flip
00242                 -1,                         // patch for face
00243                 -1,                         // zone for face
00244                 false                       // face zone flip
00245             )
00246         );
00247 
00248 
00251         //label masterPointI = -1;
00252         //
00253         //const labelList& pFaces = mesh_.pointFaces()[meshPointI];
00254         //
00255         //forAll(pFaces, i)
00256         //{
00257         //    if (mesh_.isInternalFace(pFaces[i]))
00258         //    {
00259         //        // meshPoint uses internal faces so ok to inflate from it
00260         //        masterPointI = meshPointI;
00261         //
00262         //        break;
00263         //    }
00264         //}
00265         //
00266         //return meshMod.setAction
00267         //(
00268         //    polyAddFace
00269         //    (
00270         //        newFace,                    // face
00271         //        own,                        // owner
00272         //        nei,                        // neighbour
00273         //        masterPointI,               // master point
00274         //        -1,                         // master edge
00275         //        -1,                         // master face for addition
00276         //        false,                      // flux flip
00277         //        -1,                         // patch for face
00278         //        -1,                         // zone for face
00279         //        false                       // face zone flip
00280         //    )
00281         //);
00282     }
00283 }
00284 
00285 
00286 // Modifies existing faceI for either new owner/neighbour or new face points.
00287 void Foam::hexRef8::modFace
00288 (
00289     polyTopoChange& meshMod,
00290     const label faceI,
00291     const face& newFace,
00292     const label own,
00293     const label nei
00294 ) const
00295 {
00296     label patchID, zoneID, zoneFlip;
00297 
00298     getFaceInfo(faceI, patchID, zoneID, zoneFlip);
00299 
00300     if
00301     (
00302         (own != mesh_.faceOwner()[faceI])
00303      || (
00304             mesh_.isInternalFace(faceI)
00305          && (nei != mesh_.faceNeighbour()[faceI])
00306         )
00307      || (newFace != mesh_.faces()[faceI])
00308     )
00309     {
00310         if ((nei == -1) || (own < nei))
00311         {
00312             meshMod.setAction
00313             (
00314                 polyModifyFace
00315                 (
00316                     newFace,            // modified face
00317                     faceI,              // label of face being modified
00318                     own,                // owner
00319                     nei,                // neighbour
00320                     false,              // face flip
00321                     patchID,            // patch for face
00322                     false,              // remove from zone
00323                     zoneID,             // zone for face
00324                     zoneFlip            // face flip in zone
00325                 )
00326             );
00327         }
00328         else
00329         {
00330             meshMod.setAction
00331             (
00332                 polyModifyFace
00333                 (
00334                     newFace.reverseFace(),  // modified face
00335                     faceI,                  // label of face being modified
00336                     nei,                    // owner
00337                     own,                    // neighbour
00338                     false,                  // face flip
00339                     patchID,                // patch for face
00340                     false,                  // remove from zone
00341                     zoneID,                 // zone for face
00342                     zoneFlip                // face flip in zone
00343                 )
00344             );
00345         }
00346     }
00347 }
00348 
00349 
00350 // Bit complex way to determine the unrefined edge length.
00351 Foam::scalar Foam::hexRef8::getLevel0EdgeLength() const
00352 {
00353     // Determine minimum edge length per refinement level
00354     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00355 
00356     const scalar GREAT2 = sqr(GREAT);
00357 
00358     label nLevels = gMax(cellLevel_)+1;
00359 
00360     scalarField typEdgeLenSqr(nLevels, GREAT2);
00361 
00362 
00363     // 1. Look only at edges surrounded by cellLevel cells only.
00364     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00365 
00366     {
00367         // Per edge the cellLevel of connected cells. -1 if not set,
00368         // labelMax if different levels, otherwise levels of connected cells.
00369         labelList edgeLevel(mesh_.nEdges(), -1);
00370 
00371         forAll(cellLevel_, cellI)
00372         {
00373             const label cLevel = cellLevel_[cellI];
00374 
00375             const labelList& cEdges = mesh_.cellEdges(cellI);
00376 
00377             forAll(cEdges, i)
00378             {
00379                 label edgeI = cEdges[i];
00380 
00381                 if (edgeLevel[edgeI] == -1)
00382                 {
00383                     edgeLevel[edgeI] = cLevel;
00384                 }
00385                 else if (edgeLevel[edgeI] == labelMax)
00386                 {
00387                     // Already marked as on different cellLevels
00388                 }
00389                 else if (edgeLevel[edgeI] != cLevel)
00390                 {
00391                     edgeLevel[edgeI] = labelMax;
00392                 }
00393             }
00394         }
00395 
00396         // Make sure that edges with different levels on different processors
00397         // are also marked. Do the same test (edgeLevel != cLevel) on coupled
00398         // edges.
00399         syncTools::syncEdgeList
00400         (
00401             mesh_,
00402             edgeLevel,
00403             ifEqEqOp<labelMax>(),
00404             labelMin,
00405             false               // no separation
00406         );
00407 
00408         // Now use the edgeLevel with a valid value to determine the
00409         // length per level.
00410         forAll(edgeLevel, edgeI)
00411         {
00412             const label eLevel = edgeLevel[edgeI];
00413 
00414             if (eLevel >= 0 && eLevel < labelMax)
00415             {
00416                 const edge& e = mesh_.edges()[edgeI];
00417 
00418                 scalar edgeLenSqr = magSqr(e.vec(mesh_.points()));
00419 
00420                 typEdgeLenSqr[eLevel] = min(typEdgeLenSqr[eLevel], edgeLenSqr);
00421             }
00422         }
00423     }
00424 
00425     // Get the minimum per level over all processors. Note minimum so if
00426     // cells are not cubic we use the smallest edge side.
00427     Pstream::listCombineGather(typEdgeLenSqr, minEqOp<scalar>());
00428     Pstream::listCombineScatter(typEdgeLenSqr);
00429 
00430     if (debug)
00431     {
00432         Pout<< "hexRef8::getLevel0EdgeLength() :"
00433             << " After phase1: Edgelengths (squared) per refinementlevel:"
00434             << typEdgeLenSqr << endl;
00435     }
00436 
00437 
00438     // 2. For any levels where we haven't determined a valid length yet
00439     //    use any surrounding cell level. Here we use the max so we don't
00440     //    pick up levels between celllevel and higher celllevel (will have
00441     //    edges sized according to highest celllevel)
00442     //    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00443 
00444     scalarField maxEdgeLenSqr(nLevels, -GREAT2);
00445 
00446     forAll(cellLevel_, cellI)
00447     {
00448         const label cLevel = cellLevel_[cellI];
00449 
00450         const labelList& cEdges = mesh_.cellEdges(cellI);
00451 
00452         forAll(cEdges, i)
00453         {
00454             const edge& e = mesh_.edges()[cEdges[i]];
00455 
00456             scalar edgeLenSqr = magSqr(e.vec(mesh_.points()));
00457 
00458             maxEdgeLenSqr[cLevel] = max(maxEdgeLenSqr[cLevel], edgeLenSqr);
00459         }
00460     }
00461 
00462     Pstream::listCombineGather(maxEdgeLenSqr, maxEqOp<scalar>());
00463     Pstream::listCombineScatter(maxEdgeLenSqr);
00464 
00465     if (debug)
00466     {
00467         Pout<< "hexRef8::getLevel0EdgeLength() :"
00468             << " Crappy Edgelengths (squared) per refinementlevel:"
00469             << maxEdgeLenSqr << endl;
00470     }
00471 
00472 
00473     // 3. Combine the two sets of lengths
00474     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00475 
00476     forAll(typEdgeLenSqr, levelI)
00477     {
00478         if (typEdgeLenSqr[levelI] == GREAT2 && maxEdgeLenSqr[levelI] >= 0)
00479         {
00480             typEdgeLenSqr[levelI] = maxEdgeLenSqr[levelI];
00481         }
00482     }
00483 
00484     if (debug)
00485     {
00486         Pout<< "hexRef8::getLevel0EdgeLength() :"
00487             << " Final Edgelengths (squared) per refinementlevel:"
00488             << typEdgeLenSqr << endl;
00489     }
00490 
00491     // Find lowest level present
00492     scalar level0Size = -1;
00493 
00494     forAll(typEdgeLenSqr, levelI)
00495     {
00496         scalar lenSqr = typEdgeLenSqr[levelI];
00497 
00498         if (lenSqr < GREAT2)
00499         {
00500             level0Size = Foam::sqrt(lenSqr)*(1<<levelI);
00501 
00502             if (debug)
00503             {
00504                 Pout<< "hexRef8::getLevel0EdgeLength() :"
00505                     << " For level:" << levelI
00506                     << " have edgeLen:" << Foam::sqrt(lenSqr)
00507                     << " with equivalent level0 len:" << level0Size
00508                     << endl;
00509             }
00510             break;
00511         }
00512     }
00513 
00514     if (level0Size == -1)
00515     {
00516         FatalErrorIn("hexRef8::getLevel0EdgeLength()")
00517             << "Problem : typEdgeLenSqr:" << typEdgeLenSqr << abort(FatalError);
00518     }
00519 
00520     return level0Size;
00521 }
00522 
00523 
00524 // Check whether pointI is an anchor on cellI.
00525 // If it is not check whether any other point on the face is an anchor cell.
00526 Foam::label Foam::hexRef8::getAnchorCell
00527 (
00528     const labelListList& cellAnchorPoints,
00529     const labelListList& cellAddedCells,
00530     const label cellI,
00531     const label faceI,
00532     const label pointI
00533 ) const
00534 {
00535     if (cellAnchorPoints[cellI].size())
00536     {
00537         label index = findIndex(cellAnchorPoints[cellI], pointI);
00538 
00539         if (index != -1)
00540         {
00541             return cellAddedCells[cellI][index];
00542         }
00543 
00544 
00545         // pointI is not an anchor cell.
00546         // Maybe we are already a refined face so check all the face
00547         // vertices.
00548         const face& f = mesh_.faces()[faceI];
00549 
00550         forAll(f, fp)
00551         {
00552             label index = findIndex(cellAnchorPoints[cellI], f[fp]);
00553 
00554             if (index != -1)
00555             {
00556                 return cellAddedCells[cellI][index];
00557             }
00558         }
00559 
00560         // Problem.
00561         dumpCell(cellI);
00562         Perr<< "cell:" << cellI << " anchorPoints:" << cellAnchorPoints[cellI]
00563             << endl;
00564 
00565         FatalErrorIn("hexRef8::getAnchorCell(..)")
00566             << "Could not find point " << pointI
00567             << " in the anchorPoints for cell " << cellI << endl
00568             << "Does your original mesh obey the 2:1 constraint and"
00569             << " did you use consistentRefinement to make your cells to refine"
00570             << " obey this constraint as well?"
00571             << abort(FatalError);
00572 
00573         return -1;
00574     }
00575     else
00576     {
00577         return cellI;
00578     }
00579 }
00580 
00581 
00582 // Get new owner and neighbour
00583 void Foam::hexRef8::getFaceNeighbours
00584 (
00585     const labelListList& cellAnchorPoints,
00586     const labelListList& cellAddedCells,
00587     const label faceI,
00588     const label pointI,
00589 
00590     label& own,
00591     label& nei
00592 ) const
00593 {
00594     // Is owner split?
00595     own = getAnchorCell
00596     (
00597         cellAnchorPoints,
00598         cellAddedCells,
00599         mesh_.faceOwner()[faceI],
00600         faceI,
00601         pointI
00602     );
00603 
00604     if (mesh_.isInternalFace(faceI))
00605     {
00606         nei = getAnchorCell
00607         (
00608             cellAnchorPoints,
00609             cellAddedCells,
00610             mesh_.faceNeighbour()[faceI],
00611             faceI,
00612             pointI
00613         );
00614     }
00615     else
00616     {
00617         nei = -1;
00618     }
00619 }
00620 
00621 
00622 // Get point with the lowest pointLevel
00623 Foam::label Foam::hexRef8::findMinLevel(const labelList& f) const
00624 {
00625     label minLevel = labelMax;
00626     label minFp = -1;
00627 
00628     forAll(f, fp)
00629     {
00630         label level = pointLevel_[f[fp]];
00631 
00632         if (level < minLevel)
00633         {
00634             minLevel = level;
00635             minFp = fp;
00636         }
00637     }
00638 
00639     return minFp;
00640 }
00641 
00642 
00643 // Get point with the highest pointLevel
00644 Foam::label Foam::hexRef8::findMaxLevel(const labelList& f) const
00645 {
00646     label maxLevel = labelMin;
00647     label maxFp = -1;
00648 
00649     forAll(f, fp)
00650     {
00651         label level = pointLevel_[f[fp]];
00652 
00653         if (level > maxLevel)
00654         {
00655             maxLevel = level;
00656             maxFp = fp;
00657         }
00658     }
00659 
00660     return maxFp;
00661 }
00662 
00663 
00664 Foam::label Foam::hexRef8::countAnchors
00665 (
00666     const labelList& f,
00667     const label anchorLevel
00668 ) const
00669 {
00670     label nAnchors = 0;
00671 
00672     forAll(f, fp)
00673     {
00674         if (pointLevel_[f[fp]] <= anchorLevel)
00675         {
00676             nAnchors++;
00677         }
00678     }
00679     return nAnchors;
00680 }
00681 
00682 
00683 void Foam::hexRef8::dumpCell(const label cellI) const
00684 {
00685     OFstream str(mesh_.time().path()/"cell_" + Foam::name(cellI) + ".obj");
00686     Pout<< "hexRef8 : Dumping cell as obj to " << str.name() << endl;
00687 
00688     const cell& cFaces = mesh_.cells()[cellI];
00689 
00690     Map<label> pointToObjVert;
00691     label objVertI = 0;
00692 
00693     forAll(cFaces, i)
00694     {
00695         const face& f = mesh_.faces()[cFaces[i]];
00696 
00697         forAll(f, fp)
00698         {
00699             if (pointToObjVert.insert(f[fp], objVertI))
00700             {
00701                 meshTools::writeOBJ(str, mesh_.points()[f[fp]]);
00702                 objVertI++;
00703             }
00704         }
00705     }
00706 
00707     forAll(cFaces, i)
00708     {
00709         const face& f = mesh_.faces()[cFaces[i]];
00710 
00711         forAll(f, fp)
00712         {
00713             label pointI = f[fp];
00714             label nexPointI = f[f.fcIndex(fp)];
00715 
00716             str << "l " << pointToObjVert[pointI]+1
00717                 << ' ' << pointToObjVert[nexPointI]+1 << nl;
00718         }
00719     }
00720 }
00721 
00722 
00723 // Find point with certain pointLevel. Skip any higher levels.
00724 Foam::label Foam::hexRef8::findLevel
00725 (
00726     const label faceI,
00727     const face& f,
00728     const label startFp,
00729     const bool searchForward,
00730     const label wantedLevel
00731 ) const
00732 {
00733     label fp = startFp;
00734 
00735     forAll(f, i)
00736     {
00737         label pointI = f[fp];
00738 
00739         if (pointLevel_[pointI] < wantedLevel)
00740         {
00741             dumpCell(mesh_.faceOwner()[faceI]);
00742             if (mesh_.isInternalFace(faceI))
00743             {
00744                 dumpCell(mesh_.faceNeighbour()[faceI]);
00745             }
00746 
00747             FatalErrorIn("hexRef8::findLevel(..)")
00748                 << "face:" << f
00749                 << " level:" << UIndirectList<label>(pointLevel_, f)()
00750                 << " startFp:" << startFp
00751                 << " wantedLevel:" << wantedLevel
00752                 << abort(FatalError);
00753         }
00754         else if (pointLevel_[pointI] == wantedLevel)
00755         {
00756             return fp;
00757         }
00758 
00759         if (searchForward)
00760         {
00761             fp = f.fcIndex(fp);
00762         }
00763         else
00764         {
00765             fp = f.rcIndex(fp);
00766         }
00767     }
00768 
00769     dumpCell(mesh_.faceOwner()[faceI]);
00770     if (mesh_.isInternalFace(faceI))
00771     {
00772         dumpCell(mesh_.faceNeighbour()[faceI]);
00773     }
00774 
00775     FatalErrorIn("hexRef8::findLevel(..)")
00776         << "face:" << f
00777         << " level:" << UIndirectList<label>(pointLevel_, f)()
00778         << " startFp:" << startFp
00779         << " wantedLevel:" << wantedLevel
00780         << abort(FatalError);
00781 
00782     return -1;
00783 }
00784 
00785 
00786 // Gets cell level such that the face has four points <= level.
00787 Foam::label Foam::hexRef8::getAnchorLevel(const label faceI) const
00788 {
00789     const face& f = mesh_.faces()[faceI];
00790 
00791     if (f.size() <= 4)
00792     {
00793         return pointLevel_[f[findMaxLevel(f)]];
00794     }
00795     else
00796     {
00797         label ownLevel = cellLevel_[mesh_.faceOwner()[faceI]];
00798 
00799         if (countAnchors(f, ownLevel) == 4)
00800         {
00801             return ownLevel;
00802         }
00803         else if (countAnchors(f, ownLevel+1) == 4)
00804         {
00805             return ownLevel+1;
00806         }
00807         else
00808         {
00809             return -1;
00810         }
00811     }
00812 }
00813 
00814 
00815 void Foam::hexRef8::checkInternalOrientation
00816 (
00817     polyTopoChange& meshMod,
00818     const label cellI,
00819     const label faceI,
00820     const point& ownPt,
00821     const point& neiPt,
00822     const face& newFace
00823 )
00824 {
00825     face compactFace(identity(newFace.size()));
00826     pointField compactPoints(meshMod.points(), newFace);
00827 
00828     vector n(compactFace.normal(compactPoints));
00829 
00830     vector dir(neiPt - ownPt);
00831 
00832     if ((dir & n) < 0)
00833     {
00834         FatalErrorIn("checkInternalOrientation(..)")
00835             << "cell:" << cellI << " old face:" << faceI
00836             << " newFace:" << newFace << endl
00837             << " coords:" << compactPoints
00838             << " ownPt:" << ownPt
00839             << " neiPt:" << neiPt
00840             << abort(FatalError);
00841     }
00842 
00843     vector fcToOwn(compactFace.centre(compactPoints) - ownPt);
00844 
00845     scalar s = (fcToOwn&n) / (dir&n);
00846 
00847     if (s < 0.1 || s > 0.9)
00848     {
00849         FatalErrorIn("checkInternalOrientation(..)")
00850             << "cell:" << cellI << " old face:" << faceI
00851             << " newFace:" << newFace << endl
00852             << " coords:" << compactPoints
00853             << " ownPt:" << ownPt
00854             << " neiPt:" << neiPt
00855             << " s:" << s
00856             << abort(FatalError);
00857     }
00858 }
00859 
00860 
00861 void Foam::hexRef8::checkBoundaryOrientation
00862 (
00863     polyTopoChange& meshMod,
00864     const label cellI,
00865     const label faceI,
00866     const point& ownPt,
00867     const point& boundaryPt,
00868     const face& newFace
00869 )
00870 {
00871     face compactFace(identity(newFace.size()));
00872     pointField compactPoints(meshMod.points(), newFace);
00873 
00874     vector n(compactFace.normal(compactPoints));
00875 
00876     vector dir(boundaryPt - ownPt);
00877 
00878     if ((dir & n) < 0)
00879     {
00880         FatalErrorIn("checkBoundaryOrientation(..)")
00881             << "cell:" << cellI << " old face:" << faceI
00882             << " newFace:" << newFace
00883             << " coords:" << compactPoints
00884             << " ownPt:" << ownPt
00885             << " boundaryPt:" << boundaryPt
00886             << abort(FatalError);
00887     }
00888 
00889     vector fcToOwn(compactFace.centre(compactPoints) - ownPt);
00890 
00891     scalar s = (fcToOwn&dir) / magSqr(dir);
00892 
00893     if (s < 0.7 || s > 1.3)
00894     {
00895         WarningIn("checkBoundaryOrientation(..)")
00896             << "cell:" << cellI << " old face:" << faceI
00897             << " newFace:" << newFace
00898             << " coords:" << compactPoints
00899             << " ownPt:" << ownPt
00900             << " boundaryPt:" << boundaryPt
00901             << " s:" << s
00902             << endl;
00903             //<< abort(FatalError);
00904     }
00905 }
00906 
00907 
00908 // If p0 and p1 are existing vertices check if edge is split and insert
00909 // splitPoint.
00910 void Foam::hexRef8::insertEdgeSplit
00911 (
00912     const labelList& edgeMidPoint,
00913     const label p0,
00914     const label p1,
00915     DynamicList<label>& verts
00916 ) const
00917 {
00918     if (p0 < mesh_.nPoints() && p1 < mesh_.nPoints())
00919     {
00920         label edgeI = meshTools::findEdge(mesh_, p0, p1);
00921 
00922         if (edgeI != -1 && edgeMidPoint[edgeI] != -1)
00923         {
00924             verts.append(edgeMidPoint[edgeI]);
00925         }
00926     }
00927 }
00928 
00929 
00930 // Internal faces are one per edge between anchor points. So one per midPoint
00931 // between the anchor points. Here we store the information on the midPoint
00932 // and if we have enough information:
00933 // - two anchors
00934 // - two face mid points
00935 // we add the face. Note that this routine can get called anywhere from
00936 // two times (two unrefined faces) to four times (two refined faces) so
00937 // the first call that adds the information creates the face.
00938 Foam::label Foam::hexRef8::storeMidPointInfo
00939 (
00940     const labelListList& cellAnchorPoints,
00941     const labelListList& cellAddedCells,
00942     const labelList& cellMidPoint,
00943     const labelList& edgeMidPoint,
00944     const label cellI,
00945     const label faceI,
00946     const bool faceOrder,
00947     const label edgeMidPointI,
00948     const label anchorPointI,
00949     const label faceMidPointI,
00950 
00951     Map<edge>& midPointToAnchors,
00952     Map<edge>& midPointToFaceMids,
00953     polyTopoChange& meshMod
00954 ) const
00955 {
00956     // See if need to store anchors.
00957 
00958     bool changed = false;
00959     bool haveTwoAnchors = false;
00960 
00961     Map<edge>::iterator edgeMidFnd = midPointToAnchors.find(edgeMidPointI);
00962 
00963     if (edgeMidFnd == midPointToAnchors.end())
00964     {
00965         midPointToAnchors.insert(edgeMidPointI, edge(anchorPointI, -1));
00966     }
00967     else
00968     {
00969         edge& e = edgeMidFnd();
00970 
00971         if (anchorPointI != e[0])
00972         {
00973             if (e[1] == -1)
00974             {
00975                 e[1] = anchorPointI;
00976                 changed = true;
00977             }
00978         }
00979 
00980         if (e[0] != -1 && e[1] != -1)
00981         {
00982             haveTwoAnchors = true;
00983         }
00984     }
00985 
00986     bool haveTwoFaceMids = false;
00987 
00988     Map<edge>::iterator faceMidFnd = midPointToFaceMids.find(edgeMidPointI);
00989 
00990     if (faceMidFnd == midPointToFaceMids.end())
00991     {
00992         midPointToFaceMids.insert(edgeMidPointI, edge(faceMidPointI, -1));
00993     }
00994     else
00995     {
00996         edge& e = faceMidFnd();
00997 
00998         if (faceMidPointI != e[0])
00999         {
01000             if (e[1] == -1)
01001             {
01002                 e[1] = faceMidPointI;
01003                 changed = true;
01004             }
01005         }
01006 
01007         if (e[0] != -1 && e[1] != -1)
01008         {
01009             haveTwoFaceMids = true;
01010         }
01011     }
01012 
01013     // Check if this call of storeMidPointInfo is the one that completed all
01014     // the nessecary information.
01015 
01016     if (changed && haveTwoAnchors && haveTwoFaceMids)
01017     {
01018         const edge& anchors = midPointToAnchors[edgeMidPointI];
01019         const edge& faceMids = midPointToFaceMids[edgeMidPointI];
01020 
01021         label otherFaceMidPointI = faceMids.otherVertex(faceMidPointI);
01022 
01023         // Create face consistent with anchorI being the owner.
01024         // Note that the edges between the edge mid point and the face mids
01025         // might be marked for splitting. Note that these edge splits cannot
01026         // be between cellMid and face mids.
01027 
01028         DynamicList<label> newFaceVerts(4);
01029         if (faceOrder == (mesh_.faceOwner()[faceI] == cellI))
01030         {
01031             newFaceVerts.append(faceMidPointI);
01032 
01033             // Check & insert edge split if any
01034             insertEdgeSplit
01035             (
01036                 edgeMidPoint,
01037                 faceMidPointI,  // edge between faceMid and
01038                 edgeMidPointI,  // edgeMid
01039                 newFaceVerts
01040             );
01041 
01042             newFaceVerts.append(edgeMidPointI);
01043 
01044             insertEdgeSplit
01045             (
01046                 edgeMidPoint,
01047                 edgeMidPointI,
01048                 otherFaceMidPointI,
01049                 newFaceVerts
01050             );
01051 
01052             newFaceVerts.append(otherFaceMidPointI);
01053             newFaceVerts.append(cellMidPoint[cellI]);
01054         }
01055         else
01056         {
01057             newFaceVerts.append(otherFaceMidPointI);
01058 
01059             insertEdgeSplit
01060             (
01061                 edgeMidPoint,
01062                 otherFaceMidPointI,
01063                 edgeMidPointI,
01064                 newFaceVerts
01065             );
01066 
01067             newFaceVerts.append(edgeMidPointI);
01068 
01069             insertEdgeSplit
01070             (
01071                 edgeMidPoint,
01072                 edgeMidPointI,
01073                 faceMidPointI,
01074                 newFaceVerts
01075             );
01076 
01077             newFaceVerts.append(faceMidPointI);
01078             newFaceVerts.append(cellMidPoint[cellI]);
01079         }
01080 
01081         face newFace;
01082         newFace.transfer(newFaceVerts);
01083 
01084         label anchorCell0 = getAnchorCell
01085         (
01086             cellAnchorPoints,
01087             cellAddedCells,
01088             cellI,
01089             faceI,
01090             anchorPointI
01091         );
01092         label anchorCell1 = getAnchorCell
01093         (
01094             cellAnchorPoints,
01095             cellAddedCells,
01096             cellI,
01097             faceI,
01098             anchors.otherVertex(anchorPointI)
01099         );
01100 
01101 
01102         label own, nei;
01103         point ownPt, neiPt;
01104 
01105         if (anchorCell0 < anchorCell1)
01106         {
01107             own = anchorCell0;
01108             nei = anchorCell1;
01109 
01110             ownPt = mesh_.points()[anchorPointI];
01111             neiPt = mesh_.points()[anchors.otherVertex(anchorPointI)];
01112 
01113         }
01114         else
01115         {
01116             own = anchorCell1;
01117             nei = anchorCell0;
01118             newFace = newFace.reverseFace();
01119 
01120             ownPt = mesh_.points()[anchors.otherVertex(anchorPointI)];
01121             neiPt = mesh_.points()[anchorPointI];
01122         }
01123 
01124         if (debug)
01125         {
01126             point ownPt, neiPt;
01127 
01128             if (anchorCell0 < anchorCell1)
01129             {
01130                 ownPt = mesh_.points()[anchorPointI];
01131                 neiPt = mesh_.points()[anchors.otherVertex(anchorPointI)];
01132             }
01133             else
01134             {
01135                 ownPt = mesh_.points()[anchors.otherVertex(anchorPointI)];
01136                 neiPt = mesh_.points()[anchorPointI];
01137             }
01138 
01139             checkInternalOrientation
01140             (
01141                 meshMod,
01142                 cellI,
01143                 faceI,
01144                 ownPt,
01145                 neiPt,
01146                 newFace
01147             );
01148         }
01149 
01150         return addInternalFace
01151         (
01152             meshMod,
01153             faceI,
01154             anchorPointI,
01155             newFace,
01156             own,
01157             nei
01158         );
01159     }
01160     else
01161     {
01162         return -1;
01163     }
01164 }
01165 
01166 
01167 // Creates all the 12 internal faces for cellI.
01168 void Foam::hexRef8::createInternalFaces
01169 (
01170     const labelListList& cellAnchorPoints,
01171     const labelListList& cellAddedCells,
01172     const labelList& cellMidPoint,
01173     const labelList& faceMidPoint,
01174     const labelList& faceAnchorLevel,
01175     const labelList& edgeMidPoint,
01176     const label cellI,
01177 
01178     polyTopoChange& meshMod
01179 ) const
01180 {
01181     // Find in every face the cellLevel+1 points (from edge subdivision)
01182     // and the anchor points.
01183 
01184     const cell& cFaces = mesh_.cells()[cellI];
01185     const label cLevel = cellLevel_[cellI];
01186 
01187     // From edge mid to anchor points
01188     Map<edge> midPointToAnchors(24);
01189     // From edge mid to face mids
01190     Map<edge> midPointToFaceMids(24);
01191 
01192     // Storage for on-the-fly addressing
01193     DynamicList<label> storage;
01194 
01195 
01196     // Running count of number of internal faces added so far.
01197     label nFacesAdded = 0;
01198 
01199     forAll(cFaces, i)
01200     {
01201         label faceI = cFaces[i];
01202 
01203         const face& f = mesh_.faces()[faceI];
01204         const labelList& fEdges = mesh_.faceEdges(faceI, storage);
01205 
01206         // We are on the cellI side of face f. The face will have 1 or 4
01207         // cLevel points and lots of higher numbered ones.
01208 
01209         label faceMidPointI = -1;
01210 
01211         label nAnchors = countAnchors(f, cLevel);
01212 
01213         if (nAnchors == 1)
01214         {
01215             // Only one anchor point. So the other side of the face has already
01216             // been split using cLevel+1 and cLevel+2 points.
01217 
01218             // Find the one anchor.
01219             label anchorFp = -1;
01220 
01221             forAll(f, fp)
01222             {
01223                 if (pointLevel_[f[fp]] <= cLevel)
01224                 {
01225                     anchorFp = fp;
01226                     break;
01227                 }
01228             }
01229 
01230             // Now the face mid point is the second cLevel+1 point
01231             label edgeMid = findLevel
01232             (
01233                 faceI,
01234                 f,
01235                 f.fcIndex(anchorFp),
01236                 true,
01237                 cLevel+1
01238             );
01239             label faceMid = findLevel
01240             (
01241                 faceI,
01242                 f,
01243                 f.fcIndex(edgeMid),
01244                 true,
01245                 cLevel+1
01246             );
01247 
01248             faceMidPointI = f[faceMid];
01249         }
01250         else if (nAnchors == 4)
01251         {
01252             // There is no face middle yet but the face will be marked for
01253             // splitting.
01254 
01255             faceMidPointI = faceMidPoint[faceI];
01256         }
01257         else
01258         {
01259             dumpCell(mesh_.faceOwner()[faceI]);
01260             if (mesh_.isInternalFace(faceI))
01261             {
01262                 dumpCell(mesh_.faceNeighbour()[faceI]);
01263             }
01264 
01265             FatalErrorIn("createInternalFaces(..)")
01266                 << "nAnchors:" << nAnchors
01267                 << " faceI:" << faceI
01268                 << abort(FatalError);
01269         }
01270 
01271 
01272 
01273         // Now loop over all the anchors (might be just one) and store
01274         // the edge mids connected to it. storeMidPointInfo will collect
01275         // all the info and combine it all.
01276 
01277         forAll(f, fp0)
01278         {
01279             label point0 = f[fp0];
01280 
01281             if (pointLevel_[point0] <= cLevel)
01282             {
01283                 // Anchor.
01284 
01285                 // Walk forward
01286                 // ~~~~~~~~~~~~
01287                 // to cLevel+1 or edgeMidPoint of this level.
01288 
01289 
01290                 label edgeMidPointI = -1;
01291 
01292                 label fp1 = f.fcIndex(fp0);
01293 
01294                 if (pointLevel_[f[fp1]] <= cLevel)
01295                 {
01296                     // Anchor. Edge will be split.
01297                     label edgeI = fEdges[fp0];
01298 
01299                     edgeMidPointI = edgeMidPoint[edgeI];
01300 
01301                     if (edgeMidPointI == -1)
01302                     {
01303                         dumpCell(cellI);
01304 
01305                         const labelList& cPoints = mesh_.cellPoints(cellI);
01306 
01307                         FatalErrorIn("createInternalFaces(..)")
01308                             << "cell:" << cellI << " cLevel:" << cLevel
01309                             << " cell points:" << cPoints
01310                             << " pointLevel:"
01311                             << UIndirectList<label>(pointLevel_, cPoints)()
01312                             << " face:" << faceI
01313                             << " f:" << f
01314                             << " pointLevel:"
01315                             << UIndirectList<label>(pointLevel_, f)()
01316                             << " faceAnchorLevel:" << faceAnchorLevel[faceI]
01317                             << " faceMidPoint:" << faceMidPoint[faceI]
01318                             << " faceMidPointI:" << faceMidPointI
01319                             << " fp:" << fp0
01320                             << abort(FatalError);
01321                     }
01322                 }
01323                 else
01324                 {
01325                     // Search forward in face to clevel+1
01326                     label edgeMid = findLevel(faceI, f, fp1, true, cLevel+1);
01327 
01328                     edgeMidPointI = f[edgeMid];
01329                 }
01330 
01331                 label newFaceI = storeMidPointInfo
01332                 (
01333                     cellAnchorPoints,
01334                     cellAddedCells,
01335                     cellMidPoint,
01336                     edgeMidPoint,
01337 
01338                     cellI,
01339                     faceI,
01340                     true,                   // mid point after anchor
01341                     edgeMidPointI,          // edgemid
01342                     point0,                 // anchor
01343                     faceMidPointI,
01344 
01345                     midPointToAnchors,
01346                     midPointToFaceMids,
01347                     meshMod
01348                 );
01349 
01350                 if (newFaceI != -1)
01351                 {
01352                     nFacesAdded++;
01353 
01354                     if (nFacesAdded == 12)
01355                     {
01356                         break;
01357                     }
01358                 }
01359 
01360 
01361 
01362                 // Walk backward
01363                 // ~~~~~~~~~~~~~
01364 
01365                 label fpMin1 = f.rcIndex(fp0);
01366 
01367                 if (pointLevel_[f[fpMin1]] <= cLevel)
01368                 {
01369                     // Anchor. Edge will be split.
01370                     label edgeI = fEdges[fpMin1];
01371 
01372                     edgeMidPointI = edgeMidPoint[edgeI];
01373 
01374                     if (edgeMidPointI == -1)
01375                     {
01376                         dumpCell(cellI);
01377 
01378                         const labelList& cPoints = mesh_.cellPoints(cellI);
01379 
01380                         FatalErrorIn("createInternalFaces(..)")
01381                             << "cell:" << cellI << " cLevel:" << cLevel
01382                             << " cell points:" << cPoints
01383                             << " pointLevel:"
01384                             << UIndirectList<label>(pointLevel_, cPoints)()
01385                             << " face:" << faceI
01386                             << " f:" << f
01387                             << " pointLevel:"
01388                             << UIndirectList<label>(pointLevel_, f)()
01389                             << " faceAnchorLevel:" << faceAnchorLevel[faceI]
01390                             << " faceMidPoint:" << faceMidPoint[faceI]
01391                             << " faceMidPointI:" << faceMidPointI
01392                             << " fp:" << fp0
01393                             << abort(FatalError);
01394                     }
01395                 }
01396                 else
01397                 {
01398                     // Search back to clevel+1
01399                     label edgeMid = findLevel
01400                     (
01401                         faceI,
01402                         f,
01403                         fpMin1,
01404                         false,
01405                         cLevel+1
01406                     );
01407 
01408                     edgeMidPointI = f[edgeMid];
01409                 }
01410 
01411                 newFaceI = storeMidPointInfo
01412                 (
01413                     cellAnchorPoints,
01414                     cellAddedCells,
01415                     cellMidPoint,
01416                     edgeMidPoint,
01417 
01418                     cellI,
01419                     faceI,
01420                     false,                  // mid point before anchor
01421                     edgeMidPointI,          // edgemid
01422                     point0,                 // anchor
01423                     faceMidPointI,
01424 
01425                     midPointToAnchors,
01426                     midPointToFaceMids,
01427                     meshMod
01428                 );
01429 
01430                 if (newFaceI != -1)
01431                 {
01432                     nFacesAdded++;
01433 
01434                     if (nFacesAdded == 12)
01435                     {
01436                         break;
01437                     }
01438                 }
01439             }   // done anchor
01440         }   // done face
01441 
01442         if (nFacesAdded == 12)
01443         {
01444             break;
01445         }
01446     }
01447 }
01448 
01449 
01450 void Foam::hexRef8::walkFaceToMid
01451 (
01452     const labelList& edgeMidPoint,
01453     const label cLevel,
01454     const label faceI,
01455     const label startFp,
01456     DynamicList<label>& faceVerts
01457 ) const
01458 {
01459     const face& f = mesh_.faces()[faceI];
01460     const labelList& fEdges = mesh_.faceEdges(faceI);
01461 
01462     label fp = startFp;
01463 
01464     // Starting from fp store all (1 or 2) vertices until where the face
01465     // gets split
01466 
01467     while (true)
01468     {
01469         if (edgeMidPoint[fEdges[fp]] >= 0)
01470         {
01471             faceVerts.append(edgeMidPoint[fEdges[fp]]);
01472         }
01473 
01474         fp = f.fcIndex(fp);
01475 
01476         if (pointLevel_[f[fp]] <= cLevel)
01477         {
01478             // Next anchor. Have already append split point on edge in code
01479             // above.
01480             return;
01481         }
01482         else if (pointLevel_[f[fp]] == cLevel+1)
01483         {
01484             // Mid level
01485             faceVerts.append(f[fp]);
01486 
01487             return;
01488         }
01489         else if (pointLevel_[f[fp]] == cLevel+2)
01490         {
01491             // Store and continue to cLevel+1.
01492             faceVerts.append(f[fp]);
01493         }
01494     }
01495 }
01496 
01497 
01498 // Same as walkFaceToMid but now walk back.
01499 void Foam::hexRef8::walkFaceFromMid
01500 (
01501     const labelList& edgeMidPoint,
01502     const label cLevel,
01503     const label faceI,
01504     const label startFp,
01505     DynamicList<label>& faceVerts
01506 ) const
01507 {
01508     const face& f = mesh_.faces()[faceI];
01509     const labelList& fEdges = mesh_.faceEdges(faceI);
01510 
01511     label fp = f.rcIndex(startFp);
01512 
01513     while (true)
01514     {
01515         if (pointLevel_[f[fp]] <= cLevel)
01516         {
01517             // anchor.
01518             break;
01519         }
01520         else if (pointLevel_[f[fp]] == cLevel+1)
01521         {
01522             // Mid level
01523             faceVerts.append(f[fp]);
01524             break;
01525         }
01526         else if (pointLevel_[f[fp]] == cLevel+2)
01527         {
01528             // Continue to cLevel+1.
01529         }
01530         fp = f.rcIndex(fp);
01531     }
01532 
01533     // Store
01534     while (true)
01535     {
01536         if (edgeMidPoint[fEdges[fp]] >= 0)
01537         {
01538             faceVerts.append(edgeMidPoint[fEdges[fp]]);
01539         }
01540 
01541         fp = f.fcIndex(fp);
01542 
01543         if (fp == startFp)
01544         {
01545             break;
01546         }
01547         faceVerts.append(f[fp]);
01548     }
01549 }
01550 
01551 
01552 // Updates refineCell (cells marked for refinement) so across all faces
01553 // there will be 2:1 consistency after refinement.
01554 Foam::label Foam::hexRef8::faceConsistentRefinement
01555 (
01556     const bool maxSet,
01557     PackedBoolList& refineCell
01558 ) const
01559 {
01560     label nChanged = 0;
01561 
01562     // Internal faces.
01563     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
01564     {
01565         label own = mesh_.faceOwner()[faceI];
01566         label ownLevel = cellLevel_[own] + refineCell.get(own);
01567 
01568         label nei = mesh_.faceNeighbour()[faceI];
01569         label neiLevel = cellLevel_[nei] + refineCell.get(nei);
01570 
01571         if (ownLevel > (neiLevel+1))
01572         {
01573             if (maxSet)
01574             {
01575                 refineCell.set(nei, 1);
01576             }
01577             else
01578             {
01579                 refineCell.set(own, 0);
01580             }
01581             nChanged++;
01582         }
01583         else if (neiLevel > (ownLevel+1))
01584         {
01585             if (maxSet)
01586             {
01587                 refineCell.set(own, 1);
01588             }
01589             else
01590             {
01591                 refineCell.set(nei, 0);
01592             }
01593             nChanged++;
01594         }
01595     }
01596 
01597 
01598     // Coupled faces. Swap owner level to get neighbouring cell level.
01599     // (only boundary faces of neiLevel used)
01600     labelList neiLevel(mesh_.nFaces()-mesh_.nInternalFaces());
01601 
01602     forAll(neiLevel, i)
01603     {
01604         label own = mesh_.faceOwner()[i+mesh_.nInternalFaces()];
01605 
01606         neiLevel[i] = cellLevel_[own] + refineCell.get(own);
01607     }
01608 
01609     // Swap to neighbour
01610     syncTools::swapBoundaryFaceList(mesh_, neiLevel, false);
01611 
01612     // Now we have neighbour value see which cells need refinement
01613     forAll(neiLevel, i)
01614     {
01615         label own = mesh_.faceOwner()[i+mesh_.nInternalFaces()];
01616         label ownLevel = cellLevel_[own] + refineCell.get(own);
01617 
01618         if (ownLevel > (neiLevel[i]+1))
01619         {
01620             if (!maxSet)
01621             {
01622                 refineCell.set(own, 0);
01623                 nChanged++;
01624             }
01625         }
01626         else if (neiLevel[i] > (ownLevel+1))
01627         {
01628             if (maxSet)
01629             {
01630                 refineCell.set(own, 1);
01631                 nChanged++;
01632             }
01633         }
01634     }
01635 
01636     return nChanged;
01637 }
01638 
01639 
01640 // Debug: check if wanted refinement is compatible with 2:1
01641 void Foam::hexRef8::checkWantedRefinementLevels
01642 (
01643     const labelList& cellsToRefine
01644 ) const
01645 {
01646     PackedBoolList refineCell(mesh_.nCells());
01647     forAll(cellsToRefine, i)
01648     {
01649         refineCell.set(cellsToRefine[i], 1);
01650     }
01651 
01652     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
01653     {
01654         label own = mesh_.faceOwner()[faceI];
01655         label ownLevel = cellLevel_[own] + refineCell.get(own);
01656 
01657         label nei = mesh_.faceNeighbour()[faceI];
01658         label neiLevel = cellLevel_[nei] + refineCell.get(nei);
01659 
01660         if (mag(ownLevel-neiLevel) > 1)
01661         {
01662             dumpCell(own);
01663             dumpCell(nei);
01664             FatalErrorIn
01665             (
01666                 "hexRef8::checkWantedRefinementLevels(const labelList&)"
01667             )   << "cell:" << own
01668                 << " current level:" << cellLevel_[own]
01669                 << " level after refinement:" << ownLevel
01670                 << nl
01671                 << "neighbour cell:" << nei
01672                 << " current level:" << cellLevel_[nei]
01673                 << " level after refinement:" << neiLevel
01674                 << nl
01675                 << "which does not satisfy 2:1 constraints anymore."
01676                 << abort(FatalError);
01677         }
01678     }
01679 
01680     // Coupled faces. Swap owner level to get neighbouring cell level.
01681     // (only boundary faces of neiLevel used)
01682     labelList neiLevel(mesh_.nFaces()-mesh_.nInternalFaces());
01683 
01684     forAll(neiLevel, i)
01685     {
01686         label own = mesh_.faceOwner()[i+mesh_.nInternalFaces()];
01687 
01688         neiLevel[i] = cellLevel_[own] + refineCell.get(own);
01689     }
01690 
01691     // Swap to neighbour
01692     syncTools::swapBoundaryFaceList(mesh_, neiLevel, false);
01693 
01694     // Now we have neighbour value see which cells need refinement
01695     forAll(neiLevel, i)
01696     {
01697         label faceI = i + mesh_.nInternalFaces();
01698 
01699         label own = mesh_.faceOwner()[faceI];
01700         label ownLevel = cellLevel_[own] + refineCell.get(own);
01701 
01702         if (mag(ownLevel - neiLevel[i]) > 1)
01703         {
01704             label patchI = mesh_.boundaryMesh().whichPatch(faceI);
01705 
01706             dumpCell(own);
01707             FatalErrorIn
01708             (
01709                 "hexRef8::checkWantedRefinementLevels(const labelList&)"
01710             )   << "Celllevel does not satisfy 2:1 constraint."
01711                 << " On coupled face "
01712                 << faceI
01713                 << " on patch " << patchI << " "
01714                 << mesh_.boundaryMesh()[patchI].name()
01715                 << " owner cell " << own
01716                 << " current level:" << cellLevel_[own]
01717                 << " level after refinement:" << ownLevel
01718                 << nl
01719                 << " (coupled) neighbour cell will get refinement "
01720                 << neiLevel[i]
01721                 << abort(FatalError);
01722         }
01723     }
01724 }
01725 
01726 
01727 // Set instance for mesh files
01728 void Foam::hexRef8::setInstance(const fileName& inst)
01729 {
01730     if (debug)
01731     {
01732         Pout<< "hexRef8::setInstance(const fileName& inst) : "
01733             << "Resetting file instance to " << inst << endl;
01734     }
01735 
01736     cellLevel_.instance() = inst;
01737     pointLevel_.instance() = inst;
01738     history_.instance() = inst;
01739 }
01740 
01741 
01742 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
01743 
01744 // Construct from mesh, read refinement data
01745 Foam::hexRef8::hexRef8(const polyMesh& mesh)
01746 :
01747     mesh_(mesh),
01748     cellLevel_
01749     (
01750         IOobject
01751         (
01752             "cellLevel",
01753             mesh_.facesInstance(),
01754             polyMesh::meshSubDir,
01755             mesh_,
01756             IOobject::READ_IF_PRESENT,
01757             IOobject::AUTO_WRITE
01758         ),
01759         labelList(mesh_.nCells(), 0)
01760     ),
01761     pointLevel_
01762     (
01763         IOobject
01764         (
01765             "pointLevel",
01766             mesh_.facesInstance(),
01767             polyMesh::meshSubDir,
01768             mesh_,
01769             IOobject::READ_IF_PRESENT,
01770             IOobject::AUTO_WRITE
01771         ),
01772         labelList(mesh_.nPoints(), 0)
01773     ),
01774     level0Edge_(getLevel0EdgeLength()),
01775     history_
01776     (
01777         IOobject
01778         (
01779             "refinementHistory",
01780             mesh_.facesInstance(),
01781             polyMesh::meshSubDir,
01782             mesh_,
01783             IOobject::READ_IF_PRESENT,
01784             IOobject::AUTO_WRITE
01785         ),
01786         mesh_.nCells()    // All cells visible if could not be read
01787     ),
01788     faceRemover_(mesh_, GREAT),     // merge boundary faces wherever possible
01789     savedPointLevel_(0),
01790     savedCellLevel_(0)
01791 {
01792     if (history_.active() && history_.visibleCells().size() != mesh_.nCells())
01793     {
01794         FatalErrorIn
01795         (
01796             "hexRef8::hexRef8(const polyMesh&)"
01797         )   << "History enabled but number of visible cells in it "
01798             << history_.visibleCells().size()
01799             << " is not equal to the number of cells in the mesh "
01800             << mesh_.nCells()
01801             << abort(FatalError);
01802     }
01803 
01804     if
01805     (
01806         cellLevel_.size() != mesh_.nCells()
01807      || pointLevel_.size() != mesh_.nPoints()
01808     )
01809     {
01810         FatalErrorIn
01811         (
01812             "hexRef8::hexRef8(const polyMesh&)"
01813         )   << "Restarted from inconsistent cellLevel or pointLevel files."
01814             << endl
01815             << "Number of cells in mesh:" << mesh_.nCells()
01816             << " does not equal size of cellLevel:" << cellLevel_.size() << endl
01817             << "Number of points in mesh:" << mesh_.nPoints()
01818             << " does not equal size of pointLevel:" << pointLevel_.size()
01819             << abort(FatalError);
01820     }
01821 
01822 
01823     // Check refinement levels for consistency
01824     checkRefinementLevels(-1, labelList(0));
01825 
01826 
01827     // Check initial mesh for consistency
01828 
01829     //if (debug)
01830     {
01831         checkMesh();
01832     }
01833 }
01834 
01835 
01836 // Construct from components
01837 Foam::hexRef8::hexRef8
01838 (
01839     const polyMesh& mesh,
01840     const labelList& cellLevel,
01841     const labelList& pointLevel,
01842     const refinementHistory& history
01843 )
01844 :
01845     mesh_(mesh),
01846     cellLevel_
01847     (
01848         IOobject
01849         (
01850             "cellLevel",
01851             mesh_.facesInstance(),
01852             polyMesh::meshSubDir,
01853             mesh_,
01854             IOobject::NO_READ,
01855             IOobject::AUTO_WRITE
01856         ),
01857         cellLevel
01858     ),
01859     pointLevel_
01860     (
01861         IOobject
01862         (
01863             "pointLevel",
01864             mesh_.facesInstance(),
01865             polyMesh::meshSubDir,
01866             mesh_,
01867             IOobject::NO_READ,
01868             IOobject::AUTO_WRITE
01869         ),
01870         pointLevel
01871     ),
01872     level0Edge_(getLevel0EdgeLength()),
01873     history_
01874     (
01875         IOobject
01876         (
01877             "refinementHistory",
01878             mesh_.facesInstance(),
01879             polyMesh::meshSubDir,
01880             mesh_,
01881             IOobject::NO_READ,
01882             IOobject::AUTO_WRITE
01883         ),
01884         history
01885     ),
01886     faceRemover_(mesh_, GREAT),     // merge boundary faces wherever possible
01887     savedPointLevel_(0),
01888     savedCellLevel_(0)
01889 {
01890     if (history_.active() && history_.visibleCells().size() != mesh_.nCells())
01891     {
01892         FatalErrorIn
01893         (
01894             "hexRef8::hexRef8(const polyMesh&, const labelList&"
01895             ", const labelList&, const refinementHistory&)"
01896         )   << "History enabled but number of visible cells in it "
01897             << history_.visibleCells().size()
01898             << " is not equal to the number of cells in the mesh "
01899             << mesh_.nCells() << abort(FatalError);
01900     }
01901 
01902     if
01903     (
01904         cellLevel_.size() != mesh_.nCells()
01905      || pointLevel_.size() != mesh_.nPoints()
01906     )
01907     {
01908         FatalErrorIn
01909         (
01910             "hexRef8::hexRef8(const polyMesh&, const labelList&"
01911             ", const labelList&, const refinementHistory&)"
01912         )   << "Incorrect cellLevel or pointLevel size." << endl
01913             << "Number of cells in mesh:" << mesh_.nCells()
01914             << " does not equal size of cellLevel:" << cellLevel_.size() << endl
01915             << "Number of points in mesh:" << mesh_.nPoints()
01916             << " does not equal size of pointLevel:" << pointLevel_.size()
01917             << abort(FatalError);
01918     }
01919 
01920     // Check refinement levels for consistency
01921     checkRefinementLevels(-1, labelList(0));
01922 
01923 
01924     // Check initial mesh for consistency
01925 
01926     //if (debug)
01927     {
01928         checkMesh();
01929     }
01930 }
01931 
01932 
01933 // Construct from components
01934 Foam::hexRef8::hexRef8
01935 (
01936     const polyMesh& mesh,
01937     const labelList& cellLevel,
01938     const labelList& pointLevel
01939 )
01940 :
01941     mesh_(mesh),
01942     cellLevel_
01943     (
01944         IOobject
01945         (
01946             "cellLevel",
01947             mesh_.facesInstance(),
01948             polyMesh::meshSubDir,
01949             mesh_,
01950             IOobject::NO_READ,
01951             IOobject::AUTO_WRITE
01952         ),
01953         cellLevel
01954     ),
01955     pointLevel_
01956     (
01957         IOobject
01958         (
01959             "pointLevel",
01960             mesh_.facesInstance(),
01961             polyMesh::meshSubDir,
01962             mesh_,
01963             IOobject::NO_READ,
01964             IOobject::AUTO_WRITE
01965         ),
01966         pointLevel
01967     ),
01968     level0Edge_(getLevel0EdgeLength()),
01969     history_
01970     (
01971         IOobject
01972         (
01973             "refinementHistory",
01974             mesh_.facesInstance(),
01975             polyMesh::meshSubDir,
01976             mesh_,
01977             IOobject::NO_READ,
01978             IOobject::AUTO_WRITE
01979         ),
01980         List<refinementHistory::splitCell8>(0),
01981         labelList(0)
01982     ),
01983     faceRemover_(mesh_, GREAT),     // merge boundary faces wherever possible
01984     savedPointLevel_(0),
01985     savedCellLevel_(0)
01986 {
01987     if
01988     (
01989         cellLevel_.size() != mesh_.nCells()
01990      || pointLevel_.size() != mesh_.nPoints()
01991     )
01992     {
01993         FatalErrorIn
01994         (
01995             "hexRef8::hexRef8(const polyMesh&, const labelList&"
01996             ", const labelList&)"
01997         )   << "Incorrect cellLevel or pointLevel size." << endl
01998             << "Number of cells in mesh:" << mesh_.nCells()
01999             << " does not equal size of cellLevel:" << cellLevel_.size() << endl
02000             << "Number of points in mesh:" << mesh_.nPoints()
02001             << " does not equal size of pointLevel:" << pointLevel_.size()
02002             << abort(FatalError);
02003     }
02004 
02005     // Check refinement levels for consistency
02006     checkRefinementLevels(-1, labelList(0));
02007 
02008     // Check initial mesh for consistency
02009 
02010     //if (debug)
02011     {
02012         checkMesh();
02013     }
02014 }
02015 
02016 
02017 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
02018 
02019 Foam::labelList Foam::hexRef8::consistentRefinement
02020 (
02021     const labelList& cellsToRefine,
02022     const bool maxSet
02023 ) const
02024 {
02025     // Loop, modifying cellsToRefine, until no more changes to due to 2:1
02026     // conflicts.
02027     // maxSet = false : unselect cells to refine
02028     // maxSet = true  : select cells to refine
02029 
02030     // Go to straight boolList.
02031     PackedBoolList refineCell(mesh_.nCells());
02032     forAll(cellsToRefine, i)
02033     {
02034         refineCell.set(cellsToRefine[i], 1);
02035     }
02036 
02037     while (true)
02038     {
02039         label nChanged = faceConsistentRefinement(maxSet, refineCell);
02040 
02041         reduce(nChanged, sumOp<label>());
02042 
02043         if (debug)
02044         {
02045             Pout<< "hexRef8::consistentRefinement : Changed " << nChanged
02046                 << " refinement levels due to 2:1 conflicts."
02047                 << endl;
02048         }
02049 
02050         if (nChanged == 0)
02051         {
02052             break;
02053         }
02054     }
02055 
02056 
02057     // Convert back to labelList.
02058     label nRefined = 0;
02059 
02060     forAll(refineCell, cellI)
02061     {
02062         if (refineCell.get(cellI) == 1)
02063         {
02064             nRefined++;
02065         }
02066     }
02067 
02068     labelList newCellsToRefine(nRefined);
02069     nRefined = 0;
02070 
02071     forAll(refineCell, cellI)
02072     {
02073         if (refineCell.get(cellI) == 1)
02074         {
02075             newCellsToRefine[nRefined++] = cellI;
02076         }
02077     }
02078 
02079     if (debug)
02080     {
02081         checkWantedRefinementLevels(newCellsToRefine);
02082     }
02083 
02084     return newCellsToRefine;
02085 }
02086 
02087 
02088 // Given a list of cells to refine determine additional cells to refine
02089 // such that the overall refinement:
02090 // - satisfies maxFaceDiff (e.g. 2:1) across neighbouring faces
02091 // - satisfies maxPointDiff (e.g. 4:1) across selected point connected
02092 //   cells. This is used to ensure that e.g. cells on the surface are not
02093 //   point connected to cells which are 8 times smaller.
02094 Foam::labelList Foam::hexRef8::consistentSlowRefinement
02095 (
02096     const label maxFaceDiff,
02097     const labelList& cellsToRefine,
02098     const labelList& facesToCheck,
02099     const label maxPointDiff,
02100     const labelList& pointsToCheck
02101 ) const
02102 {
02103     const labelList& faceOwner = mesh_.faceOwner();
02104     const labelList& faceNeighbour = mesh_.faceNeighbour();
02105 
02106 
02107     if (maxFaceDiff <= 0)
02108     {
02109         FatalErrorIn
02110         (
02111             "hexRef8::consistentSlowRefinement"
02112             "(const label, const labelList&, const labelList&"
02113             ", const label, const labelList&)"
02114         )   << "Illegal maxFaceDiff " << maxFaceDiff << nl
02115             << "Value should be >= 1" << exit(FatalError);
02116     }
02117 
02118 
02119     // Bit tricky. Say we want a distance of three cells between two
02120     // consecutive refinement levels. This is done by using FaceCellWave to
02121     // transport out the new refinement level. It gets decremented by one
02122     // every cell it crosses so if we initialize it to maxFaceDiff
02123     // we will get a field everywhere that tells us whether an unselected cell
02124     // needs refining as well.
02125 
02126 
02127     // Initial information about (distance to) cellLevel on all cells
02128     List<refinementData> allCellInfo(mesh_.nCells());
02129 
02130     // Initial information about (distance to) cellLevel on all faces
02131     List<refinementData> allFaceInfo(mesh_.nFaces());
02132 
02133     forAll(allCellInfo, cellI)
02134     {
02135         // maxFaceDiff since refinementData counts both
02136         // faces and cells.
02137         allCellInfo[cellI] = refinementData
02138         (
02139             maxFaceDiff*(cellLevel_[cellI]+1),// when cell is to be refined
02140             maxFaceDiff*cellLevel_[cellI]     // current level
02141         );
02142     }
02143 
02144     // Cells to be refined will have cellLevel+1
02145     forAll(cellsToRefine, i)
02146     {
02147         label cellI = cellsToRefine[i];
02148 
02149         allCellInfo[cellI].count() = allCellInfo[cellI].refinementCount();
02150     }
02151 
02152 
02153     // Labels of seed faces
02154     DynamicList<label> seedFaces(mesh_.nFaces()/100);
02155     // refinementLevel data on seed faces
02156     DynamicList<refinementData> seedFacesInfo(mesh_.nFaces()/100);
02157 
02158 
02159     // Additional buffer layer thickness by changing initial count. Usually
02160     // this happens on boundary faces. Bit tricky. Use allFaceInfo to mark
02161     // off thus marked faces so they're skipped in the next loop.
02162     forAll(facesToCheck, i)
02163     {
02164         label faceI = facesToCheck[i];
02165 
02166         if (allFaceInfo[faceI].valid())
02167         {
02168             // Can only occur if face has already gone through loop below.
02169             FatalErrorIn
02170             (
02171                 "hexRef8::consistentSlowRefinement"
02172                 "(const label, const labelList&, const labelList&"
02173                 ", const label, const labelList&)"
02174             )   << "Argument facesToCheck seems to have duplicate entries!"
02175                 << endl
02176                 << "face:" << faceI << " occurs at positions "
02177                 << findIndices(facesToCheck, faceI)
02178                 << abort(FatalError);
02179         }
02180 
02181 
02182         const refinementData& ownData = allCellInfo[faceOwner[faceI]];
02183 
02184         if (mesh_.isInternalFace(faceI))
02185         {
02186             // Seed face if neighbouring cell (after possible refinement)
02187             // will be refined one more than the current owner or neighbour.
02188 
02189             const refinementData& neiData = allCellInfo[faceNeighbour[faceI]];
02190 
02191             label faceCount;
02192             label faceRefineCount;
02193             if (neiData.count() > ownData.count())
02194             {
02195                 faceCount = neiData.count() + maxFaceDiff;
02196                 faceRefineCount = faceCount + maxFaceDiff;
02197             }
02198             else
02199             {
02200                 faceCount = ownData.count() + maxFaceDiff;
02201                 faceRefineCount = faceCount + maxFaceDiff;
02202             }
02203 
02204             seedFaces.append(faceI);
02205             seedFacesInfo.append
02206             (
02207                 refinementData
02208                 (
02209                     faceRefineCount,
02210                     faceCount
02211                 )
02212             );
02213             allFaceInfo[faceI] = seedFacesInfo[seedFacesInfo.size()-1];
02214         }
02215         else
02216         {
02217             label faceCount = ownData.count() + maxFaceDiff;
02218             label faceRefineCount = faceCount + maxFaceDiff;
02219 
02220             seedFaces.append(faceI);
02221             seedFacesInfo.append
02222             (
02223                 refinementData
02224                 (
02225                     faceRefineCount,
02226                     faceCount
02227                 )
02228             );
02229             allFaceInfo[faceI] = seedFacesInfo[seedFacesInfo.size()-1];
02230         }
02231     }
02232 
02233 
02234     // Just seed with all faces inbetween different refinement levels for now
02235     // (alternatively only seed faces on cellsToRefine but that gives problems
02236     //  if no cells to refine)
02237     forAll(faceNeighbour, faceI)
02238     {
02239         // Check if face already handled in loop above
02240         if (!allFaceInfo[faceI].valid())
02241         {
02242             label own = faceOwner[faceI];
02243             label nei = faceNeighbour[faceI];
02244 
02245             // Seed face with transported data from highest cell.
02246 
02247             if (allCellInfo[own].count() > allCellInfo[nei].count())
02248             {
02249                 allFaceInfo[faceI].updateFace
02250                 (
02251                     mesh_,
02252                     faceI,
02253                     own,
02254                     allCellInfo[own],
02255                     FaceCellWave<refinementData>::propagationTol()
02256                 );
02257                 seedFaces.append(faceI);
02258                 seedFacesInfo.append(allFaceInfo[faceI]);
02259             }
02260             else if (allCellInfo[own].count() < allCellInfo[nei].count())
02261             {
02262                 allFaceInfo[faceI].updateFace
02263                 (
02264                     mesh_,
02265                     faceI,
02266                     nei,
02267                     allCellInfo[nei],
02268                     FaceCellWave<refinementData>::propagationTol()
02269                 );
02270                 seedFaces.append(faceI);
02271                 seedFacesInfo.append(allFaceInfo[faceI]);
02272             }
02273         }
02274     }
02275 
02276     // Seed all boundary faces with owner value. This is to make sure that
02277     // they are visited (probably only important for coupled faces since
02278     // these need to be visited from both sides)
02279     for (label faceI = mesh_.nInternalFaces(); faceI < mesh_.nFaces(); faceI++)
02280     {
02281         // Check if face already handled in loop above
02282         if (!allFaceInfo[faceI].valid())
02283         {
02284             label own = faceOwner[faceI];
02285 
02286             // Seed face with transported data from owner.
02287             refinementData faceData;
02288             faceData.updateFace
02289             (
02290                 mesh_,
02291                 faceI,
02292                 own,
02293                 allCellInfo[own],
02294                 FaceCellWave<refinementData>::propagationTol()
02295             );
02296             seedFaces.append(faceI);
02297             seedFacesInfo.append(faceData);
02298         }
02299     }
02300 
02301 
02302     // face-cell-face transport engine
02303     FaceCellWave<refinementData> levelCalc
02304     (
02305         mesh_,
02306         allFaceInfo,
02307         allCellInfo
02308     );
02309 
02310     while(true)
02311     {
02312         if (debug)
02313         {
02314             Pout<< "hexRef8::consistentSlowRefinement : Seeded "
02315                 << seedFaces.size() << " faces between cells with different"
02316                 << " refinement level." << endl;
02317         }
02318 
02319         // Set seed faces
02320         levelCalc.setFaceInfo(seedFaces.shrink(), seedFacesInfo.shrink());
02321         seedFaces.clear();
02322         seedFacesInfo.clear();
02323 
02324         // Iterate until no change. Now 2:1 face difference should be satisfied
02325         levelCalc.iterate(mesh_.globalData().nTotalFaces());
02326 
02327 
02328         // Now check point-connected cells (face-connected cells already ok):
02329         // - get per point max of connected cells
02330         // - sync across coupled points
02331         // - check cells against above point max
02332 
02333         if (maxPointDiff == -1)
02334         {
02335             // No need to do any point checking.
02336             break;
02337         }
02338 
02339         // Determine per point the max cell level. (done as count, not
02340         // as cell level purely for ease)
02341         labelList maxPointCount(mesh_.nPoints(), 0);
02342 
02343         forAll(maxPointCount, pointI)
02344         {
02345             label& pLevel = maxPointCount[pointI];
02346 
02347             const labelList& pCells = mesh_.pointCells(pointI);
02348 
02349             forAll(pCells, i)
02350             {
02351                 pLevel = max(pLevel, allCellInfo[pCells[i]].count());
02352             }
02353         }
02354 
02355         // Sync maxPointCount to neighbour
02356         syncTools::syncPointList
02357         (
02358             mesh_,
02359             maxPointCount,
02360             maxEqOp<label>(),
02361             labelMin,           // null value
02362             false               // no separation
02363         );
02364 
02365         // Update allFaceInfo from maxPointCount for all points to check
02366         // (usually on boundary faces)
02367 
02368         // Per face the new refinement data
02369         Map<refinementData> changedFacesInfo(pointsToCheck.size());
02370 
02371         forAll(pointsToCheck, i)
02372         {
02373             label pointI = pointsToCheck[i];
02374 
02375             // Loop over all cells using the point and check whether their
02376             // refinement level is much less than the maximum.
02377 
02378             const labelList& pCells = mesh_.pointCells(pointI);
02379 
02380             forAll(pCells, pCellI)
02381             {
02382                 label cellI = pCells[pCellI];
02383 
02384                 refinementData& cellInfo = allCellInfo[cellI];
02385 
02386                 if
02387                 (
02388                    !cellInfo.isRefined()
02389                  && (
02390                         maxPointCount[pointI]
02391                       > cellInfo.count() + maxFaceDiff*maxPointDiff
02392                     )
02393                 )
02394                 {
02395                     // Mark cell for refinement
02396                     cellInfo.count() = cellInfo.refinementCount();
02397 
02398                     // Insert faces of cell as seed faces.
02399                     const cell& cFaces = mesh_.cells()[cellI];
02400 
02401                     forAll(cFaces, cFaceI)
02402                     {
02403                         label faceI = cFaces[cFaceI];
02404 
02405                         refinementData faceData;
02406                         faceData.updateFace
02407                         (
02408                             mesh_,
02409                             faceI,
02410                             cellI,
02411                             cellInfo,
02412                             FaceCellWave<refinementData>::propagationTol()
02413                         );
02414 
02415                         if (faceData.count() > allFaceInfo[faceI].count())
02416                         {
02417                             changedFacesInfo.insert(faceI, faceData);
02418                         }
02419                     }
02420                 }
02421             }
02422         }
02423 
02424         label nChanged = changedFacesInfo.size();
02425         reduce(nChanged, sumOp<label>());
02426 
02427         if (nChanged == 0)
02428         {
02429             break;
02430         }
02431 
02432 
02433         // Transfer into seedFaces, seedFacesInfo
02434         seedFaces.setCapacity(changedFacesInfo.size());
02435         seedFacesInfo.setCapacity(changedFacesInfo.size());
02436 
02437         forAllConstIter(Map<refinementData>, changedFacesInfo, iter)
02438         {
02439             seedFaces.append(iter.key());
02440             seedFacesInfo.append(iter());
02441         }
02442     }
02443 
02444 
02445     if (debug)
02446     {
02447         for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
02448         {
02449             label own = mesh_.faceOwner()[faceI];
02450             label ownLevel =
02451                 cellLevel_[own]
02452               + (allCellInfo[own].isRefined() ? 1 : 0);
02453 
02454             label nei = mesh_.faceNeighbour()[faceI];
02455             label neiLevel =
02456                 cellLevel_[nei]
02457               + (allCellInfo[nei].isRefined() ? 1 : 0);
02458 
02459             if (mag(ownLevel-neiLevel) > 1)
02460             {
02461                 dumpCell(own);
02462                 dumpCell(nei);
02463                 FatalErrorIn
02464                 (
02465                     "hexRef8::consistentSlowRefinement"
02466                     "(const label, const labelList&, const labelList&"
02467                     ", const label, const labelList&)"
02468                 )   << "cell:" << own
02469                     << " current level:" << cellLevel_[own]
02470                     << " current refData:" << allCellInfo[own]
02471                     << " level after refinement:" << ownLevel
02472                     << nl
02473                     << "neighbour cell:" << nei
02474                     << " current level:" << cellLevel_[nei]
02475                     << " current refData:" << allCellInfo[nei]
02476                     << " level after refinement:" << neiLevel
02477                     << nl
02478                     << "which does not satisfy 2:1 constraints anymore." << nl
02479                     << "face:" << faceI << " faceRefData:" << allFaceInfo[faceI]
02480                     << abort(FatalError);
02481             }
02482         }
02483 
02484 
02485         // Coupled faces. Swap owner level to get neighbouring cell level.
02486         // (only boundary faces of neiLevel used)
02487 
02488         labelList neiLevel(mesh_.nFaces()-mesh_.nInternalFaces());
02489         labelList neiCount(mesh_.nFaces()-mesh_.nInternalFaces());
02490         labelList neiRefCount(mesh_.nFaces()-mesh_.nInternalFaces());
02491 
02492         forAll(neiLevel, i)
02493         {
02494             label own = mesh_.faceOwner()[i+mesh_.nInternalFaces()];
02495             neiLevel[i] = cellLevel_[own];
02496             neiCount[i] = allCellInfo[own].count();
02497             neiRefCount[i] = allCellInfo[own].refinementCount();
02498         }
02499 
02500         // Swap to neighbour
02501         syncTools::swapBoundaryFaceList(mesh_, neiLevel, false);
02502         syncTools::swapBoundaryFaceList(mesh_, neiCount, false);
02503         syncTools::swapBoundaryFaceList(mesh_, neiRefCount, false);
02504 
02505         // Now we have neighbour value see which cells need refinement
02506         forAll(neiLevel, i)
02507         {
02508             label faceI = i+mesh_.nInternalFaces();
02509 
02510             label own = mesh_.faceOwner()[faceI];
02511             label ownLevel =
02512                 cellLevel_[own]
02513               + (allCellInfo[own].isRefined() ? 1 : 0);
02514 
02515             label nbrLevel =
02516                 neiLevel[i]
02517               + ((neiCount[i] >= neiRefCount[i]) ? 1 : 0);
02518 
02519             if (mag(ownLevel - nbrLevel) > 1)
02520             {
02521                 dumpCell(own);
02522                 label patchI = mesh_.boundaryMesh().whichPatch(faceI);
02523 
02524                 FatalErrorIn
02525                 (
02526                     "hexRef8::consistentSlowRefinement"
02527                     "(const label, const labelList&, const labelList&"
02528                     ", const label, const labelList&)"
02529                 )   << "Celllevel does not satisfy 2:1 constraint."
02530                     << " On coupled face "
02531                     << faceI
02532                     << " refData:" << allFaceInfo[faceI]
02533                     << " on patch " << patchI << " "
02534                     << mesh_.boundaryMesh()[patchI].name() << nl
02535                     << "owner cell " << own
02536                     << " current level:" << cellLevel_[own]
02537                     << " current count:" << allCellInfo[own].count()
02538                     << " current refCount:"
02539                     << allCellInfo[own].refinementCount()
02540                     << " level after refinement:" << ownLevel
02541                     << nl
02542                     << "(coupled) neighbour cell"
02543                     << " has current level:" << neiLevel[i]
02544                     << " current count:" << neiCount[i]
02545                     << " current refCount:" << neiRefCount[i]
02546                     << " level after refinement:" << nbrLevel
02547                     << abort(FatalError);
02548             }
02549         }
02550     }
02551 
02552     // Convert back to labelList of cells to refine.
02553 
02554     label nRefined = 0;
02555 
02556     forAll(allCellInfo, cellI)
02557     {
02558         if (allCellInfo[cellI].isRefined())
02559         {
02560             nRefined++;
02561         }
02562     }
02563 
02564     // Updated list of cells to refine
02565     labelList newCellsToRefine(nRefined);
02566     nRefined = 0;
02567 
02568     forAll(allCellInfo, cellI)
02569     {
02570         if (allCellInfo[cellI].isRefined())
02571         {
02572             newCellsToRefine[nRefined++] = cellI;
02573         }
02574     }
02575 
02576     if (debug)
02577     {
02578         Pout<< "hexRef8::consistentSlowRefinement : From "
02579             << cellsToRefine.size() << " to " << newCellsToRefine.size()
02580             << " cells to refine." << endl;
02581     }
02582 
02583     return newCellsToRefine;
02584 }
02585 
02586 
02587 Foam::labelList Foam::hexRef8::consistentSlowRefinement2
02588 (
02589     const label maxFaceDiff,
02590     const labelList& cellsToRefine,
02591     const labelList& facesToCheck
02592 ) const
02593 {
02594     const labelList& faceOwner = mesh_.faceOwner();
02595     const labelList& faceNeighbour = mesh_.faceNeighbour();
02596 
02597     if (maxFaceDiff <= 0)
02598     {
02599         FatalErrorIn
02600         (
02601             "hexRef8::consistentSlowRefinement2"
02602             "(const label, const labelList&, const labelList&)"
02603         )   << "Illegal maxFaceDiff " << maxFaceDiff << nl
02604             << "Value should be >= 1" << exit(FatalError);
02605     }
02606 
02607     const scalar level0Size = 2*maxFaceDiff*level0Edge_;
02608 
02609 
02610     // Bit tricky. Say we want a distance of three cells between two
02611     // consecutive refinement levels. This is done by using FaceCellWave to
02612     // transport out the 'refinement shell'. Anything inside the refinement
02613     // shell (given by a distance) gets marked for refinement.
02614 
02615     // Initial information about (distance to) cellLevel on all cells
02616     List<refinementDistanceData> allCellInfo(mesh_.nCells());
02617 
02618     // Initial information about (distance to) cellLevel on all faces
02619     List<refinementDistanceData> allFaceInfo(mesh_.nFaces());
02620 
02621 
02622     // Mark cells with wanted refinement level
02623     forAll(cellsToRefine, i)
02624     {
02625         label cellI = cellsToRefine[i];
02626 
02627         allCellInfo[cellI] = refinementDistanceData
02628         (
02629             level0Size,
02630             mesh_.cellCentres()[cellI],
02631             cellLevel_[cellI]+1             // wanted refinement
02632         );
02633     }
02634     // Mark all others with existing refinement level
02635     forAll(allCellInfo, cellI)
02636     {
02637         if (!allCellInfo[cellI].valid())
02638         {
02639             allCellInfo[cellI] = refinementDistanceData
02640             (
02641                 level0Size,
02642                 mesh_.cellCentres()[cellI],
02643                 cellLevel_[cellI]           // wanted refinement
02644             );
02645         }
02646     }
02647 
02648 
02649     // Labels of seed faces
02650     DynamicList<label> seedFaces(mesh_.nFaces()/100);
02651     // refinementLevel data on seed faces
02652     DynamicList<refinementDistanceData> seedFacesInfo(mesh_.nFaces()/100);
02653 
02654 
02655     const pointField& cc = mesh_.cellCentres();
02656 
02657     forAll(facesToCheck, i)
02658     {
02659         label faceI = facesToCheck[i];
02660 
02661         if (allFaceInfo[faceI].valid())
02662         {
02663             // Can only occur if face has already gone through loop below.
02664             FatalErrorIn
02665             (
02666                 "hexRef8::consistentSlowRefinement2"
02667                 "(const label, const labelList&, const labelList&)"
02668             )   << "Argument facesToCheck seems to have duplicate entries!"
02669                 << endl
02670                 << "face:" << faceI << " occurs at positions "
02671                 << findIndices(facesToCheck, faceI)
02672                 << abort(FatalError);
02673         }
02674 
02675         label own = faceOwner[faceI];
02676 
02677         label ownLevel =
02678         (
02679             allCellInfo[own].valid()
02680           ? allCellInfo[own].originLevel()
02681           : cellLevel_[own]
02682         );
02683 
02684         if (!mesh_.isInternalFace(faceI))
02685         {
02686             // Do as if boundary face would have neighbour with one higher
02687             // refinement level.
02688             const point& fc = mesh_.faceCentres()[faceI];
02689 
02690             refinementDistanceData neiData
02691             (
02692                 level0Size,
02693                 2*fc - cc[own],    // est'd cell centre
02694                 ownLevel+1
02695             );
02696 
02697             allFaceInfo[faceI].updateFace
02698             (
02699                 mesh_,
02700                 faceI,
02701                 own,        // not used (should be nei)
02702                 neiData,
02703                 FaceCellWave<refinementDistanceData>::propagationTol()
02704             );
02705         }
02706         else
02707         {
02708             label nei = faceNeighbour[faceI];
02709 
02710             label neiLevel =
02711             (
02712                 allCellInfo[nei].valid()
02713               ? allCellInfo[nei].originLevel()
02714               : cellLevel_[nei]
02715             );
02716 
02717             if (ownLevel == neiLevel)
02718             {
02719                 // Fake as if nei>own or own>nei (whichever one 'wins')
02720                 allFaceInfo[faceI].updateFace
02721                 (
02722                     mesh_,
02723                     faceI,
02724                     nei,
02725                     refinementDistanceData(level0Size, cc[nei], neiLevel+1),
02726                     FaceCellWave<refinementDistanceData>::propagationTol()
02727                 );
02728                 allFaceInfo[faceI].updateFace
02729                 (
02730                     mesh_,
02731                     faceI,
02732                     own,
02733                     refinementDistanceData(level0Size, cc[own], ownLevel+1),
02734                     FaceCellWave<refinementDistanceData>::propagationTol()
02735                 );
02736             }
02737             else
02738             {
02739                 // Difference in level anyway.
02740                 allFaceInfo[faceI].updateFace
02741                 (
02742                     mesh_,
02743                     faceI,
02744                     nei,
02745                     refinementDistanceData(level0Size, cc[nei], neiLevel),
02746                     FaceCellWave<refinementDistanceData>::propagationTol()
02747                 );
02748                 allFaceInfo[faceI].updateFace
02749                 (
02750                     mesh_,
02751                     faceI,
02752                     own,
02753                     refinementDistanceData(level0Size, cc[own], ownLevel),
02754                     FaceCellWave<refinementDistanceData>::propagationTol()
02755                 );
02756             }
02757         }
02758         seedFaces.append(faceI);
02759         seedFacesInfo.append(allFaceInfo[faceI]);
02760     }
02761 
02762 
02763     // Create some initial seeds to start walking from. This is only if there
02764     // are no facesToCheck.
02765     // Just seed with all faces inbetween different refinement levels for now
02766     forAll(faceNeighbour, faceI)
02767     {
02768         // Check if face already handled in loop above
02769         if (!allFaceInfo[faceI].valid())
02770         {
02771             label own = faceOwner[faceI];
02772 
02773             label ownLevel =
02774             (
02775                 allCellInfo[own].valid()
02776               ? allCellInfo[own].originLevel()
02777               : cellLevel_[own]
02778             );
02779 
02780             label nei = faceNeighbour[faceI];
02781 
02782             label neiLevel =
02783             (
02784                 allCellInfo[nei].valid()
02785               ? allCellInfo[nei].originLevel()
02786               : cellLevel_[nei]
02787             );
02788 
02789             if (ownLevel > neiLevel)
02790             {
02791                 // Set face to owner data. (since face not yet would be copy)
02792                 seedFaces.append(faceI);
02793                 allFaceInfo[faceI].updateFace
02794                 (
02795                     mesh_,
02796                     faceI,
02797                     own,
02798                     refinementDistanceData(level0Size, cc[own], ownLevel),
02799                     FaceCellWave<refinementDistanceData>::propagationTol()
02800                 );
02801                 seedFacesInfo.append(allFaceInfo[faceI]);
02802             }
02803             else if (neiLevel > ownLevel)
02804             {
02805                 seedFaces.append(faceI);
02806                 allFaceInfo[faceI].updateFace
02807                 (
02808                     mesh_,
02809                     faceI,
02810                     nei,
02811                     refinementDistanceData(level0Size, cc[nei], neiLevel),
02812                     FaceCellWave<refinementDistanceData>::propagationTol()
02813                 );
02814                 seedFacesInfo.append(allFaceInfo[faceI]);
02815             }
02816         }
02817     }
02818 
02819     seedFaces.shrink();
02820     seedFacesInfo.shrink();
02821 
02822     // face-cell-face transport engine
02823     FaceCellWave<refinementDistanceData> levelCalc
02824     (
02825         mesh_,
02826         seedFaces,
02827         seedFacesInfo,
02828         allFaceInfo,
02829         allCellInfo,
02830         mesh_.globalData().nTotalCells()
02831     );
02832 
02833 
02834     //if (debug)
02835     //{
02836     //    // Dump wanted level
02837     //    volScalarField wantedLevel
02838     //    (
02839     //        IOobject
02840     //        (
02841     //            "wantedLevel",
02842     //            fMesh.time().timeName(),
02843     //            fMesh,
02844     //            IOobject::NO_READ,
02845     //            IOobject::AUTO_WRITE,
02846     //            false
02847     //        ),
02848     //        fMesh,
02849     //        dimensionedScalar("zero", dimless, 0)
02850     //    );
02851     //
02852     //    forAll(wantedLevel, cellI)
02853     //    {
02854     //        wantedLevel[cellI] = allCellInfo[cellI].wantedLevel(cc[cellI]);
02855     //    }
02856     //
02857     //    Pout<< "Writing " << wantedLevel.objectPath() << endl;
02858     //    wantedLevel.write();
02859     //}
02860 
02861 
02862     // Convert back to labelList of cells to refine.
02863     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
02864 
02865     // 1. Force original refinement cells to be picked up by setting the
02866     // originLevel of input cells to be a very large level (but within range
02867     // of 1<< shift inside refinementDistanceData::wantedLevel)
02868     forAll(cellsToRefine, i)
02869     {
02870         label cellI = cellsToRefine[i];
02871 
02872         allCellInfo[cellI].originLevel() = sizeof(label)*8-2;
02873         allCellInfo[cellI].origin() = cc[cellI];
02874     }
02875 
02876     // 2. Extend to 2:1. I don't understand yet why this is not done
02877     // 2. Extend to 2:1. For non-cube cells the scalar distance does not work
02878     // so make sure it at least provides 2:1.
02879     PackedBoolList refineCell(mesh_.nCells());
02880     forAll(allCellInfo, cellI)
02881     {
02882         label wanted = allCellInfo[cellI].wantedLevel(cc[cellI]);
02883 
02884         if (wanted > cellLevel_[cellI]+1)
02885         {
02886             refineCell.set(cellI, 1);
02887         }
02888     }
02889     faceConsistentRefinement(true, refineCell);
02890 
02891     while (true)
02892     {
02893         label nChanged = faceConsistentRefinement(true, refineCell);
02894 
02895         reduce(nChanged, sumOp<label>());
02896 
02897         if (debug)
02898         {
02899             Pout<< "hexRef8::consistentSlowRefinement2 : Changed " << nChanged
02900                 << " refinement levels due to 2:1 conflicts."
02901                 << endl;
02902         }
02903 
02904         if (nChanged == 0)
02905         {
02906             break;
02907         }
02908     }
02909 
02910     // 3. Convert back to labelList.
02911     label nRefined = 0;
02912 
02913     forAll(refineCell, cellI)
02914     {
02915         if (refineCell.get(cellI) == 1)
02916         {
02917             nRefined++;
02918         }
02919     }
02920 
02921     labelList newCellsToRefine(nRefined);
02922     nRefined = 0;
02923 
02924     forAll(refineCell, cellI)
02925     {
02926         if (refineCell.get(cellI) == 1)
02927         {
02928             newCellsToRefine[nRefined++] = cellI;
02929         }
02930     }
02931 
02932     if (debug)
02933     {
02934         Pout<< "hexRef8::consistentSlowRefinement2 : From "
02935             << cellsToRefine.size() << " to " << newCellsToRefine.size()
02936             << " cells to refine." << endl;
02937 
02938         // Check that newCellsToRefine obeys at least 2:1.
02939 
02940         {
02941             cellSet cellsIn(mesh_, "cellsToRefineIn", cellsToRefine);
02942             Pout<< "hexRef8::consistentSlowRefinement2 : writing "
02943                 << cellsIn.size() << " to cellSet "
02944                 << cellsIn.objectPath() << endl;
02945             cellsIn.write();
02946         }
02947         {
02948             cellSet cellsOut(mesh_, "cellsToRefineOut", newCellsToRefine);
02949             Pout<< "hexRef8::consistentSlowRefinement2 : writing "
02950                 << cellsOut.size() << " to cellSet "
02951                 << cellsOut.objectPath() << endl;
02952             cellsOut.write();
02953         }
02954 
02955         // Extend to 2:1
02956         PackedBoolList refineCell(mesh_.nCells());
02957         forAll(newCellsToRefine, i)
02958         {
02959             refineCell.set(newCellsToRefine[i], 1);
02960         }
02961         const PackedBoolList savedRefineCell(refineCell);
02962 
02963         label nChanged = faceConsistentRefinement(true, refineCell);
02964 
02965         {
02966             cellSet cellsOut2
02967             (
02968                 mesh_, "cellsToRefineOut2", newCellsToRefine.size()
02969             );
02970             forAll(refineCell, cellI)
02971             {
02972                 if (refineCell.get(cellI) == 1)
02973                 {
02974                     cellsOut2.insert(cellI);
02975                 }
02976             }
02977             Pout<< "hexRef8::consistentSlowRefinement2 : writing "
02978                 << cellsOut2.size() << " to cellSet "
02979                 << cellsOut2.objectPath() << endl;
02980             cellsOut2.write();
02981         }
02982 
02983         if (nChanged > 0)
02984         {
02985             forAll(refineCell, cellI)
02986             {
02987                 if
02988                 (
02989                     refineCell.get(cellI) == 1
02990                  && savedRefineCell.get(cellI) == 0
02991                 )
02992                 {
02993                     dumpCell(cellI);
02994                     FatalErrorIn
02995                     (
02996                         "hexRef8::consistentSlowRefinement2"
02997                         "(const label, const labelList&, const labelList&)"
02998                     )   << "Cell:" << cellI << " cc:"
02999                         << mesh_.cellCentres()[cellI]
03000                         << " was not marked for refinement but does not obey"
03001                         << " 2:1 constraints."
03002                         << abort(FatalError);
03003                 }
03004             }
03005         }
03006     }
03007 
03008     return newCellsToRefine;
03009 }
03010 
03011 
03012 // Top level driver to insert topo changes to do all refinement.
03013 Foam::labelListList Foam::hexRef8::setRefinement
03014 (
03015     const labelList& cellLabels,
03016     polyTopoChange& meshMod
03017 )
03018 {
03019     if (debug)
03020     {
03021         Pout<< "hexRef8::setRefinement :"
03022             << " Checking initial mesh just to make sure" << endl;
03023 
03024         checkMesh();
03025         // Cannot call checkRefinementlevels since hanging points might
03026         // get triggered by the mesher after subsetting.
03027         //checkRefinementLevels(-1, labelList(0));
03028     }
03029 
03030     // Clear any saved point/cell data.
03031     savedPointLevel_.clear();
03032     savedCellLevel_.clear();
03033 
03034 
03035     // New point/cell level. Copy of pointLevel for existing points.
03036     DynamicList<label> newCellLevel(cellLevel_.size());
03037     forAll(cellLevel_, cellI)
03038     {
03039         newCellLevel.append(cellLevel_[cellI]);
03040     }
03041     DynamicList<label> newPointLevel(pointLevel_.size());
03042     forAll(pointLevel_, pointI)
03043     {
03044         newPointLevel.append(pointLevel_[pointI]);
03045     }
03046 
03047 
03048     if (debug)
03049     {
03050         Pout<< "hexRef8::setRefinement :"
03051             << " Allocating " << cellLabels.size() << " cell midpoints."
03052             << endl;
03053     }
03054 
03055 
03056     // Mid point per refined cell.
03057     // -1 : not refined
03058     // >=0: label of mid point.
03059     labelList cellMidPoint(mesh_.nCells(), -1);
03060 
03061     forAll(cellLabels, i)
03062     {
03063         label cellI = cellLabels[i];
03064 
03065         label anchorPointI = mesh_.faces()[mesh_.cells()[cellI][0]][0];
03066 
03067         cellMidPoint[cellI] = meshMod.setAction
03068         (
03069             polyAddPoint
03070             (
03071                 mesh_.cellCentres()[cellI],     // point
03072                 anchorPointI,                   // master point
03073                 -1,                             // zone for point
03074                 true                            // supports a cell
03075             )
03076         );
03077 
03078         newPointLevel(cellMidPoint[cellI]) = cellLevel_[cellI]+1;
03079     }
03080 
03081 
03082     if (debug)
03083     {
03084         cellSet splitCells(mesh_, "splitCells", cellLabels.size());
03085 
03086         forAll(cellMidPoint, cellI)
03087         {
03088             if (cellMidPoint[cellI] >= 0)
03089             {
03090                 splitCells.insert(cellI);
03091             }
03092         }
03093 
03094         Pout<< "hexRef8::setRefinement : Dumping " << splitCells.size()
03095             << " cells to split to cellSet " << splitCells.objectPath()
03096             << endl;
03097 
03098         splitCells.write();
03099     }
03100 
03101 
03102 
03103     // Split edges
03104     // ~~~~~~~~~~~
03105 
03106     if (debug)
03107     {
03108         Pout<< "hexRef8::setRefinement :"
03109             << " Allocating edge midpoints."
03110             << endl;
03111     }
03112 
03113     // Unrefined edges are ones between cellLevel or lower points.
03114     // If any cell using this edge gets split then the edge needs to be split.
03115 
03116     // -1  : no need to split edge
03117     // >=0 : label of introduced mid point
03118     labelList edgeMidPoint(mesh_.nEdges(), -1);
03119 
03120     // Note: Loop over cells to be refined or edges?
03121     forAll(cellMidPoint, cellI)
03122     {
03123         if (cellMidPoint[cellI] >= 0)
03124         {
03125             const labelList& cEdges = mesh_.cellEdges(cellI);
03126 
03127             forAll(cEdges, i)
03128             {
03129                 label edgeI = cEdges[i];
03130 
03131                 const edge& e = mesh_.edges()[edgeI];
03132 
03133                 if
03134                 (
03135                     pointLevel_[e[0]] <= cellLevel_[cellI]
03136                  && pointLevel_[e[1]] <= cellLevel_[cellI]
03137                 )
03138                 {
03139                     edgeMidPoint[edgeI] = 12345;    // mark need for splitting
03140                 }
03141             }
03142         }
03143     }
03144 
03145     // Synchronize edgeMidPoint across coupled patches. Take max so that
03146     // any split takes precedence.
03147     syncTools::syncEdgeList
03148     (
03149         mesh_,
03150         edgeMidPoint,
03151         maxEqOp<label>(),
03152         labelMin,
03153         false               // no separation
03154     );
03155 
03156 
03157     // Introduce edge points
03158     // ~~~~~~~~~~~~~~~~~~~~~
03159 
03160     {
03161         // Phase 1: calculate midpoints and sync.
03162         // This needs doing for if people do not write binary and we slowly
03163         // get differences.
03164 
03165         pointField edgeMids(mesh_.nEdges(), point(-GREAT, -GREAT, -GREAT));
03166 
03167         forAll(edgeMidPoint, edgeI)
03168         {
03169             if (edgeMidPoint[edgeI] >= 0)
03170             {
03171                 // Edge marked to be split.
03172                 edgeMids[edgeI] = mesh_.edges()[edgeI].centre(mesh_.points());
03173             }
03174         }
03175         syncTools::syncEdgeList
03176         (
03177             mesh_,
03178             edgeMids,
03179             maxEqOp<vector>(),
03180             point(-GREAT, -GREAT, -GREAT),
03181             true               // apply separation
03182         );
03183 
03184 
03185         // Phase 2: introduce points at the synced locations.
03186         forAll(edgeMidPoint, edgeI)
03187         {
03188             if (edgeMidPoint[edgeI] >= 0)
03189             {
03190                 // Edge marked to be split. Replace edgeMidPoint with actual
03191                 // point label.
03192 
03193                 const edge& e = mesh_.edges()[edgeI];
03194 
03195                 edgeMidPoint[edgeI] = meshMod.setAction
03196                 (
03197                     polyAddPoint
03198                     (
03199                         edgeMids[edgeI],            // point
03200                         e[0],                       // master point
03201                         -1,                         // zone for point
03202                         true                        // supports a cell
03203                     )
03204                 );
03205 
03206                 newPointLevel(edgeMidPoint[edgeI]) =
03207                     max
03208                     (
03209                         pointLevel_[e[0]],
03210                         pointLevel_[e[1]]
03211                     )
03212                   + 1;
03213             }
03214         }
03215     }
03216 
03217     if (debug)
03218     {
03219         OFstream str(mesh_.time().path()/"edgeMidPoint.obj");
03220 
03221         forAll(edgeMidPoint, edgeI)
03222         {
03223             if (edgeMidPoint[edgeI] >= 0)
03224             {
03225                 const edge& e = mesh_.edges()[edgeI];
03226 
03227                 meshTools::writeOBJ(str, e.centre(mesh_.points()));
03228             }
03229         }
03230 
03231         Pout<< "hexRef8::setRefinement :"
03232             << " Dumping edge centres to split to file " << str.name() << endl;
03233     }
03234 
03235 
03236     // Calculate face level
03237     // ~~~~~~~~~~~~~~~~~~~~
03238     // (after splitting)
03239 
03240     if (debug)
03241     {
03242         Pout<< "hexRef8::setRefinement :"
03243             << " Allocating face midpoints."
03244             << endl;
03245     }
03246 
03247     // Face anchor level. There are guaranteed 4 points with level
03248     // <= anchorLevel. These are the corner points.
03249     labelList faceAnchorLevel(mesh_.nFaces());
03250 
03251     for (label faceI = 0; faceI < mesh_.nFaces(); faceI++)
03252     {
03253         faceAnchorLevel[faceI] = getAnchorLevel(faceI);
03254     }
03255 
03256     // -1  : no need to split face
03257     // >=0 : label of introduced mid point
03258     labelList faceMidPoint(mesh_.nFaces(), -1);
03259 
03260 
03261     // Internal faces: look at cells on both sides. Uniquely determined since
03262     // face itself guaranteed to be same level as most refined neighbour.
03263     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
03264     {
03265         if (faceAnchorLevel[faceI] >= 0)
03266         {
03267             label own = mesh_.faceOwner()[faceI];
03268             label ownLevel = cellLevel_[own];
03269             label newOwnLevel = ownLevel + (cellMidPoint[own] >= 0 ? 1 : 0);
03270 
03271             label nei = mesh_.faceNeighbour()[faceI];
03272             label neiLevel = cellLevel_[nei];
03273             label newNeiLevel = neiLevel + (cellMidPoint[nei] >= 0 ? 1 : 0);
03274 
03275             if
03276             (
03277                 newOwnLevel > faceAnchorLevel[faceI]
03278              || newNeiLevel > faceAnchorLevel[faceI]
03279             )
03280             {
03281                 faceMidPoint[faceI] = 12345;    // mark to be split
03282             }
03283         }
03284     }
03285 
03286     // Coupled patches handled like internal faces except now all information
03287     // from neighbour comes from across processor.
03288     // Boundary faces are more complicated since the boundary face can
03289     // be more refined than its owner (or neighbour for coupled patches)
03290     // (does not happen if refining/unrefining only, but does e.g. when
03291     //  refinining and subsetting)
03292 
03293     {
03294         labelList newNeiLevel(mesh_.nFaces()-mesh_.nInternalFaces());
03295 
03296         forAll(newNeiLevel, i)
03297         {
03298             label own = mesh_.faceOwner()[i+mesh_.nInternalFaces()];
03299             label ownLevel = cellLevel_[own];
03300             label newOwnLevel = ownLevel + (cellMidPoint[own] >= 0 ? 1 : 0);
03301 
03302             newNeiLevel[i] = newOwnLevel;
03303         }
03304 
03305         // Swap.
03306         syncTools::swapBoundaryFaceList(mesh_, newNeiLevel, false);
03307 
03308         // So now we have information on the neighbour.
03309 
03310         forAll(newNeiLevel, i)
03311         {
03312             label faceI = i+mesh_.nInternalFaces();
03313 
03314             if (faceAnchorLevel[faceI] >= 0)
03315             {
03316                 label own = mesh_.faceOwner()[faceI];
03317                 label ownLevel = cellLevel_[own];
03318                 label newOwnLevel = ownLevel + (cellMidPoint[own] >= 0 ? 1 : 0);
03319 
03320                 if
03321                 (
03322                     newOwnLevel > faceAnchorLevel[faceI]
03323                  || newNeiLevel[i] > faceAnchorLevel[faceI]
03324                 )
03325                 {
03326                     faceMidPoint[faceI] = 12345;    // mark to be split
03327                 }
03328             }
03329         }
03330     }
03331 
03332 
03333     // Synchronize faceMidPoint across coupled patches. (logical or)
03334     syncTools::syncFaceList
03335     (
03336         mesh_,
03337         faceMidPoint,
03338         maxEqOp<label>(),
03339         false
03340     );
03341 
03342 
03343 
03344     // Introduce face points
03345     // ~~~~~~~~~~~~~~~~~~~~~
03346 
03347     {
03348         // Phase 1: determine mid points and sync. See comment for edgeMids
03349         // above
03350         pointField bFaceMids
03351         (
03352             mesh_.nFaces()-mesh_.nInternalFaces(),
03353             point(-GREAT, -GREAT, -GREAT)
03354         );
03355 
03356         forAll(bFaceMids, i)
03357         {
03358             label faceI = i+mesh_.nInternalFaces();
03359 
03360             if (faceMidPoint[faceI] >= 0)
03361             {
03362                 bFaceMids[i] = mesh_.faceCentres()[faceI];
03363             }
03364         }
03365         syncTools::syncBoundaryFaceList
03366         (
03367             mesh_,
03368             bFaceMids,
03369             maxEqOp<vector>(),
03370             true               // apply separation
03371         );
03372 
03373         forAll(faceMidPoint, faceI)
03374         {
03375             if (faceMidPoint[faceI] >= 0)
03376             {
03377                 // Face marked to be split. Replace faceMidPoint with actual
03378                 // point label.
03379 
03380                 const face& f = mesh_.faces()[faceI];
03381 
03382                 faceMidPoint[faceI] = meshMod.setAction
03383                 (
03384                     polyAddPoint
03385                     (
03386                         (
03387                             faceI < mesh_.nInternalFaces()
03388                           ? mesh_.faceCentres()[faceI]
03389                           : bFaceMids[faceI-mesh_.nInternalFaces()]
03390                         ),                          // point
03391                         f[0],                       // master point
03392                         -1,                         // zone for point
03393                         true                        // supports a cell
03394                     )
03395                 );
03396 
03397                 // Determine the level of the corner points and midpoint will
03398                 // be one higher.
03399                 newPointLevel(faceMidPoint[faceI]) = faceAnchorLevel[faceI]+1;
03400             }
03401         }
03402     }
03403 
03404     if (debug)
03405     {
03406         faceSet splitFaces(mesh_, "splitFaces", cellLabels.size());
03407 
03408         forAll(faceMidPoint, faceI)
03409         {
03410             if (faceMidPoint[faceI] >= 0)
03411             {
03412                 splitFaces.insert(faceI);
03413             }
03414         }
03415 
03416         Pout<< "hexRef8::setRefinement : Dumping " << splitFaces.size()
03417             << " faces to split to faceSet " << splitFaces.objectPath() << endl;
03418 
03419         splitFaces.write();
03420     }
03421 
03422 
03423     // Information complete
03424     // ~~~~~~~~~~~~~~~~~~~~
03425     // At this point we have all the information we need. We should no
03426     // longer reference the cellLabels to refine. All the information is:
03427     // - cellMidPoint >= 0 : cell needs to be split
03428     // - faceMidPoint >= 0 : face needs to be split
03429     // - edgeMidPoint >= 0 : edge needs to be split
03430 
03431 
03432 
03433     // Get the corner/anchor points
03434     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
03435 
03436     if (debug)
03437     {
03438         Pout<< "hexRef8::setRefinement :"
03439             << " Finding cell anchorPoints (8 per cell)"
03440             << endl;
03441     }
03442 
03443     // There will always be 8 points on the hex that have were introduced
03444     // with the hex and will have the same or lower refinement level.
03445 
03446     // Per cell the 8 corner points.
03447     labelListList cellAnchorPoints(mesh_.nCells());
03448 
03449     {
03450         labelList nAnchorPoints(mesh_.nCells(), 0);
03451 
03452         forAll(cellMidPoint, cellI)
03453         {
03454             if (cellMidPoint[cellI] >= 0)
03455             {
03456                 cellAnchorPoints[cellI].setSize(8);
03457             }
03458         }
03459 
03460         forAll(pointLevel_, pointI)
03461         {
03462             const labelList& pCells = mesh_.pointCells(pointI);
03463 
03464             forAll(pCells, pCellI)
03465             {
03466                 label cellI = pCells[pCellI];
03467 
03468                 if
03469                 (
03470                     cellMidPoint[cellI] >= 0
03471                  && pointLevel_[pointI] <= cellLevel_[cellI]
03472                 )
03473                 {
03474                     if (nAnchorPoints[cellI] == 8)
03475                     {
03476                         dumpCell(cellI);
03477                         FatalErrorIn
03478                         (
03479                             "hexRef8::setRefinement(const labelList&"
03480                             ", polyTopoChange&)"
03481                         )   << "cell " << cellI
03482                             << " of level " << cellLevel_[cellI]
03483                             << " uses more than 8 points of equal or"
03484                             << " lower level" << nl
03485                             << "Points so far:" << cellAnchorPoints[cellI]
03486                             << abort(FatalError);
03487                     }
03488 
03489                     cellAnchorPoints[cellI][nAnchorPoints[cellI]++] = pointI;
03490                 }
03491             }
03492         }
03493 
03494         forAll(cellMidPoint, cellI)
03495         {
03496             if (cellMidPoint[cellI] >= 0)
03497             {
03498                 if (nAnchorPoints[cellI] != 8)
03499                 {
03500                     dumpCell(cellI);
03501 
03502                     const labelList& cPoints = mesh_.cellPoints(cellI);
03503 
03504                     FatalErrorIn
03505                     (
03506                         "hexRef8::setRefinement(const labelList&"
03507                         ", polyTopoChange&)"
03508                     )   << "cell " << cellI
03509                         << " of level " << cellLevel_[cellI]
03510                         << " does not seem to have 8 points of equal or"
03511                         << " lower level" << endl
03512                         << "cellPoints:" << cPoints << endl
03513                         << "pointLevels:"
03514                         << UIndirectList<label>(pointLevel_, cPoints)() << endl
03515                         << abort(FatalError);
03516                 }
03517             }
03518         }
03519     }
03520 
03521 
03522     // Add the cells
03523     // ~~~~~~~~~~~~~
03524 
03525     if (debug)
03526     {
03527         Pout<< "hexRef8::setRefinement :"
03528             << " Adding cells (1 per anchorPoint)"
03529             << endl;
03530     }
03531 
03532     // Per cell the 7 added cells (+ original cell)
03533     labelListList cellAddedCells(mesh_.nCells());
03534 
03535     forAll(cellAnchorPoints, cellI)
03536     {
03537         const labelList& cAnchors = cellAnchorPoints[cellI];
03538 
03539         if (cAnchors.size() == 8)
03540         {
03541             labelList& cAdded = cellAddedCells[cellI];
03542             cAdded.setSize(8);
03543 
03544             // Original cell at 0
03545             cAdded[0] = cellI;
03546 
03547             // Update cell level
03548             newCellLevel[cellI] = cellLevel_[cellI]+1;
03549 
03550 
03551             for (label i = 1; i < 8; i++)
03552             {
03553                 cAdded[i] = meshMod.setAction
03554                 (
03555                     polyAddCell
03556                     (
03557                         -1,                                 // master point
03558                         -1,                                 // master edge
03559                         -1,                                 // master face
03560                         cellI,                              // master cell
03561                         mesh_.cellZones().whichZone(cellI)  // zone for cell
03562                     )
03563                 );
03564 
03565                 newCellLevel(cAdded[i]) = cellLevel_[cellI]+1;
03566             }
03567         }
03568     }
03569 
03570 
03571     // Faces
03572     // ~~~~~
03573     // 1. existing faces that get split (into four always)
03574     // 2. existing faces that do not get split but only edges get split
03575     // 3. existing faces that do not get split but get new owner/neighbour
03576     // 4. new internal faces inside split cells.
03577 
03578     if (debug)
03579     {
03580         Pout<< "hexRef8::setRefinement :"
03581             << " Marking faces to be handled"
03582             << endl;
03583     }
03584 
03585     // Get all affected faces.
03586     PackedBoolList affectedFace(mesh_.nFaces());
03587 
03588     {
03589         forAll(cellMidPoint, cellI)
03590         {
03591             if (cellMidPoint[cellI] >= 0)
03592             {
03593                 const cell& cFaces = mesh_.cells()[cellI];
03594 
03595                 forAll(cFaces, i)
03596                 {
03597                     affectedFace.set(cFaces[i], 1);
03598                 }
03599             }
03600         }
03601 
03602         forAll(faceMidPoint, faceI)
03603         {
03604             if (faceMidPoint[faceI] >= 0)
03605             {
03606                 affectedFace.set(faceI, 1);
03607             }
03608         }
03609 
03610         forAll(edgeMidPoint, edgeI)
03611         {
03612             if (edgeMidPoint[edgeI] >= 0)
03613             {
03614                 const labelList& eFaces = mesh_.edgeFaces(edgeI);
03615 
03616                 forAll(eFaces, i)
03617                 {
03618                     affectedFace.set(eFaces[i], 1);
03619                 }
03620             }
03621         }
03622     }
03623 
03624 
03625     // 1. Faces that get split
03626     // ~~~~~~~~~~~~~~~~~~~~~~~
03627 
03628     if (debug)
03629     {
03630         Pout<< "hexRef8::setRefinement : Splitting faces" << endl;
03631     }
03632 
03633     forAll(faceMidPoint, faceI)
03634     {
03635         if (faceMidPoint[faceI] >= 0 && affectedFace.get(faceI) == 1)
03636         {
03637             // Face needs to be split and hasn't yet been done in some way
03638             // (affectedFace - is impossible since this is first change but
03639             //  just for completeness)
03640 
03641             const face& f = mesh_.faces()[faceI];
03642 
03643             // Has original faceI been used (three faces added, original gets
03644             // modified)
03645             bool modifiedFace = false;
03646 
03647             label anchorLevel = faceAnchorLevel[faceI];
03648 
03649             face newFace(4);
03650 
03651             forAll(f, fp)
03652             {
03653                 label pointI = f[fp];
03654 
03655                 if (pointLevel_[pointI] <= anchorLevel)
03656                 {
03657                     // point is anchor. Start collecting face.
03658 
03659                     DynamicList<label> faceVerts(4);
03660 
03661                     faceVerts.append(pointI);
03662 
03663                     // Walk forward to mid point.
03664                     // - if next is +2 midpoint is +1
03665                     // - if next is +1 it is midpoint
03666                     // - if next is +0 there has to be edgeMidPoint
03667 
03668                     walkFaceToMid
03669                     (
03670                         edgeMidPoint,
03671                         anchorLevel,
03672                         faceI,
03673                         fp,
03674                         faceVerts
03675                     );
03676 
03677                     faceVerts.append(faceMidPoint[faceI]);
03678 
03679                     walkFaceFromMid
03680                     (
03681                         edgeMidPoint,
03682                         anchorLevel,
03683                         faceI,
03684                         fp,
03685                         faceVerts
03686                     );
03687 
03688                     // Convert dynamiclist to face.
03689                     newFace.transfer(faceVerts);
03690 
03691                     //Pout<< "Split face:" << faceI << " verts:" << f
03692                     //    << " into quad:" << newFace << endl;
03693 
03694                     // Get new owner/neighbour
03695                     label own, nei;
03696                     getFaceNeighbours
03697                     (
03698                         cellAnchorPoints,
03699                         cellAddedCells,
03700                         faceI,
03701                         pointI,          // Anchor point
03702 
03703                         own,
03704                         nei
03705                     );
03706 
03707 
03708                     if (debug)
03709                     {
03710                         if (mesh_.isInternalFace(faceI))
03711                         {
03712                             label oldOwn = mesh_.faceOwner()[faceI];
03713                             label oldNei = mesh_.faceNeighbour()[faceI];
03714 
03715                             checkInternalOrientation
03716                             (
03717                                 meshMod,
03718                                 oldOwn,
03719                                 faceI,
03720                                 mesh_.cellCentres()[oldOwn],
03721                                 mesh_.cellCentres()[oldNei],
03722                                 newFace
03723                             );
03724                         }
03725                         else
03726                         {
03727                             label oldOwn = mesh_.faceOwner()[faceI];
03728 
03729                             checkBoundaryOrientation
03730                             (
03731                                 meshMod,
03732                                 oldOwn,
03733                                 faceI,
03734                                 mesh_.cellCentres()[oldOwn],
03735                                 mesh_.faceCentres()[faceI],
03736                                 newFace
03737                             );
03738                         }
03739                     }
03740 
03741 
03742                     if (!modifiedFace)
03743                     {
03744                         modifiedFace = true;
03745 
03746                         modFace(meshMod, faceI, newFace, own, nei);
03747                     }
03748                     else
03749                     {
03750                         addFace(meshMod, faceI, newFace, own, nei);
03751                     }
03752                 }
03753             }
03754 
03755             // Mark face as having been handled
03756             affectedFace.set(faceI, 0);
03757         }
03758     }
03759 
03760 
03761     // 2. faces that do not get split but use edges that get split
03762     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
03763 
03764     if (debug)
03765     {
03766         Pout<< "hexRef8::setRefinement :"
03767             << " Adding edge splits to unsplit faces"
03768             << endl;
03769     }
03770 
03771     DynamicList<label> eFacesStorage;
03772     DynamicList<label> fEdgesStorage;
03773 
03774     forAll(edgeMidPoint, edgeI)
03775     {
03776         if (edgeMidPoint[edgeI] >= 0)
03777         {
03778             // Split edge. Check that face not already handled above.
03779 
03780             const labelList& eFaces = mesh_.edgeFaces(edgeI, eFacesStorage);
03781 
03782             forAll(eFaces, i)
03783             {
03784                 label faceI = eFaces[i];
03785 
03786                 if (faceMidPoint[faceI] < 0 && affectedFace.get(faceI) == 1)
03787                 {
03788                     // Unsplit face. Add edge splits to face.
03789 
03790                     const face& f = mesh_.faces()[faceI];
03791                     const labelList& fEdges = mesh_.faceEdges
03792                     (
03793                         faceI,
03794                         fEdgesStorage
03795                     );
03796 
03797                     DynamicList<label> newFaceVerts(f.size());
03798 
03799                     forAll(f, fp)
03800                     {
03801                         newFaceVerts.append(f[fp]);
03802 
03803                         label edgeI = fEdges[fp];
03804 
03805                         if (edgeMidPoint[edgeI] >= 0)
03806                         {
03807                             newFaceVerts.append(edgeMidPoint[edgeI]);
03808                         }
03809                     }
03810 
03811                     face newFace;
03812                     newFace.transfer(newFaceVerts);
03813 
03814                     // The point with the lowest level should be an anchor
03815                     // point of the neighbouring cells.
03816                     label anchorFp = findMinLevel(f);
03817 
03818                     label own, nei;
03819                     getFaceNeighbours
03820                     (
03821                         cellAnchorPoints,
03822                         cellAddedCells,
03823                         faceI,
03824                         f[anchorFp],          // Anchor point
03825 
03826                         own,
03827                         nei
03828                     );
03829 
03830 
03831                     if (debug)
03832                     {
03833                         if (mesh_.isInternalFace(faceI))
03834                         {
03835                             label oldOwn = mesh_.faceOwner()[faceI];
03836                             label oldNei = mesh_.faceNeighbour()[faceI];
03837 
03838                             checkInternalOrientation
03839                             (
03840                                 meshMod,
03841                                 oldOwn,
03842                                 faceI,
03843                                 mesh_.cellCentres()[oldOwn],
03844                                 mesh_.cellCentres()[oldNei],
03845                                 newFace
03846                             );
03847                         }
03848                         else
03849                         {
03850                             label oldOwn = mesh_.faceOwner()[faceI];
03851 
03852                             checkBoundaryOrientation
03853                             (
03854                                 meshMod,
03855                                 oldOwn,
03856                                 faceI,
03857                                 mesh_.cellCentres()[oldOwn],
03858                                 mesh_.faceCentres()[faceI],
03859                                 newFace
03860                             );
03861                         }
03862                     }
03863 
03864                     modFace(meshMod, faceI, newFace, own, nei);
03865 
03866                     // Mark face as having been handled
03867                     affectedFace.set(faceI, 0);
03868                 }
03869             }
03870         }
03871     }
03872 
03873 
03874     // 3. faces that do not get split but whose owner/neighbour change
03875     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
03876 
03877     if (debug)
03878     {
03879         Pout<< "hexRef8::setRefinement :"
03880             << " Changing owner/neighbour for otherwise unaffected faces"
03881             << endl;
03882     }
03883 
03884     forAll(affectedFace, faceI)
03885     {
03886         if (affectedFace.get(faceI) == 1)
03887         {
03888             const face& f = mesh_.faces()[faceI];
03889 
03890             // The point with the lowest level should be an anchor
03891             // point of the neighbouring cells.
03892             label anchorFp = findMinLevel(f);
03893 
03894             label own, nei;
03895             getFaceNeighbours
03896             (
03897                 cellAnchorPoints,
03898                 cellAddedCells,
03899                 faceI,
03900                 f[anchorFp],          // Anchor point
03901 
03902                 own,
03903                 nei
03904             );
03905 
03906             modFace(meshMod, faceI, f, own, nei);
03907 
03908             // Mark face as having been handled
03909             affectedFace.set(faceI, 0);
03910         }
03911     }
03912 
03913 
03914     // 4. new internal faces inside split cells.
03915     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
03916 
03917 
03918     // This is the hard one. We have to find the splitting points between
03919     // the anchor points. But the edges between the anchor points might have
03920     // been split (into two,three or four edges).
03921 
03922     if (debug)
03923     {
03924         Pout<< "hexRef8::setRefinement :"
03925             << " Create new internal faces for split cells"
03926             << endl;
03927     }
03928 
03929     forAll(cellMidPoint, cellI)
03930     {
03931         if (cellMidPoint[cellI] >= 0)
03932         {
03933             createInternalFaces
03934             (
03935                 cellAnchorPoints,
03936                 cellAddedCells,
03937                 cellMidPoint,
03938                 faceMidPoint,
03939                 faceAnchorLevel,
03940                 edgeMidPoint,
03941                 cellI,
03942                 meshMod
03943             );
03944         }
03945     }
03946 
03947     // Extend pointLevels and cellLevels for the new cells. Could also be done
03948     // in updateMesh but saves passing cellAddedCells out of this routine.
03949 
03950     // Check
03951     if (debug)
03952     {
03953         label minPointI = labelMax;
03954         label maxPointI = labelMin;
03955 
03956         forAll(cellMidPoint, cellI)
03957         {
03958             if (cellMidPoint[cellI] >= 0)
03959             {
03960                 minPointI = min(minPointI, cellMidPoint[cellI]);
03961                 maxPointI = max(maxPointI, cellMidPoint[cellI]);
03962             }
03963         }
03964         forAll(faceMidPoint, faceI)
03965         {
03966             if (faceMidPoint[faceI] >= 0)
03967             {
03968                 minPointI = min(minPointI, faceMidPoint[faceI]);
03969                 maxPointI = max(maxPointI, faceMidPoint[faceI]);
03970             }
03971         }
03972         forAll(edgeMidPoint, edgeI)
03973         {
03974             if (edgeMidPoint[edgeI] >= 0)
03975             {
03976                 minPointI = min(minPointI, edgeMidPoint[edgeI]);
03977                 maxPointI = max(maxPointI, edgeMidPoint[edgeI]);
03978             }
03979         }
03980 
03981         if (minPointI != labelMax && minPointI != mesh_.nPoints())
03982         {
03983             FatalErrorIn("hexRef8::setRefinement(..)")
03984                 << "Added point labels not consecutive to existing mesh points."
03985                 << nl
03986                 << "mesh_.nPoints():" << mesh_.nPoints()
03987                 << " minPointI:" << minPointI
03988                 << " maxPointI:" << maxPointI
03989                 << abort(FatalError);
03990         }
03991     }
03992 
03993     pointLevel_.transfer(newPointLevel);
03994     cellLevel_.transfer(newCellLevel);
03995 
03996     // Mark files as changed
03997     setInstance(mesh_.facesInstance());
03998 
03999 
04000     // Update the live split cells tree.
04001     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
04002 
04003     // New unrefinement structure
04004     if (history_.active())
04005     {
04006         if (debug)
04007         {
04008             Pout<< "hexRef8::setRefinement :"
04009                 << " Updating refinement history to " << cellLevel_.size()
04010                 << " cells" << endl;
04011         }
04012 
04013         // Extend refinement history for new cells
04014         history_.resize(cellLevel_.size());
04015 
04016         forAll(cellAddedCells, cellI)
04017         {
04018             const labelList& addedCells = cellAddedCells[cellI];
04019 
04020             if (addedCells.size())
04021             {
04022                 // Cell was split.
04023                 history_.storeSplit(cellI, addedCells);
04024             }
04025         }
04026     }
04027 
04028     // Compact cellAddedCells.
04029 
04030     labelListList refinedCells(cellLabels.size());
04031 
04032     forAll(cellLabels, i)
04033     {
04034         label cellI = cellLabels[i];
04035 
04036         refinedCells[i].transfer(cellAddedCells[cellI]);
04037     }
04038 
04039     return refinedCells;
04040 }
04041 
04042 
04043 void Foam::hexRef8::storeData
04044 (
04045     const labelList& pointsToStore,
04046     const labelList& facesToStore,
04047     const labelList& cellsToStore
04048 )
04049 {
04050     savedPointLevel_.resize(2*pointsToStore.size());
04051     forAll(pointsToStore, i)
04052     {
04053         label pointI = pointsToStore[i];
04054         savedPointLevel_.insert(pointI, pointLevel_[pointI]);
04055     }
04056 
04057     savedCellLevel_.resize(2*cellsToStore.size());
04058     forAll(cellsToStore, i)
04059     {
04060         label cellI = cellsToStore[i];
04061         savedCellLevel_.insert(cellI, cellLevel_[cellI]);
04062     }
04063 }
04064 
04065 
04066 // Gets called after the mesh change. setRefinement will already have made
04067 // sure the pointLevel_ and cellLevel_ are the size of the new mesh so we
04068 // only need to account for reordering.
04069 void Foam::hexRef8::updateMesh(const mapPolyMesh& map)
04070 {
04071     Map<label> dummyMap(0);
04072 
04073     updateMesh(map, dummyMap, dummyMap, dummyMap);
04074 }
04075 
04076 
04077 // Gets called after the mesh change. setRefinement will already have made
04078 // sure the pointLevel_ and cellLevel_ are the size of the new mesh so we
04079 // only need to account for reordering.
04080 void Foam::hexRef8::updateMesh
04081 (
04082     const mapPolyMesh& map,
04083     const Map<label>& pointsToRestore,
04084     const Map<label>& facesToRestore,
04085     const Map<label>& cellsToRestore
04086 )
04087 {
04088     // Update celllevel
04089     if (debug)
04090     {
04091         Pout<< "hexRef8::updateMesh :"
04092             << " Updating various lists"
04093             << endl;
04094     }
04095 
04096     {
04097         const labelList& reverseCellMap = map.reverseCellMap();
04098 
04099         if (debug)
04100         {
04101             Pout<< "hexRef8::updateMesh :"
04102                 << " reverseCellMap:" << map.reverseCellMap().size()
04103                 << " cellMap:" << map.cellMap().size()
04104                 << " nCells:" << mesh_.nCells()
04105                 << " nOldCells:" << map.nOldCells()
04106                 << " cellLevel_:" << cellLevel_.size()
04107                 << " reversePointMap:" << map.reversePointMap().size()
04108                 << " pointMap:" << map.pointMap().size()
04109                 << " nPoints:" << mesh_.nPoints()
04110                 << " nOldPoints:" << map.nOldPoints()
04111                 << " pointLevel_:" << pointLevel_.size()
04112                 << endl;
04113         }
04114 
04115         if (reverseCellMap.size() == cellLevel_.size())
04116         {
04117             // Assume it is after hexRef8 that this routine is called.
04118             // Just account for reordering. We cannot use cellMap since
04119             // then cells created from cells would get cellLevel_ of
04120             // cell they were created from.
04121             reorder(reverseCellMap, mesh_.nCells(), -1, cellLevel_);
04122         }
04123         else
04124         {
04125             // Map data
04126             const labelList& cellMap = map.cellMap();
04127 
04128             labelList newCellLevel(cellMap.size());
04129             forAll(cellMap, newCellI)
04130             {
04131                 label oldCellI = cellMap[newCellI];
04132 
04133                 if (oldCellI == -1)
04134                 {
04135                     newCellLevel[newCellI] = -1;
04136                 }
04137                 else
04138                 {
04139                     newCellLevel[newCellI] = cellLevel_[oldCellI];
04140                 }
04141             }
04142             cellLevel_.transfer(newCellLevel);
04143         }
04144 
04145         // See if any cells to restore. This will be for some new cells
04146         // the corresponding old cell.
04147         forAllConstIter(Map<label>, cellsToRestore, iter)
04148         {
04149             label newCellI = iter.key();
04150             label storedCellI = iter();
04151 
04152             Map<label>::iterator fnd = savedCellLevel_.find(storedCellI);
04153 
04154             if (fnd == savedCellLevel_.end())
04155             {
04156                 FatalErrorIn("hexRef8::updateMesh(const mapPolyMesh&)")
04157                     << "Problem : trying to restore old value for new cell "
04158                     << newCellI << " but cannot find old cell " << storedCellI
04159                     << " in map of stored values " << savedCellLevel_
04160                     << abort(FatalError);
04161             }
04162             cellLevel_[newCellI] = fnd();
04163         }
04164 
04165         //if (findIndex(cellLevel_, -1) != -1)
04166         //{
04167         //    WarningIn("hexRef8::updateMesh(const mapPolyMesh&)")
04168         //        << "Problem : "
04169         //        << "cellLevel_ contains illegal value -1 after mapping
04170         //        << " at cell " << findIndex(cellLevel_, -1) << endl
04171         //        << "This means that another program has inflated cells"
04172         //        << " (created cells out-of-nothing) and hence we don't know"
04173         //        << " their cell level. Continuing with illegal value."
04174         //        << abort(FatalError);
04175         //}
04176     }
04177 
04178 
04179     // Update pointlevel
04180     {
04181         const labelList& reversePointMap = map.reversePointMap();
04182 
04183         if (reversePointMap.size() == pointLevel_.size())
04184         {
04185             // Assume it is after hexRef8 that this routine is called.
04186             reorder(reversePointMap, mesh_.nPoints(), -1,  pointLevel_);
04187         }
04188         else
04189         {
04190             // Map data
04191             const labelList& pointMap = map.pointMap();
04192 
04193             labelList newPointLevel(pointMap.size());
04194 
04195             forAll(pointMap, newPointI)
04196             {
04197                 label oldPointI = pointMap[newPointI];
04198 
04199                 if (oldPointI == -1)
04200                 {
04201                     //FatalErrorIn("hexRef8::updateMesh(const mapPolyMesh&)")
04202                     //    << "Problem : point " << newPointI
04203                     //    << " at " << mesh_.points()[newPointI]
04204                     //    << " does not originate from another point"
04205                     //    << " (i.e. is inflated)." << nl
04206                     //    << "Hence we cannot determine the new pointLevel"
04207                     //    << " for it." << abort(FatalError);
04208                     newPointLevel[newPointI] = -1;
04209                 }
04210                 else
04211                 {
04212                     newPointLevel[newPointI] = pointLevel_[oldPointI];
04213                 }
04214             }
04215             pointLevel_.transfer(newPointLevel);
04216         }
04217 
04218         // See if any points to restore. This will be for some new points
04219         // the corresponding old point (the one from the call to storeData)
04220         forAllConstIter(Map<label>, pointsToRestore, iter)
04221         {
04222             label newPointI = iter.key();
04223             label storedPointI = iter();
04224 
04225             Map<label>::iterator fnd = savedPointLevel_.find(storedPointI);
04226 
04227             if (fnd == savedPointLevel_.end())
04228             {
04229                 FatalErrorIn("hexRef8::updateMesh(const mapPolyMesh&)")
04230                     << "Problem : trying to restore old value for new point "
04231                     << newPointI << " but cannot find old point "
04232                     << storedPointI
04233                     << " in map of stored values " << savedPointLevel_
04234                     << abort(FatalError);
04235             }
04236             pointLevel_[newPointI] = fnd();
04237         }
04238 
04239         //if (findIndex(pointLevel_, -1) != -1)
04240         //{
04241         //    WarningIn("hexRef8::updateMesh(const mapPolyMesh&)")
04242         //        << "Problem : "
04243         //        << "pointLevel_ contains illegal value -1 after mapping"
04244         //        << " at point" << findIndex(pointLevel_, -1) << endl
04245         //        << "This means that another program has inflated points"
04246         //        << " (created points out-of-nothing) and hence we don't know"
04247         //        << " their point level. Continuing with illegal value."
04248         //        //<< abort(FatalError);
04249         //}
04250     }
04251 
04252     // Update refinement tree
04253     if (history_.active())
04254     {
04255         history_.updateMesh(map);
04256     }
04257 
04258     // Mark files as changed
04259     setInstance(mesh_.facesInstance());
04260 
04261     // Update face removal engine
04262     faceRemover_.updateMesh(map);
04263 }
04264 
04265 
04266 // Gets called after mesh subsetting. Maps are from new back to old.
04267 void Foam::hexRef8::subset
04268 (
04269     const labelList& pointMap,
04270     const labelList& faceMap,
04271     const labelList& cellMap
04272 )
04273 {
04274     // Update celllevel
04275     if (debug)
04276     {
04277         Pout<< "hexRef8::subset :"
04278             << " Updating various lists"
04279             << endl;
04280     }
04281 
04282     if (history_.active())
04283     {
04284         WarningIn
04285         (
04286             "hexRef8::subset(const labelList&, const labelList&"
04287             ", const labelList&)"
04288         )   << "Subsetting will not work in combination with unrefinement."
04289             << nl
04290             << "Proceed at your own risk." << endl;
04291     }
04292 
04293 
04294     // Update celllevel
04295     {
04296         labelList newCellLevel(cellMap.size());
04297 
04298         forAll(cellMap, newCellI)
04299         {
04300             newCellLevel[newCellI] = cellLevel_[cellMap[newCellI]];
04301         }
04302 
04303         cellLevel_.transfer(newCellLevel);
04304 
04305         if (findIndex(cellLevel_, -1) != -1)
04306         {
04307             FatalErrorIn("hexRef8::subset(..)")
04308                 << "Problem : "
04309                 << "cellLevel_ contains illegal value -1 after mapping:"
04310                 << cellLevel_
04311                 << abort(FatalError);
04312         }
04313     }
04314 
04315     // Update pointlevel
04316     {
04317         labelList newPointLevel(pointMap.size());
04318 
04319         forAll(pointMap, newPointI)
04320         {
04321             newPointLevel[newPointI] = pointLevel_[pointMap[newPointI]];
04322         }
04323 
04324         pointLevel_.transfer(newPointLevel);
04325 
04326         if (findIndex(pointLevel_, -1) != -1)
04327         {
04328             FatalErrorIn("hexRef8::subset(..)")
04329                 << "Problem : "
04330                 << "pointLevel_ contains illegal value -1 after mapping:"
04331                 << pointLevel_
04332                 << abort(FatalError);
04333         }
04334     }
04335 
04336     // Update refinement tree
04337     if (history_.active())
04338     {
04339         history_.subset(pointMap, faceMap, cellMap);
04340     }
04341 
04342     // Mark files as changed
04343     setInstance(mesh_.facesInstance());
04344 
04345     // Nothing needs doing to faceRemover.
04346     //faceRemover_.subset(pointMap, faceMap, cellMap);
04347 }
04348 
04349 
04350 // Gets called after the mesh distribution
04351 void Foam::hexRef8::distribute(const mapDistributePolyMesh& map)
04352 {
04353     if (debug)
04354     {
04355         Pout<< "hexRef8::distribute :"
04356             << " Updating various lists"
04357             << endl;
04358     }
04359 
04360     // Update celllevel
04361     map.distributeCellData(cellLevel_);
04362     // Update pointlevel
04363     map.distributePointData(pointLevel_);
04364 
04365     // Update refinement tree
04366     if (history_.active())
04367     {
04368         history_.distribute(map);
04369     }
04370 
04371     // Update face removal engine
04372     faceRemover_.distribute(map);
04373 }
04374 
04375 
04376 void Foam::hexRef8::checkMesh() const
04377 {
04378     const scalar smallDim = 1E-6 * mesh_.globalData().bb().mag();
04379 
04380     if (debug)
04381     {
04382         Pout<< "hexRef8::checkMesh : Using matching tolerance smallDim:"
04383             << smallDim << endl;
04384     }
04385 
04386     // Check owner on coupled faces.
04387     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
04388 
04389     // There should be only one coupled face between two cells. Why? Since
04390     // otherwise mesh redistribution might cause multiple faces between two
04391     // cells
04392     {
04393         labelList nei(mesh_.nFaces()-mesh_.nInternalFaces());
04394         forAll(nei, i)
04395         {
04396             nei[i] = mesh_.faceOwner()[i+mesh_.nInternalFaces()];
04397         }
04398 
04399         // Replace data on coupled patches with their neighbour ones.
04400         syncTools::swapBoundaryFaceList(mesh_, nei, false);
04401 
04402         const polyBoundaryMesh& patches = mesh_.boundaryMesh();
04403 
04404         forAll(patches, patchI)
04405         {
04406             const polyPatch& pp = patches[patchI];
04407 
04408             if (pp.coupled())
04409             {
04410                 // Check how many faces between owner and neighbour. Should
04411                 // be only one.
04412                 HashTable<label, labelPair, labelPair::Hash<> >
04413                     cellToFace(2*pp.size());
04414 
04415                 label faceI = pp.start();
04416 
04417                 forAll(pp, i)
04418                 {
04419                     label own = mesh_.faceOwner()[faceI];
04420                     label bFaceI = faceI-mesh_.nInternalFaces();
04421 
04422                     if (!cellToFace.insert(labelPair(own, nei[bFaceI]), faceI))
04423                     {
04424                         dumpCell(own);
04425                         FatalErrorIn("hexRef8::checkMesh()")
04426                             << "Faces do not seem to be correct across coupled"
04427                             << " boundaries" << endl
04428                             << "Coupled face " << faceI
04429                             << " between owner " << own
04430                             << " on patch " << pp.name()
04431                             << " and coupled neighbour " << nei[bFaceI]
04432                             << " has two faces connected to it:"
04433                             << faceI << " and "
04434                             << cellToFace[labelPair(own, nei[bFaceI])]
04435                             << abort(FatalError);
04436                     }
04437 
04438                     faceI++;
04439                 }
04440             }
04441         }
04442     }
04443 
04444     // Check face areas.
04445     // ~~~~~~~~~~~~~~~~~
04446 
04447     {
04448         scalarField neiFaceAreas(mesh_.nFaces()-mesh_.nInternalFaces());
04449         forAll(neiFaceAreas, i)
04450         {
04451             neiFaceAreas[i] = mag(mesh_.faceAreas()[i+mesh_.nInternalFaces()]);
04452         }
04453 
04454         // Replace data on coupled patches with their neighbour ones.
04455         syncTools::swapBoundaryFaceList(mesh_, neiFaceAreas, false);
04456 
04457         forAll(neiFaceAreas, i)
04458         {
04459             label faceI = i+mesh_.nInternalFaces();
04460 
04461             const scalar magArea = mag(mesh_.faceAreas()[faceI]);
04462 
04463             if (mag(magArea - neiFaceAreas[i]) > smallDim)
04464             {
04465                 const face& f = mesh_.faces()[faceI];
04466                 label patchI = mesh_.boundaryMesh().whichPatch(faceI);
04467 
04468                 dumpCell(mesh_.faceOwner()[faceI]);
04469 
04470                 FatalErrorIn("hexRef8::checkMesh()")
04471                     << "Faces do not seem to be correct across coupled"
04472                     << " boundaries" << endl
04473                     << "Coupled face " << faceI
04474                     << " on patch " << patchI
04475                     << " " << mesh_.boundaryMesh()[patchI].name()
04476                     << " coords:" << UIndirectList<point>(mesh_.points(), f)()
04477                     << " has face area:" << magArea
04478                     << " (coupled) neighbour face area differs:"
04479                     << neiFaceAreas[i]
04480                     << " to within tolerance " << smallDim
04481                     << abort(FatalError);
04482             }
04483         }
04484     }
04485 
04486 
04487     // Check number of points on faces.
04488     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
04489     {
04490         labelList nVerts(mesh_.nFaces()-mesh_.nInternalFaces());
04491 
04492         forAll(nVerts, i)
04493         {
04494             nVerts[i] = mesh_.faces()[i+mesh_.nInternalFaces()].size();
04495         }
04496 
04497         // Replace data on coupled patches with their neighbour ones.
04498         syncTools::swapBoundaryFaceList(mesh_, nVerts, false);
04499 
04500         forAll(nVerts, i)
04501         {
04502             label faceI = i+mesh_.nInternalFaces();
04503 
04504             const face& f = mesh_.faces()[faceI];
04505 
04506             if (f.size() != nVerts[i])
04507             {
04508                 dumpCell(mesh_.faceOwner()[faceI]);
04509 
04510                 label patchI = mesh_.boundaryMesh().whichPatch(faceI);
04511 
04512                 FatalErrorIn("hexRef8::checkMesh()")
04513                     << "Faces do not seem to be correct across coupled"
04514                     << " boundaries" << endl
04515                     << "Coupled face " << faceI
04516                     << " on patch " << patchI
04517                     << " " << mesh_.boundaryMesh()[patchI].name()
04518                     << " coords:" << UIndirectList<point>(mesh_.points(), f)()
04519                     << " has size:" << f.size()
04520                     << " (coupled) neighbour face has size:"
04521                     << nVerts[i]
04522                     << abort(FatalError);
04523             }
04524         }
04525     }
04526 
04527 
04528     // Check points of face
04529     // ~~~~~~~~~~~~~~~~~~~~
04530     {
04531         // Anchor points.
04532         pointField anchorPoints(mesh_.nFaces()-mesh_.nInternalFaces());
04533 
04534         forAll(anchorPoints, i)
04535         {
04536             label faceI = i+mesh_.nInternalFaces();
04537             const point& fc = mesh_.faceCentres()[faceI];
04538             const face& f = mesh_.faces()[faceI];
04539             const vector anchorVec(mesh_.points()[f[0]] - fc);
04540 
04541             anchorPoints[i] = anchorVec;
04542         }
04543 
04544         // Replace data on coupled patches with their neighbour ones. Apply
04545         // rotation transformation (but not separation since is relative vector
04546         // to point on same face.
04547         syncTools::swapBoundaryFaceList(mesh_, anchorPoints, false);
04548 
04549         forAll(anchorPoints, i)
04550         {
04551             label faceI = i+mesh_.nInternalFaces();
04552             const point& fc = mesh_.faceCentres()[faceI];
04553             const face& f = mesh_.faces()[faceI];
04554             const vector anchorVec(mesh_.points()[f[0]] - fc);
04555 
04556             if (mag(anchorVec - anchorPoints[i]) > smallDim)
04557             {
04558                 dumpCell(mesh_.faceOwner()[faceI]);
04559 
04560                 label patchI = mesh_.boundaryMesh().whichPatch(faceI);
04561 
04562                 FatalErrorIn("hexRef8::checkMesh()")
04563                     << "Faces do not seem to be correct across coupled"
04564                     << " boundaries" << endl
04565                     << "Coupled face " << faceI
04566                     << " on patch " << patchI
04567                     << " " << mesh_.boundaryMesh()[patchI].name()
04568                     << " coords:" << UIndirectList<point>(mesh_.points(), f)()
04569                     << " has anchor vector:" << anchorVec
04570                     << " (coupled) neighbour face anchor vector differs:"
04571                     << anchorPoints[i]
04572                     << " to within tolerance " << smallDim
04573                     << abort(FatalError);
04574             }
04575         }
04576     }
04577 
04578     if (debug)
04579     {
04580         Pout<< "hexRef8::checkMesh : Returning" << endl;
04581     }
04582 }
04583 
04584 
04585 void Foam::hexRef8::checkRefinementLevels
04586 (
04587     const label maxPointDiff,
04588     const labelList& pointsToCheck
04589 ) const
04590 {
04591     if (debug)
04592     {
04593         Pout<< "hexRef8::checkRefinementLevels :"
04594             << " Checking 2:1 refinement level" << endl;
04595     }
04596 
04597     if
04598     (
04599         cellLevel_.size() != mesh_.nCells()
04600      || pointLevel_.size() != mesh_.nPoints()
04601     )
04602     {
04603         FatalErrorIn("hexRef8::checkRefinementLevels(const label)")
04604             << "cellLevel size should be number of cells"
04605             << " and pointLevel size should be number of points."<< nl
04606             << "cellLevel:" << cellLevel_.size()
04607             << " mesh.nCells():" << mesh_.nCells() << nl
04608             << "pointLevel:" << pointLevel_.size()
04609             << " mesh.nPoints():" << mesh_.nPoints()
04610             << abort(FatalError);
04611     }
04612 
04613 
04614     // Check 2:1 consistency.
04615     // ~~~~~~~~~~~~~~~~~~~~~~
04616 
04617     {
04618         // Internal faces.
04619         for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
04620         {
04621             label own = mesh_.faceOwner()[faceI];
04622             label nei = mesh_.faceNeighbour()[faceI];
04623 
04624             if (mag(cellLevel_[own] - cellLevel_[nei]) > 1)
04625             {
04626                 dumpCell(own);
04627                 dumpCell(nei);
04628 
04629                 FatalErrorIn
04630                 (
04631                     "hexRef8::checkRefinementLevels(const label)"
04632                 )   << "Celllevel does not satisfy 2:1 constraint." << nl
04633                     << "On face " << faceI << " owner cell " << own
04634                     << " has refinement " << cellLevel_[own]
04635                     << " neighbour cell " << nei << " has refinement "
04636                     << cellLevel_[nei]
04637                     << abort(FatalError);
04638             }
04639         }
04640 
04641         // Coupled faces. Get neighbouring value
04642         labelList neiLevel(mesh_.nFaces()-mesh_.nInternalFaces());
04643 
04644         forAll(neiLevel, i)
04645         {
04646             label own = mesh_.faceOwner()[i+mesh_.nInternalFaces()];
04647 
04648             neiLevel[i] = cellLevel_[own];
04649         }
04650 
04651         // No separation
04652         syncTools::swapBoundaryFaceList(mesh_, neiLevel, false);
04653 
04654         forAll(neiLevel, i)
04655         {
04656             label faceI = i+mesh_.nInternalFaces();
04657 
04658             label own = mesh_.faceOwner()[faceI];
04659 
04660             if (mag(cellLevel_[own] - neiLevel[i]) > 1)
04661             {
04662                 dumpCell(own);
04663 
04664                 label patchI = mesh_.boundaryMesh().whichPatch(faceI);
04665 
04666                 FatalErrorIn
04667                 (
04668                     "hexRef8::checkRefinementLevels(const label)"
04669                 )   << "Celllevel does not satisfy 2:1 constraint."
04670                     << " On coupled face " << faceI
04671                     << " on patch " << patchI << " "
04672                     << mesh_.boundaryMesh()[patchI].name()
04673                     << " owner cell " << own << " has refinement "
04674                     << cellLevel_[own]
04675                     << " (coupled) neighbour cell has refinement "
04676                     << neiLevel[i]
04677                     << abort(FatalError);
04678             }
04679         }
04680     }
04681 
04682 
04683     // Check pointLevel is synchronized
04684     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
04685     {
04686         labelList syncPointLevel(pointLevel_);
04687 
04688         // Get min level
04689         syncTools::syncPointList
04690         (
04691             mesh_,
04692             syncPointLevel,
04693             minEqOp<label>(),
04694             labelMax,
04695             false               // no separation
04696         );
04697 
04698 
04699         forAll(syncPointLevel, pointI)
04700         {
04701             if (pointLevel_[pointI] != syncPointLevel[pointI])
04702             {
04703                 FatalErrorIn
04704                 (
04705                     "hexRef8::checkRefinementLevels(const label)"
04706                 )   << "PointLevel is not consistent across coupled patches."
04707                     << endl
04708                     << "point:" << pointI << " coord:" << mesh_.points()[pointI]
04709                     << " has level " << pointLevel_[pointI]
04710                     << " whereas the coupled point has level "
04711                     << syncPointLevel[pointI]
04712                     << abort(FatalError);
04713             }
04714         }
04715     }
04716 
04717 
04718     // Check 2:1 across points (instead of faces)
04719     if (maxPointDiff != -1)
04720     {
04721         // Determine per point the max cell level.
04722         labelList maxPointLevel(mesh_.nPoints(), 0);
04723 
04724         forAll(maxPointLevel, pointI)
04725         {
04726             const labelList& pCells = mesh_.pointCells(pointI);
04727 
04728             label& pLevel = maxPointLevel[pointI];
04729 
04730             forAll(pCells, i)
04731             {
04732                 pLevel = max(pLevel, cellLevel_[pCells[i]]);
04733             }
04734         }
04735 
04736         // Sync maxPointLevel to neighbour
04737         syncTools::syncPointList
04738         (
04739             mesh_,
04740             maxPointLevel,
04741             maxEqOp<label>(),
04742             labelMin,           // null value
04743             false               // no separation
04744         );
04745 
04746         // Check 2:1 across boundary points
04747         forAll(pointsToCheck, i)
04748         {
04749             label pointI = pointsToCheck[i];
04750 
04751             const labelList& pCells = mesh_.pointCells(pointI);
04752 
04753             forAll(pCells, i)
04754             {
04755                 label cellI = pCells[i];
04756 
04757                 if
04758                 (
04759                     mag(cellLevel_[cellI]-maxPointLevel[pointI])
04760                   > maxPointDiff
04761                 )
04762                 {
04763                     dumpCell(cellI);
04764 
04765                     FatalErrorIn
04766                     (
04767                         "hexRef8::checkRefinementLevels(const label)"
04768                     )   << "Too big a difference between"
04769                         << " point-connected cells." << nl
04770                         << "cell:" << cellI
04771                         << " cellLevel:" << cellLevel_[cellI]
04772                         << " uses point:" << pointI
04773                         << " coord:" << mesh_.points()[pointI]
04774                         << " which is also used by a cell with level:"
04775                         << maxPointLevel[pointI]
04776                         << abort(FatalError);
04777                 }
04778             }
04779         }
04780     }
04781 
04782 
04783     //- Gives problems after first splitting off inside mesher.
04785     //{
04786     //    // Any patches with points having only two edges.
04787     //
04788     //    boolList isHangingPoint(mesh_.nPoints(), false);
04789     //
04790     //    const polyBoundaryMesh& patches = mesh_.boundaryMesh();
04791     //
04792     //    forAll(patches, patchI)
04793     //    {
04794     //        const polyPatch& pp = patches[patchI];
04795     //
04796     //        const labelList& meshPoints = pp.meshPoints();
04797     //
04798     //        forAll(meshPoints, i)
04799     //        {
04800     //            label pointI = meshPoints[i];
04801     //
04802     //            const labelList& pEdges = mesh_.pointEdges()[pointI];
04803     //
04804     //            if (pEdges.size() == 2)
04805     //            {
04806     //                isHangingPoint[pointI] = true;
04807     //            }
04808     //        }
04809     //    }
04810     //
04811     //    syncTools::syncPointList
04812     //    (
04813     //        mesh_,
04814     //        isHangingPoint,
04815     //        andEqOp<bool>(),        // only if all decide it is hanging point
04816     //        true,                   // null
04817     //        false                   // no separation
04818     //    );
04819     //
04820     //    //OFstream str(mesh_.time().path()/"hangingPoints.obj");
04821     //
04822     //    label nHanging = 0;
04823     //
04824     //    forAll(isHangingPoint, pointI)
04825     //    {
04826     //        if (isHangingPoint[pointI])
04827     //        {
04828     //            nHanging++;
04829     //
04830     //            Pout<< "Hanging boundary point " << pointI
04831     //                << " at " << mesh_.points()[pointI]
04832     //                << endl;
04833     //            //meshTools::writeOBJ(str, mesh_.points()[pointI]);
04834     //        }
04835     //    }
04836     //
04837     //    if (returnReduce(nHanging, sumOp<label>()) > 0)
04838     //    {
04839     //        FatalErrorIn
04840     //        (
04841     //            "hexRef8::checkRefinementLevels(const label)"
04842     //        )   << "Detected a point used by two edges only (hanging point)"
04843     //            << nl << "This is not allowed"
04844     //            << abort(FatalError);
04845     //    }
04846     //}
04847 }
04848 
04849 
04850 
04851 //
04852 // Unrefinement
04853 // ~~~~~~~~~~~~
04854 //
04855 
04856 
04857 Foam::labelList Foam::hexRef8::getSplitPoints() const
04858 {
04859     if (debug)
04860     {
04861         checkRefinementLevels(-1, labelList(0));
04862     }
04863 
04864     if (debug)
04865     {
04866         Pout<< "hexRef8::getSplitPoints :"
04867             << " Calculating unrefineable points" << endl;
04868     }
04869 
04870 
04871     if (!history_.active())
04872     {
04873         FatalErrorIn("hexRef8::getSplitPoints()")
04874             << "Only call if constructed with history capability"
04875             << abort(FatalError);
04876     }
04877 
04878     // Master cell
04879     // -1 undetermined
04880     // -2 certainly not split point
04881     // >= label of master cell
04882     labelList splitMaster(mesh_.nPoints(), -1);
04883     labelList splitMasterLevel(mesh_.nPoints(), 0);
04884 
04885     // Unmark all with not 8 cells
04886     //const labelListList& pointCells = mesh_.pointCells();
04887 
04888     for (label pointI = 0; pointI < mesh_.nPoints(); pointI++)
04889     {
04890         const labelList& pCells = mesh_.pointCells(pointI);
04891 
04892         if (pCells.size() != 8)
04893         {
04894             splitMaster[pointI] = -2;
04895         }
04896     }
04897 
04898     // Unmark all with different master cells
04899     const labelList& visibleCells = history_.visibleCells();
04900 
04901     forAll(visibleCells, cellI)
04902     {
04903         const labelList& cPoints = mesh_.cellPoints(cellI);
04904 
04905         if (visibleCells[cellI] != -1 && history_.parentIndex(cellI) >= 0)
04906         {
04907             label parentIndex = history_.parentIndex(cellI);
04908 
04909             // Check same master.
04910             forAll(cPoints, i)
04911             {
04912                 label pointI = cPoints[i];
04913 
04914                 label masterCellI = splitMaster[pointI];
04915 
04916                 if (masterCellI == -1)
04917                 {
04918                     // First time visit of point. Store parent cell and
04919                     // level of the parent cell (with respect to cellI). This
04920                     // is additional guarantee that we're referring to the
04921                     // same master at the same refinement level.
04922 
04923                     splitMaster[pointI] = parentIndex;
04924                     splitMasterLevel[pointI] = cellLevel_[cellI] - 1;
04925                 }
04926                 else if (masterCellI == -2)
04927                 {
04928                     // Already decided that point is not splitPoint
04929                 }
04930                 else if
04931                 (
04932                     (masterCellI != parentIndex)
04933                  || (splitMasterLevel[pointI] != cellLevel_[cellI] - 1)
04934                 )
04935                 {
04936                     // Different masters so point is on two refinement
04937                     // patterns
04938                     splitMaster[pointI] = -2;
04939                 }
04940             }
04941         }
04942         else
04943         {
04944             // Either not visible or is unrefined cell
04945             forAll(cPoints, i)
04946             {
04947                 label pointI = cPoints[i];
04948 
04949                 splitMaster[pointI] = -2;
04950             }
04951         }
04952     }
04953 
04954     // Unmark boundary faces
04955     for
04956     (
04957         label faceI = mesh_.nInternalFaces();
04958         faceI < mesh_.nFaces();
04959         faceI++
04960     )
04961     {
04962         const face& f = mesh_.faces()[faceI];
04963 
04964         forAll(f, fp)
04965         {
04966             splitMaster[f[fp]] = -2;
04967         }
04968     }
04969 
04970 
04971     // Collect into labelList
04972 
04973     label nSplitPoints = 0;
04974 
04975     forAll(splitMaster, pointI)
04976     {
04977         if (splitMaster[pointI] >= 0)
04978         {
04979             nSplitPoints++;
04980         }
04981     }
04982 
04983     labelList splitPoints(nSplitPoints);
04984     nSplitPoints = 0;
04985 
04986     forAll(splitMaster, pointI)
04987     {
04988         if (splitMaster[pointI] >= 0)
04989         {
04990             splitPoints[nSplitPoints++] = pointI;
04991         }
04992     }
04993 
04994     return splitPoints;
04995 }
04996 
04997 
04998 //void Foam::hexRef8::markIndex
04999 //(
05000 //    const label maxLevel,
05001 //    const label level,
05002 //    const label index,
05003 //    const label markValue,
05004 //    labelList& indexValues
05005 //) const
05006 //{
05007 //    if (level < maxLevel && indexValues[index] == -1)
05008 //    {
05009 //        // Mark
05010 //        indexValues[index] = markValue;
05011 //
05012 //        // Mark parent
05013 //        const splitCell8& split = history_.splitCells()[index];
05014 //
05015 //        if (split.parent_ >= 0)
05016 //        {
05017 //            markIndex
05018 //            (
05019 //              maxLevel, level+1, split.parent_, markValue, indexValues);
05020 //            )
05021 //        }
05022 //    }
05023 //}
05024 //
05025 //
05030 //void Foam::hexRef8::markCellClusters
05031 //(
05032 //    const label maxLevel,
05033 //    labelList& cluster
05034 //) const
05035 //{
05036 //    cluster.setSize(mesh_.nCells());
05037 //    cluster = -1;
05038 //
05039 //    const DynamicList<splitCell8>& splitCells = history_.splitCells();
05040 //
05041 //    // Mark all splitCells down to level maxLevel with a cell originating from
05042 //    // it.
05043 //
05044 //    labelList indexLevel(splitCells.size(), -1);
05045 //
05046 //    forAll(visibleCells, cellI)
05047 //    {
05048 //        label index = visibleCells[cellI];
05049 //
05050 //        if (index >= 0)
05051 //        {
05052 //            markIndex(maxLevel, 0, index, cellI, indexLevel);
05053 //        }
05054 //    }
05055 //
05056 //    // Mark cells with splitCell
05057 //}
05058 
05059 
05060 Foam::labelList Foam::hexRef8::consistentUnrefinement
05061 (
05062     const labelList& pointsToUnrefine,
05063     const bool maxSet
05064 ) const
05065 {
05066     if (debug)
05067     {
05068         Pout<< "hexRef8::consistentUnrefinement :"
05069             << " Determining 2:1 consistent unrefinement" << endl;
05070     }
05071 
05072     if (maxSet)
05073     {
05074         FatalErrorIn
05075         (
05076             "hexRef8::consistentUnrefinement(const labelList&, const bool"
05077         )   << "maxSet not implemented yet."
05078             << abort(FatalError);
05079     }
05080 
05081     // Loop, modifying pointsToUnrefine, until no more changes to due to 2:1
05082     // conflicts.
05083     // maxSet = false : unselect points to refine
05084     // maxSet = true: select points to refine
05085 
05086     // Maintain boolList for pointsToUnrefine and cellsToUnrefine
05087     PackedBoolList unrefinePoint(mesh_.nPoints());
05088 
05089     forAll(pointsToUnrefine, i)
05090     {
05091         label pointI = pointsToUnrefine[i];
05092 
05093         unrefinePoint.set(pointI, 1);
05094     }
05095 
05096 
05097     while (true)
05098     {
05099         // Construct cells to unrefine
05100         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
05101 
05102         PackedBoolList unrefineCell(mesh_.nCells());
05103 
05104         forAll(unrefinePoint, pointI)
05105         {
05106             if (unrefinePoint.get(pointI) == 1)
05107             {
05108                 const labelList& pCells = mesh_.pointCells(pointI);
05109 
05110                 forAll(pCells, j)
05111                 {
05112                     unrefineCell.set(pCells[j], 1);
05113                 }
05114             }
05115         }
05116 
05117 
05118         label nChanged = 0;
05119 
05120 
05121         // Check 2:1 consistency taking refinement into account
05122         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
05123 
05124         // Internal faces.
05125         for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
05126         {
05127             label own = mesh_.faceOwner()[faceI];
05128             label ownLevel = cellLevel_[own] - unrefineCell.get(own);
05129 
05130             label nei = mesh_.faceNeighbour()[faceI];
05131             label neiLevel = cellLevel_[nei] - unrefineCell.get(nei);
05132 
05133             if (ownLevel < (neiLevel-1))
05134             {
05135                 // Since was 2:1 this can only occur if own is marked for
05136                 // unrefinement.
05137 
05138                 if (maxSet)
05139                 {
05140                     unrefineCell.set(nei, 1);
05141                 }
05142                 else
05143                 {
05144                     if (unrefineCell.get(own) == 0)
05145                     {
05146                         FatalErrorIn("hexRef8::consistentUnrefinement(..)")
05147                             << "problem" << abort(FatalError);
05148                     }
05149 
05150                     unrefineCell.set(own, 0);
05151                 }
05152                 nChanged++;
05153             }
05154             else if (neiLevel < (ownLevel-1))
05155             {
05156                 if (maxSet)
05157                 {
05158                     unrefineCell.set(own, 1);
05159                 }
05160                 else
05161                 {
05162                     if (unrefineCell.get(nei) == 0)
05163                     {
05164                         FatalErrorIn("hexRef8::consistentUnrefinement(..)")
05165                             << "problem" << abort(FatalError);
05166                     }
05167 
05168                     unrefineCell.set(nei, 0);
05169                 }
05170                 nChanged++;
05171             }
05172         }
05173 
05174 
05175         // Coupled faces. Swap owner level to get neighbouring cell level.
05176         labelList neiLevel(mesh_.nFaces()-mesh_.nInternalFaces());
05177 
05178         forAll(neiLevel, i)
05179         {
05180             label own = mesh_.faceOwner()[i+mesh_.nInternalFaces()];
05181 
05182             neiLevel[i] = cellLevel_[own] - unrefineCell.get(own);
05183         }
05184 
05185         // Swap to neighbour
05186         syncTools::swapBoundaryFaceList(mesh_, neiLevel, false);
05187 
05188         forAll(neiLevel, i)
05189         {
05190             label faceI = i+mesh_.nInternalFaces();
05191             label own = mesh_.faceOwner()[faceI];
05192             label ownLevel = cellLevel_[own] - unrefineCell.get(own);
05193 
05194             if (ownLevel < (neiLevel[i]-1))
05195             {
05196                 if (!maxSet)
05197                 {
05198                     if (unrefineCell.get(own) == 0)
05199                     {
05200                         FatalErrorIn("hexRef8::consistentUnrefinement(..)")
05201                             << "problem" << abort(FatalError);
05202                     }
05203 
05204                     unrefineCell.set(own, 0);
05205                     nChanged++;
05206                 }
05207             }
05208             else if (neiLevel[i] < (ownLevel-1))
05209             {
05210                 if (maxSet)
05211                 {
05212                     if (unrefineCell.get(own) == 1)
05213                     {
05214                         FatalErrorIn("hexRef8::consistentUnrefinement(..)")
05215                             << "problem" << abort(FatalError);
05216                     }
05217 
05218                     unrefineCell.set(own, 1);
05219                     nChanged++;
05220                 }
05221             }
05222         }
05223 
05224         reduce(nChanged, sumOp<label>());
05225 
05226         if (debug)
05227         {
05228             Pout<< "hexRef8::consistentUnrefinement :"
05229                 << " Changed " << nChanged
05230                 << " refinement levels due to 2:1 conflicts."
05231                 << endl;
05232         }
05233 
05234         if (nChanged == 0)
05235         {
05236             break;
05237         }
05238 
05239 
05240         // Convert cellsToUnrefine back into points to unrefine
05241         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
05242 
05243         // Knock out any point whose cell neighbour cannot be unrefined.
05244         forAll(unrefinePoint, pointI)
05245         {
05246             if (unrefinePoint.get(pointI) == 1)
05247             {
05248                 const labelList& pCells = mesh_.pointCells(pointI);
05249 
05250                 forAll(pCells, j)
05251                 {
05252                     if (unrefineCell.get(pCells[j]) == 0)
05253                     {
05254                         unrefinePoint.set(pointI, 0);
05255                         break;
05256                     }
05257                 }
05258             }
05259         }
05260     }
05261 
05262 
05263     // Convert back to labelList.
05264     label nSet = 0;
05265 
05266     forAll(unrefinePoint, pointI)
05267     {
05268         if (unrefinePoint.get(pointI) == 1)
05269         {
05270             nSet++;
05271         }
05272     }
05273 
05274     labelList newPointsToUnrefine(nSet);
05275     nSet = 0;
05276 
05277     forAll(unrefinePoint, pointI)
05278     {
05279         if (unrefinePoint.get(pointI) == 1)
05280         {
05281             newPointsToUnrefine[nSet++] = pointI;
05282         }
05283     }
05284 
05285     return newPointsToUnrefine;
05286 }
05287 
05288 
05289 void Foam::hexRef8::setUnrefinement
05290 (
05291     const labelList& splitPointLabels,
05292     polyTopoChange& meshMod
05293 )
05294 {
05295     if (!history_.active())
05296     {
05297         FatalErrorIn
05298         (
05299             "hexRef8::setUnrefinement(const labelList&, polyTopoChange&)"
05300         )   << "Only call if constructed with history capability"
05301             << abort(FatalError);
05302     }
05303 
05304     if (debug)
05305     {
05306         Pout<< "hexRef8::setUnrefinement :"
05307             << " Checking initial mesh just to make sure" << endl;
05308 
05309         checkMesh();
05310 
05311         forAll(cellLevel_, cellI)
05312         {
05313             if (cellLevel_[cellI] < 0)
05314             {
05315                 FatalErrorIn
05316                 (
05317                     "hexRef8::setUnrefinement(const labelList&, polyTopoChange&)"
05318                 )   << "Illegal cell level " << cellLevel_[cellI]
05319                     << " for cell " << cellI
05320                     << abort(FatalError);
05321             }
05322         }
05323 
05324 
05325         // Write to sets.
05326         pointSet pSet(mesh_, "splitPoints", splitPointLabels);
05327         pSet.write();
05328 
05329         cellSet cSet(mesh_, "splitPointCells", splitPointLabels.size());
05330 
05331         forAll(splitPointLabels, i)
05332         {
05333             const labelList& pCells = mesh_.pointCells(splitPointLabels[i]);
05334 
05335             forAll(pCells, j)
05336             {
05337                 cSet.insert(pCells[j]);
05338             }
05339         }
05340         cSet.write();
05341 
05342         Pout<< "hexRef8::setRefinement : Dumping " << pSet.size()
05343             << " points and "
05344             << cSet.size() << " cells for unrefinement to" << nl
05345             << "    pointSet " << pSet.objectPath() << nl
05346             << "    cellSet " << cSet.objectPath()
05347             << endl;
05348     }
05349 
05350 
05351     labelList cellRegion;
05352     labelList cellRegionMaster;
05353     labelList facesToRemove;
05354 
05355     {
05356         labelHashSet splitFaces(12*splitPointLabels.size());
05357 
05358         forAll(splitPointLabels, i)
05359         {
05360             const labelList& pFaces = mesh_.pointFaces()[splitPointLabels[i]];
05361 
05362             forAll(pFaces, j)
05363             {
05364                 splitFaces.insert(pFaces[j]);
05365             }
05366         }
05367 
05368         // Check with faceRemover what faces will get removed. Note that this
05369         // can be more (but never less) than splitFaces provided.
05370         faceRemover_.compatibleRemoves
05371         (
05372             splitFaces.toc(),   // pierced faces
05373             cellRegion,         // per cell -1 or region it is merged into
05374             cellRegionMaster,   // per region the master cell
05375             facesToRemove       // new faces to be removed.
05376         );
05377 
05378         if (facesToRemove.size() != splitFaces.size())
05379         {
05380             FatalErrorIn
05381             (
05382                 "hexRef8::setUnrefinement(const labelList&, polyTopoChange&)"
05383             )   << "Ininitial set of split points to unrefine does not"
05384                 << " seem to be consistent or not mid points of refined cells"
05385                 << abort(FatalError);
05386         }
05387     }
05388 
05389     // Redo the region master so it is consistent with our master.
05390     // This will guarantee that the new cell (for which faceRemover uses
05391     // the region master) is already compatible with our refinement structure.
05392 
05393     forAll(splitPointLabels, i)
05394     {
05395         label pointI = splitPointLabels[i];
05396 
05397         // Get original cell label
05398 
05399         const labelList& pCells = mesh_.pointCells(pointI);
05400 
05401         // Check
05402         if (pCells.size() != 8)
05403         {
05404             FatalErrorIn
05405             (
05406                 "hexRef8::setUnrefinement(const labelList&, polyTopoChange&)"
05407             )   << "splitPoint " << pointI
05408                 << " should have 8 cells using it. It has " << pCells
05409                 << abort(FatalError);
05410         }
05411 
05412 
05413         // Check that the lowest numbered pCells is the master of the region
05414         // (should be guaranteed by directRemoveFaces)
05415         //if (debug)
05416         {
05417             label masterCellI = min(pCells);
05418 
05419             forAll(pCells, j)
05420             {
05421                 label cellI = pCells[j];
05422 
05423                 label region = cellRegion[cellI];
05424 
05425                 if (region == -1)
05426                 {
05427                     FatalErrorIn("hexRef8::setUnrefinement(..)")
05428                         << "Ininitial set of split points to unrefine does not"
05429                         << " seem to be consistent or not mid points"
05430                         << " of refined cells" << nl
05431                         << "cell:" << cellI << " on splitPoint " << pointI
05432                         << " has no region to be merged into"
05433                         << abort(FatalError);
05434                 }
05435 
05436                 if (masterCellI != cellRegionMaster[region])
05437                 {
05438                     FatalErrorIn("hexRef8::setUnrefinement(..)")
05439                         << "cell:" << cellI << " on splitPoint:" << pointI
05440                         << " in region " << region
05441                         << " has master:" << cellRegionMaster[region]
05442                         << " which is not the lowest numbered cell"
05443                         << " among the pointCells:" << pCells
05444                         << abort(FatalError);
05445                 }
05446             }
05447         }
05448     }
05449 
05450     // Insert all commands to combine cells. Never fails so don't have to
05451     // test for success.
05452     faceRemover_.setRefinement
05453     (
05454         facesToRemove,
05455         cellRegion,
05456         cellRegionMaster,
05457         meshMod
05458     );
05459 
05460     // Remove the 8 cells that originated from merging around the split point
05461     // and adapt cell levels (not that pointLevels stay the same since points
05462     // either get removed or stay at the same position.
05463     forAll(splitPointLabels, i)
05464     {
05465         label pointI = splitPointLabels[i];
05466 
05467         const labelList& pCells = mesh_.pointCells(pointI);
05468 
05469         label masterCellI = min(pCells);
05470 
05471         forAll(pCells, j)
05472         {
05473             cellLevel_[pCells[j]]--;
05474         }
05475 
05476         history_.combineCells(masterCellI, pCells);
05477     }
05478 
05479     // Mark files as changed
05480     setInstance(mesh_.facesInstance());
05481 
05482     // history_.updateMesh will take care of truncating.
05483 }
05484 
05485 
05486 // Write refinement to polyMesh directory.
05487 bool Foam::hexRef8::write() const
05488 {
05489     bool writeOk = cellLevel_.write() && pointLevel_.write();
05490 
05491     if (history_.active())
05492     {
05493         writeOk = writeOk && history_.write();
05494     }
05495 
05496     return writeOk;
05497 }
05498 
05499 
05500 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd