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

renumberMesh.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 anispulation  |
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 Application
00026     renumberMesh
00027 
00028 Description
00029     Renumbers the cell list in order to reduce the bandwidth, reading and
00030     renumbering all fields from all the time directories.
00031 
00032 \*---------------------------------------------------------------------------*/
00033 
00034 #include "argList.H"
00035 #include "IOobjectList.H"
00036 #include "fvMesh.H"
00037 #include "polyTopoChange.H"
00038 #include "ReadFields.H"
00039 #include "volFields.H"
00040 #include "surfaceFields.H"
00041 #include "bandCompression.H"
00042 #include "faceSet.H"
00043 #include "SortableList.H"
00044 #include "decompositionMethod.H"
00045 #include "fvMeshSubset.H"
00046 #include "zeroGradientFvPatchFields.H"
00047 
00048 using namespace Foam;
00049 
00050 
00051 // Calculate band of matrix
00052 label getBand(const labelList& owner, const labelList& neighbour)
00053 {
00054     label band = 0;
00055 
00056     forAll(neighbour, faceI)
00057     {
00058         label diff = neighbour[faceI] - owner[faceI];
00059 
00060         if (diff > band)
00061         {
00062             band = diff;
00063         }
00064     }
00065     return band;
00066 }
00067 
00068 
00069 // Return new to old cell numbering
00070 labelList regionBandCompression
00071 (
00072     const fvMesh& mesh,
00073     const labelList& cellToRegion
00074 )
00075 {
00076     Pout<< "Determining cell order:" << endl;
00077 
00078     labelList cellOrder(cellToRegion.size());
00079 
00080     label nRegions = max(cellToRegion)+1;
00081 
00082     labelListList regionToCells(invertOneToMany(nRegions, cellToRegion));
00083 
00084     label cellI = 0;
00085 
00086     forAll(regionToCells, regionI)
00087     {
00088         Pout<< "    region " << regionI << " starts at " << cellI << endl;
00089 
00090         // Per region do a reordering.
00091         fvMeshSubset subsetter(mesh);
00092         subsetter.setLargeCellSubset(cellToRegion, regionI);
00093         const fvMesh& subMesh = subsetter.subMesh();
00094         labelList subCellOrder(bandCompression(subMesh.cellCells()));
00095 
00096         const labelList& cellMap = subsetter.cellMap();
00097 
00098         forAll(subCellOrder, i)
00099         {
00100             cellOrder[cellI++] = cellMap[subCellOrder[i]];
00101         }
00102     }
00103     Pout<< endl;
00104 
00105     return cellOrder;
00106 }
00107 
00108 
00109 // Determine face order such that inside region faces are sorted
00110 // upper-triangular but inbetween region faces are handled like boundary faces.
00111 labelList regionFaceOrder
00112 (
00113     const primitiveMesh& mesh,
00114     const labelList& cellOrder,     // new to old cell
00115     const labelList& cellToRegion   // old cell to region
00116 )
00117 {
00118     Pout<< "Determining face order:" << endl;
00119 
00120     labelList reverseCellOrder(invert(cellOrder.size(), cellOrder));
00121 
00122     labelList oldToNewFace(mesh.nFaces(), -1);
00123 
00124     label newFaceI = 0;
00125 
00126     label prevRegion = -1;
00127 
00128     forAll (cellOrder, newCellI)
00129     {
00130         label oldCellI = cellOrder[newCellI];
00131 
00132         if (cellToRegion[oldCellI] != prevRegion)
00133         {
00134             prevRegion = cellToRegion[oldCellI];
00135             Pout<< "    region " << prevRegion << " internal faces start at "
00136                 << newFaceI << endl;
00137         }
00138 
00139         const cell& cFaces = mesh.cells()[oldCellI];
00140 
00141         SortableList<label> nbr(cFaces.size());
00142 
00143         forAll(cFaces, i)
00144         {
00145             label faceI = cFaces[i];
00146 
00147             if (mesh.isInternalFace(faceI))
00148             {
00149                 // Internal face. Get cell on other side.
00150                 label nbrCellI = reverseCellOrder[mesh.faceNeighbour()[faceI]];
00151                 if (nbrCellI == newCellI)
00152                 {
00153                     nbrCellI = reverseCellOrder[mesh.faceOwner()[faceI]];
00154                 }
00155 
00156                 if (cellToRegion[oldCellI] != cellToRegion[cellOrder[nbrCellI]])
00157                 {
00158                     // Treat like external face. Do later.
00159                     nbr[i] = -1;
00160                 }
00161                 else if (newCellI < nbrCellI)
00162                 {
00163                     // CellI is master
00164                     nbr[i] = nbrCellI;
00165                 }
00166                 else
00167                 {
00168                     // nbrCell is master. Let it handle this face.
00169                     nbr[i] = -1;
00170                 }
00171             }
00172             else
00173             {
00174                 // External face. Do later.
00175                 nbr[i] = -1;
00176             }
00177         }
00178 
00179         nbr.sort();
00180 
00181         forAll(nbr, i)
00182         {
00183             if (nbr[i] != -1)
00184             {
00185                 oldToNewFace[cFaces[nbr.indices()[i]]] = newFaceI++;
00186             }
00187         }
00188     }
00189 
00190     // Do region interfaces
00191     label nRegions = max(cellToRegion)+1;
00192     {
00193         // Sort in increasing region
00194         SortableList<label> sortKey(mesh.nFaces(), labelMax);
00195 
00196         for (label faceI = 0; faceI < mesh.nInternalFaces(); faceI++)
00197         {
00198             label ownRegion = cellToRegion[mesh.faceOwner()[faceI]];
00199             label neiRegion = cellToRegion[mesh.faceNeighbour()[faceI]];
00200 
00201             if (ownRegion != neiRegion)
00202             {
00203                 sortKey[faceI] =
00204                     min(ownRegion, neiRegion)*nRegions
00205                    +max(ownRegion, neiRegion);
00206             }
00207         }
00208         sortKey.sort();
00209 
00210         // Extract.
00211         label prevKey = -1;
00212         forAll(sortKey, i)
00213         {
00214             label key = sortKey[i];
00215 
00216             if (key == labelMax)
00217             {
00218                 break;
00219             }
00220 
00221             if (prevKey != key)
00222             {
00223                 Pout<< "    faces inbetween region " << key/nRegions
00224                     << " and " << key%nRegions
00225                     << " start at " << newFaceI << endl;
00226                 prevKey = key;
00227             }
00228 
00229             oldToNewFace[sortKey.indices()[i]] = newFaceI++;
00230         }
00231     }
00232 
00233     // Leave patch faces intact.
00234     for (label faceI = newFaceI; faceI < mesh.nFaces(); faceI++)
00235     {
00236         oldToNewFace[faceI] = faceI;
00237     }
00238 
00239 
00240     // Check done all faces.
00241     forAll(oldToNewFace, faceI)
00242     {
00243         if (oldToNewFace[faceI] == -1)
00244         {
00245             FatalErrorIn
00246             (
00247                 "polyDualMesh::getFaceOrder"
00248                 "(const labelList&, const labelList&, const label) const"
00249             )   << "Did not determine new position"
00250                 << " for face " << faceI
00251                 << abort(FatalError);
00252         }
00253     }
00254     Pout<< endl;
00255 
00256     return invert(mesh.nFaces(), oldToNewFace);
00257 }
00258 
00259 
00260 // cellOrder: old cell for every new cell
00261 // faceOrder: old face for every new face. Ordering of boundary faces not
00262 // changed.
00263 autoPtr<mapPolyMesh> reorderMesh
00264 (
00265     polyMesh& mesh,
00266     const labelList& cellOrder,
00267     const labelList& faceOrder
00268 )
00269 {
00270     labelList reverseCellOrder(invert(cellOrder.size(), cellOrder));
00271     labelList reverseFaceOrder(invert(faceOrder.size(), faceOrder));
00272 
00273     faceList newFaces(reorder(reverseFaceOrder, mesh.faces()));
00274     labelList newOwner
00275     (
00276         renumber
00277         (
00278             reverseCellOrder,
00279             reorder(reverseFaceOrder, mesh.faceOwner())
00280         )
00281     );
00282     labelList newNeighbour
00283     (
00284         renumber
00285         (
00286             reverseCellOrder,
00287             reorder(reverseFaceOrder, mesh.faceNeighbour())
00288         )
00289     );
00290 
00291     // Check if any faces need swapping.
00292     forAll(newNeighbour, faceI)
00293     {
00294         label own = newOwner[faceI];
00295         label nei = newNeighbour[faceI];
00296 
00297         if (nei < own)
00298         {
00299             newFaces[faceI] = newFaces[faceI].reverseFace();
00300             Swap(newOwner[faceI], newNeighbour[faceI]);
00301         }
00302     }
00303 
00304     const polyBoundaryMesh& patches = mesh.boundaryMesh();
00305     labelList patchSizes(patches.size());
00306     labelList patchStarts(patches.size());
00307     labelList oldPatchNMeshPoints(patches.size());
00308     labelListList patchPointMap(patches.size());
00309     forAll(patches, patchI)
00310     {
00311         patchSizes[patchI] = patches[patchI].size();
00312         patchStarts[patchI] = patches[patchI].start();
00313         oldPatchNMeshPoints[patchI] = patches[patchI].nPoints();
00314         patchPointMap[patchI] = identity(patches[patchI].nPoints());
00315     }
00316 
00317     mesh.resetPrimitives
00318     (
00319         Xfer<pointField>::null(),
00320         xferMove(newFaces),
00321         xferMove(newOwner),
00322         xferMove(newNeighbour),
00323         patchSizes,
00324         patchStarts
00325     );
00326 
00327     return autoPtr<mapPolyMesh>
00328     (
00329         new mapPolyMesh
00330         (
00331             mesh,                       //const polyMesh& mesh,
00332             mesh.nPoints(),             // nOldPoints,
00333             mesh.nFaces(),              // nOldFaces,
00334             mesh.nCells(),              // nOldCells,
00335             identity(mesh.nPoints()),   // pointMap,
00336             List<objectMap>(0),         // pointsFromPoints,
00337             faceOrder,                  // faceMap,
00338             List<objectMap>(0),         // facesFromPoints,
00339             List<objectMap>(0),         // facesFromEdges,
00340             List<objectMap>(0),         // facesFromFaces,
00341             cellOrder,                  // cellMap,
00342             List<objectMap>(0),         // cellsFromPoints,
00343             List<objectMap>(0),         // cellsFromEdges,
00344             List<objectMap>(0),         // cellsFromFaces,
00345             List<objectMap>(0),         // cellsFromCells,
00346             identity(mesh.nPoints()),   // reversePointMap,
00347             reverseFaceOrder,           // reverseFaceMap,
00348             reverseCellOrder,           // reverseCellMap,
00349             labelHashSet(0),            // flipFaceFlux,
00350             patchPointMap,              // patchPointMap,
00351             labelListList(0),           // pointZoneMap,
00352             labelListList(0),           // faceZonePointMap,
00353             labelListList(0),           // faceZoneFaceMap,
00354             labelListList(0),           // cellZoneMap,
00355             pointField(0),              // preMotionPoints,
00356             patchStarts,                // oldPatchStarts,
00357             oldPatchNMeshPoints         // oldPatchNMeshPoints
00358         )
00359     );
00360 }
00361 
00362 
00363 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00364 
00365 int main(int argc, char *argv[])
00366 {
00367     argList::validOptions.insert("blockOrder", "");
00368     argList::validOptions.insert("orderPoints", "");
00369     argList::validOptions.insert("writeMaps", "");
00370     argList::validOptions.insert("overwrite", "");
00371 
00372 #   include "addTimeOptions.H"
00373 
00374 #   include "setRootCase.H"
00375 #   include "createTime.H"
00376     runTime.functionObjects().off();
00377 
00378     // Get times list
00379     instantList Times = runTime.times();
00380 
00381     // set startTime and endTime depending on -time and -latestTime options
00382 #   include "checkTimeOptions.H"
00383 
00384     runTime.setTime(Times[startTime], startTime);
00385 
00386 #   include "createMesh.H"
00387     const word oldInstance = mesh.pointsInstance();
00388 
00389     const bool blockOrder = args.optionFound("blockOrder");
00390     if (blockOrder)
00391     {
00392         Info<< "Ordering cells into regions (using decomposition);"
00393             << " ordering faces into region-internal and region-external." << nl
00394             << endl;
00395     }
00396 
00397     const bool orderPoints = args.optionFound("orderPoints");
00398     if (orderPoints)
00399     {
00400         Info<< "Ordering points into internal and boundary points." << nl
00401             << endl;
00402     }
00403 
00404     const bool writeMaps = args.optionFound("writeMaps");
00405 
00406     if (writeMaps)
00407     {
00408         Info<< "Writing renumber maps (new to old) to polyMesh." << nl
00409             << endl;
00410     }
00411 
00412     bool overwrite = args.optionFound("overwrite");
00413 
00414     label band = getBand(mesh.faceOwner(), mesh.faceNeighbour());
00415 
00416     Info<< "Mesh size: " << returnReduce(mesh.nCells(), sumOp<label>()) << nl
00417         << "Band before renumbering: "
00418         << returnReduce(band, maxOp<label>()) << nl << endl;
00419 
00420     // Read objects in time directory
00421     IOobjectList objects(mesh, runTime.timeName());
00422 
00423     // Read vol fields.
00424 
00425     PtrList<volScalarField> vsFlds;
00426     ReadFields(mesh, objects, vsFlds);
00427 
00428     PtrList<volVectorField> vvFlds;
00429     ReadFields(mesh, objects, vvFlds);
00430 
00431     PtrList<volSphericalTensorField> vstFlds;
00432     ReadFields(mesh, objects, vstFlds);
00433 
00434     PtrList<volSymmTensorField> vsymtFlds;
00435     ReadFields(mesh, objects, vsymtFlds);
00436 
00437     PtrList<volTensorField> vtFlds;
00438     ReadFields(mesh, objects, vtFlds);
00439 
00440     // Read surface fields.
00441 
00442     PtrList<surfaceScalarField> ssFlds;
00443     ReadFields(mesh, objects, ssFlds);
00444 
00445     PtrList<surfaceVectorField> svFlds;
00446     ReadFields(mesh, objects, svFlds);
00447 
00448     PtrList<surfaceSphericalTensorField> sstFlds;
00449     ReadFields(mesh, objects, sstFlds);
00450 
00451     PtrList<surfaceSymmTensorField> ssymtFlds;
00452     ReadFields(mesh, objects, ssymtFlds);
00453 
00454     PtrList<surfaceTensorField> stFlds;
00455     ReadFields(mesh, objects, stFlds);
00456 
00457 
00458     autoPtr<mapPolyMesh> map;
00459 
00460     if (blockOrder)
00461     {
00462         // Renumbering in two phases. Should be done in one so mapping of
00463         // fields is done correctly!
00464 
00465         // Read decomposePar dictionary
00466         IOdictionary decomposeDict
00467         (
00468             IOobject
00469             (
00470                 "decomposeParDict",
00471                 runTime.system(),
00472                 mesh,
00473                 IOobject::MUST_READ,
00474                 IOobject::NO_WRITE
00475             )
00476         );
00477         autoPtr<decompositionMethod> decomposePtr = decompositionMethod::New
00478         (
00479             decomposeDict,
00480             mesh
00481         );
00482 
00483         labelList cellToRegion(decomposePtr().decompose(mesh.cellCentres()));
00484 
00485         // For debugging: write out region
00486         {
00487             volScalarField cellDist
00488             (
00489                 IOobject
00490                 (
00491                     "cellDist",
00492                     runTime.timeName(),
00493                     mesh,
00494                     IOobject::NO_READ,
00495                     IOobject::NO_WRITE,
00496                     false
00497                 ),
00498                 mesh,
00499                 dimensionedScalar("cellDist", dimless, 0),
00500                 zeroGradientFvPatchScalarField::typeName
00501             );
00502 
00503             forAll(cellToRegion, cellI)
00504             {
00505                cellDist[cellI] = cellToRegion[cellI];
00506             }
00507 
00508             cellDist.write();
00509 
00510             Info<< nl << "Written decomposition as volScalarField to "
00511                 << cellDist.name() << " for use in postprocessing."
00512                 << nl << endl;
00513         }
00514 
00515         // Use block based renumbering.
00516         //labelList cellOrder(bandCompression(mesh.cellCells()));
00517         labelList cellOrder(regionBandCompression(mesh, cellToRegion));
00518 
00519         // Determine new to old face order with new cell numbering
00520         labelList faceOrder
00521         (
00522             regionFaceOrder
00523             (
00524                 mesh,
00525                 cellOrder,
00526                 cellToRegion
00527             )
00528         );
00529 
00530         if (!overwrite)
00531         {
00532             runTime++;
00533         }
00534 
00535         // Change the mesh.
00536         map = reorderMesh(mesh, cellOrder, faceOrder);
00537     }
00538     else
00539     {
00540         // Use built-in renumbering.
00541 
00542         polyTopoChange meshMod(mesh);
00543 
00544         if (!overwrite)
00545         {
00546             runTime++;
00547         }
00548 
00549         // Change the mesh.
00550         map = meshMod.changeMesh
00551         (
00552             mesh,
00553             false,      // inflate
00554             true,       // parallel sync
00555             true,       // cell ordering
00556             orderPoints // point ordering
00557         );
00558     }
00559 
00560     // Update fields
00561     mesh.updateMesh(map);
00562 
00563     // Move mesh (since morphing might not do this)
00564     if (map().hasMotionPoints())
00565     {
00566         mesh.movePoints(map().preMotionPoints());
00567     }
00568 
00569 
00570     band = getBand(mesh.faceOwner(), mesh.faceNeighbour());
00571 
00572     Info<< "Band after renumbering: "
00573         << returnReduce(band, maxOp<label>()) << nl << endl;
00574 
00575 
00576     if (orderPoints)
00577     {
00578         // Force edge calculation (since only reason that points would need to
00579         // be sorted)
00580         (void)mesh.edges();
00581 
00582         label nTotPoints = returnReduce
00583         (
00584             mesh.nPoints(),
00585             sumOp<label>()
00586         );
00587         label nTotIntPoints = returnReduce
00588         (
00589             mesh.nInternalPoints(),
00590             sumOp<label>()
00591         );
00592 
00593         label nTotEdges = returnReduce
00594         (
00595             mesh.nEdges(),
00596             sumOp<label>()
00597         );
00598         label nTotIntEdges = returnReduce
00599         (
00600             mesh.nInternalEdges(),
00601             sumOp<label>()
00602         );
00603         label nTotInt0Edges = returnReduce
00604         (
00605             mesh.nInternal0Edges(),
00606             sumOp<label>()
00607         );
00608         label nTotInt1Edges = returnReduce
00609         (
00610             mesh.nInternal1Edges(),
00611             sumOp<label>()
00612         );
00613 
00614         Info<< "Points:" << nl
00615             << "    total   : " << nTotPoints << nl
00616             << "    internal: " << nTotIntPoints << nl
00617             << "    boundary: " << nTotPoints-nTotIntPoints << nl
00618             << "Edges:" << nl
00619             << "    total   : " << nTotEdges << nl
00620             << "    internal: " << nTotIntEdges << nl
00621             << "        internal using 0 boundary points: "
00622             << nTotInt0Edges << nl
00623             << "        internal using 1 boundary points: "
00624             << nTotInt1Edges-nTotInt0Edges << nl
00625             << "        internal using 2 boundary points: "
00626             << nTotIntEdges-nTotInt1Edges << nl
00627             << "    boundary: " << nTotEdges-nTotIntEdges << nl
00628             << endl;
00629     }
00630 
00631 
00632     if (overwrite)
00633     {
00634         mesh.setInstance(oldInstance);
00635     }
00636     Info<< "Writing mesh to " << runTime.timeName() << endl;
00637 
00638     mesh.write();
00639 
00640     if (writeMaps)
00641     {
00642         labelIOList
00643         (
00644             IOobject
00645             (
00646                 "cellMap",
00647                 mesh.facesInstance(),
00648                 polyMesh::meshSubDir,
00649                 mesh,
00650                 IOobject::NO_READ,
00651                 IOobject::NO_WRITE,
00652                 false
00653             ),
00654             map().cellMap()
00655         ).write();
00656         labelIOList
00657         (
00658             IOobject
00659             (
00660                 "faceMap",
00661                 mesh.facesInstance(),
00662                 polyMesh::meshSubDir,
00663                 mesh,
00664                 IOobject::NO_READ,
00665                 IOobject::NO_WRITE,
00666                 false
00667             ),
00668             map().faceMap()
00669         ).write();
00670         labelIOList
00671         (
00672             IOobject
00673             (
00674                 "pointMap",
00675                 mesh.facesInstance(),
00676                 polyMesh::meshSubDir,
00677                 mesh,
00678                 IOobject::NO_READ,
00679                 IOobject::NO_WRITE,
00680                 false
00681             ),
00682             map().pointMap()
00683         ).write();
00684     }
00685 
00686     Info<< "\nEnd.\n" << endl;
00687 
00688     return 0;
00689 }
00690 
00691 
00692 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd