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

refineMesh.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     Utility to refine cells in multiple directions.
00027 
00028     Either supply -all option to refine all cells (3D refinement for 3D
00029     cases; 2D for 2D cases) or reads a refineMeshDict with
00030     - cellSet to refine
00031     - directions to refine
00032 
00033 \*---------------------------------------------------------------------------*/
00034 
00035 #include "argList.H"
00036 #include "polyMesh.H"
00037 #include "Time.H"
00038 #include "undoableMeshCutter.H"
00039 #include "hexCellLooper.H"
00040 #include "cellSet.H"
00041 #include "twoDPointCorrector.H"
00042 #include "directions.H"
00043 #include "OFstream.H"
00044 #include "multiDirRefinement.H"
00045 #include "labelIOList.H"
00046 #include "wedgePolyPatch.H"
00047 #include "plane.H"
00048 
00049 using namespace Foam;
00050 
00051 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00052 
00053 
00054 // Max cos angle for edges to be considered aligned with axis.
00055 static const scalar edgeTol = 1E-3;
00056 
00057 
00058 // Calculate some edge statistics on mesh.
00059 void printEdgeStats(const primitiveMesh& mesh)
00060 {
00061     label nX = 0;
00062     label nY = 0;
00063     label nZ = 0;
00064 
00065     scalar minX = GREAT;
00066     scalar maxX = -GREAT;
00067     vector x(1, 0, 0);
00068 
00069     scalar minY = GREAT;
00070     scalar maxY = -GREAT;
00071     vector y(0, 1, 0);
00072 
00073     scalar minZ = GREAT;
00074     scalar maxZ = -GREAT;
00075     vector z(0, 0, 1);
00076 
00077     scalar minOther = GREAT;
00078     scalar maxOther = -GREAT;
00079 
00080     const edgeList& edges = mesh.edges();
00081 
00082     forAll(edges, edgeI)
00083     {
00084         const edge& e = edges[edgeI];
00085 
00086         vector eVec(e.vec(mesh.points()));
00087 
00088         scalar eMag = mag(eVec);
00089 
00090         eVec /= eMag;
00091 
00092         if (mag(eVec & x) > 1-edgeTol)
00093         {
00094             minX = min(minX, eMag);
00095             maxX = max(maxX, eMag);
00096             nX++;
00097         }
00098         else if (mag(eVec & y) > 1-edgeTol)
00099         {
00100             minY = min(minY, eMag);
00101             maxY = max(maxY, eMag);
00102             nY++;
00103         }
00104         else if (mag(eVec & z) > 1-edgeTol)
00105         {
00106             minZ = min(minZ, eMag);
00107             maxZ = max(maxZ, eMag);
00108             nZ++;
00109         }
00110         else
00111         {
00112             minOther = min(minOther, eMag);
00113             maxOther = max(maxOther, eMag);
00114         }
00115     }
00116 
00117     Pout<< "Mesh edge statistics:" << endl
00118         << "    x aligned :  number:" << nX << "\tminLen:" << minX
00119         << "\tmaxLen:" << maxX << endl
00120         << "    y aligned :  number:" << nY << "\tminLen:" << minY
00121         << "\tmaxLen:" << maxY << endl
00122         << "    z aligned :  number:" << nZ << "\tminLen:" << minZ
00123         << "\tmaxLen:" << maxZ << endl
00124         << "    other     :  number:" << mesh.nEdges() - nX - nY - nZ
00125         << "\tminLen:" << minOther
00126         << "\tmaxLen:" << maxOther << endl << endl;
00127 }
00128 
00129 
00130 // Return index of coordinate axis.
00131 label axis(const vector& normal)
00132 {
00133     label axisIndex = -1;
00134 
00135     if (mag(normal & point(1, 0, 0)) > (1-edgeTol))
00136     {
00137         axisIndex = 0;
00138     }
00139     else if (mag(normal & point(0, 1, 0)) > (1-edgeTol))
00140     {
00141         axisIndex = 1;
00142     }
00143     else if (mag(normal & point(0, 0, 1)) > (1-edgeTol))
00144     {
00145         axisIndex = 2;
00146     }
00147 
00148     return axisIndex;
00149 }
00150 
00151 
00152 //- Returns -1 or cartesian coordinate component (0=x, 1=y, 2=z) of normal
00153 //  in case of 2D mesh
00154 label twoDNess(const polyMesh& mesh)
00155 {
00156     const pointField& ctrs = mesh.cellCentres();
00157 
00158     if (ctrs.size() < 2)
00159     {
00160         return -1;
00161     }
00162 
00163 
00164     //
00165     // 1. All cell centres on single plane aligned with x, y or z
00166     //
00167 
00168     // Determine 3 points to base plane on.
00169     vector vec10 = ctrs[1] - ctrs[0];
00170     vec10 /= mag(vec10);
00171 
00172     label otherCellI = -1;
00173 
00174     for (label cellI = 2; cellI < ctrs.size(); cellI++)
00175     {
00176         vector vec(ctrs[cellI] - ctrs[0]);
00177         vec /= mag(vec);
00178 
00179         if (mag(vec & vec10) < 0.9)
00180         {
00181             // ctrs[cellI] not in line with n
00182             otherCellI = cellI;
00183 
00184             break;
00185         }
00186     }
00187 
00188     if (otherCellI == -1)
00189     {
00190         // Cannot find cell to make decent angle with cell0-cell1 vector.
00191         // Note: what to do here? All cells (almost) in one line. Maybe 1D case?
00192         return -1;
00193     }
00194 
00195     plane cellPlane(ctrs[0], ctrs[1], ctrs[otherCellI]);
00196 
00197 
00198     forAll(ctrs, cellI)
00199     {
00200         const labelList& cEdges = mesh.cellEdges()[cellI];
00201 
00202         scalar minLen = GREAT;
00203 
00204         forAll(cEdges, i)
00205         {
00206             minLen = min(minLen, mesh.edges()[cEdges[i]].mag(mesh.points()));
00207         }
00208 
00209         if (cellPlane.distance(ctrs[cellI]) > 1E-6*minLen)
00210         {
00211             // Centres not in plane
00212             return  -1;
00213         }
00214     }
00215 
00216     label axisIndex = axis(cellPlane.normal());
00217 
00218     if (axisIndex == -1)
00219     {
00220         return axisIndex;
00221     }
00222 
00223 
00224     const polyBoundaryMesh& patches = mesh.boundaryMesh();
00225 
00226 
00227     //
00228     // 2. No edges without points on boundary
00229     //
00230 
00231     // Mark boundary points
00232     boolList boundaryPoint(mesh.points().size(), false);
00233 
00234     forAll(patches, patchI)
00235     {
00236         const polyPatch& patch = patches[patchI];
00237 
00238         forAll(patch, patchFaceI)
00239         {
00240             const face& f = patch[patchFaceI];
00241 
00242             forAll(f, fp)
00243             {
00244                 boundaryPoint[f[fp]] = true;
00245             }
00246         }
00247     }
00248 
00249 
00250     const edgeList& edges = mesh.edges();
00251 
00252     forAll(edges, edgeI)
00253     {
00254         const edge& e = edges[edgeI];
00255 
00256         if (!boundaryPoint[e.start()] && !boundaryPoint[e.end()])
00257         {
00258             // Edge has no point on boundary.
00259             return -1;
00260         }
00261     }
00262 
00263 
00264     // 3. For all non-wedge patches: all faces either perp or aligned with
00265     //    cell-plane normal. (wedge patches already checked upon construction)
00266 
00267     forAll(patches, patchI)
00268     {
00269         const polyPatch& patch = patches[patchI];
00270 
00271         if
00272         (
00273             typeid(patch) != typeid(wedgePolyPatch)
00274         )
00275         {
00276             const vectorField& n = patch.faceAreas();
00277 
00278             scalarField cosAngle = mag(n/mag(n) & cellPlane.normal());
00279 
00280             if (mag(min(cosAngle) - max(cosAngle)) > 1E-6)
00281             {
00282                 // cosAngle should be either ~1 over all faces (2D front and
00283                 // back) or ~0 (all other patches perp to 2D)
00284                 return -1;
00285             }
00286         }
00287     }
00288 
00289     return axisIndex;
00290 }
00291 
00292 
00293 // Main program:
00294 
00295 int main(int argc, char *argv[])
00296 {
00297     Foam::argList::validOptions.insert("dict", "");
00298     Foam::argList::validOptions.insert("overwrite", "");
00299 
00300 #   include "setRootCase.H"
00301 #   include "createTime.H"
00302     runTime.functionObjects().off();
00303 #   include "createPolyMesh.H"
00304     const word oldInstance = mesh.pointsInstance();
00305 
00306     printEdgeStats(mesh);
00307 
00308 
00309     //
00310     // Read/construct control dictionary
00311     //
00312 
00313     bool readDict = args.optionFound("dict");
00314     bool overwrite = args.optionFound("overwrite");
00315 
00316     // List of cells to refine
00317     labelList refCells;
00318 
00319     // Dictionary to control refinement
00320     dictionary refineDict;
00321 
00322     if (readDict)
00323     {
00324         Info<< "Refining according to refineMeshDict" << nl << endl;
00325 
00326         refineDict =
00327             IOdictionary
00328             (
00329                 IOobject
00330                 (
00331                     "refineMeshDict",
00332                     runTime.system(),
00333                     mesh,
00334                     IOobject::MUST_READ,
00335                     IOobject::NO_WRITE
00336                 )
00337             );
00338 
00339         word setName(refineDict.lookup("set"));
00340 
00341         cellSet cells(mesh, setName);
00342 
00343         Pout<< "Read " << cells.size() << " cells from cellSet "
00344             << cells.instance()/cells.local()/cells.name()
00345             << endl << endl;
00346 
00347         refCells = cells.toc();
00348     }
00349     else
00350     {
00351         Info<< "Refining all cells" << nl << endl;
00352 
00353         // Select all cells
00354         refCells.setSize(mesh.nCells());
00355 
00356         forAll(mesh.cells(), cellI)
00357         {
00358             refCells[cellI] = cellI;
00359         }
00360 
00361 
00362         // Set refinement directions based on 2D/3D
00363         label axisIndex = twoDNess(mesh);
00364 
00365         if (axisIndex == -1)
00366         {
00367             Info<< "3D case; refining all directions" << nl << endl;
00368 
00369             wordList directions(3);
00370             directions[0] = "tan1";
00371             directions[1] = "tan2";
00372             directions[2] = "normal";
00373             refineDict.add("directions", directions);
00374 
00375             // Use hex cutter
00376             refineDict.add("useHexTopology", "true");
00377         }
00378         else
00379         {
00380             wordList directions(2);
00381 
00382             if (axisIndex == 0)
00383             {
00384                 Info<< "2D case; refining in directions y,z\n" << endl;
00385                 directions[0] = "tan2";
00386                 directions[1] = "normal";
00387             }
00388             else if (axisIndex == 1)
00389             {
00390                 Info<< "2D case; refining in directions x,z\n" << endl;
00391                 directions[0] = "tan1";
00392                 directions[1] = "normal";
00393             }
00394             else
00395             {
00396                 Info<< "2D case; refining in directions x,y\n" << endl;
00397                 directions[0] = "tan1";
00398                 directions[1] = "tan2";
00399             }
00400 
00401             refineDict.add("directions", directions);
00402 
00403             // Use standard cutter
00404             refineDict.add("useHexTopology", "false");
00405         }
00406 
00407         refineDict.add("coordinateSystem", "global");
00408 
00409         dictionary coeffsDict;
00410         coeffsDict.add("tan1", vector(1, 0, 0));
00411         coeffsDict.add("tan2", vector(0, 1, 0));
00412         refineDict.add("globalCoeffs", coeffsDict);
00413 
00414         refineDict.add("geometricCut", "false");
00415         refineDict.add("writeMesh", "false");
00416     }
00417 
00418 
00419     string oldTimeName(runTime.timeName());
00420 
00421     if (!overwrite)
00422     {
00423         runTime++;
00424     }
00425 
00426 
00427     // Multi-directional refinement (does multiple iterations)
00428     multiDirRefinement multiRef(mesh, refCells, refineDict);
00429 
00430 
00431     // Write resulting mesh
00432     if (overwrite)
00433     {
00434         mesh.setInstance(oldInstance);
00435     }
00436     mesh.write();
00437 
00438 
00439     // Get list of cell splits.
00440     // (is for every cell in old mesh the cells they have been split into)
00441     const labelListList& oldToNew = multiRef.addedCells();
00442 
00443 
00444     // Create cellSet with added cells for easy inspection
00445     cellSet newCells(mesh, "refinedCells", refCells.size());
00446 
00447     forAll(oldToNew, oldCellI)
00448     {
00449         const labelList& added = oldToNew[oldCellI];
00450 
00451         forAll(added, i)
00452         {
00453             newCells.insert(added[i]);
00454         }
00455     }
00456 
00457     Pout<< "Writing refined cells (" << newCells.size() << ") to cellSet "
00458         << newCells.instance()/newCells.local()/newCells.name()
00459         << endl << endl;
00460 
00461     newCells.write();
00462 
00463 
00464 
00465 
00466     //
00467     // Invert cell split to construct map from new to old
00468     //
00469 
00470     labelIOList newToOld
00471     (
00472         IOobject
00473         (
00474             "cellMap",
00475             runTime.timeName(),
00476             polyMesh::meshSubDir,
00477             mesh,
00478             IOobject::NO_READ,
00479             IOobject::AUTO_WRITE
00480         ),
00481         mesh.nCells()
00482     );
00483     newToOld.note() =
00484         "From cells in mesh at "
00485       + runTime.timeName()
00486       + " to cells in mesh at "
00487       + oldTimeName;
00488 
00489 
00490     forAll(oldToNew, oldCellI)
00491     {
00492         const labelList& added = oldToNew[oldCellI];
00493 
00494         if (added.size())
00495         {
00496             forAll(added, i)
00497             {
00498                 newToOld[added[i]] = oldCellI;
00499             }
00500         }
00501         else
00502         {
00503             // Unrefined cell
00504             newToOld[oldCellI] = oldCellI;
00505         }
00506     }
00507 
00508     Info<< "Writing map from new to old cell to "
00509         << newToOld.objectPath() << nl << endl;
00510 
00511     newToOld.write();
00512 
00513 
00514     // Some statistics.
00515 
00516     printEdgeStats(mesh);
00517 
00518     Info<< "End\n" << endl;
00519 
00520     return 0;
00521 }
00522 
00523 
00524 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd