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

dynamicRefineFvMesh.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 "dynamicRefineFvMesh.H"
00028 #include "addToRunTimeSelectionTable.H"
00029 #include "volFields.H"
00030 #include "polyTopoChange.H"
00031 #include "surfaceFields.H"
00032 #include "fvCFD.H"
00033 #include "syncTools.H"
00034 #include "pointFields.H"
00035 
00036 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00037 
00038 namespace Foam
00039 {
00040 
00041 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00042 
00043 defineTypeNameAndDebug(dynamicRefineFvMesh, 0);
00044 
00045 addToRunTimeSelectionTable(dynamicFvMesh, dynamicRefineFvMesh, IOobject);
00046 
00047 
00048 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00049 
00050 label dynamicRefineFvMesh::count
00051 (
00052     const PackedBoolList& l,
00053     const unsigned int val
00054 )
00055 {
00056     label n = 0;
00057     forAll(l, i)
00058     {
00059         if (l.get(i) == val)
00060         {
00061             n++;
00062         }
00063     }
00064     return n;
00065 }
00066 
00067 
00068 void dynamicRefineFvMesh::calculateProtectedCells
00069 (
00070     PackedBoolList& unrefineableCell
00071 ) const
00072 {
00073     if (protectedCell_.empty())
00074     {
00075         unrefineableCell.clear();
00076         return;
00077     }
00078 
00079     const labelList& cellLevel = meshCutter_.cellLevel();
00080 
00081     unrefineableCell = protectedCell_;
00082 
00083     // Get neighbouring cell level
00084     labelList neiLevel(nFaces()-nInternalFaces());
00085 
00086     for (label faceI = nInternalFaces(); faceI < nFaces(); faceI++)
00087     {
00088         neiLevel[faceI-nInternalFaces()] = cellLevel[faceOwner()[faceI]];
00089     }
00090     syncTools::swapBoundaryFaceList(*this, neiLevel, false);
00091 
00092 
00093     while (true)
00094     {
00095         // Pick up faces on border of protected cells
00096         boolList seedFace(nFaces(), false);
00097 
00098         forAll(faceNeighbour(), faceI)
00099         {
00100             label own = faceOwner()[faceI];
00101             bool ownProtected = (unrefineableCell.get(own) == 1);
00102             label nei = faceNeighbour()[faceI];
00103             bool neiProtected = (unrefineableCell.get(nei) == 1);
00104 
00105             if (ownProtected && (cellLevel[nei] > cellLevel[own]))
00106             {
00107                 seedFace[faceI] = true;
00108             }
00109             else if (neiProtected && (cellLevel[own] > cellLevel[nei]))
00110             {
00111                 seedFace[faceI] = true;
00112             }
00113         }
00114         for (label faceI = nInternalFaces(); faceI < nFaces(); faceI++)
00115         {
00116             label own = faceOwner()[faceI];
00117             bool ownProtected = (unrefineableCell.get(own) == 1);
00118             if
00119             (
00120                 ownProtected
00121              && (neiLevel[faceI-nInternalFaces()] > cellLevel[own])
00122             )
00123             {
00124                 seedFace[faceI] = true;
00125             }
00126         }
00127 
00128         syncTools::syncFaceList(*this, seedFace, orEqOp<bool>(), false);
00129 
00130 
00131         // Extend unrefineableCell
00132         bool hasExtended = false;
00133 
00134         for (label faceI = 0; faceI < nInternalFaces(); faceI++)
00135         {
00136             if (seedFace[faceI])
00137             {
00138                 label own = faceOwner()[faceI];
00139                 if (unrefineableCell.get(own) == 0)
00140                 {
00141                     unrefineableCell.set(own, 1);
00142                     hasExtended = true;
00143                 }
00144 
00145                 label nei = faceNeighbour()[faceI];
00146                 if (unrefineableCell.get(nei) == 0)
00147                 {
00148                     unrefineableCell.set(nei, 1);
00149                     hasExtended = true;
00150                 }
00151             }
00152         }
00153         for (label faceI = nInternalFaces(); faceI < nFaces(); faceI++)
00154         {
00155             if (seedFace[faceI])
00156             {
00157                 label own = faceOwner()[faceI];
00158                 if (unrefineableCell.get(own) == 0)
00159                 {
00160                     unrefineableCell.set(own, 1);
00161                     hasExtended = true;
00162                 }
00163             }
00164         }
00165 
00166         if (!returnReduce(hasExtended, orOp<bool>()))
00167         {
00168             break;
00169         }
00170     }
00171 }
00172 
00173 
00174 void dynamicRefineFvMesh::readDict()
00175 {
00176     dictionary refineDict
00177     (
00178         IOdictionary
00179         (
00180             IOobject
00181             (
00182                 "dynamicMeshDict",
00183                 time().constant(),
00184                 *this,
00185                 IOobject::MUST_READ,
00186                 IOobject::NO_WRITE,
00187                 false
00188             )
00189         ).subDict(typeName + "Coeffs")
00190     );
00191 
00192     correctFluxes_ = List<Pair<word> >(refineDict.lookup("correctFluxes"));
00193 
00194     dumpLevel_ = Switch(refineDict.lookup("dumpLevel"));
00195 }
00196 
00197 
00198 // Refines cells, maps fields and recalculates (an approximate) flux
00199 autoPtr<mapPolyMesh> dynamicRefineFvMesh::refine
00200 (
00201     const labelList& cellsToRefine
00202 )
00203 {
00204     // Mesh changing engine.
00205     polyTopoChange meshMod(*this);
00206 
00207     // Play refinement commands into mesh changer.
00208     meshCutter_.setRefinement(cellsToRefine, meshMod);
00209 
00210     // Create mesh (with inflation), return map from old to new mesh.
00211     //autoPtr<mapPolyMesh> map = meshMod.changeMesh(*this, true);
00212     autoPtr<mapPolyMesh> map = meshMod.changeMesh(*this, false);
00213 
00214     Info<< "Refined from "
00215         << returnReduce(map().nOldCells(), sumOp<label>())
00216         << " to " << globalData().nTotalCells() << " cells." << endl;
00217 
00218     if (debug)
00219     {
00220         // Check map.
00221         for (label faceI = 0; faceI < nInternalFaces(); faceI++)
00222         {
00223             label oldFaceI = map().faceMap()[faceI];
00224 
00225             if (oldFaceI >= nInternalFaces())
00226             {
00227                 FatalErrorIn("dynamicRefineFvMesh::refine(const labelList&)")
00228                     << "New internal face:" << faceI
00229                     << " fc:" << faceCentres()[faceI]
00230                     << " originates from boundary oldFace:" << oldFaceI
00231                     << abort(FatalError);
00232             }
00233         }
00234     }
00235 
00236 
00237     // Update fields
00238     updateMesh(map);
00239 
00240     // Move mesh
00241     /*
00242     pointField newPoints;
00243     if (map().hasMotionPoints())
00244     {
00245         newPoints = map().preMotionPoints();
00246     }
00247     else
00248     {
00249         newPoints = points();
00250     }
00251     movePoints(newPoints);
00252     */
00253 
00254     // Correct the flux for modified/added faces. All the faces which only
00255     // have been renumbered will already have been handled by the mapping.
00256     {
00257         const labelList& faceMap = map().faceMap();
00258         const labelList& reverseFaceMap = map().reverseFaceMap();
00259 
00260         // Storage for any master faces. These will be the original faces
00261         // on the coarse cell that get split into four (or rather the
00262         // master face gets modified and three faces get added from the master)
00263         labelHashSet masterFaces(4*cellsToRefine.size());
00264 
00265         forAll(faceMap, faceI)
00266         {
00267             label oldFaceI = faceMap[faceI];
00268 
00269             if (oldFaceI >= 0)
00270             {
00271                 label masterFaceI = reverseFaceMap[oldFaceI];
00272 
00273                 if (masterFaceI < 0)
00274                 {
00275                     FatalErrorIn
00276                     (
00277                         "dynamicRefineFvMesh::refine(const labelList&)"
00278                     )   << "Problem: should not have removed faces"
00279                         << " when refining."
00280                         << nl << "face:" << faceI << abort(FatalError);
00281                 }
00282                 else if (masterFaceI != faceI)
00283                 {
00284                     masterFaces.insert(masterFaceI);
00285                 }
00286             }
00287         }
00288         if (debug)
00289         {
00290             Info<< "Found " << returnReduce(masterFaces.size(), sumOp<label>())
00291                 << " split faces " << endl;
00292         }
00293 
00294         forAll(correctFluxes_, i)
00295         {
00296             if (debug)
00297             {
00298                 Info<< "Mapping flux " << correctFluxes_[i][0]
00299                     << " using interpolated flux " << correctFluxes_[i][1]
00300                     << endl;
00301             }
00302             surfaceScalarField& phi = const_cast<surfaceScalarField&>
00303             (
00304                 lookupObject<surfaceScalarField>(correctFluxes_[i][0])
00305             );
00306             surfaceScalarField phiU =
00307                 fvc::interpolate
00308                 (
00309                     lookupObject<volVectorField>(correctFluxes_[i][1])
00310                 )
00311               & Sf();
00312 
00313             // Recalculate new internal faces.
00314             for (label faceI = 0; faceI < nInternalFaces(); faceI++)
00315             {
00316                 label oldFaceI = faceMap[faceI];
00317 
00318                 if (oldFaceI == -1)
00319                 {
00320                     // Inflated/appended
00321                     phi[faceI] = phiU[faceI];
00322                 }
00323                 else if (reverseFaceMap[oldFaceI] != faceI)
00324                 {
00325                     // face-from-masterface
00326                     phi[faceI] = phiU[faceI];
00327                 }
00328             }
00329 
00330             // Recalculate new boundary faces.
00331             forAll(phi.boundaryField(), patchI)
00332             {
00333                 fvsPatchScalarField& patchPhi = phi.boundaryField()[patchI];
00334                 const fvsPatchScalarField& patchPhiU =
00335                     phiU.boundaryField()[patchI];
00336 
00337                 label faceI = patchPhi.patch().patch().start();
00338 
00339                 forAll(patchPhi, i)
00340                 {
00341                     label oldFaceI = faceMap[faceI];
00342 
00343                     if (oldFaceI == -1)
00344                     {
00345                         // Inflated/appended
00346                         patchPhi[i] = patchPhiU[i];
00347                     }
00348                     else if (reverseFaceMap[oldFaceI] != faceI)
00349                     {
00350                         // face-from-masterface
00351                         patchPhi[i] = patchPhiU[i];
00352                     }
00353 
00354                     faceI++;
00355                 }
00356             }
00357 
00358             // Update master faces
00359             forAllConstIter(labelHashSet, masterFaces, iter)
00360             {
00361                 label faceI = iter.key();
00362 
00363                 if (isInternalFace(faceI))
00364                 {
00365                     phi[faceI] = phiU[faceI];
00366                 }
00367                 else
00368                 {
00369                     label patchI = boundaryMesh().whichPatch(faceI);
00370                     label i = faceI - boundaryMesh()[patchI].start();
00371 
00372                     const fvsPatchScalarField& patchPhiU =
00373                         phiU.boundaryField()[patchI];
00374 
00375                     fvsPatchScalarField& patchPhi =
00376                         phi.boundaryField()[patchI];
00377 
00378                     patchPhi[i] = patchPhiU[i];
00379                 }
00380             }
00381         }
00382     }
00383 
00384 
00385 
00386     // Update numbering of cells/vertices.
00387     meshCutter_.updateMesh(map);
00388 
00389     // Update numbering of protectedCell_
00390     if (protectedCell_.size())
00391     {
00392         PackedBoolList newProtectedCell(nCells());
00393 
00394         forAll(newProtectedCell, cellI)
00395         {
00396             label oldCellI = map().cellMap()[cellI];
00397             newProtectedCell.set(cellI, protectedCell_.get(oldCellI));
00398         }
00399         protectedCell_.transfer(newProtectedCell);
00400     }
00401 
00402     // Debug: Check refinement levels (across faces only)
00403     meshCutter_.checkRefinementLevels(-1, labelList(0));
00404 
00405     return map;
00406 }
00407 
00408 
00409 // Combines previously split cells, maps fields and recalculates
00410 // (an approximate) flux
00411 autoPtr<mapPolyMesh> dynamicRefineFvMesh::unrefine
00412 (
00413     const labelList& splitPoints
00414 )
00415 {
00416     polyTopoChange meshMod(*this);
00417 
00418     // Play refinement commands into mesh changer.
00419     meshCutter_.setUnrefinement(splitPoints, meshMod);
00420 
00421 
00422     // Save information on faces that will be combined
00423     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00424 
00425     // Find the faceMidPoints on cells to be combined.
00426     // for each face resulting of split of face into four store the
00427     // midpoint
00428     Map<label> faceToSplitPoint(3*splitPoints.size());
00429 
00430     {
00431         forAll(splitPoints, i)
00432         {
00433             label pointI = splitPoints[i];
00434 
00435             const labelList& pEdges = pointEdges()[pointI];
00436 
00437             forAll(pEdges, j)
00438             {
00439                 label otherPointI = edges()[pEdges[j]].otherVertex(pointI);
00440 
00441                 const labelList& pFaces = pointFaces()[otherPointI];
00442 
00443                 forAll(pFaces, pFaceI)
00444                 {
00445                     faceToSplitPoint.insert(pFaces[pFaceI], otherPointI);
00446                 }
00447             }
00448         }
00449     }
00450 
00451 
00452     // Change mesh and generate map.
00453     //autoPtr<mapPolyMesh> map = meshMod.changeMesh(*this, true);
00454     autoPtr<mapPolyMesh> map = meshMod.changeMesh(*this, false);
00455 
00456     Info<< "Unrefined from "
00457         << returnReduce(map().nOldCells(), sumOp<label>())
00458         << " to " << globalData().nTotalCells() << " cells."
00459         << endl;
00460 
00461     // Update fields
00462     updateMesh(map);
00463 
00464 
00465     // Move mesh
00466     /*
00467     pointField newPoints;
00468     if (map().hasMotionPoints())
00469     {
00470         newPoints = map().preMotionPoints();
00471     }
00472     else
00473     {
00474         newPoints = points();
00475     }
00476     movePoints(newPoints);
00477     */
00478 
00479     // Correct the flux for modified faces.
00480     {
00481         const labelList& reversePointMap = map().reversePointMap();
00482         const labelList& reverseFaceMap = map().reverseFaceMap();
00483 
00484         forAll(correctFluxes_, i)
00485         {
00486             if (debug)
00487             {
00488                 Info<< "Mapping flux " << correctFluxes_[i][0]
00489                     << " using interpolated flux " << correctFluxes_[i][1]
00490                     << endl;
00491             }
00492             surfaceScalarField& phi = const_cast<surfaceScalarField&>
00493             (
00494                 lookupObject<surfaceScalarField>(correctFluxes_[i][0])
00495             );
00496             surfaceScalarField phiU =
00497                 fvc::interpolate
00498                 (
00499                     lookupObject<volVectorField>(correctFluxes_[i][1])
00500                 )
00501               & Sf();
00502 
00503             forAllConstIter(Map<label>, faceToSplitPoint, iter)
00504             {
00505                 label oldFaceI = iter.key();
00506                 label oldPointI = iter();
00507 
00508                 if (reversePointMap[oldPointI] < 0)
00509                 {
00510                     // midpoint was removed. See if face still exists.
00511                     label faceI = reverseFaceMap[oldFaceI];
00512 
00513                     if (faceI >= 0)
00514                     {
00515                         if (isInternalFace(faceI))
00516                         {
00517                             phi[faceI] = phiU[faceI];
00518                         }
00519                         else
00520                         {
00521                             label patchI = boundaryMesh().whichPatch(faceI);
00522                             label i = faceI - boundaryMesh()[patchI].start();
00523 
00524                             const fvsPatchScalarField& patchPhiU =
00525                                 phiU.boundaryField()[patchI];
00526 
00527                             fvsPatchScalarField& patchPhi =
00528                                 phi.boundaryField()[patchI];
00529 
00530                             patchPhi[i] = patchPhiU[i];
00531                         }
00532                     }
00533                 }
00534             }
00535         }
00536     }
00537 
00538 
00539     // Update numbering of cells/vertices.
00540     meshCutter_.updateMesh(map);
00541 
00542     // Update numbering of protectedCell_
00543     if (protectedCell_.size())
00544     {
00545         PackedBoolList newProtectedCell(nCells());
00546 
00547         forAll(newProtectedCell, cellI)
00548         {
00549             label oldCellI = map().cellMap()[cellI];
00550             if (oldCellI >= 0)
00551             {
00552                 newProtectedCell.set(cellI, protectedCell_.get(oldCellI));
00553             }
00554         }
00555         protectedCell_.transfer(newProtectedCell);
00556     }
00557 
00558     // Debug: Check refinement levels (across faces only)
00559     meshCutter_.checkRefinementLevels(-1, labelList(0));
00560 
00561     return map;
00562 }
00563 
00564 
00565 // Get max of connected point
00566 scalarField dynamicRefineFvMesh::maxPointField(const scalarField& pFld) const
00567 {
00568     scalarField vFld(nCells(), -GREAT);
00569 
00570     forAll(pointCells(), pointI)
00571     {
00572         const labelList& pCells = pointCells()[pointI];
00573 
00574         forAll(pCells, i)
00575         {
00576             vFld[pCells[i]] = max(vFld[pCells[i]], pFld[pointI]);
00577         }
00578     }
00579     return vFld;
00580 }
00581 
00582 
00583 // Get min of connected cell
00584 scalarField dynamicRefineFvMesh::minCellField(const volScalarField& vFld) const
00585 {
00586     scalarField pFld(nPoints(), GREAT);
00587 
00588     forAll(pointCells(), pointI)
00589     {
00590         const labelList& pCells = pointCells()[pointI];
00591 
00592         forAll(pCells, i)
00593         {
00594             pFld[pointI] = min(pFld[pointI], vFld[pCells[i]]);
00595         }
00596     }
00597     return pFld;
00598 }
00599 
00600 
00601 // Simple (non-parallel) interpolation by averaging.
00602 scalarField dynamicRefineFvMesh::cellToPoint(const scalarField& vFld) const
00603 {
00604     scalarField pFld(nPoints());
00605 
00606     forAll(pointCells(), pointI)
00607     {
00608         const labelList& pCells = pointCells()[pointI];
00609 
00610         scalar sum = 0.0;
00611         forAll(pCells, i)
00612         {
00613             sum += vFld[pCells[i]];
00614         }
00615         pFld[pointI] = sum/pCells.size();
00616     }
00617     return pFld;
00618 }
00619 
00620 
00621 // Calculate error. Is < 0 or distance from inbetween levels
00622 scalarField dynamicRefineFvMesh::error
00623 (
00624     const scalarField& fld,
00625     const scalar minLevel,
00626     const scalar maxLevel
00627 ) const
00628 {
00629     const scalar halfLevel = 0.5*(minLevel + maxLevel);
00630 
00631     scalarField c(fld.size(), -1);
00632 
00633     forAll(fld, i)
00634     {
00635         if (fld[i] >= minLevel && fld[i] < maxLevel)
00636         {
00637             c[i] = mag(fld[i] - halfLevel);
00638         }
00639     }
00640     return c;
00641 }
00642 
00643 
00644 void dynamicRefineFvMesh::selectRefineCandidates
00645 (
00646     const scalar lowerRefineLevel,
00647     const scalar upperRefineLevel,
00648     const scalarField& vFld,
00649     PackedBoolList& candidateCell
00650 ) const
00651 {
00652     // Get error per cell. Is -1 (not to be refined) to >0 (to be refined,
00653     // higher more desirable to be refined).
00654     scalarField cellError
00655     (
00656         maxPointField
00657         (
00658             error
00659             (
00660                 cellToPoint(vFld),
00661                 lowerRefineLevel,
00662                 upperRefineLevel
00663             )
00664         )
00665     );
00666 
00667     // Mark cells that are candidates for refinement.
00668     forAll(cellError, cellI)
00669     {
00670         if (cellError[cellI] > 0)
00671         {
00672             candidateCell.set(cellI, 1);
00673         }
00674     }
00675 }
00676 
00677 
00678 labelList dynamicRefineFvMesh::selectRefineCells
00679 (
00680     const label maxCells,
00681     const label maxRefinement,
00682     const PackedBoolList& candidateCell
00683 ) const
00684 {
00685     // Every refined cell causes 7 extra cells
00686     label nTotToRefine = (maxCells - globalData().nTotalCells()) / 7;
00687 
00688     const labelList& cellLevel = meshCutter_.cellLevel();
00689 
00690     // Mark cells that cannot be refined since they would trigger refinement
00691     // of protected cells (since 2:1 cascade)
00692     PackedBoolList unrefineableCell;
00693     calculateProtectedCells(unrefineableCell);
00694 
00695     // Count current selection
00696     label nCandidates = returnReduce(count(candidateCell, 1), sumOp<label>());
00697 
00698     // Collect all cells
00699     DynamicList<label> candidates(nCells());
00700 
00701     if (nCandidates < nTotToRefine)
00702     {
00703         forAll(candidateCell, cellI)
00704         {
00705             if
00706             (
00707                 cellLevel[cellI] < maxRefinement
00708              && candidateCell.get(cellI) == 1
00709              && (
00710                     unrefineableCell.empty()
00711                  || unrefineableCell.get(cellI) == 0
00712                 )
00713             )
00714             {
00715                 candidates.append(cellI);
00716             }
00717         }
00718     }
00719     else
00720     {
00721         // Sort by error? For now just truncate.
00722         for (label level = 0; level < maxRefinement; level++)
00723         {
00724             forAll(candidateCell, cellI)
00725             {
00726                 if
00727                 (
00728                     cellLevel[cellI] == level
00729                  && candidateCell.get(cellI) == 1
00730                  && (
00731                         unrefineableCell.empty()
00732                      || unrefineableCell.get(cellI) == 0
00733                     )
00734                 )
00735                 {
00736                     candidates.append(cellI);
00737                 }
00738             }
00739 
00740             if (returnReduce(candidates.size(), sumOp<label>()) > nTotToRefine)
00741             {
00742                 break;
00743             }
00744         }
00745     }
00746 
00747     // Guarantee 2:1 refinement after refinement
00748     labelList consistentSet
00749     (
00750         meshCutter_.consistentRefinement
00751         (
00752             candidates.shrink(),
00753             true               // Add to set to guarantee 2:1
00754         )
00755     );
00756 
00757     Info<< "Selected " << returnReduce(consistentSet.size(), sumOp<label>())
00758         << " cells for refinement out of " << globalData().nTotalCells()
00759         << "." << endl;
00760 
00761     return consistentSet;
00762 }
00763 
00764 
00765 labelList dynamicRefineFvMesh::selectUnrefinePoints
00766 (
00767     const scalar unrefineLevel,
00768     const PackedBoolList& markedCell,
00769     const scalarField& pFld
00770 ) const
00771 {
00772     // All points that can be unrefined
00773     const labelList splitPoints(meshCutter_.getSplitPoints());
00774 
00775     DynamicList<label> newSplitPoints(splitPoints.size());
00776 
00777     forAll(splitPoints, i)
00778     {
00779         label pointI = splitPoints[i];
00780 
00781         if (pFld[pointI] < unrefineLevel)
00782         {
00783             // Check that all cells are not marked
00784             const labelList& pCells = pointCells()[pointI];
00785 
00786             bool hasMarked = false;
00787 
00788             forAll(pCells, pCellI)
00789             {
00790                 if (markedCell.get(pCells[pCellI]) == 1)
00791                 {
00792                     hasMarked = true;
00793                     break;
00794                 }
00795             }
00796 
00797             if (!hasMarked)
00798             {
00799                 newSplitPoints.append(pointI);
00800             }
00801         }
00802     }
00803 
00804 
00805     newSplitPoints.shrink();
00806 
00807     // Guarantee 2:1 refinement after unrefinement
00808     labelList consistentSet
00809     (
00810         meshCutter_.consistentUnrefinement
00811         (
00812             newSplitPoints,
00813             false
00814         )
00815     );
00816     Info<< "Selected " << returnReduce(consistentSet.size(), sumOp<label>())
00817         << " split points out of a possible "
00818         << returnReduce(splitPoints.size(), sumOp<label>())
00819         << "." << endl;
00820 
00821     return consistentSet;
00822 }
00823 
00824 
00825 void dynamicRefineFvMesh::extendMarkedCells(PackedBoolList& markedCell) const
00826 {
00827     // Mark faces using any marked cell
00828     boolList markedFace(nFaces(), false);
00829 
00830     forAll(markedCell, cellI)
00831     {
00832         if (markedCell.get(cellI) == 1)
00833         {
00834             const cell& cFaces = cells()[cellI];
00835 
00836             forAll(cFaces, i)
00837             {
00838                 markedFace[cFaces[i]] = true;
00839             }
00840         }
00841     }
00842 
00843     syncTools::syncFaceList(*this, markedFace, orEqOp<bool>(), false);
00844 
00845     // Update cells using any markedFace
00846     for (label faceI = 0; faceI < nInternalFaces(); faceI++)
00847     {
00848         if (markedFace[faceI])
00849         {
00850             markedCell.set(faceOwner()[faceI], 1);
00851             markedCell.set(faceNeighbour()[faceI], 1);
00852         }
00853     }
00854     for (label faceI = nInternalFaces(); faceI < nFaces(); faceI++)
00855     {
00856         if (markedFace[faceI])
00857         {
00858             markedCell.set(faceOwner()[faceI], 1);
00859         }
00860     }
00861 }
00862 
00863 
00864 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00865 
00866 dynamicRefineFvMesh::dynamicRefineFvMesh(const IOobject& io)
00867 :
00868     dynamicFvMesh(io),
00869     meshCutter_(*this),
00870     dumpLevel_(false),
00871     nRefinementIterations_(0),
00872     protectedCell_(nCells(), 0)
00873 {
00874     const labelList& cellLevel = meshCutter_.cellLevel();
00875     const labelList& pointLevel = meshCutter_.pointLevel();
00876 
00877     // Set cells that should not be refined.
00878     // This is currently any cell which does not have 8 anchor points or
00879     // uses any face which does not have 4 anchor points.
00880     // Note: do not use cellPoint addressing
00881 
00882     // Count number of points <= cellLevel
00883     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00884 
00885     labelList nAnchors(nCells(), 0);
00886 
00887     label nProtected = 0;
00888 
00889     forAll(pointCells(), pointI)
00890     {
00891         const labelList& pCells = pointCells()[pointI];
00892 
00893         forAll(pCells, i)
00894         {
00895             label cellI = pCells[i];
00896 
00897             if (protectedCell_.get(cellI) == 0)
00898             {
00899                 if (pointLevel[pointI] <= cellLevel[cellI])
00900                 {
00901                     nAnchors[cellI]++;
00902 
00903                     if (nAnchors[cellI] > 8)
00904                     {
00905                         protectedCell_.set(cellI, 1);
00906                         nProtected++;
00907                     }
00908                 }
00909             }
00910         }
00911     }
00912 
00913 
00914     // Count number of points <= faceLevel
00915     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00916     // Bit tricky since proc face might be one more refined than the owner since
00917     // the coupled one is refined.
00918 
00919     {
00920         labelList neiLevel(nFaces());
00921 
00922         for (label faceI = 0; faceI < nInternalFaces(); faceI++)
00923         {
00924             neiLevel[faceI] = cellLevel[faceNeighbour()[faceI]];
00925         }
00926         for (label faceI = nInternalFaces(); faceI < nFaces(); faceI++)
00927         {
00928             neiLevel[faceI] = cellLevel[faceOwner()[faceI]];
00929         }
00930         syncTools::swapFaceList(*this, neiLevel, false);
00931 
00932 
00933         boolList protectedFace(nFaces(), false);
00934 
00935         forAll(faceOwner(), faceI)
00936         {
00937             label faceLevel = max
00938             (
00939                 cellLevel[faceOwner()[faceI]],
00940                 neiLevel[faceI]
00941             );
00942 
00943             const face& f = faces()[faceI];
00944 
00945             label nAnchors = 0;
00946 
00947             forAll(f, fp)
00948             {
00949                 if (pointLevel[f[fp]] <= faceLevel)
00950                 {
00951                     nAnchors++;
00952 
00953                     if (nAnchors > 4)
00954                     {
00955                         protectedFace[faceI] = true;
00956                         break;
00957                     }
00958                 }
00959             }
00960         }
00961 
00962         syncTools::syncFaceList
00963         (
00964             *this,
00965             protectedFace,
00966             orEqOp<bool>(),
00967             false
00968         );
00969 
00970         for (label faceI = 0; faceI < nInternalFaces(); faceI++)
00971         {
00972             if (protectedFace[faceI])
00973             {
00974                 protectedCell_.set(faceOwner()[faceI], 1);
00975                 nProtected++;
00976                 protectedCell_.set(faceNeighbour()[faceI], 1);
00977                 nProtected++;
00978             }
00979         }
00980         for (label faceI = nInternalFaces(); faceI < nFaces(); faceI++)
00981         {
00982             if (protectedFace[faceI])
00983             {
00984                 protectedCell_.set(faceOwner()[faceI], 1);
00985                 nProtected++;
00986             }
00987         }
00988     }
00989 
00990     if (returnReduce(nProtected, sumOp<label>()) == 0)
00991     {
00992         protectedCell_.clear();
00993     }
00994 }
00995 
00996 
00997 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00998 
00999 dynamicRefineFvMesh::~dynamicRefineFvMesh()
01000 {}
01001 
01002 
01003 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
01004 
01005 bool dynamicRefineFvMesh::update()
01006 {
01007     // Re-read dictionary. Choosen since usually -small so trivial amount
01008     // of time compared to actual refinement. Also very useful to be able
01009     // to modify on-the-fly.
01010     dictionary refineDict
01011     (
01012         IOdictionary
01013         (
01014             IOobject
01015             (
01016                 "dynamicMeshDict",
01017                 time().constant(),
01018                 *this,
01019                 IOobject::MUST_READ,
01020                 IOobject::NO_WRITE,
01021                 false
01022             )
01023         ).subDict(typeName + "Coeffs")
01024     );
01025 
01026     label refineInterval = readLabel(refineDict.lookup("refineInterval"));
01027 
01028     bool hasChanged = false;
01029 
01030     if (refineInterval == 0)
01031     {
01032         changing(hasChanged);
01033 
01034         return false;
01035     }
01036     else if (refineInterval < 0)
01037     {
01038         FatalErrorIn("dynamicRefineFvMesh::update()")
01039             << "Illegal refineInterval " << refineInterval << nl
01040             << "The refineInterval setting in the dynamicMeshDict should"
01041             << " be >= 1." << nl
01042             << exit(FatalError);
01043     }
01044 
01045 
01046 
01047 
01048     // Note: cannot refine at time 0 since no V0 present since mesh not
01049     //       moved yet.
01050 
01051     if (time().timeIndex() > 0 && time().timeIndex() % refineInterval == 0)
01052     {
01053         label maxCells = readLabel(refineDict.lookup("maxCells"));
01054 
01055         if (maxCells <= 0)
01056         {
01057             FatalErrorIn("dynamicRefineFvMesh::update()")
01058                 << "Illegal maximum number of cells " << maxCells << nl
01059                 << "The maxCells setting in the dynamicMeshDict should"
01060                 << " be > 0." << nl
01061                 << exit(FatalError);
01062         }
01063 
01064         label maxRefinement = readLabel(refineDict.lookup("maxRefinement"));
01065 
01066         if (maxRefinement <= 0)
01067         {
01068             FatalErrorIn("dynamicRefineFvMesh::update()")
01069                 << "Illegal maximum refinement level " << maxRefinement << nl
01070                 << "The maxCells setting in the dynamicMeshDict should"
01071                 << " be > 0." << nl
01072                 << exit(FatalError);
01073         }
01074 
01075         word field(refineDict.lookup("field"));
01076 
01077         const volScalarField& vFld = lookupObject<volScalarField>(field);
01078 
01079         const scalar lowerRefineLevel =
01080             readScalar(refineDict.lookup("lowerRefineLevel"));
01081         const scalar upperRefineLevel =
01082             readScalar(refineDict.lookup("upperRefineLevel"));
01083         const scalar unrefineLevel =
01084             readScalar(refineDict.lookup("unrefineLevel"));
01085         const label nBufferLayers =
01086             readLabel(refineDict.lookup("nBufferLayers"));
01087 
01088         // Cells marked for refinement or otherwise protected from unrefinement.
01089         PackedBoolList refineCell(nCells());
01090 
01091         if (globalData().nTotalCells() < maxCells)
01092         {
01093             // Determine candidates for refinement (looking at field only)
01094             selectRefineCandidates
01095             (
01096                 lowerRefineLevel,
01097                 upperRefineLevel,
01098                 vFld,
01099                 refineCell
01100             );
01101 
01102             // Select subset of candidates. Take into account max allowable
01103             // cells, refinement level, protected cells.
01104             labelList cellsToRefine
01105             (
01106                 selectRefineCells
01107                 (
01108                     maxCells,
01109                     maxRefinement,
01110                     refineCell
01111                 )
01112             );
01113 
01114             label nCellsToRefine = returnReduce
01115             (
01116                 cellsToRefine.size(), sumOp<label>()
01117             );
01118 
01119             if (nCellsToRefine > 0)
01120             {
01121                 // Refine/update mesh and map fields
01122                 autoPtr<mapPolyMesh> map = refine(cellsToRefine);
01123 
01124                 // Update refineCell. Note that some of the marked ones have
01125                 // not been refined due to constraints.
01126                 {
01127                     const labelList& cellMap = map().cellMap();
01128                     const labelList& reverseCellMap = map().reverseCellMap();
01129 
01130                     PackedBoolList newRefineCell(cellMap.size());
01131 
01132                     forAll(cellMap, cellI)
01133                     {
01134                         label oldCellI = cellMap[cellI];
01135 
01136                         if (oldCellI < 0)
01137                         {
01138                             newRefineCell.set(cellI, 1);
01139                         }
01140                         else if (reverseCellMap[oldCellI] != cellI)
01141                         {
01142                             newRefineCell.set(cellI, 1);
01143                         }
01144                         else
01145                         {
01146                             newRefineCell.set(cellI, refineCell.get(oldCellI));
01147                         }
01148                     }
01149                     refineCell.transfer(newRefineCell);
01150                 }
01151 
01152                 // Extend with a buffer layer to prevent neighbouring points
01153                 // being unrefined.
01154                 for (label i = 0; i < nBufferLayers; i++)
01155                 {
01156                     extendMarkedCells(refineCell);
01157                 }
01158 
01159                 hasChanged = true;
01160             }
01161         }
01162 
01163 
01164         {
01165             // Select unrefineable points that are not marked in refineCell
01166             labelList pointsToUnrefine
01167             (
01168                 selectUnrefinePoints
01169                 (
01170                     unrefineLevel,
01171                     refineCell,
01172                     minCellField(vFld)
01173                 )
01174             );
01175 
01176             label nSplitPoints = returnReduce
01177             (
01178                 pointsToUnrefine.size(),
01179                 sumOp<label>()
01180             );
01181 
01182             if (nSplitPoints > 0)
01183             {
01184                 // Refine/update mesh
01185                 unrefine(pointsToUnrefine);
01186 
01187                 hasChanged = true;
01188             }
01189         }
01190 
01191 
01192         if ((nRefinementIterations_ % 10) == 0)
01193         {
01194             // Compact refinement history occassionally (how often?).
01195             // Unrefinement causes holes in the refinementHistory.
01196             const_cast<refinementHistory&>(meshCutter().history()).compact();
01197         }
01198         nRefinementIterations_++;
01199     }
01200 
01201     changing(hasChanged);
01202 
01203     return hasChanged;
01204 }
01205 
01206 
01207 bool dynamicRefineFvMesh::writeObject
01208 (
01209     IOstream::streamFormat fmt,
01210     IOstream::versionNumber ver,
01211     IOstream::compressionType cmp
01212 ) const
01213 {
01214     // Force refinement data to go to the current time directory.
01215     const_cast<hexRef8&>(meshCutter_).setInstance(time().timeName());
01216 
01217     bool writeOk =
01218         dynamicFvMesh::writeObjects(fmt, ver, cmp)
01219      && meshCutter_.write();
01220 
01221     if (dumpLevel_)
01222     {
01223         volScalarField scalarCellLevel
01224         (
01225             IOobject
01226             (
01227                 "cellLevel",
01228                 time().timeName(),
01229                 *this,
01230                 IOobject::NO_READ,
01231                 IOobject::AUTO_WRITE,
01232                 false
01233             ),
01234             *this,
01235             dimensionedScalar("level", dimless, 0)
01236         );
01237 
01238         const labelList& cellLevel = meshCutter_.cellLevel();
01239 
01240         forAll(cellLevel, cellI)
01241         {
01242             scalarCellLevel[cellI] = cellLevel[cellI];
01243         }
01244 
01245         writeOk = writeOk && scalarCellLevel.write();
01246     }
01247 
01248     return writeOk;
01249 }
01250 
01251 
01252 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
01253 
01254 } // End namespace Foam
01255 
01256 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd