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

polyDualMeshApp.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     Calculate the dual of a polyMesh. Adheres to all the feature and patch
00027     edges.
00028 
00029 Usage
00030 
00031     - polyDualMesh featureAngle
00032 
00033     Detects any boundary edge > angle and creates multiple boundary faces
00034     for it. Normal behaviour is to have each point become a cell
00035     (1.5 behaviour)
00036 
00037     @param -concaveMultiCells
00038     Creates multiple cells for each point on a concave edge. Might limit
00039     the amount of distortion on some meshes.
00040 
00041     @param -splitAllFaces
00042     Normally only constructs a single face between two cells. This single face
00043     might be too distorted. splitAllFaces will create a single face for every
00044     original cell the face passes through. The mesh will thus have
00045     multiple faces inbetween two cells! (so is not strictly upper-triangular
00046     anymore - checkMesh will complain)
00047 
00048     @param -doNotPreserveFaceZones:
00049     By default all faceZones are preserved by marking all faces, edges and
00050     points on them as features. The -doNotPreserveFaceZones disables this
00051     behaviour.
00052 
00053     Note: is just a driver for meshDualiser. Substitute your own
00054     simpleMarkFeatures to have different behaviour.
00055 
00056 \*---------------------------------------------------------------------------*/
00057 
00058 #include "argList.H"
00059 #include "Time.H"
00060 #include "timeSelector.H"
00061 #include "fvMesh.H"
00062 #include "mathematicalConstants.H"
00063 #include "polyTopoChange.H"
00064 #include "mapPolyMesh.H"
00065 #include "PackedBoolList.H"
00066 #include "meshTools.H"
00067 #include "OFstream.H"
00068 #include "meshDualiser.H"
00069 #include "ReadFields.H"
00070 #include "volFields.H"
00071 #include "surfaceFields.H"
00072 
00073 using namespace Foam;
00074 
00075 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00076 
00077 // Naive feature detection. All boundary edges with angle > featureAngle become
00078 // feature edges. All points on feature edges become feature points. All
00079 // boundary faces become feature faces.
00080 void simpleMarkFeatures
00081 (
00082     const polyMesh& mesh,
00083     const PackedBoolList& isBoundaryEdge,
00084     const scalar featureAngle,
00085     const bool concaveMultiCells,
00086     const bool doNotPreserveFaceZones,
00087 
00088     labelList& featureFaces,
00089     labelList& featureEdges,
00090     labelList& singleCellFeaturePoints,
00091     labelList& multiCellFeaturePoints
00092 )
00093 {
00094     scalar minCos = Foam::cos(featureAngle * mathematicalConstant::pi/180.0);
00095 
00096     const polyBoundaryMesh& patches = mesh.boundaryMesh();
00097 
00098     // Working sets
00099     labelHashSet featureEdgeSet;
00100     labelHashSet singleCellFeaturePointSet;
00101     labelHashSet multiCellFeaturePointSet;
00102 
00103 
00104     // 1. Mark all edges between patches
00105     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00106 
00107     forAll(patches, patchI)
00108     {
00109         const polyPatch& pp = patches[patchI];
00110         const labelList& meshEdges = pp.meshEdges();
00111 
00112         // All patch corner edges. These need to be feature points & edges!
00113         for (label edgeI = pp.nInternalEdges(); edgeI < pp.nEdges(); edgeI++)
00114         {
00115             label meshEdgeI = meshEdges[edgeI];
00116             featureEdgeSet.insert(meshEdgeI);
00117             singleCellFeaturePointSet.insert(mesh.edges()[meshEdgeI][0]);
00118             singleCellFeaturePointSet.insert(mesh.edges()[meshEdgeI][1]);
00119         }
00120     }
00121 
00122 
00123 
00124     // 2. Mark all geometric feature edges
00125     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00126     // Make distinction between convex features where the boundary point becomes
00127     // a single cell and concave features where the boundary point becomes
00128     // multiple 'half' cells.
00129 
00130     // Addressing for all outside faces
00131     primitivePatch allBoundary
00132     (
00133         SubList<face>
00134         (
00135             mesh.faces(),
00136             mesh.nFaces()-mesh.nInternalFaces(),
00137             mesh.nInternalFaces()
00138         ),
00139         mesh.points()
00140     );
00141 
00142     // Check for non-manifold points (surface pinched at point)
00143     allBoundary.checkPointManifold(false, &singleCellFeaturePointSet);
00144 
00145     // Check for non-manifold edges (surface pinched at edge)
00146     const labelListList& edgeFaces = allBoundary.edgeFaces();
00147     const labelList& meshPoints = allBoundary.meshPoints();
00148 
00149     forAll(edgeFaces, edgeI)
00150     {
00151         const labelList& eFaces = edgeFaces[edgeI];
00152 
00153         if (eFaces.size() > 2)
00154         {
00155             const edge& e = allBoundary.edges()[edgeI];
00156 
00157             //Info<< "Detected non-manifold boundary edge:" << edgeI
00158             //    << " coords:"
00159             //    << allBoundary.points()[meshPoints[e[0]]]
00160             //    << allBoundary.points()[meshPoints[e[1]]] << endl;
00161 
00162             singleCellFeaturePointSet.insert(meshPoints[e[0]]);
00163             singleCellFeaturePointSet.insert(meshPoints[e[1]]);
00164         }
00165     }
00166 
00167     // Check for features.
00168     forAll(edgeFaces, edgeI)
00169     {
00170         const labelList& eFaces = edgeFaces[edgeI];
00171 
00172         if (eFaces.size() == 2)
00173         {
00174             label f0 = eFaces[0];
00175             label f1 = eFaces[1];
00176 
00177             // check angle
00178             const vector& n0 = allBoundary.faceNormals()[f0];
00179             const vector& n1 = allBoundary.faceNormals()[f1];
00180 
00181             if ((n0 & n1) < minCos)
00182             {
00183                 const edge& e = allBoundary.edges()[edgeI];
00184                 label v0 = meshPoints[e[0]];
00185                 label v1 = meshPoints[e[1]];
00186 
00187                 label meshEdgeI = meshTools::findEdge(mesh, v0, v1);
00188                 featureEdgeSet.insert(meshEdgeI);
00189 
00190                 // Check if convex or concave by looking at angle
00191                 // between face centres and normal
00192                 vector c1c0
00193                 (
00194                     allBoundary[f1].centre(allBoundary.points())
00195                   - allBoundary[f0].centre(allBoundary.points())
00196                 );
00197 
00198                 if (concaveMultiCells && (c1c0 & n0) > SMALL)
00199                 {
00200                     // Found concave edge. Make into multiCell features
00201                     Info<< "Detected concave feature edge:" << edgeI
00202                         << " cos:" << (c1c0 & n0)
00203                         << " coords:"
00204                         << allBoundary.points()[v0]
00205                         << allBoundary.points()[v1]
00206                         << endl;
00207 
00208                     singleCellFeaturePointSet.erase(v0);
00209                     multiCellFeaturePointSet.insert(v0);
00210                     singleCellFeaturePointSet.erase(v1);
00211                     multiCellFeaturePointSet.insert(v1);
00212                 }
00213                 else
00214                 {
00215                     // Convex. singleCell feature.
00216                     if (!multiCellFeaturePointSet.found(v0))
00217                     {
00218                         singleCellFeaturePointSet.insert(v0);
00219                     }
00220                     if (!multiCellFeaturePointSet.found(v1))
00221                     {
00222                         singleCellFeaturePointSet.insert(v1);
00223                     }
00224                 }
00225             }
00226         }
00227     }
00228 
00229 
00230     // 3. Mark all feature faces
00231     // ~~~~~~~~~~~~~~~~~~~~~~~~~
00232 
00233     // Face centres that need inclusion in the dual mesh
00234     labelHashSet featureFaceSet(mesh.nFaces()-mesh.nInternalFaces());
00235     // A. boundary faces.
00236     for (label faceI = mesh.nInternalFaces(); faceI < mesh.nFaces(); faceI++)
00237     {
00238         featureFaceSet.insert(faceI);
00239     }
00240 
00241     // B. face zones.
00242     const faceZoneMesh& faceZones = mesh.faceZones();
00243 
00244     if (doNotPreserveFaceZones)
00245     {
00246         if (faceZones.size() > 0)
00247         {
00248             WarningIn("simpleMarkFeatures(..)")
00249                 << "Detected " << faceZones.size()
00250                 << " faceZones. These will not be preserved."
00251                 << endl;
00252         }
00253     }
00254     else
00255     {
00256         if (faceZones.size() > 0)
00257         {
00258             Info<< "Detected " << faceZones.size()
00259                 << " faceZones. Preserving these by marking their"
00260                 << " points, edges and faces as features." << endl;
00261         }
00262 
00263         forAll(faceZones, zoneI)
00264         {
00265             const faceZone& fz = faceZones[zoneI];
00266 
00267             Info<< "Inserting all faces in faceZone " << fz.name()
00268                 << " as features." << endl;
00269 
00270             forAll(fz, i)
00271             {
00272                 label faceI = fz[i];
00273                 const face& f = mesh.faces()[faceI];
00274                 const labelList& fEdges = mesh.faceEdges()[faceI];
00275 
00276                 featureFaceSet.insert(faceI);
00277                 forAll(f, fp)
00278                 {
00279                     // Mark point as multi cell point (since both sides of
00280                     // face should have different cells)
00281                     singleCellFeaturePointSet.erase(f[fp]);
00282                     multiCellFeaturePointSet.insert(f[fp]);
00283 
00284                     // Make sure there are points on the edges.
00285                     featureEdgeSet.insert(fEdges[fp]);
00286                 }
00287             }
00288         }
00289     }
00290 
00291     // Transfer to arguments
00292     featureFaces = featureFaceSet.toc();
00293     featureEdges = featureEdgeSet.toc();
00294     singleCellFeaturePoints = singleCellFeaturePointSet.toc();
00295     multiCellFeaturePoints = multiCellFeaturePointSet.toc();
00296 }
00297 
00298 
00299 // Dump features to .obj files
00300 void dumpFeatures
00301 (
00302     const polyMesh& mesh,
00303     const labelList& featureFaces,
00304     const labelList& featureEdges,
00305     const labelList& singleCellFeaturePoints,
00306     const labelList& multiCellFeaturePoints
00307 )
00308 {
00309     {
00310         OFstream str("featureFaces.obj");
00311         Info<< "Dumping centres of featureFaces to obj file " << str.name()
00312             << endl;
00313         forAll(featureFaces, i)
00314         {
00315             meshTools::writeOBJ(str, mesh.faceCentres()[featureFaces[i]]);
00316         }
00317     }
00318     {
00319         OFstream str("featureEdges.obj");
00320         Info<< "Dumping featureEdges to obj file " << str.name() << endl;
00321         label vertI = 0;
00322 
00323         forAll(featureEdges, i)
00324         {
00325             const edge& e = mesh.edges()[featureEdges[i]];
00326             meshTools::writeOBJ(str, mesh.points()[e[0]]);
00327             vertI++;
00328             meshTools::writeOBJ(str, mesh.points()[e[1]]);
00329             vertI++;
00330             str<< "l " << vertI-1 << ' ' << vertI << nl;
00331         }
00332     }
00333     {
00334         OFstream str("singleCellFeaturePoints.obj");
00335         Info<< "Dumping featurePoints that become a single cell to obj file "
00336             << str.name() << endl;
00337         forAll(singleCellFeaturePoints, i)
00338         {
00339             meshTools::writeOBJ(str, mesh.points()[singleCellFeaturePoints[i]]);
00340         }
00341     }
00342     {
00343         OFstream str("multiCellFeaturePoints.obj");
00344         Info<< "Dumping featurePoints that become multiple cells to obj file "
00345             << str.name() << endl;
00346         forAll(multiCellFeaturePoints, i)
00347         {
00348             meshTools::writeOBJ(str, mesh.points()[multiCellFeaturePoints[i]]);
00349         }
00350     }
00351 }
00352 
00353 
00354 int main(int argc, char *argv[])
00355 {
00356     argList::noParallel();
00357     timeSelector::addOptions(true, false);
00358 
00359     argList::validArgs.append("feature angle[0-180]");
00360     argList::validOptions.insert("splitAllFaces", "");
00361     argList::validOptions.insert("concaveMultiCells", "");
00362     argList::validOptions.insert("doNotPreserveFaceZones", "");
00363     argList::validOptions.insert("overwrite", "");
00364 
00365 #   include "setRootCase.H"
00366 #   include "createTime.H"
00367 
00368     instantList timeDirs = timeSelector::select0(runTime, args);
00369 
00370 #   include "createMesh.H"
00371 
00372     const word oldInstance = mesh.pointsInstance();
00373 
00374     // Mark boundary edges and points.
00375     // (Note: in 1.4.2 we can use the built-in mesh point ordering
00376     //  facility instead)
00377     PackedBoolList isBoundaryEdge(mesh.nEdges());
00378     for (label faceI = mesh.nInternalFaces(); faceI < mesh.nFaces(); faceI++)
00379     {
00380         const labelList& fEdges = mesh.faceEdges()[faceI];
00381 
00382         forAll(fEdges, i)
00383         {
00384             isBoundaryEdge.set(fEdges[i], 1);
00385         }
00386     }
00387 
00388     scalar featureAngle(readScalar(IStringStream(args.additionalArgs()[0])()));
00389 
00390     scalar minCos = Foam::cos(featureAngle * mathematicalConstant::pi/180.0);
00391 
00392     Info<< "Feature:" << featureAngle << endl
00393         << "minCos :" << minCos << endl
00394         << endl;
00395 
00396 
00397     const bool splitAllFaces = args.optionFound("splitAllFaces");
00398     if (splitAllFaces)
00399     {
00400         Info<< "Splitting all internal faces to create multiple faces"
00401             << " between two cells." << nl
00402             << endl;
00403     }
00404 
00405     const bool overwrite = args.optionFound("overwrite");
00406     const bool doNotPreserveFaceZones = args.optionFound
00407     (
00408         "doNotPreserveFaceZones"
00409     );
00410     const bool concaveMultiCells = args.optionFound("concaveMultiCells");
00411     if (concaveMultiCells)
00412     {
00413         Info<< "Generating multiple cells for points on concave feature edges."
00414             << nl << endl;
00415     }
00416 
00417 
00418     // Face(centre)s that need inclusion in the dual mesh
00419     labelList featureFaces;
00420     // Edge(centre)s  ,,
00421     labelList featureEdges;
00422     // Points (that become a single cell) that need inclusion in the dual mesh
00423     labelList singleCellFeaturePoints;
00424     // Points (that become a multiple cells)        ,,
00425     labelList multiCellFeaturePoints;
00426 
00427     // Sample implementation of feature detection.
00428     simpleMarkFeatures
00429     (
00430         mesh,
00431         isBoundaryEdge,
00432         featureAngle,
00433         concaveMultiCells,
00434         doNotPreserveFaceZones,
00435 
00436         featureFaces,
00437         featureEdges,
00438         singleCellFeaturePoints,
00439         multiCellFeaturePoints
00440     );
00441 
00442     // If we want to split all polyMesh faces into one dualface per cell
00443     // we are passing through we also need a point
00444     // at the polyMesh facecentre and edgemid of the faces we want to
00445     // split.
00446     if (splitAllFaces)
00447     {
00448         featureEdges = identity(mesh.nEdges());
00449         featureFaces = identity(mesh.nFaces());
00450     }
00451 
00452     // Write obj files for debugging
00453     dumpFeatures
00454     (
00455         mesh,
00456         featureFaces,
00457         featureEdges,
00458         singleCellFeaturePoints,
00459         multiCellFeaturePoints
00460     );
00461 
00462 
00463 
00464     // Read objects in time directory
00465     IOobjectList objects(mesh, runTime.timeName());
00466 
00467     // Read vol fields.
00468     PtrList<volScalarField> vsFlds;
00469     ReadFields(mesh, objects, vsFlds);
00470 
00471     PtrList<volVectorField> vvFlds;
00472     ReadFields(mesh, objects, vvFlds);
00473 
00474     PtrList<volSphericalTensorField> vstFlds;
00475     ReadFields(mesh, objects, vstFlds);
00476 
00477     PtrList<volSymmTensorField> vsymtFlds;
00478     ReadFields(mesh, objects, vsymtFlds);
00479 
00480     PtrList<volTensorField> vtFlds;
00481     ReadFields(mesh, objects, vtFlds);
00482 
00483     // Read surface fields.
00484     PtrList<surfaceScalarField> ssFlds;
00485     ReadFields(mesh, objects, ssFlds);
00486 
00487     PtrList<surfaceVectorField> svFlds;
00488     ReadFields(mesh, objects, svFlds);
00489 
00490     PtrList<surfaceSphericalTensorField> sstFlds;
00491     ReadFields(mesh, objects, sstFlds);
00492 
00493     PtrList<surfaceSymmTensorField> ssymtFlds;
00494     ReadFields(mesh, objects, ssymtFlds);
00495 
00496     PtrList<surfaceTensorField> stFlds;
00497     ReadFields(mesh, objects, stFlds);
00498 
00499 
00500     // Topo change container
00501     polyTopoChange meshMod(mesh.boundaryMesh().size());
00502 
00503     // Mesh dualiser engine
00504     meshDualiser dualMaker(mesh);
00505 
00506     // Insert all commands into polyTopoChange to create dual of mesh. This does
00507     // all the hard work.
00508     dualMaker.setRefinement
00509     (
00510         splitAllFaces,
00511         featureFaces,
00512         featureEdges,
00513         singleCellFeaturePoints,
00514         multiCellFeaturePoints,
00515         meshMod
00516     );
00517 
00518     // Create mesh, return map from old to new mesh.
00519     autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, false);
00520 
00521     // Update fields
00522     mesh.updateMesh(map);
00523 
00524     // Optionally inflate mesh
00525     if (map().hasMotionPoints())
00526     {
00527         mesh.movePoints(map().preMotionPoints());
00528     }
00529 
00530     if (!overwrite)
00531     {
00532         runTime++;
00533     }
00534     else
00535     {
00536         mesh.setInstance(oldInstance);
00537     }
00538 
00539     Info<< "Writing dual mesh to " << runTime.timeName() << endl;
00540 
00541     mesh.write();
00542 
00543     Info<< "End\n" << endl;
00544 
00545     return 0;
00546 }
00547 
00548 
00549 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd