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

refinementLevel.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     Tries to figure out what the refinement level is on refined cartesian
00027     meshes. Run BEFORE snapping.
00028 
00029     Writes
00030     - volScalarField 'refinementLevel' with current refinement level.
00031     - cellSet 'refCells' which are the cells that need to be refined to satisfy
00032       2:1 refinement.
00033 
00034     Works by dividing cells into volume bins.
00035 
00036 \*---------------------------------------------------------------------------*/
00037 
00038 #include "argList.H"
00039 #include "Time.H"
00040 #include "polyMesh.H"
00041 #include "cellSet.H"
00042 #include "SortableList.H"
00043 #include "labelIOList.H"
00044 #include "fvMesh.H"
00045 #include "volFields.H"
00046 
00047 using namespace Foam;
00048 
00049 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00050 
00051 // Return true if any cells had to be split to keep a difference between
00052 // neighbouring refinement levels < limitDiff. Puts cells into refCells and
00053 // update refLevel to account for refinement.
00054 bool limitRefinementLevel
00055 (
00056     const primitiveMesh& mesh,
00057     labelList& refLevel,
00058     cellSet& refCells
00059 )
00060 {
00061     const labelListList& cellCells = mesh.cellCells();
00062 
00063     label oldNCells = refCells.size();
00064 
00065     forAll(cellCells, cellI)
00066     {
00067         const labelList& cCells = cellCells[cellI];
00068 
00069         forAll(cCells, i)
00070         {
00071             if (refLevel[cCells[i]] > (refLevel[cellI]+1))
00072             {
00073                 // Found neighbour with >=2 difference in refLevel.
00074                 refCells.insert(cellI);
00075                 refLevel[cellI]++;
00076                 break;
00077             }
00078         }
00079     }
00080 
00081     if (refCells.size() > oldNCells)
00082     {
00083         Info<< "Added an additional " << refCells.size() - oldNCells
00084             << " cells to satisfy 1:2 refinement level"
00085             << endl;
00086 
00087         return true;
00088     }
00089     else
00090     {
00091         return false;
00092     }
00093 }
00094 
00095 
00096 // Main program:
00097 
00098 int main(int argc, char *argv[])
00099 {
00100     argList::validOptions.insert("readLevel", "");
00101 
00102 #   include "setRootCase.H"
00103 #   include "createTime.H"
00104 #   include "createPolyMesh.H"
00105 
00106     Info<< "Dividing cells into bins depending on cell volume.\nThis will"
00107         << " correspond to refinement levels for a mesh with only 2x2x2"
00108         << " refinement\n"
00109         << "The upper range for every bin is always 1.1 times the lower range"
00110         << " to allow for some truncation error."
00111         << nl << endl;
00112 
00113     bool readLevel = args.optionFound("readLevel");
00114 
00115     const scalarField& vols = mesh.cellVolumes();
00116 
00117     SortableList<scalar> sortedVols(vols);
00118 
00119     // All cell labels, sorted per bin.
00120     DynamicList<DynamicList<label> > bins;
00121 
00122     // Lower/upper limits
00123     DynamicList<scalar> lowerLimits;
00124     DynamicList<scalar> upperLimits;
00125 
00126     // Create bin0. Have upperlimit as factor times lowerlimit.
00127     bins.append(DynamicList<label>());
00128     lowerLimits.append(sortedVols[0]);
00129     upperLimits.append(1.1*lowerLimits[lowerLimits.size()-1]);
00130 
00131     forAll(sortedVols, i)
00132     {
00133         if (sortedVols[i] > upperLimits[upperLimits.size()-1])
00134         {
00135             // New value outside of current bin
00136 
00137             // Shrink old bin.
00138             DynamicList<label>& bin = bins[bins.size()-1];
00139 
00140             bin.shrink();
00141 
00142             Info<< "Collected " << bin.size() << " elements in bin "
00143                 << lowerLimits[lowerLimits.size()-1] << " .. "
00144                 << upperLimits[upperLimits.size()-1] << endl;
00145 
00146             // Create new bin.
00147             bins.append(DynamicList<label>());
00148             lowerLimits.append(sortedVols[i]);
00149             upperLimits.append(1.1*lowerLimits[lowerLimits.size()-1]);
00150 
00151             Info<< "Creating new bin " << lowerLimits[lowerLimits.size()-1]
00152                 << " .. " << upperLimits[upperLimits.size()-1]
00153                 << endl;
00154         }
00155 
00156         // Append to current bin.
00157         DynamicList<label>& bin = bins[bins.size()-1];
00158 
00159         bin.append(sortedVols.indices()[i]);
00160     }
00161     Info<< endl;
00162 
00163     bins[bins.size()-1].shrink();
00164     bins.shrink();
00165     lowerLimits.shrink();
00166     upperLimits.shrink();
00167 
00168 
00169     //
00170     // Write to cellSets.
00171     //
00172 
00173     Info<< "Volume bins:" << nl;
00174     forAll(bins, binI)
00175     {
00176         const DynamicList<label>& bin = bins[binI];
00177 
00178         cellSet cells(mesh, "vol" + name(binI), bin.size());
00179 
00180         forAll(bin, i)
00181         {
00182             cells.insert(bin[i]);
00183         }
00184 
00185         Info<< "    " << lowerLimits[binI] << " .. " << upperLimits[binI]
00186             << "  : writing " << bin.size() << " cells to cellSet "
00187             << cells.name() << endl;
00188 
00189         cells.write();
00190     }
00191 
00192 
00193 
00194     //
00195     // Convert bins into refinement level.
00196     //
00197 
00198 
00199     // Construct fvMesh to be able to construct volScalarField
00200 
00201     fvMesh fMesh
00202     (
00203         IOobject
00204         (
00205             fvMesh::defaultRegion,
00206             runTime.timeName(),
00207             runTime
00208         ),
00209         xferCopy(mesh.points()),   // could we safely re-use the data?
00210         xferCopy(mesh.faces()),
00211         xferCopy(mesh.cells())
00212     );
00213 
00214     // Add the boundary patches
00215     const polyBoundaryMesh& patches = mesh.boundaryMesh();
00216 
00217     List<polyPatch*> p(patches.size());
00218 
00219     forAll (p, patchI)
00220     {
00221         p[patchI] = patches[patchI].clone(fMesh.boundaryMesh()).ptr();
00222     }
00223 
00224     fMesh.addFvPatches(p);
00225 
00226 
00227     // Refinement level
00228     IOobject refHeader
00229     (
00230         "refinementLevel",
00231         runTime.timeName(),
00232         polyMesh::defaultRegion,
00233         runTime
00234     );
00235 
00236     if (!readLevel && refHeader.headerOk())
00237     {
00238         WarningIn(args.executable())
00239             << "Detected " << refHeader.name() << " file in "
00240             << polyMesh::defaultRegion <<  " directory. Please remove to"
00241             << " recreate it or use the -readLevel option to use it"
00242             << endl;
00243         return 1;
00244     }
00245 
00246 
00247     labelIOList refLevel
00248     (
00249         IOobject
00250         (
00251             "refinementLevel",
00252             runTime.timeName(),
00253             mesh,
00254             IOobject::NO_READ,
00255             IOobject::AUTO_WRITE
00256         ),
00257         labelList(mesh.nCells(), 0)
00258     );
00259 
00260     if (readLevel)
00261     {
00262         refLevel = labelIOList(refHeader);
00263     }
00264 
00265     // Construct volScalarField with same info for post processing
00266     volScalarField postRefLevel
00267     (
00268         IOobject
00269         (
00270             "refinementLevel",
00271             runTime.timeName(),
00272             mesh,
00273             IOobject::NO_READ,
00274             IOobject::NO_WRITE
00275         ),
00276         fMesh,
00277         dimensionedScalar("zero", dimless/dimTime, 0)
00278     );
00279 
00280     // Set cell values
00281     forAll(bins, binI)
00282     {
00283         const DynamicList<label>& bin = bins[binI];
00284 
00285         forAll(bin, i)
00286         {
00287             refLevel[bin[i]] = bins.size() - binI - 1;
00288             postRefLevel[bin[i]] = refLevel[bin[i]];
00289         }
00290     }
00291 
00292 
00293     // For volScalarField: set boundary values to same as cell.
00294     // Note: could also put
00295     // zeroGradient b.c. on postRefLevel and do evaluate.
00296     forAll(postRefLevel.boundaryField(), patchI)
00297     {
00298         const polyPatch& pp = patches[patchI];
00299 
00300         fvPatchScalarField& bField = postRefLevel.boundaryField()[patchI];
00301 
00302         Info<< "Setting field for patch "<< endl;
00303 
00304         forAll(bField, faceI)
00305         {
00306             label own = mesh.faceOwner()[pp.start() + faceI];
00307 
00308             bField[faceI] = postRefLevel[own];
00309         }
00310     }
00311 
00312     Info<< "Determined current refinement level and writing to "
00313         << postRefLevel.name() << " (as volScalarField; for post processing)"
00314         << nl
00315         << polyMesh::defaultRegion/refLevel.name()
00316         << " (as labelIOList; for meshing)" << nl
00317         << endl;
00318 
00319     refLevel.write();
00320     postRefLevel.write();
00321 
00322 
00323     // Find out cells to refine to keep to 2:1 refinement level restriction
00324 
00325     // Cells to refine
00326     cellSet refCells(mesh, "refCells", 100);
00327 
00328     while
00329     (
00330         limitRefinementLevel
00331         (
00332             mesh,
00333             refLevel,       // current refinement level
00334             refCells        // cells to refine
00335         )
00336     )
00337     {}
00338 
00339     if (refCells.size())
00340     {
00341         Info<< "Collected " << refCells.size() << " cells that need to be"
00342             << " refined to get closer to overall 2:1 refinement level limit"
00343             << nl
00344             << "Written cells to be refined to cellSet " << refCells.name()
00345             << nl << endl;
00346 
00347         refCells.write();
00348 
00349         Info<< "After refinement this tool can be run again to see if the 2:1"
00350             << " limit is observed all over the mesh" << nl << endl;
00351     }
00352     else
00353     {
00354         Info<< "All cells in the mesh observe the 2:1 refinement level limit"
00355             << nl << endl;
00356     }
00357 
00358     Info << nl << "End" << endl;
00359 
00360     return 0;
00361 }
00362 
00363 
00364 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd