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

combinePatchFaces.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     Checks for multiple patch faces on same cell and combines them. These
00027     result from e.g. refined neighbouring cells getting removed, leaving 4
00028     exposed faces with same owner.
00029 
00030     Rules for merging:
00031     - only boundary faces (since multiple internal faces between two cells
00032       not allowed anyway)
00033     - faces have to have same owner
00034     - faces have to be connected via edge which are not features (so angle
00035       between them < feature angle)
00036     - outside of faces has to be single loop
00037     - outside of face should not be (or just slightly) concave (so angle
00038       between consecutive edges < concaveangle
00039 
00040     E.g. to allow all faces on same patch to be merged:
00041 
00042         combinePatchFaces .. cavity 180 -concaveAngle 90
00043 
00044 \*---------------------------------------------------------------------------*/
00045 
00046 #include "PstreamReduceOps.H"
00047 #include "argList.H"
00048 #include "Time.H"
00049 #include "polyTopoChange.H"
00050 #include "polyModifyFace.H"
00051 #include "polyAddFace.H"
00052 #include "combineFaces.H"
00053 #include "removePoints.H"
00054 #include "polyMesh.H"
00055 #include "mapPolyMesh.H"
00056 #include "mathematicalConstants.H"
00057 
00058 using namespace Foam;
00059 
00060 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00061 
00062 // Sin of angle between two consecutive edges on a face. If sin(angle) larger
00063 // than this the face will be considered concave.
00064 const scalar defaultConcaveAngle = 30;
00065 
00066 
00067 // Same check as snapMesh
00068 void checkSnapMesh
00069 (
00070     const Time& runTime,
00071     const polyMesh& mesh,
00072     labelHashSet& wrongFaces
00073 )
00074 {
00075     IOdictionary snapDict
00076     (
00077         IOobject
00078         (
00079             "snapMeshDict",
00080             runTime.system(),
00081             mesh,
00082             IOobject::MUST_READ,
00083             IOobject::NO_WRITE
00084         )
00085     );
00086 
00087     // Max nonorthogonality allowed
00088     scalar maxNonOrtho(readScalar(snapDict.lookup("maxNonOrtho")));
00089     // Max concaveness allowed.
00090     scalar maxConcave(readScalar(snapDict.lookup("maxConcave")));
00091     // Min volume allowed (factor of minimum cellVolume)
00092     scalar relMinVol(readScalar(snapDict.lookup("minVol")));
00093     const scalar minCellVol = min(mesh.cellVolumes());
00094     const scalar minPyrVol = relMinVol*minCellVol;
00095     // Min area
00096     scalar minArea(readScalar(snapDict.lookup("minArea")));
00097 
00098     if (maxNonOrtho < 180.0-SMALL)
00099     {
00100         Pout<< "Checking non orthogonality" << endl;
00101 
00102         label nOldSize = wrongFaces.size();
00103         mesh.setNonOrthThreshold(maxNonOrtho);
00104         mesh.checkFaceOrthogonality(false, &wrongFaces);
00105 
00106         Pout<< "Detected " << wrongFaces.size() - nOldSize
00107             << " faces with non-orthogonality > " << maxNonOrtho << " degrees"
00108             << endl;
00109     }
00110 
00111     if (minPyrVol > -GREAT)
00112     {
00113         Pout<< "Checking face pyramids" << endl;
00114 
00115         label nOldSize = wrongFaces.size();
00116         mesh.checkFacePyramids(false, minPyrVol, &wrongFaces);
00117         Pout<< "Detected additional " << wrongFaces.size() - nOldSize
00118             << " faces with illegal face pyramids" << endl;
00119     }
00120 
00121     if (maxConcave < 180.0-SMALL)
00122     {
00123         Pout<< "Checking face angles" << endl;
00124 
00125         label nOldSize = wrongFaces.size();
00126         mesh.checkFaceAngles(false, maxConcave, &wrongFaces);
00127         Pout<< "Detected additional " << wrongFaces.size() - nOldSize
00128             << " faces with concavity > " << maxConcave << " degrees"
00129             << endl;
00130     }
00131 
00132     if (minArea > -SMALL)
00133     {
00134         Pout<< "Checking face areas" << endl;
00135 
00136         label nOldSize = wrongFaces.size();
00137 
00138         const scalarField magFaceAreas = mag(mesh.faceAreas());
00139 
00140         forAll(magFaceAreas, faceI)
00141         {
00142             if (magFaceAreas[faceI] < minArea)
00143             {
00144                 wrongFaces.insert(faceI);
00145             }
00146         }
00147         Pout<< "Detected additional " << wrongFaces.size() - nOldSize
00148             << " faces with area < " << minArea << " m^2" << endl;
00149     }
00150 }
00151 
00152 
00153 // Merge faces on the same patch (usually from exposing refinement)
00154 // Can undo merges if these cause problems.
00155 label mergePatchFaces
00156 (
00157     const scalar minCos,
00158     const scalar concaveSin,
00159     const bool snapMeshDict,
00160     const Time& runTime,
00161     polyMesh& mesh
00162 )
00163 {
00164     // Patch face merging engine
00165     combineFaces faceCombiner(mesh);
00166 
00167     // Get all sets of faces that can be merged
00168     labelListList allFaceSets(faceCombiner.getMergeSets(minCos, concaveSin));
00169 
00170     label nFaceSets = returnReduce(allFaceSets.size(), sumOp<label>());
00171 
00172     Info<< "Merging " << nFaceSets << " sets of faces." << endl;
00173 
00174     if (nFaceSets > 0)
00175     {
00176         // Store the faces of the face sets
00177         List<faceList> allFaceSetsFaces(allFaceSets.size());
00178         forAll(allFaceSets, setI)
00179         {
00180             allFaceSetsFaces[setI] = UIndirectList<face>
00181             (
00182                 mesh.faces(),
00183                 allFaceSets[setI]
00184             );
00185         }
00186 
00187         autoPtr<mapPolyMesh> map;
00188         {
00189             // Topology changes container
00190             polyTopoChange meshMod(mesh);
00191 
00192             // Merge all faces of a set into the first face of the set.
00193             faceCombiner.setRefinement(allFaceSets, meshMod);
00194 
00195             // Change the mesh (no inflation)
00196             map = meshMod.changeMesh(mesh, false, true);
00197 
00198             // Update fields
00199             mesh.updateMesh(map);
00200 
00201             // Move mesh (since morphing does not do this)
00202             if (map().hasMotionPoints())
00203             {
00204                 mesh.movePoints(map().preMotionPoints());
00205             }
00206             else
00207             {
00208                 // Delete mesh volumes. No other way to do this?
00209                 mesh.clearOut();
00210             }
00211         }
00212 
00213 
00214         // Check for errors and undo
00215         // ~~~~~~~~~~~~~~~~~~~~~~~~~
00216 
00217         // Faces in error.
00218         labelHashSet errorFaces;
00219 
00220         if (snapMeshDict)
00221         {
00222             checkSnapMesh(runTime, mesh, errorFaces);
00223         }
00224         else
00225         {
00226             mesh.checkFacePyramids(false, -SMALL, &errorFaces);
00227         }
00228 
00229         // Sets where the master is in error
00230         labelHashSet errorSets;
00231 
00232         forAll(allFaceSets, setI)
00233         {
00234             label newMasterI = map().reverseFaceMap()[allFaceSets[setI][0]];
00235 
00236             if (errorFaces.found(newMasterI))
00237             {
00238                 errorSets.insert(setI);
00239             }
00240         }
00241         label nErrorSets = returnReduce(errorSets.size(), sumOp<label>());
00242 
00243         Info<< "Detected " << nErrorSets
00244             << " error faces on boundaries that have been merged."
00245             << " These will be restored to their original faces."
00246             << endl;
00247 
00248         if (nErrorSets > 0)
00249         {
00250             // Renumber stored faces to new vertex numbering.
00251             forAllConstIter(labelHashSet, errorSets, iter)
00252             {
00253                 label setI = iter.key();
00254 
00255                 faceList& setFaceVerts = allFaceSetsFaces[setI];
00256 
00257                 forAll(setFaceVerts, i)
00258                 {
00259                     inplaceRenumber(map().reversePointMap(), setFaceVerts[i]);
00260 
00261                     // Debug: check that all points are still there.
00262                     forAll(setFaceVerts[i], j)
00263                     {
00264                         label newVertI = setFaceVerts[i][j];
00265 
00266                         if (newVertI < 0)
00267                         {
00268                             FatalErrorIn("mergePatchFaces")
00269                                 << "In set:" << setI << " old face labels:"
00270                                 << allFaceSets[setI] << " new face vertices:"
00271                                 << setFaceVerts[i] << " are unmapped vertices!"
00272                                 << abort(FatalError);
00273                         }
00274                     }
00275                 }
00276             }
00277 
00278 
00279             // Topology changes container
00280             polyTopoChange meshMod(mesh);
00281 
00282 
00283             // Restore faces
00284             forAllConstIter(labelHashSet, errorSets, iter)
00285             {
00286                 label setI = iter.key();
00287 
00288                 const labelList& setFaces = allFaceSets[setI];
00289                 const faceList& setFaceVerts = allFaceSetsFaces[setI];
00290 
00291                 label newMasterI = map().reverseFaceMap()[setFaces[0]];
00292 
00293                 // Restore. Get face properties.
00294 
00295                 label own = mesh.faceOwner()[newMasterI];
00296                 label zoneID = mesh.faceZones().whichZone(newMasterI);
00297                 bool zoneFlip = false;
00298                 if (zoneID >= 0)
00299                 {
00300                     const faceZone& fZone = mesh.faceZones()[zoneID];
00301                     zoneFlip = fZone.flipMap()[fZone.whichFace(newMasterI)];
00302                 }
00303                 label patchI = mesh.boundaryMesh().whichPatch(newMasterI);
00304 
00305 
00306                 Pout<< "Restoring new master face " << newMasterI
00307                     << " to vertices " << setFaceVerts[0] << endl;
00308 
00309                 // Modify the master face.
00310                 meshMod.setAction
00311                 (
00312                     polyModifyFace
00313                     (
00314                         setFaceVerts[0],                // original face
00315                         newMasterI,                     // label of face
00316                         own,                            // owner
00317                         -1,                             // neighbour
00318                         false,                          // face flip
00319                         patchI,                         // patch for face
00320                         false,                          // remove from zone
00321                         zoneID,                         // zone for face
00322                         zoneFlip                        // face flip in zone
00323                     )
00324                 );
00325 
00326 
00327                 // Add the previously removed faces
00328                 for (label i = 1; i < setFaces.size(); i++)
00329                 {
00330                     Pout<< "Restoring removed face " << setFaces[i]
00331                         << " with vertices " << setFaceVerts[i] << endl;
00332 
00333                     meshMod.setAction
00334                     (
00335                         polyAddFace
00336                         (
00337                             setFaceVerts[i],        // vertices
00338                             own,                    // owner,
00339                             -1,                     // neighbour,
00340                             -1,                     // masterPointID,
00341                             -1,                     // masterEdgeID,
00342                             newMasterI,             // masterFaceID,
00343                             false,                  // flipFaceFlux,
00344                             patchI,                 // patchID,
00345                             zoneID,                 // zoneID,
00346                             zoneFlip                // zoneFlip
00347                         )
00348                     );
00349                 }
00350             }
00351 
00352             // Change the mesh (no inflation)
00353             map = meshMod.changeMesh(mesh, false, true);
00354 
00355             // Update fields
00356             mesh.updateMesh(map);
00357 
00358             // Move mesh (since morphing does not do this)
00359             if (map().hasMotionPoints())
00360             {
00361                 mesh.movePoints(map().preMotionPoints());
00362             }
00363             else
00364             {
00365                 // Delete mesh volumes. No other way to do this?
00366                 mesh.clearOut();
00367             }
00368         }
00369     }
00370     else
00371     {
00372         Info<< "No faces merged ..." << endl;
00373     }
00374 
00375     return nFaceSets;
00376 }
00377 
00378 
00379 // Remove points not used by any face or points used by only two faces where
00380 // the edges are in line
00381 label mergeEdges(const scalar minCos, polyMesh& mesh)
00382 {
00383     Info<< "Merging all points on surface that" << nl
00384         << "- are used by only two boundary faces and" << nl
00385         << "- make an angle with a cosine of more than " << minCos
00386         << "." << nl << endl;
00387 
00388     // Point removal analysis engine
00389     removePoints pointRemover(mesh);
00390 
00391     // Count usage of points
00392     boolList pointCanBeDeleted;
00393     label nRemove = pointRemover.countPointUsage(minCos, pointCanBeDeleted);
00394 
00395     if (nRemove > 0)
00396     {
00397         Info<< "Removing " << nRemove
00398             << " straight edge points ..." << endl;
00399 
00400         // Topology changes container
00401         polyTopoChange meshMod(mesh);
00402 
00403         pointRemover.setRefinement(pointCanBeDeleted, meshMod);
00404 
00405         // Change the mesh (no inflation)
00406         autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, false, true);
00407 
00408         // Update fields
00409         mesh.updateMesh(map);
00410 
00411         // Move mesh (since morphing does not do this)
00412         if (map().hasMotionPoints())
00413         {
00414             mesh.movePoints(map().preMotionPoints());
00415         }
00416         else
00417         {
00418             // Delete mesh volumes. No other way to do this?
00419             mesh.clearOut();
00420         }
00421     }
00422     else
00423     {
00424         Info<< "No straight edges simplified and no points removed ..." << endl;
00425     }
00426 
00427     return nRemove;
00428 }
00429 
00430 
00431 // Main program:
00432 
00433 int main(int argc, char *argv[])
00434 {
00435     argList::validArgs.append("feature angle [0..180]");
00436     argList::validOptions.insert("concaveAngle", "[0..180]");
00437     argList::validOptions.insert("snapMesh", "");
00438     argList::validOptions.insert("overwrite", "");
00439 
00440 #   include "setRootCase.H"
00441 #   include "createTime.H"
00442     runTime.functionObjects().off();
00443 #   include "createPolyMesh.H"
00444     const word oldInstance = mesh.pointsInstance();
00445 
00446     scalar featureAngle(readScalar(IStringStream(args.additionalArgs()[0])()));
00447 
00448     scalar minCos = Foam::cos(featureAngle*mathematicalConstant::pi/180.0);
00449 
00450     scalar concaveAngle = defaultConcaveAngle;
00451     args.optionReadIfPresent("concaveAngle", concaveAngle);
00452 
00453     scalar concaveSin = Foam::sin(concaveAngle*mathematicalConstant::pi/180.0);
00454 
00455     bool snapMeshDict = args.optionFound("snapMesh");
00456     bool overwrite = args.optionFound("overwrite");
00457 
00458     Info<< "Merging all faces of a cell" << nl
00459         << "    - which are on the same patch" << nl
00460         << "    - which make an angle < " << featureAngle << " degrees"
00461         << nl
00462         << "      (cos:" << minCos << ')' << nl
00463         << "    - even when resulting face becomes concave by more than "
00464         << concaveAngle << " degrees" << nl
00465         << "      (sin:" << concaveSin << ')' << nl
00466         << endl;
00467 
00468     if (!overwrite)
00469     {
00470         runTime++;
00471     }
00472 
00473     // Merge faces on same patch
00474     label nChanged = mergePatchFaces
00475     (
00476         minCos,
00477         concaveSin,
00478         snapMeshDict,
00479         runTime,
00480         mesh
00481     );
00482 
00483     // Merge points on straight edges and remove unused points
00484     if (snapMeshDict)
00485     {
00486         Info<< "Merging all 'loose' points on surface edges"
00487             << ", regardless of the angle they make." << endl;
00488 
00489         // Surface bnound to be used to extrude. Merge all loose points.
00490         nChanged += mergeEdges(-1, mesh);
00491     }
00492     else
00493     {
00494         nChanged += mergeEdges(minCos, mesh);
00495     }
00496 
00497     if (nChanged > 0)
00498     {
00499         if (overwrite)
00500         {
00501             mesh.setInstance(oldInstance);
00502         }
00503 
00504         Info<< "Writing morphed mesh to time " << runTime.timeName() << endl;
00505 
00506         mesh.write();
00507     }
00508     else
00509     {
00510         Info<< "Mesh unchanged." << endl;
00511     }
00512 
00513     Info << "End\n" << endl;
00514 
00515     return 0;
00516 }
00517 
00518 
00519 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd