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

modifyMesh.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     Manipulates mesh elements.
00027 
00028     Actions are:
00029         (boundary)points:
00030             - move
00031 
00032         (boundary)edges:
00033             - split and move introduced point
00034 
00035         (boundary)faces:
00036             - split(triangulate) and move introduced point
00037 
00038         edges:
00039             - collapse
00040 
00041         cells:
00042             - split into polygonal base pyramids around newly introduced mid
00043               point
00044 
00045     Is a bit of a loose collection of mesh change drivers.
00046 
00047 \*---------------------------------------------------------------------------*/
00048 
00049 #include "argList.H"
00050 #include "Time.H"
00051 #include "polyMesh.H"
00052 #include "polyTopoChange.H"
00053 #include "mapPolyMesh.H"
00054 #include "boundaryCutter.H"
00055 #include "cellSplitter.H"
00056 #include "edgeCollapser.H"
00057 #include "meshTools.H"
00058 #include "Pair.H"
00059 
00060 using namespace Foam;
00061 
00062 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00063 
00064 // Locate point on patch. Returns (mesh) point label.
00065 label findPoint(const primitivePatch& pp, const point& nearPoint)
00066 {
00067     const pointField& points = pp.points();
00068     const labelList& meshPoints = pp.meshPoints();
00069 
00070     // Find nearest and next nearest
00071     scalar minDistSqr = GREAT;
00072     label minI = -1;
00073 
00074     scalar almostMinDistSqr = GREAT;
00075     label almostMinI = -1;
00076 
00077     forAll(meshPoints, i)
00078     {
00079         label pointI = meshPoints[i];
00080 
00081         scalar distSqr = magSqr(nearPoint - points[pointI]);
00082 
00083         if (distSqr < minDistSqr)
00084         {
00085             almostMinDistSqr = minDistSqr;
00086             almostMinI = minI;
00087 
00088             minDistSqr = distSqr;
00089             minI = pointI;
00090         }
00091         else if (distSqr < almostMinDistSqr)
00092         {
00093             almostMinDistSqr = distSqr;
00094             almostMinI = pointI;
00095         }
00096     }
00097 
00098 
00099     // Decide if nearPoint unique enough.
00100     Info<< "Found to point " << nearPoint << nl
00101         << "    nearest point      : " << minI
00102         << " distance " <<  Foam::sqrt(minDistSqr)
00103         << " at " << points[minI] << nl
00104         << "    next nearest point : " << almostMinI
00105         << " distance " <<  Foam::sqrt(almostMinDistSqr)
00106         << " at " << points[almostMinI] << endl;
00107 
00108     if (almostMinDistSqr < 4*minDistSqr)
00109     {
00110         Info<< "Next nearest too close to nearest. Aborting" << endl;
00111 
00112         return -1;
00113     }
00114     else
00115     {
00116         return minI;
00117     }
00118 }
00119 
00120 
00121 // Locate edge on patch. Return mesh edge label.
00122 label findEdge
00123 (
00124     const primitiveMesh& mesh,
00125     const primitivePatch& pp,
00126     const point& nearPoint
00127 )
00128 {
00129     const pointField& localPoints = pp.localPoints();
00130     const pointField& points = pp.points();
00131     const labelList& meshPoints = pp.meshPoints();
00132     const edgeList& edges = pp.edges();
00133 
00134     // Find nearest and next nearest
00135     scalar minDist = GREAT;
00136     label minI = -1;
00137 
00138     scalar almostMinDist = GREAT;
00139     label almostMinI = -1;
00140 
00141     forAll(edges, edgeI)
00142     {
00143         const edge& e = edges[edgeI];
00144 
00145         pointHit pHit(e.line(localPoints).nearestDist(nearPoint));
00146 
00147         if (pHit.hit())
00148         {
00149             if (pHit.distance() < minDist)
00150             {
00151                 almostMinDist = minDist;
00152                 almostMinI = minI;
00153 
00154                 minDist = pHit.distance();
00155                 minI = meshTools::findEdge
00156                 (
00157                     mesh,
00158                     meshPoints[e[0]],
00159                     meshPoints[e[1]]
00160                 );
00161             }
00162             else if (pHit.distance() < almostMinDist)
00163             {
00164                 almostMinDist = pHit.distance();
00165                 almostMinI = meshTools::findEdge
00166                 (
00167                     mesh,
00168                     meshPoints[e[0]],
00169                     meshPoints[e[1]]
00170                 );
00171             }
00172         }
00173     }
00174 
00175     if (minI == -1)
00176     {
00177         Info<< "Did not find edge close to point " << nearPoint << endl;
00178 
00179         return -1;
00180     }
00181 
00182 
00183     // Decide if nearPoint unique enough.
00184     Info<< "Found to point " << nearPoint << nl
00185         << "    nearest edge      : " << minI
00186         << " distance " << minDist << " endpoints "
00187         << mesh.edges()[minI].line(points) << nl
00188         << "    next nearest edge : " << almostMinI
00189         << " distance " << almostMinDist << " endpoints "
00190         << mesh.edges()[almostMinI].line(points) << nl
00191         << endl;
00192 
00193     if (almostMinDist < 2*minDist)
00194     {
00195         Info<< "Next nearest too close to nearest. Aborting" << endl;
00196 
00197         return -1;
00198     }
00199     else
00200     {
00201         return minI;
00202     }
00203 }
00204 
00205 
00206 // Find face on patch. Return mesh face label.
00207 label findFace
00208 (
00209     const primitiveMesh& mesh,
00210     const primitivePatch& pp,
00211     const point& nearPoint
00212 )
00213 {
00214     const pointField& points = pp.points();
00215 
00216     // Find nearest and next nearest
00217     scalar minDist = GREAT;
00218     label minI = -1;
00219 
00220     scalar almostMinDist = GREAT;
00221     label almostMinI = -1;
00222 
00223     forAll(pp, patchFaceI)
00224     {
00225         pointHit pHit(pp[patchFaceI].nearestPoint(nearPoint, points));
00226 
00227         if (pHit.hit())
00228         {
00229             if (pHit.distance() < minDist)
00230             {
00231                 almostMinDist = minDist;
00232                 almostMinI = minI;
00233 
00234                 minDist = pHit.distance();
00235                 minI = patchFaceI + mesh.nInternalFaces();
00236             }
00237             else if (pHit.distance() < almostMinDist)
00238             {
00239                 almostMinDist = pHit.distance();
00240                 almostMinI = patchFaceI + mesh.nInternalFaces();
00241             }
00242         }
00243     }
00244 
00245     if (minI == -1)
00246     {
00247         Info<< "Did not find face close to point " << nearPoint << endl;
00248 
00249         return -1;
00250     }
00251 
00252 
00253     // Decide if nearPoint unique enough.
00254     Info<< "Found to point " << nearPoint << nl
00255         << "    nearest face      : " << minI
00256         << " distance " << minDist
00257         << " to face centre " << mesh.faceCentres()[minI] << nl
00258         << "    next nearest face : " << almostMinI
00259         << " distance " << almostMinDist
00260         << " to face centre " << mesh.faceCentres()[almostMinI] << nl
00261         << endl;
00262 
00263     if (almostMinDist < 2*minDist)
00264     {
00265         Info<< "Next nearest too close to nearest. Aborting" << endl;
00266 
00267         return -1;
00268     }
00269     else
00270     {
00271         return minI;
00272     }
00273 }
00274 
00275 
00276 // Find cell with cell centre close to given point.
00277 label findCell(const primitiveMesh& mesh, const point& nearPoint)
00278 {
00279     label cellI = mesh.findCell(nearPoint);
00280 
00281     if (cellI != -1)
00282     {
00283         scalar distToCcSqr = magSqr(nearPoint - mesh.cellCentres()[cellI]);
00284 
00285         const labelList& cPoints = mesh.cellPoints()[cellI];
00286 
00287         label minI = -1;
00288         scalar minDistSqr = GREAT;
00289 
00290         forAll(cPoints, i)
00291         {
00292             label pointI = cPoints[i];
00293 
00294             scalar distSqr = magSqr(nearPoint - mesh.points()[pointI]);
00295 
00296             if (distSqr < minDistSqr)
00297             {
00298                 minDistSqr = distSqr;
00299                 minI = pointI;
00300             }
00301         }
00302 
00303         // Decide if nearPoint unique enough.
00304         Info<< "Found to point " << nearPoint << nl
00305             << "    nearest cell       : " << cellI
00306             << " distance " << Foam::sqrt(distToCcSqr)
00307             << " to cell centre " << mesh.cellCentres()[cellI] << nl
00308             << "    nearest mesh point : " << minI
00309             << " distance " << Foam::sqrt(minDistSqr)
00310             << " to " << mesh.points()[minI] << nl
00311             << endl;
00312 
00313         if (minDistSqr < 4*distToCcSqr)
00314         {
00315             Info<< "Mesh point too close to nearest cell centre. Aborting"
00316                 << endl;
00317 
00318             cellI = -1;
00319         }
00320     }
00321 
00322     return cellI;
00323 }
00324 
00325 
00326 
00327 // Main program:
00328 
00329 int main(int argc, char *argv[])
00330 {
00331     argList::validOptions.insert("overwrite", "");
00332 
00333 #   include "setRootCase.H"
00334 #   include "createTime.H"
00335     runTime.functionObjects().off();
00336 #   include "createPolyMesh.H"
00337     const word oldInstance = mesh.pointsInstance();
00338 
00339     bool overwrite = args.optionFound("overwrite");
00340 
00341     Info<< "Reading modifyMeshDict\n" << endl;
00342 
00343     IOdictionary dict
00344     (
00345         IOobject
00346         (
00347             "modifyMeshDict",
00348             runTime.system(),
00349             mesh,
00350             IOobject::MUST_READ,
00351             IOobject::NO_WRITE
00352         )
00353     );
00354 
00355     // Read all from the dictionary.
00356     List<Pair<point> > pointsToMove(dict.lookup("pointsToMove"));
00357     List<Pair<point> > edgesToSplit(dict.lookup("edgesToSplit"));
00358     List<Pair<point> > facesToTriangulate
00359     (
00360         dict.lookup("facesToTriangulate")
00361     );
00362 
00363     bool cutBoundary =
00364     (
00365         pointsToMove.size()
00366      || edgesToSplit.size()
00367      || facesToTriangulate.size()
00368     );
00369 
00370     List<Pair<point> > edgesToCollapse(dict.lookup("edgesToCollapse"));
00371 
00372     bool collapseEdge = edgesToCollapse.size();
00373 
00374     List<Pair<point> > cellsToPyramidise(dict.lookup("cellsToSplit"));
00375 
00376     bool cellsToSplit = cellsToPyramidise.size();
00377 
00378     //List<Tuple<pointField,point> >
00379     //  cellsToCreate(dict.lookup("cellsToCreate"));
00380 
00381     Info<< "Read from " << dict.name() << nl
00382         << "  Boundary cutting module:" << nl
00383         << "    points to move      :" << pointsToMove.size() << nl
00384         << "    edges to split      :" << edgesToSplit.size() << nl
00385         << "    faces to triangulate:" << facesToTriangulate.size() << nl
00386         << "  Cell splitting module:" << nl
00387         << "    cells to split      :" << cellsToPyramidise.size() << nl
00388         << "  Edge collapsing module:" << nl
00389         << "    edges to collapse   :" << edgesToCollapse.size() << nl
00390         //<< "    cells to create     :" << cellsToCreate.size() << nl
00391         << endl;
00392 
00393     if
00394     (
00395         (cutBoundary && collapseEdge)
00396      || (cutBoundary && cellsToSplit)
00397      || (collapseEdge && cellsToSplit)
00398     )
00399     {
00400         FatalErrorIn(args.executable())
00401             << "Used more than one mesh modifying module "
00402             << "(boundary cutting, cell splitting, edge collapsing)" << nl
00403             << "Please do them in separate passes." << exit(FatalError);
00404     }
00405 
00406 
00407 
00408     // Get calculating engine for all of outside
00409     const SubList<face> outsideFaces
00410     (
00411         mesh.faces(),
00412         mesh.nFaces() - mesh.nInternalFaces(),
00413         mesh.nInternalFaces()
00414     );
00415 
00416     primitivePatch allBoundary(outsideFaces, mesh.points());
00417 
00418 
00419     // Look up mesh labels and convert to input for boundaryCutter.
00420 
00421     bool validInputs = true;
00422 
00423 
00424     Info<< nl << "Looking up points to move ..." << nl << endl;
00425     Map<point> pointToPos(pointsToMove.size());
00426     forAll(pointsToMove, i)
00427     {
00428         const Pair<point>& pts = pointsToMove[i];
00429 
00430         label pointI = findPoint(allBoundary, pts.first());
00431 
00432         if (pointI == -1 || !pointToPos.insert(pointI, pts.second()))
00433         {
00434             Info<< "Could not insert mesh point " << pointI
00435                 << " for input point " << pts.first() << nl
00436                 << "Perhaps the point is already marked for moving?" << endl;
00437             validInputs = false;
00438         }
00439     }
00440 
00441 
00442     Info<< nl << "Looking up edges to split ..." << nl << endl;
00443     Map<List<point> > edgeToCuts(edgesToSplit.size());
00444     forAll(edgesToSplit, i)
00445     {
00446         const Pair<point>& pts = edgesToSplit[i];
00447 
00448         label edgeI = findEdge(mesh, allBoundary, pts.first());
00449 
00450         if
00451         (
00452             edgeI == -1
00453         || !edgeToCuts.insert(edgeI, List<point>(1, pts.second()))
00454         )
00455         {
00456             Info<< "Could not insert mesh edge " << edgeI
00457                 << " for input point " << pts.first() << nl
00458                 << "Perhaps the edge is already marked for cutting?" << endl;
00459 
00460             validInputs = false;
00461         }
00462     }
00463 
00464 
00465     Info<< nl << "Looking up faces to triangulate ..." << nl << endl;
00466     Map<point> faceToDecompose(facesToTriangulate.size());
00467     forAll(facesToTriangulate, i)
00468     {
00469         const Pair<point>& pts = facesToTriangulate[i];
00470 
00471         label faceI = findFace(mesh, allBoundary, pts.first());
00472 
00473         if (faceI == -1 || !faceToDecompose.insert(faceI, pts.second()))
00474         {
00475             Info<< "Could not insert mesh face " << faceI
00476                 << " for input point " << pts.first() << nl
00477                 << "Perhaps the face is already marked for splitting?" << endl;
00478 
00479             validInputs = false;
00480         }
00481     }
00482 
00483 
00484 
00485     Info<< nl << "Looking up cells to convert to pyramids around"
00486         << " cell centre ..." << nl << endl;
00487     Map<point> cellToPyrCentre(cellsToPyramidise.size());
00488     forAll(cellsToPyramidise, i)
00489     {
00490         const Pair<point>& pts = cellsToPyramidise[i];
00491 
00492         label cellI = findCell(mesh, pts.first());
00493 
00494         if (cellI == -1 || !cellToPyrCentre.insert(cellI, pts.second()))
00495         {
00496             Info<< "Could not insert mesh cell " << cellI
00497                 << " for input point " << pts.first() << nl
00498                 << "Perhaps the cell is already marked for splitting?" << endl;
00499 
00500             validInputs = false;
00501         }
00502     }
00503 
00504 
00505     Info<< nl << "Looking up edges to collapse ..." << nl << endl;
00506     Map<point> edgeToPos(edgesToCollapse.size());
00507     forAll(edgesToCollapse, i)
00508     {
00509         const Pair<point>& pts = edgesToCollapse[i];
00510 
00511         label edgeI = findEdge(mesh, allBoundary, pts.first());
00512 
00513         if (edgeI == -1 || !edgeToPos.insert(edgeI, pts.second()))
00514         {
00515             Info<< "Could not insert mesh edge " << edgeI
00516                 << " for input point " << pts.first() << nl
00517                 << "Perhaps the edge is already marked for collaping?" << endl;
00518 
00519             validInputs = false;
00520         }
00521     }
00522 
00523 
00524 
00525     if (!validInputs)
00526     {
00527         Info<< nl << "There was a problem in one of the inputs in the"
00528             << " dictionary. Not modifying mesh." << endl;
00529     }
00530     else if (cellToPyrCentre.size())
00531     {
00532         Info<< nl << "All input cells located. Modifying mesh." << endl;
00533 
00534         // Mesh change engine
00535         cellSplitter cutter(mesh);
00536 
00537         // Topo change container
00538         polyTopoChange meshMod(mesh);
00539 
00540         // Insert commands into meshMod
00541         cutter.setRefinement(cellToPyrCentre, meshMod);
00542 
00543         // Do changes
00544         autoPtr<mapPolyMesh> morphMap = meshMod.changeMesh(mesh, false);
00545 
00546         if (morphMap().hasMotionPoints())
00547         {
00548             mesh.movePoints(morphMap().preMotionPoints());
00549         }
00550 
00551         cutter.updateMesh(morphMap());
00552 
00553         if (!overwrite)
00554         {
00555             runTime++;
00556         }
00557         else
00558         {
00559             mesh.setInstance(oldInstance);
00560         }
00561 
00562         // Write resulting mesh
00563         Info << "Writing modified mesh to time " << runTime.timeName() << endl;
00564         mesh.write();
00565     }
00566     else if (edgeToPos.size())
00567     {
00568         Info<< nl << "All input edges located. Modifying mesh." << endl;
00569 
00570         // Mesh change engine
00571         edgeCollapser cutter(mesh);
00572 
00573         pointField newPoints(mesh.points());
00574 
00575         // Get new positions and construct collapse network
00576         forAllConstIter(Map<point>, edgeToPos, iter)
00577         {
00578             label edgeI = iter.key();
00579             const edge& e = mesh.edges()[edgeI];
00580 
00581             cutter.collapseEdge(edgeI, e[0]);
00582             newPoints[e[0]] = iter();
00583         }
00584 
00585         // Move master point to destination.
00586         mesh.movePoints(newPoints);
00587 
00588         // Topo change container
00589         polyTopoChange meshMod(mesh);
00590 
00591         // Insert
00592         cutter.setRefinement(meshMod);
00593 
00594         // Do changes
00595         autoPtr<mapPolyMesh> morphMap = meshMod.changeMesh(mesh, false);
00596 
00597         if (morphMap().hasMotionPoints())
00598         {
00599             mesh.movePoints(morphMap().preMotionPoints());
00600         }
00601 
00602         // Not implemented yet:
00603         //cutter.updateMesh(morphMap());
00604 
00605 
00606         if (!overwrite)
00607         {
00608             runTime++;
00609         }
00610         else
00611         {
00612             mesh.setInstance(oldInstance);
00613         }
00614 
00615         // Write resulting mesh
00616         Info << "Writing modified mesh to time " << runTime.timeName() << endl;
00617         mesh.write();
00618     }
00619     else
00620     {
00621         Info<< nl << "All input points located. Modifying mesh." << endl;
00622 
00623         // Mesh change engine
00624         boundaryCutter cutter(mesh);
00625 
00626         // Topo change container
00627         polyTopoChange meshMod(mesh);
00628 
00629         // Insert commands into meshMod
00630         cutter.setRefinement
00631         (
00632             pointToPos,
00633             edgeToCuts,
00634             Map<labelPair>(0),  // Faces to split diagonally
00635             faceToDecompose,    // Faces to triangulate
00636             meshMod
00637         );
00638 
00639         // Do changes
00640         autoPtr<mapPolyMesh> morphMap = meshMod.changeMesh(mesh, false);
00641 
00642         if (morphMap().hasMotionPoints())
00643         {
00644             mesh.movePoints(morphMap().preMotionPoints());
00645         }
00646 
00647         cutter.updateMesh(morphMap());
00648 
00649         if (!overwrite)
00650         {
00651             runTime++;
00652         }
00653         else
00654         {
00655             mesh.setInstance(oldInstance);
00656         }
00657 
00658         // Write resulting mesh
00659         Info << "Writing modified mesh to time " << runTime.timeName() << endl;
00660         mesh.write();
00661     }
00662 
00663 
00664     Info << nl << "End" << endl;
00665 
00666     return 0;
00667 }
00668 
00669 
00670 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd