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

autoLayerDriver.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     All to do with adding cell layers
00027 
00028 \*----------------------------------------------------------------------------*/
00029 
00030 #include "autoLayerDriver.H"
00031 #include "fvMesh.H"
00032 #include "Time.H"
00033 #include "meshRefinement.H"
00034 #include "removePoints.H"
00035 #include "pointFields.H"
00036 #include "motionSmoother.H"
00037 #include "mathematicalConstants.H"
00038 #include "pointSet.H"
00039 #include "faceSet.H"
00040 #include "cellSet.H"
00041 #include "polyTopoChange.H"
00042 #include "mapPolyMesh.H"
00043 #include "addPatchCellLayer.H"
00044 #include "mapDistributePolyMesh.H"
00045 #include "OFstream.H"
00046 #include "layerParameters.H"
00047 #include "combineFaces.H"
00048 #include "IOmanip.H"
00049 
00050 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00051 
00052 namespace Foam
00053 {
00054 
00055 defineTypeNameAndDebug(autoLayerDriver, 0);
00056 
00057 } // End namespace Foam
00058 
00059 
00060 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00061 
00062 Foam::label Foam::autoLayerDriver::mergePatchFacesUndo
00063 (
00064     const scalar minCos,
00065     const scalar concaveCos,
00066     const dictionary& motionDict
00067 )
00068 {
00069     fvMesh& mesh = meshRefiner_.mesh();
00070 
00071     // Patch face merging engine
00072     combineFaces faceCombiner(mesh, true);
00073 
00074     // Pick up all candidate cells on boundary
00075     labelHashSet boundaryCells(mesh.nFaces()-mesh.nInternalFaces());
00076 
00077     {
00078         labelList patchIDs(meshRefiner_.meshedPatches());
00079 
00080         const polyBoundaryMesh& patches = mesh.boundaryMesh();
00081 
00082         forAll(patchIDs, i)
00083         {
00084             label patchI = patchIDs[i];
00085 
00086             const polyPatch& patch = patches[patchI];
00087 
00088             if (!patch.coupled())
00089             {
00090                 forAll(patch, i)
00091                 {
00092                     boundaryCells.insert(mesh.faceOwner()[patch.start()+i]);
00093                 }
00094             }
00095         }
00096     }
00097 
00098     // Get all sets of faces that can be merged
00099     labelListList allFaceSets
00100     (
00101         faceCombiner.getMergeSets
00102         (
00103             minCos,
00104             concaveCos,
00105             boundaryCells
00106         )
00107     );
00108 
00109     label nFaceSets = returnReduce(allFaceSets.size(), sumOp<label>());
00110 
00111     Info<< "Merging " << nFaceSets << " sets of faces." << nl << endl;
00112 
00113     if (nFaceSets > 0)
00114     {
00115         if (debug)
00116         {
00117             faceSet allSets(mesh, "allFaceSets", allFaceSets.size());
00118             forAll(allFaceSets, setI)
00119             {
00120                 forAll(allFaceSets[setI], i)
00121                 {
00122                     allSets.insert(allFaceSets[setI][i]);
00123                 }
00124             }
00125             Pout<< "Writing all faces to be merged to set "
00126                 << allSets.objectPath() << endl;
00127             allSets.write();
00128         }
00129 
00130 
00131         // Topology changes container
00132         polyTopoChange meshMod(mesh);
00133 
00134         // Merge all faces of a set into the first face of the set.
00135         faceCombiner.setRefinement(allFaceSets, meshMod);
00136 
00137         // Experimental: store data for all the points that have been deleted
00138         meshRefiner_.storeData
00139         (
00140             faceCombiner.savedPointLabels(),    // points to store
00141             labelList(0),                       // faces to store
00142             labelList(0)                        // cells to store
00143         );
00144 
00145         // Change the mesh (no inflation)
00146         autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, false, true);
00147 
00148         // Update fields
00149         mesh.updateMesh(map);
00150 
00151         // Move mesh (since morphing does not do this)
00152         if (map().hasMotionPoints())
00153         {
00154             mesh.movePoints(map().preMotionPoints());
00155         }
00156         else
00157         {
00158             // Delete mesh volumes.
00159             mesh.clearOut();
00160         }
00161 
00162         if (meshRefiner_.overwrite())
00163         {
00164             mesh.setInstance(meshRefiner_.oldInstance());
00165         }
00166 
00167         faceCombiner.updateMesh(map);
00168 
00169         meshRefiner_.updateMesh(map, labelList(0));
00170 
00171 
00172         for (label iteration = 0; iteration < 100; iteration++)
00173         {
00174             Info<< nl
00175                 << "Undo iteration " << iteration << nl
00176                 << "----------------" << endl;
00177 
00178 
00179             // Check mesh for errors
00180             // ~~~~~~~~~~~~~~~~~~~~~
00181 
00182             faceSet errorFaces
00183             (
00184                 mesh,
00185                 "errorFaces",
00186                 mesh.nFaces()-mesh.nInternalFaces()
00187             );
00188             bool hasErrors = motionSmoother::checkMesh
00189             (
00190                 false,  // report
00191                 mesh,
00192                 motionDict,
00193                 errorFaces
00194             );
00195             //if (checkEdgeConnectivity)
00196             //{
00197             //    Info<< "Checking edge-face connectivity (duplicate faces"
00198             //        << " or non-consecutive shared vertices)" << endl;
00199             //
00200             //    label nOldSize = errorFaces.size();
00201             //
00202             //    hasErrors =
00203             //        mesh.checkFaceFaces
00204             //        (
00205             //            false,
00206             //            &errorFaces
00207             //        )
00208             //     || hasErrors;
00209             //
00210             //    Info<< "Detected additional "
00211             //        << returnReduce(errorFaces.size()-nOldSize, sumOp<label>())
00212             //        << " faces with illegal face-face connectivity"
00213             //        << endl;
00214             //}
00215 
00216             if (!hasErrors)
00217             {
00218                 break;
00219             }
00220 
00221 
00222             if (debug)
00223             {
00224                 Pout<< "Writing all faces in error to faceSet "
00225                     << errorFaces.objectPath() << nl << endl;
00226                 errorFaces.write();
00227             }
00228 
00229 
00230             // Check any master cells for using any of the error faces
00231             // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00232 
00233             DynamicList<label> mastersToRestore(allFaceSets.size());
00234 
00235             forAll(allFaceSets, setI)
00236             {
00237                 label masterFaceI = faceCombiner.masterFace()[setI];
00238 
00239                 if (masterFaceI != -1)
00240                 {
00241                     label masterCellII = mesh.faceOwner()[masterFaceI];
00242 
00243                     const cell& cFaces = mesh.cells()[masterCellII];
00244 
00245                     forAll(cFaces, i)
00246                     {
00247                         if (errorFaces.found(cFaces[i]))
00248                         {
00249                             mastersToRestore.append(masterFaceI);
00250                             break;
00251                         }
00252                     }
00253                 }
00254             }
00255             mastersToRestore.shrink();
00256 
00257             label nRestore = returnReduce
00258             (
00259                 mastersToRestore.size(),
00260                 sumOp<label>()
00261             );
00262 
00263             Info<< "Masters that need to be restored:"
00264                 << nRestore << endl;
00265 
00266             if (debug)
00267             {
00268                 faceSet restoreSet(mesh, "mastersToRestore", mastersToRestore);
00269                 Pout<< "Writing all " << mastersToRestore.size()
00270                     << " masterfaces to be restored to set "
00271                     << restoreSet.objectPath() << endl;
00272                 restoreSet.write();
00273             }
00274 
00275 
00276             if (nRestore == 0)
00277             {
00278                 break;
00279             }
00280 
00281 
00282             // Undo
00283             // ~~~~
00284 
00285             // Topology changes container
00286             polyTopoChange meshMod(mesh);
00287 
00288             // Merge all faces of a set into the first face of the set.
00289             // Experimental:mark all points/faces/cells that have been restored.
00290             Map<label> restoredPoints(0);
00291             Map<label> restoredFaces(0);
00292             Map<label> restoredCells(0);
00293 
00294             faceCombiner.setUnrefinement
00295             (
00296                 mastersToRestore,
00297                 meshMod,
00298                 restoredPoints,
00299                 restoredFaces,
00300                 restoredCells
00301             );
00302 
00303             // Change the mesh (no inflation)
00304             autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, false, true);
00305 
00306             // Update fields
00307             mesh.updateMesh(map);
00308 
00309             // Move mesh (since morphing does not do this)
00310             if (map().hasMotionPoints())
00311             {
00312                 mesh.movePoints(map().preMotionPoints());
00313             }
00314             else
00315             {
00316                 // Delete mesh volumes.
00317                 mesh.clearOut();
00318             }
00319 
00320             if (meshRefiner_.overwrite())
00321             {
00322                 mesh.setInstance(meshRefiner_.oldInstance());
00323             }
00324 
00325             faceCombiner.updateMesh(map);
00326 
00327             // Renumber restore maps
00328             inplaceMapKey(map().reversePointMap(), restoredPoints);
00329             inplaceMapKey(map().reverseFaceMap(), restoredFaces);
00330             inplaceMapKey(map().reverseCellMap(), restoredCells);
00331 
00332             // Experimental:restore all points/face/cells in maps
00333             meshRefiner_.updateMesh
00334             (
00335                 map,
00336                 labelList(0),       // changedFaces
00337                 restoredPoints,
00338                 restoredFaces,
00339                 restoredCells
00340             );
00341 
00342             Info<< endl;
00343         }
00344 
00345         if (debug)
00346         {
00347             Pout<< "Writing merged-faces mesh to time "
00348                 << meshRefiner_.timeName() << nl << endl;
00349             mesh.write();
00350         }
00351     }
00352     else
00353     {
00354         Info<< "No faces merged ..." << endl;
00355     }
00356 
00357     return nFaceSets;
00358 }
00359 
00360 
00361 // Remove points. pointCanBeDeleted is parallel synchronised.
00362 Foam::autoPtr<Foam::mapPolyMesh> Foam::autoLayerDriver::doRemovePoints
00363 (
00364     removePoints& pointRemover,
00365     const boolList& pointCanBeDeleted
00366 )
00367 {
00368     fvMesh& mesh = meshRefiner_.mesh();
00369 
00370     // Topology changes container
00371     polyTopoChange meshMod(mesh);
00372 
00373     pointRemover.setRefinement(pointCanBeDeleted, meshMod);
00374 
00375     // Change the mesh (no inflation)
00376     autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, false, true);
00377 
00378     // Update fields
00379     mesh.updateMesh(map);
00380 
00381     // Move mesh (since morphing does not do this)
00382     if (map().hasMotionPoints())
00383     {
00384         mesh.movePoints(map().preMotionPoints());
00385     }
00386     else
00387     {
00388         // Delete mesh volumes.
00389         mesh.clearOut();
00390     }
00391 
00392     if (meshRefiner_.overwrite())
00393     {
00394         mesh.setInstance(meshRefiner_.oldInstance());
00395     }
00396 
00397     pointRemover.updateMesh(map);
00398     meshRefiner_.updateMesh(map, labelList(0));
00399 
00400     return map;
00401 }
00402 
00403 
00404 // Restore faces (which contain removed points)
00405 Foam::autoPtr<Foam::mapPolyMesh> Foam::autoLayerDriver::doRestorePoints
00406 (
00407     removePoints& pointRemover,
00408     const labelList& facesToRestore
00409 )
00410 {
00411     fvMesh& mesh = meshRefiner_.mesh();
00412 
00413     // Topology changes container
00414     polyTopoChange meshMod(mesh);
00415 
00416     // Determine sets of points and faces to restore
00417     labelList localFaces, localPoints;
00418     pointRemover.getUnrefimentSet
00419     (
00420         facesToRestore,
00421         localFaces,
00422         localPoints
00423     );
00424 
00425     // Undo the changes on the faces that are in error.
00426     pointRemover.setUnrefinement
00427     (
00428         localFaces,
00429         localPoints,
00430         meshMod
00431     );
00432 
00433     // Change the mesh (no inflation)
00434     autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, false, true);
00435 
00436     // Update fields
00437     mesh.updateMesh(map);
00438 
00439     // Move mesh (since morphing does not do this)
00440     if (map().hasMotionPoints())
00441     {
00442         mesh.movePoints(map().preMotionPoints());
00443     }
00444     else
00445     {
00446         // Delete mesh volumes.
00447         mesh.clearOut();
00448     }
00449 
00450     if (meshRefiner_.overwrite())
00451     {
00452         mesh.setInstance(meshRefiner_.oldInstance());
00453     }
00454 
00455     pointRemover.updateMesh(map);
00456     meshRefiner_.updateMesh(map, labelList(0));
00457 
00458     return map;
00459 }
00460 
00461 
00462 // Collect all faces that are both in candidateFaces and in the set.
00463 // If coupled face also collects the coupled face.
00464 Foam::labelList Foam::autoLayerDriver::collectFaces
00465 (
00466     const labelList& candidateFaces,
00467     const labelHashSet& set
00468 ) const
00469 {
00470     const fvMesh& mesh = meshRefiner_.mesh();
00471 
00472     // Has face been selected?
00473     boolList selected(mesh.nFaces(), false);
00474 
00475     forAll(candidateFaces, i)
00476     {
00477         label faceI = candidateFaces[i];
00478 
00479         if (set.found(faceI))
00480         {
00481             selected[faceI] = true;
00482         }
00483     }
00484     syncTools::syncFaceList
00485     (
00486         mesh,
00487         selected,
00488         orEqOp<bool>(),     // combine operator
00489         false               // separation
00490     );
00491 
00492     labelList selectedFaces(findIndices(selected, true));
00493 
00494     return selectedFaces;
00495 }
00496 
00497 
00498 // Pick up faces of cells of faces in set.
00499 Foam::labelList Foam::autoLayerDriver::growFaceCellFace
00500 (
00501     const labelHashSet& set
00502 ) const
00503 {
00504     const fvMesh& mesh = meshRefiner_.mesh();
00505 
00506     boolList selected(mesh.nFaces(), false);
00507 
00508     forAllConstIter(faceSet, set, iter)
00509     {
00510         label faceI = iter.key();
00511 
00512         label own = mesh.faceOwner()[faceI];
00513 
00514         const cell& ownFaces = mesh.cells()[own];
00515         forAll(ownFaces, ownFaceI)
00516         {
00517             selected[ownFaces[ownFaceI]] = true;
00518         }
00519 
00520         if (mesh.isInternalFace(faceI))
00521         {
00522             label nbr = mesh.faceNeighbour()[faceI];
00523 
00524             const cell& nbrFaces = mesh.cells()[nbr];
00525             forAll(nbrFaces, nbrFaceI)
00526             {
00527                 selected[nbrFaces[nbrFaceI]] = true;
00528             }
00529         }
00530     }
00531     syncTools::syncFaceList
00532     (
00533         mesh,
00534         selected,
00535         orEqOp<bool>(),     // combine operator
00536         false               // separation
00537     );
00538     return findIndices(selected, true);
00539 }
00540 
00541 
00542 // Remove points not used by any face or points used by only two faces where
00543 // the edges are in line
00544 Foam::label Foam::autoLayerDriver::mergeEdgesUndo
00545 (
00546     const scalar minCos,
00547     const dictionary& motionDict
00548 )
00549 {
00550     fvMesh& mesh = meshRefiner_.mesh();
00551 
00552     Info<< nl
00553         << "Merging all points on surface that" << nl
00554         << "- are used by only two boundary faces and" << nl
00555         << "- make an angle with a cosine of more than " << minCos
00556         << "." << nl << endl;
00557 
00558     // Point removal analysis engine with undo
00559     removePoints pointRemover(mesh, true);
00560 
00561     // Count usage of points
00562     boolList pointCanBeDeleted;
00563     label nRemove = pointRemover.countPointUsage(minCos, pointCanBeDeleted);
00564 
00565     if (nRemove > 0)
00566     {
00567         Info<< "Removing " << nRemove
00568             << " straight edge points ..." << nl << endl;
00569 
00570         // Remove points
00571         // ~~~~~~~~~~~~~
00572 
00573         doRemovePoints(pointRemover, pointCanBeDeleted);
00574 
00575 
00576         for (label iteration = 0; iteration < 100; iteration++)
00577         {
00578             Info<< nl
00579                 << "Undo iteration " << iteration << nl
00580                 << "----------------" << endl;
00581 
00582 
00583             // Check mesh for errors
00584             // ~~~~~~~~~~~~~~~~~~~~~
00585 
00586             faceSet errorFaces
00587             (
00588                 mesh,
00589                 "errorFaces",
00590                 mesh.nFaces()-mesh.nInternalFaces()
00591             );
00592             bool hasErrors = motionSmoother::checkMesh
00593             (
00594                 false,  // report
00595                 mesh,
00596                 motionDict,
00597                 errorFaces
00598             );
00599             //if (checkEdgeConnectivity)
00600             //{
00601             //    Info<< "Checking edge-face connectivity (duplicate faces"
00602             //        << " or non-consecutive shared vertices)" << endl;
00603             //
00604             //    label nOldSize = errorFaces.size();
00605             //
00606             //    hasErrors =
00607             //        mesh.checkFaceFaces
00608             //        (
00609             //            false,
00610             //            &errorFaces
00611             //        )
00612             //     || hasErrors;
00613             //
00614             //    Info<< "Detected additional "
00615             //        << returnReduce(errorFaces.size()-nOldSize,sumOp<label>())
00616             //        << " faces with illegal face-face connectivity"
00617             //        << endl;
00618             //}
00619 
00620             if (!hasErrors)
00621             {
00622                 break;
00623             }
00624 
00625             if (debug)
00626             {
00627                 Pout<< "**Writing all faces in error to faceSet "
00628                     << errorFaces.objectPath() << nl << endl;
00629                 errorFaces.write();
00630             }
00631 
00632             labelList masterErrorFaces
00633             (
00634                 collectFaces
00635                 (
00636                     pointRemover.savedFaceLabels(),
00637                     errorFaces
00638                 )
00639             );
00640 
00641             label n = returnReduce(masterErrorFaces.size(), sumOp<label>());
00642 
00643             Info<< "Detected " << n
00644                 << " error faces on boundaries that have been merged."
00645                 << " These will be restored to their original faces." << nl
00646                 << endl;
00647 
00648             if (n == 0)
00649             {
00650                 if (hasErrors)
00651                 {
00652                     Info<< "Detected "
00653                         << returnReduce(errorFaces.size(), sumOp<label>())
00654                         << " error faces in mesh."
00655                         << " Restoring neighbours of faces in error." << nl
00656                         << endl;
00657 
00658                     labelList expandedErrorFaces
00659                     (
00660                         growFaceCellFace
00661                         (
00662                             errorFaces
00663                         )
00664                     );
00665 
00666                     doRestorePoints(pointRemover, expandedErrorFaces);
00667                 }
00668 
00669                 break;
00670             }
00671 
00672             doRestorePoints(pointRemover, masterErrorFaces);
00673         }
00674 
00675         if (debug)
00676         {
00677             Pout<< "Writing merged-edges mesh to time "
00678                 << meshRefiner_.timeName() << nl << endl;
00679             mesh.write();
00680         }
00681     }
00682     else
00683     {
00684         Info<< "No straight edges simplified and no points removed ..." << endl;
00685     }
00686 
00687     return nRemove;
00688 }
00689 
00690 
00691 // For debugging: Dump displacement to .obj files
00692 void Foam::autoLayerDriver::dumpDisplacement
00693 (
00694     const fileName& prefix,
00695     const indirectPrimitivePatch& pp,
00696     const vectorField& patchDisp,
00697     const List<extrudeMode>& extrudeStatus
00698 )
00699 {
00700     OFstream dispStr(prefix + "_disp.obj");
00701     Info<< "Writing all displacements to " << dispStr.name() << nl << endl;
00702 
00703     label vertI = 0;
00704 
00705     forAll(patchDisp, patchPointI)
00706     {
00707         const point& pt = pp.localPoints()[patchPointI];
00708 
00709         meshTools::writeOBJ(dispStr, pt); vertI++;
00710         meshTools::writeOBJ(dispStr, pt + patchDisp[patchPointI]); vertI++;
00711 
00712         dispStr << "l " << vertI-1 << ' ' << vertI << nl;
00713     }
00714 
00715 
00716     OFstream illStr(prefix + "_illegal.obj");
00717     Info<< "Writing invalid displacements to " << illStr.name() << nl << endl;
00718 
00719     vertI = 0;
00720 
00721     forAll(patchDisp, patchPointI)
00722     {
00723         if (extrudeStatus[patchPointI] != EXTRUDE)
00724         {
00725             const point& pt = pp.localPoints()[patchPointI];
00726 
00727             meshTools::writeOBJ(illStr, pt); vertI++;
00728             meshTools::writeOBJ(illStr, pt + patchDisp[patchPointI]); vertI++;
00729 
00730             illStr << "l " << vertI-1 << ' ' << vertI << nl;
00731         }
00732     }
00733 }
00734 
00735 
00736 // Check that primitivePatch is not multiply connected. Collect non-manifold
00737 // points in pointSet.
00738 void Foam::autoLayerDriver::checkManifold
00739 (
00740     const indirectPrimitivePatch& fp,
00741     pointSet& nonManifoldPoints
00742 )
00743 {
00744     // Check for non-manifold points (surface pinched at point)
00745     fp.checkPointManifold(false, &nonManifoldPoints);
00746 
00747     // Check for edge-faces (surface pinched at edge)
00748     const labelListList& edgeFaces = fp.edgeFaces();
00749 
00750     forAll(edgeFaces, edgeI)
00751     {
00752         const labelList& eFaces = edgeFaces[edgeI];
00753 
00754         if (eFaces.size() > 2)
00755         {
00756             const edge& e = fp.edges()[edgeI];
00757 
00758             nonManifoldPoints.insert(fp.meshPoints()[e[0]]);
00759             nonManifoldPoints.insert(fp.meshPoints()[e[1]]);
00760         }
00761     }
00762 }
00763 
00764 
00765 void Foam::autoLayerDriver::checkMeshManifold() const
00766 {
00767     const fvMesh& mesh = meshRefiner_.mesh();
00768 
00769     Info<< nl << "Checking mesh manifoldness ..." << endl;
00770 
00771     // Get all outside faces
00772     labelList outsideFaces(mesh.nFaces() - mesh.nInternalFaces());
00773 
00774     for (label faceI = mesh.nInternalFaces(); faceI < mesh.nFaces(); faceI++)
00775     {
00776          outsideFaces[faceI - mesh.nInternalFaces()] = faceI;
00777     }
00778 
00779     pointSet nonManifoldPoints
00780     (
00781         mesh,
00782         "nonManifoldPoints",
00783         mesh.nPoints() / 100
00784     );
00785 
00786     // Build primitivePatch out of faces and check it for problems.
00787     checkManifold
00788     (
00789         indirectPrimitivePatch
00790         (
00791             IndirectList<face>(mesh.faces(), outsideFaces),
00792             mesh.points()
00793         ),
00794         nonManifoldPoints
00795     );
00796 
00797     label nNonManif = returnReduce(nonManifoldPoints.size(), sumOp<label>());
00798 
00799     if (nNonManif > 0)
00800     {
00801         Info<< "Outside of mesh is multiply connected across edges or"
00802             << " points." << nl
00803             << "This is not a fatal error but might cause some unexpected"
00804             << " behaviour." << nl
00805             << "Writing " << nNonManif
00806             << " points where this happens to pointSet "
00807             << nonManifoldPoints.name() << endl;
00808 
00809         nonManifoldPoints.write();
00810     }
00811     Info<< endl;
00812 }
00813 
00814 
00815 
00816 // Unset extrusion on point. Returns true if anything unset.
00817 bool Foam::autoLayerDriver::unmarkExtrusion
00818 (
00819     const label patchPointI,
00820     pointField& patchDisp,
00821     labelList& patchNLayers,
00822     List<extrudeMode>& extrudeStatus
00823 )
00824 {
00825     if (extrudeStatus[patchPointI] == EXTRUDE)
00826     {
00827         extrudeStatus[patchPointI] = NOEXTRUDE;
00828         patchNLayers[patchPointI] = 0;
00829         patchDisp[patchPointI] = vector::zero;
00830         return true;
00831     }
00832     else if (extrudeStatus[patchPointI] == EXTRUDEREMOVE)
00833     {
00834         extrudeStatus[patchPointI] = NOEXTRUDE;
00835         patchNLayers[patchPointI] = 0;
00836         patchDisp[patchPointI] = vector::zero;
00837         return true;
00838     }
00839     else
00840     {
00841         return false;
00842     }
00843 }
00844 
00845 
00846 // Unset extrusion on face. Returns true if anything unset.
00847 bool Foam::autoLayerDriver::unmarkExtrusion
00848 (
00849     const face& localFace,
00850     pointField& patchDisp,
00851     labelList& patchNLayers,
00852     List<extrudeMode>& extrudeStatus
00853 )
00854 {
00855     bool unextruded = false;
00856 
00857     forAll(localFace, fp)
00858     {
00859         if
00860         (
00861             unmarkExtrusion
00862             (
00863                 localFace[fp],
00864                 patchDisp,
00865                 patchNLayers,
00866                 extrudeStatus
00867             )
00868         )
00869         {
00870             unextruded = true;
00871         }
00872     }
00873     return unextruded;
00874 }
00875 
00876 
00877 // No extrusion at non-manifold points.
00878 void Foam::autoLayerDriver::handleNonManifolds
00879 (
00880     const indirectPrimitivePatch& pp,
00881     const labelList& meshEdges,
00882     pointField& patchDisp,
00883     labelList& patchNLayers,
00884     List<extrudeMode>& extrudeStatus
00885 ) const
00886 {
00887     const fvMesh& mesh = meshRefiner_.mesh();
00888 
00889     Info<< nl << "Handling non-manifold points ..." << endl;
00890 
00891     // Detect non-manifold points
00892     Info<< nl << "Checking patch manifoldness ..." << endl;
00893 
00894     pointSet nonManifoldPoints(mesh, "nonManifoldPoints", pp.nPoints());
00895 
00896     // 1. Local check
00897     checkManifold(pp, nonManifoldPoints);
00898 
00899     label nNonManif = returnReduce(nonManifoldPoints.size(), sumOp<label>());
00900 
00901     Info<< "Outside of local patch is multiply connected across edges or"
00902         << " points at " << nNonManif << " points." << endl;
00903 
00904 
00905     const labelList& meshPoints = pp.meshPoints();
00906 
00907 
00908     // 2. Parallel check
00909     // For now disable extrusion at any shared edges.
00910     const labelHashSet sharedEdgeSet(mesh.globalData().sharedEdgeLabels());
00911 
00912     forAll(pp.edges(), edgeI)
00913     {
00914         if (sharedEdgeSet.found(meshEdges[edgeI]))
00915         {
00916             const edge& e = mesh.edges()[meshEdges[edgeI]];
00917 
00918             Pout<< "Disabling extrusion at edge "
00919                 << mesh.points()[e[0]] << mesh.points()[e[1]]
00920                 << " since it is non-manifold across coupled patches."
00921                 << endl;
00922 
00923             nonManifoldPoints.insert(e[0]);
00924             nonManifoldPoints.insert(e[1]);
00925         }
00926     }
00927 
00928     // 3b. extrusion can produce multiple faces between 2 cells
00929     // across processor boundary
00930     // This occurs when a coupled face shares more than 1 edge with a
00931     // non-coupled boundary face.
00932     // This is now correctly handled by addPatchCellLayer in that it
00933     // extrudes a single face from the stringed up edges.
00934 
00935 
00936     nNonManif = returnReduce(nonManifoldPoints.size(), sumOp<label>());
00937 
00938     if (nNonManif > 0)
00939     {
00940         Info<< "Outside of patches is multiply connected across edges or"
00941             << " points at " << nNonManif << " points." << nl
00942             << "Writing " << nNonManif
00943             << " points where this happens to pointSet "
00944             << nonManifoldPoints.name() << nl
00945             << "and setting layer thickness to zero on these points."
00946             << endl;
00947 
00948         nonManifoldPoints.write();
00949 
00950         forAll(meshPoints, patchPointI)
00951         {
00952             if (nonManifoldPoints.found(meshPoints[patchPointI]))
00953             {
00954                 unmarkExtrusion
00955                 (
00956                     patchPointI,
00957                     patchDisp,
00958                     patchNLayers,
00959                     extrudeStatus
00960                 );
00961             }
00962         }
00963     }
00964 
00965     Info<< "Set displacement to zero for all " << nNonManif
00966         << " non-manifold points" << endl;
00967 }
00968 
00969 
00970 // Parallel feature edge detection. Assumes non-manifold edges already handled.
00971 void Foam::autoLayerDriver::handleFeatureAngle
00972 (
00973     const indirectPrimitivePatch& pp,
00974     const labelList& meshEdges,
00975     const scalar minCos,
00976     pointField& patchDisp,
00977     labelList& patchNLayers,
00978     List<extrudeMode>& extrudeStatus
00979 ) const
00980 {
00981     const fvMesh& mesh = meshRefiner_.mesh();
00982 
00983     Info<< nl << "Handling feature edges ..." << endl;
00984 
00985     if (minCos < 1-SMALL)
00986     {
00987         // Normal component of normals of connected faces.
00988         vectorField edgeNormal(mesh.nEdges(), wallPoint::greatPoint);
00989 
00990         const labelListList& edgeFaces = pp.edgeFaces();
00991 
00992         forAll(edgeFaces, edgeI)
00993         {
00994             const labelList& eFaces = pp.edgeFaces()[edgeI];
00995 
00996             label meshEdgeI = meshEdges[edgeI];
00997 
00998             forAll(eFaces, i)
00999             {
01000                 nomalsCombine()
01001                 (
01002                     edgeNormal[meshEdgeI],
01003                     pp.faceNormals()[eFaces[i]]
01004                 );
01005             }
01006         }
01007 
01008         syncTools::syncEdgeList
01009         (
01010             mesh,
01011             edgeNormal,
01012             nomalsCombine(),
01013             wallPoint::greatPoint,  // null value
01014             false                   // no separation
01015         );
01016 
01017         label vertI = 0;
01018         autoPtr<OFstream> str;
01019         if (debug)
01020         {
01021             str.reset(new OFstream(mesh.time().path()/"featureEdges.obj"));
01022             Info<< "Writing feature edges to " << str().name() << endl;
01023         }
01024 
01025         label nFeats = 0;
01026 
01027         // Now on coupled edges the edgeNormal will have been truncated and
01028         // only be still be the old value where two faces have the same normal
01029         forAll(edgeFaces, edgeI)
01030         {
01031             const labelList& eFaces = pp.edgeFaces()[edgeI];
01032 
01033             label meshEdgeI = meshEdges[edgeI];
01034 
01035             const vector& n = edgeNormal[meshEdgeI];
01036 
01037             if (n != wallPoint::greatPoint)
01038             {
01039                 scalar cos = n & pp.faceNormals()[eFaces[0]];
01040 
01041                 if (cos < minCos)
01042                 {
01043                     const edge& e = pp.edges()[edgeI];
01044 
01045                     unmarkExtrusion
01046                     (
01047                         e[0],
01048                         patchDisp,
01049                         patchNLayers,
01050                         extrudeStatus
01051                     );
01052                     unmarkExtrusion
01053                     (
01054                         e[1],
01055                         patchDisp,
01056                         patchNLayers,
01057                         extrudeStatus
01058                     );
01059 
01060                     nFeats++;
01061 
01062                     if (str.valid())
01063                     {
01064                         meshTools::writeOBJ(str(), pp.localPoints()[e[0]]);
01065                         vertI++;
01066                         meshTools::writeOBJ(str(), pp.localPoints()[e[1]]);
01067                         vertI++;
01068                         str()<< "l " << vertI-1 << ' ' << vertI << nl;
01069                     }
01070                 }
01071             }
01072         }
01073 
01074         Info<< "Set displacement to zero for points on "
01075             << returnReduce(nFeats, sumOp<label>())
01076             << " feature edges" << endl;
01077     }
01078 }
01079 
01080 
01081 // No extrusion on cells with warped faces. Calculates the thickness of the
01082 // layer and compares it to the space the warped face takes up. Disables
01083 // extrusion if layer thickness is more than faceRatio of the thickness of
01084 // the face.
01085 void Foam::autoLayerDriver::handleWarpedFaces
01086 (
01087     const indirectPrimitivePatch& pp,
01088     const scalar faceRatio,
01089     const scalar edge0Len,
01090     const labelList& cellLevel,
01091     pointField& patchDisp,
01092     labelList& patchNLayers,
01093     List<extrudeMode>& extrudeStatus
01094 ) const
01095 {
01096     const fvMesh& mesh = meshRefiner_.mesh();
01097 
01098     Info<< nl << "Handling cells with warped patch faces ..." << nl;
01099 
01100     const pointField& points = mesh.points();
01101 
01102     label nWarpedFaces = 0;
01103 
01104     forAll(pp, i)
01105     {
01106         const face& f = pp[i];
01107 
01108         if (f.size() > 3)
01109         {
01110             label faceI = pp.addressing()[i];
01111 
01112             label ownLevel = cellLevel[mesh.faceOwner()[faceI]];
01113             scalar edgeLen = edge0Len/(1<<ownLevel);
01114 
01115             // Normal distance to face centre plane
01116             const point& fc = mesh.faceCentres()[faceI];
01117             const vector& fn = pp.faceNormals()[i];
01118 
01119             scalarField vProj(f.size());
01120 
01121             forAll(f, fp)
01122             {
01123                 vector n = points[f[fp]] - fc;
01124                 vProj[fp] = (n & fn);
01125             }
01126 
01127             // Get normal 'span' of face
01128             scalar minVal = min(vProj);
01129             scalar maxVal = max(vProj);
01130 
01131             if ((maxVal - minVal) > faceRatio * edgeLen)
01132             {
01133                 if
01134                 (
01135                     unmarkExtrusion
01136                     (
01137                         pp.localFaces()[i],
01138                         patchDisp,
01139                         patchNLayers,
01140                         extrudeStatus
01141                     )
01142                 )
01143                 {
01144                     nWarpedFaces++;
01145                 }
01146             }
01147         }
01148     }
01149 
01150     Info<< "Set displacement to zero on "
01151         << returnReduce(nWarpedFaces, sumOp<label>())
01152         << " warped faces since layer would be > " << faceRatio
01153         << " of the size of the bounding box." << endl;
01154 }
01155 
01156 
01157 
01160 //void Foam::autoLayerDriver::handleMultiplePatchFaces
01161 //(
01162 //    const indirectPrimitivePatch& pp,
01163 //    pointField& patchDisp,
01164 //    labelList& patchNLayers,
01165 //    List<extrudeMode>& extrudeStatus
01166 //) const
01167 //{
01168 //    const fvMesh& mesh = meshRefiner_.mesh();
01169 //
01170 //    Info<< nl << "Handling cells with multiple patch faces ..." << nl;
01171 //
01172 //    const labelListList& pointFaces = pp.pointFaces();
01173 //
01174 //    // Cells that should not get an extrusion layer
01175 //    cellSet multiPatchCells(mesh, "multiPatchCells", pp.size());
01176 //
01177 //    // Detect points that use multiple faces on same cell.
01178 //    forAll(pointFaces, patchPointI)
01179 //    {
01180 //        const labelList& pFaces = pointFaces[patchPointI];
01181 //
01182 //        labelHashSet pointCells(pFaces.size());
01183 //
01184 //        forAll(pFaces, i)
01185 //        {
01186 //            label cellI = mesh.faceOwner()[pp.addressing()[pFaces[i]]];
01187 //
01188 //            if (!pointCells.insert(cellI))
01189 //            {
01190 //                // Second or more occurrence of cell so cell has two or more
01191 //                // pp faces connected to this point.
01192 //                multiPatchCells.insert(cellI);
01193 //            }
01194 //        }
01195 //    }
01196 //
01197 //    label nMultiPatchCells = returnReduce
01198 //    (
01199 //        multiPatchCells.size(),
01200 //        sumOp<label>()
01201 //    );
01202 //
01203 //    Info<< "Detected " << nMultiPatchCells
01204 //        << " cells with multiple (connected) patch faces." << endl;
01205 //
01206 //    label nChanged = 0;
01207 //
01208 //    if (nMultiPatchCells > 0)
01209 //    {
01210 //        Info<< "Writing " << nMultiPatchCells
01211 //            << " cells with multiple (connected) patch faces to cellSet "
01212 //            << multiPatchCells.objectPath() << endl;
01213 //        multiPatchCells.write();
01214 //
01215 //
01216 //        // Go through all points and remove extrusion on any cell in
01217 //        // multiPatchCells
01218 //        // (has to be done in separate loop since having one point on
01219 //        // multipatches has to reset extrusion on all points of cell)
01220 //
01221 //        forAll(pointFaces, patchPointI)
01222 //        {
01223 //            if (extrudeStatus[patchPointI] != NOEXTRUDE)
01224 //            {
01225 //                const labelList& pFaces = pointFaces[patchPointI];
01226 //
01227 //                forAll(pFaces, i)
01228 //                {
01229 //                    label cellI =
01230 //                        mesh.faceOwner()[pp.addressing()[pFaces[i]]];
01231 //
01232 //                    if (multiPatchCells.found(cellI))
01233 //                    {
01234 //                        if
01235 //                        (
01236 //                            unmarkExtrusion
01237 //                            (
01238 //                                patchPointI,
01239 //                                patchDisp,
01240 //                                patchNLayers,
01241 //                                extrudeStatus
01242 //                            )
01243 //                        )
01244 //                        {
01245 //                            nChanged++;
01246 //                        }
01247 //                    }
01248 //                }
01249 //            }
01250 //        }
01251 //
01252 //        reduce(nChanged, sumOp<label>());
01253 //    }
01254 //
01255 //    Info<< "Prevented extrusion on " << nChanged
01256 //        << " points due to multiple patch faces." << nl << endl;
01257 //}
01258 
01259 
01260 // No extrusion on faces with differing number of layers for points
01261 void Foam::autoLayerDriver::setNumLayers
01262 (
01263     const labelList& patchToNLayers,
01264     const labelList& patchIDs,
01265     const indirectPrimitivePatch& pp,
01266     pointField& patchDisp,
01267     labelList& patchNLayers,
01268     List<extrudeMode>& extrudeStatus
01269 ) const
01270 {
01271     const fvMesh& mesh = meshRefiner_.mesh();
01272 
01273     Info<< nl << "Handling points with inconsistent layer specification ..."
01274         << endl;
01275 
01276     // Get for every point (really only nessecary on patch external points)
01277     // the max and min of any patch faces using it.
01278     labelList maxLayers(patchNLayers.size(), labelMin);
01279     labelList minLayers(patchNLayers.size(), labelMax);
01280 
01281     forAll(patchIDs, i)
01282     {
01283         label patchI = patchIDs[i];
01284 
01285         const labelList& meshPoints = mesh.boundaryMesh()[patchI].meshPoints();
01286 
01287         label wantedLayers = patchToNLayers[patchI];
01288 
01289         forAll(meshPoints, patchPointI)
01290         {
01291             label ppPointI = pp.meshPointMap()[meshPoints[patchPointI]];
01292 
01293             maxLayers[ppPointI] = max(wantedLayers, maxLayers[ppPointI]);
01294             minLayers[ppPointI] = min(wantedLayers, minLayers[ppPointI]);
01295         }
01296     }
01297 
01298     syncTools::syncPointList
01299     (
01300         mesh,
01301         pp.meshPoints(),
01302         maxLayers,
01303         maxEqOp<label>(),
01304         labelMin,           // null value
01305         false               // no separation
01306     );
01307     syncTools::syncPointList
01308     (
01309         mesh,
01310         pp.meshPoints(),
01311         minLayers,
01312         minEqOp<label>(),
01313         labelMax,           // null value
01314         false               // no separation
01315     );
01316 
01317     // Unmark any point with different min and max
01318     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01319 
01320     //label nConflicts = 0;
01321 
01322     forAll(maxLayers, i)
01323     {
01324         if (maxLayers[i] == labelMin || minLayers[i] == labelMax)
01325         {
01326             FatalErrorIn("setNumLayers(..)")
01327                 << "Patchpoint:" << i << " coord:" << pp.localPoints()[i]
01328                 << " maxLayers:" << maxLayers
01329                 << " minLayers:" << minLayers
01330                 << abort(FatalError);
01331         }
01332         else if (maxLayers[i] == minLayers[i])
01333         {
01334             // Ok setting.
01335             patchNLayers[i] = maxLayers[i];
01336         }
01337         else
01338         {
01339             // Inconsistent num layers between patch faces using point
01340             //if
01341             //(
01342             //    unmarkExtrusion
01343             //    (
01344             //        i,
01345             //        patchDisp,
01346             //        patchNLayers,
01347             //        extrudeStatus
01348             //    )
01349             //)
01350             //{
01351             //    nConflicts++;
01352             //}
01353             patchNLayers[i] = maxLayers[i];
01354         }
01355     }
01356 
01357     //reduce(nConflicts, sumOp<label>());
01358     //
01359     //Info<< "Set displacement to zero for " << nConflicts
01360     //    << " points due to points being on multiple regions"
01361     //    << " with inconsistent nLayers specification." << endl;
01362 }
01363 
01364 
01365 // Grow no-extrusion layer
01366 void Foam::autoLayerDriver::growNoExtrusion
01367 (
01368     const indirectPrimitivePatch& pp,
01369     pointField& patchDisp,
01370     labelList& patchNLayers,
01371     List<extrudeMode>& extrudeStatus
01372 )
01373 {
01374     Info<< nl << "Growing non-extrusion points by one layer ..." << endl;
01375 
01376     List<extrudeMode> grownExtrudeStatus(extrudeStatus);
01377 
01378     const faceList& localFaces = pp.localFaces();
01379 
01380     label nGrown = 0;
01381 
01382     forAll(localFaces, faceI)
01383     {
01384         const face& f = localFaces[faceI];
01385 
01386         bool hasSqueeze = false;
01387         forAll(f, fp)
01388         {
01389             if (extrudeStatus[f[fp]] == NOEXTRUDE)
01390             {
01391                 hasSqueeze = true;
01392                 break;
01393             }
01394         }
01395 
01396         if (hasSqueeze)
01397         {
01398             // Squeeze all points of face
01399             forAll(f, fp)
01400             {
01401                 if
01402                 (
01403                     extrudeStatus[f[fp]] == NOEXTRUDE
01404                  && grownExtrudeStatus[f[fp]] != NOEXTRUDE
01405                 )
01406                 {
01407                     grownExtrudeStatus[f[fp]] = NOEXTRUDE;
01408                     nGrown++;
01409                 }
01410             }
01411         }
01412     }
01413 
01414     extrudeStatus.transfer(grownExtrudeStatus);
01415 
01416     forAll(extrudeStatus, patchPointI)
01417     {
01418         if (extrudeStatus[patchPointI] == NOEXTRUDE)
01419         {
01420             patchDisp[patchPointI] = vector::zero;
01421             patchNLayers[patchPointI] = 0;
01422         }
01423     }
01424 
01425     reduce(nGrown, sumOp<label>());
01426 
01427     Info<< "Set displacement to zero for an additional " << nGrown
01428         << " points." << endl;
01429 }
01430 
01431 
01432 void Foam::autoLayerDriver::calculateLayerThickness
01433 (
01434     const indirectPrimitivePatch& pp,
01435     const labelList& patchIDs,
01436     const scalarField& patchExpansionRatio,
01437 
01438     const bool relativeSizes,
01439     const scalarField& patchFinalLayerThickness,
01440     const scalarField& patchMinThickness,
01441 
01442     const labelList& cellLevel,
01443     const labelList& patchNLayers,
01444     const scalar edge0Len,
01445 
01446     scalarField& thickness,
01447     scalarField& minThickness,
01448     scalarField& expansionRatio
01449 ) const
01450 {
01451     const fvMesh& mesh = meshRefiner_.mesh();
01452     const polyBoundaryMesh& patches = mesh.boundaryMesh();
01453 
01454 
01455     // Rework patch-wise layer parameters into minimum per point
01456     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01457 
01458     // Reuse input fields
01459     expansionRatio.setSize(pp.nPoints());
01460     expansionRatio = GREAT;
01461     thickness.setSize(pp.nPoints());
01462     thickness = GREAT;
01463     minThickness.setSize(pp.nPoints());
01464     minThickness = GREAT;
01465 
01466     forAll(patchIDs, i)
01467     {
01468         label patchI = patchIDs[i];
01469 
01470         const labelList& meshPoints = patches[patchI].meshPoints();
01471 
01472         forAll(meshPoints, patchPointI)
01473         {
01474             label ppPointI = pp.meshPointMap()[meshPoints[patchPointI]];
01475 
01476             expansionRatio[ppPointI] = min
01477             (
01478                 expansionRatio[ppPointI],
01479                 patchExpansionRatio[patchI]
01480             );
01481             thickness[ppPointI] = min
01482             (
01483                 thickness[ppPointI],
01484                 patchFinalLayerThickness[patchI]
01485             );
01486             minThickness[ppPointI] = min
01487             (
01488                 minThickness[ppPointI],
01489                 patchMinThickness[patchI]
01490             );
01491         }
01492     }
01493 
01494     syncTools::syncPointList
01495     (
01496         mesh,
01497         pp.meshPoints(),
01498         expansionRatio,
01499         minEqOp<scalar>(),
01500         GREAT,              // null value
01501         false               // no separation
01502     );
01503     syncTools::syncPointList
01504     (
01505         mesh,
01506         pp.meshPoints(),
01507         thickness,
01508         minEqOp<scalar>(),
01509         GREAT,              // null value
01510         false               // no separation
01511     );
01512     syncTools::syncPointList
01513     (
01514         mesh,
01515         pp.meshPoints(),
01516         minThickness,
01517         minEqOp<scalar>(),
01518         GREAT,              // null value
01519         false               // no separation
01520     );
01521 
01522 
01523     // Now the thicknesses are set according to the minimum of connected
01524     // patches.
01525 
01526 
01527     // Rework relative thickness into absolute
01528     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01529     // by multiplying with the internal cell size.
01530 
01531     if (relativeSizes)
01532     {
01533         if (min(patchMinThickness) < 0 || max(patchMinThickness) > 2)
01534         {
01535             FatalErrorIn("calculateLayerThickness(..)")
01536                 << "Thickness should be factor of local undistorted cell size."
01537                 << " Valid values are [0..2]." << nl
01538                 << " minThickness:" << patchMinThickness
01539                 << exit(FatalError);
01540         }
01541 
01542 
01543         // Determine per point the max cell level of connected cells
01544         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01545 
01546         labelList maxPointLevel(pp.nPoints(), labelMin);
01547 
01548         forAll(pp, i)
01549         {
01550             label ownLevel = cellLevel[mesh.faceOwner()[pp.addressing()[i]]];
01551 
01552             const face& f = pp.localFaces()[i];
01553 
01554             forAll(f, fp)
01555             {
01556                 maxPointLevel[f[fp]] = max(maxPointLevel[f[fp]], ownLevel);
01557             }
01558         }
01559 
01560         syncTools::syncPointList
01561         (
01562             mesh,
01563             pp.meshPoints(),
01564             maxPointLevel,
01565             maxEqOp<label>(),
01566             labelMin,           // null value
01567             false               // no separation
01568         );
01569 
01570 
01571         forAll(maxPointLevel, pointI)
01572         {
01573             // Find undistorted edge size for this level.
01574             scalar edgeLen = edge0Len/(1<<maxPointLevel[pointI]);
01575             thickness[pointI] *= edgeLen;
01576             minThickness[pointI] *= edgeLen;
01577         }
01578     }
01579 
01580 
01581 
01582     // Rework thickness (of final layer) into overall thickness of all layers
01583     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01584 
01585     forAll(thickness, pointI)
01586     {
01587         // Calculate layer thickness based on expansion ratio
01588         // and final layer height
01589         if (expansionRatio[pointI] == 1)
01590         {
01591             thickness[pointI] *= patchNLayers[pointI];
01592         }
01593         else
01594         {
01595 
01596             scalar invExpansion = 1.0 / expansionRatio[pointI];
01597             label nLay = patchNLayers[pointI];
01598             thickness[pointI] *=
01599                 (1.0 - pow(invExpansion, nLay))
01600               / (1.0 - invExpansion);
01601         }
01602     }
01603 
01604 
01605     //Info<< "calculateLayerThickness : min:" << gMin(thickness)
01606     //    << " max:" << gMax(thickness) << endl;
01607 }
01608 
01609 
01610 // Synchronize displacement among coupled patches.
01611 void Foam::autoLayerDriver::syncPatchDisplacement
01612 (
01613     const motionSmoother& meshMover,
01614     const scalarField& minThickness,
01615     pointField& patchDisp,
01616     labelList& patchNLayers,
01617     List<extrudeMode>& extrudeStatus
01618 ) const
01619 {
01620     const fvMesh& mesh = meshRefiner_.mesh();
01621     const labelList& meshPoints = meshMover.patch().meshPoints();
01622 
01623     label nChangedTotal = 0;
01624 
01625     while (true)
01626     {
01627         label nChanged = 0;
01628 
01629         // Sync displacement (by taking min)
01630         syncTools::syncPointList
01631         (
01632             mesh,
01633             meshPoints,
01634             patchDisp,
01635             minEqOp<vector>(),
01636             wallPoint::greatPoint,          // null value
01637             false                           // no separation
01638         );
01639 
01640         // Unmark if displacement too small
01641         forAll(patchDisp, i)
01642         {
01643             if (mag(patchDisp[i]) < minThickness[i])
01644             {
01645                 if
01646                 (
01647                     unmarkExtrusion
01648                     (
01649                         i,
01650                         patchDisp,
01651                         patchNLayers,
01652                         extrudeStatus
01653                     )
01654                 )
01655                 {
01656                     nChanged++;
01657                 }
01658             }
01659         }
01660 
01661         labelList syncPatchNLayers(patchNLayers);
01662 
01663         syncTools::syncPointList
01664         (
01665             mesh,
01666             meshPoints,
01667             syncPatchNLayers,
01668             minEqOp<label>(),
01669             labelMax,           // null value
01670             false               // no separation
01671         );
01672 
01673         // Reset if differs
01674         forAll(syncPatchNLayers, i)
01675         {
01676             if (syncPatchNLayers[i] != patchNLayers[i])
01677             {
01678                 if
01679                 (
01680                     unmarkExtrusion
01681                     (
01682                         i,
01683                         patchDisp,
01684                         patchNLayers,
01685                         extrudeStatus
01686                     )
01687                 )
01688                 {
01689                     nChanged++;
01690                 }
01691             }
01692         }
01693 
01694         syncTools::syncPointList
01695         (
01696             mesh,
01697             meshPoints,
01698             syncPatchNLayers,
01699             maxEqOp<label>(),
01700             labelMin,           // null value
01701             false               // no separation
01702         );
01703 
01704         // Reset if differs
01705         forAll(syncPatchNLayers, i)
01706         {
01707             if (syncPatchNLayers[i] != patchNLayers[i])
01708             {
01709                 if
01710                 (
01711                     unmarkExtrusion
01712                     (
01713                         i,
01714                         patchDisp,
01715                         patchNLayers,
01716                         extrudeStatus
01717                     )
01718                 )
01719                 {
01720                     nChanged++;
01721                 }
01722             }
01723         }
01724         nChangedTotal += nChanged;
01725 
01726         if (!returnReduce(nChanged, sumOp<label>()))
01727         {
01728             break;
01729         }
01730     }
01731 
01732     Info<< "Prevented extrusion on "
01733         << returnReduce(nChangedTotal, sumOp<label>())
01734         << " coupled patch points during syncPatchDisplacement." << endl;
01735 }
01736 
01737 
01738 // Calculate displacement vector for all patch points. Uses pointNormal.
01739 // Checks that displaced patch point would be visible from all centres
01740 // of the faces using it.
01741 // extrudeStatus is both input and output and gives the status of each
01742 // patch point.
01743 void Foam::autoLayerDriver::getPatchDisplacement
01744 (
01745     const motionSmoother& meshMover,
01746     const scalarField& thickness,
01747     const scalarField& minThickness,
01748     pointField& patchDisp,
01749     labelList& patchNLayers,
01750     List<extrudeMode>& extrudeStatus
01751 ) const
01752 {
01753     Info<< nl << "Determining displacement for added points"
01754         << " according to pointNormal ..." << endl;
01755 
01756     const fvMesh& mesh = meshRefiner_.mesh();
01757     const indirectPrimitivePatch& pp = meshMover.patch();
01758     const vectorField& faceNormals = pp.faceNormals();
01759     const labelListList& pointFaces = pp.pointFaces();
01760     const pointField& localPoints = pp.localPoints();
01761     const labelList& meshPoints = pp.meshPoints();
01762 
01763     // Determine pointNormal
01764     // ~~~~~~~~~~~~~~~~~~~~~
01765 
01766     pointField pointNormals(pp.nPoints(), vector::zero);
01767     {
01768         labelList nPointFaces(pp.nPoints(), 0);
01769 
01770         forAll(faceNormals, faceI)
01771         {
01772             const face& f = pp.localFaces()[faceI];
01773 
01774             forAll(f, fp)
01775             {
01776                 pointNormals[f[fp]] += faceNormals[faceI];
01777                 nPointFaces[f[fp]] ++;
01778             }
01779         }
01780 
01781         syncTools::syncPointList
01782         (
01783             mesh,
01784             meshPoints,
01785             pointNormals,
01786             plusEqOp<vector>(),
01787             vector::zero,       // null value
01788             false               // no separation
01789         );
01790 
01791         syncTools::syncPointList
01792         (
01793             mesh,
01794             meshPoints,
01795             nPointFaces,
01796             plusEqOp<label>(),
01797             0,                  // null value
01798             false               // no separation
01799         );
01800 
01801         forAll(pointNormals, i)
01802         {
01803             pointNormals[i] /= nPointFaces[i];
01804         }
01805     }
01806 
01807 
01808     // Determine local length scale on patch
01809     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01810 
01811     // Start off from same thickness everywhere (except where no extrusion)
01812     patchDisp = thickness*pointNormals;
01813 
01814     // Check if no extrude possible.
01815     forAll(pointNormals, patchPointI)
01816     {
01817         label meshPointI = pp.meshPoints()[patchPointI];
01818 
01819         if (extrudeStatus[patchPointI] == NOEXTRUDE)
01820         {
01821             // Do not use unmarkExtrusion; forcibly set to zero extrusion.
01822             patchNLayers[patchPointI] = 0;
01823             patchDisp[patchPointI] = vector::zero;
01824         }
01825         else
01826         {
01827             // Get normal
01828             const vector& n = pointNormals[patchPointI];
01829 
01830             if (!meshTools::visNormal(n, faceNormals, pointFaces[patchPointI]))
01831             {
01832                 Pout<< "No valid normal for point " << meshPointI
01833                     << ' ' << pp.points()[meshPointI]
01834                     << "; setting displacement to " << patchDisp[patchPointI]
01835                     << endl;
01836 
01837                 extrudeStatus[patchPointI] = EXTRUDEREMOVE;
01838             }
01839         }
01840     }
01841 
01842     // At illegal points make displacement average of new neighbour positions
01843     forAll(extrudeStatus, patchPointI)
01844     {
01845         if (extrudeStatus[patchPointI] == EXTRUDEREMOVE)
01846         {
01847             point avg(vector::zero);
01848             label nPoints = 0;
01849 
01850             const labelList& pEdges = pp.pointEdges()[patchPointI];
01851 
01852             forAll(pEdges, i)
01853             {
01854                 label edgeI = pEdges[i];
01855 
01856                 label otherPointI = pp.edges()[edgeI].otherVertex(patchPointI);
01857 
01858                 if (extrudeStatus[otherPointI] != NOEXTRUDE)
01859                 {
01860                     avg += localPoints[otherPointI] + patchDisp[otherPointI];
01861                     nPoints++;
01862                 }
01863             }
01864 
01865             if (nPoints > 0)
01866             {
01867                 Pout<< "Displacement at illegal point "
01868                     << localPoints[patchPointI]
01869                     << " set to " << (avg / nPoints - localPoints[patchPointI])
01870                     << endl;
01871 
01872                 patchDisp[patchPointI] =
01873                     avg / nPoints
01874                   - localPoints[patchPointI];
01875             }
01876         }
01877     }
01878 
01879     // Make sure displacement is equal on both sides of coupled patches.
01880     syncPatchDisplacement
01881     (
01882         meshMover,
01883         minThickness,
01884         patchDisp,
01885         patchNLayers,
01886         extrudeStatus
01887     );
01888 
01889     Info<< endl;
01890 }
01891 
01892 
01893 // Truncates displacement
01894 // - for all patchFaces in the faceset displacement gets set to zero
01895 // - all displacement < minThickness gets set to zero
01896 Foam::label Foam::autoLayerDriver::truncateDisplacement
01897 (
01898     const motionSmoother& meshMover,
01899     const scalarField& minThickness,
01900     const faceSet& illegalPatchFaces,
01901     pointField& patchDisp,
01902     labelList& patchNLayers,
01903     List<extrudeMode>& extrudeStatus
01904 ) const
01905 {
01906     const polyMesh& mesh = meshMover.mesh();
01907     const indirectPrimitivePatch& pp = meshMover.patch();
01908 
01909     label nChanged = 0;
01910 
01911     const Map<label>& meshPointMap = pp.meshPointMap();
01912 
01913     forAllConstIter(faceSet, illegalPatchFaces, iter)
01914     {
01915         label faceI = iter.key();
01916 
01917         if (mesh.isInternalFace(faceI))
01918         {
01919             FatalErrorIn("truncateDisplacement(..)")
01920                 << "Faceset " << illegalPatchFaces.name()
01921                 << " contains internal face " << faceI << nl
01922                 << "It should only contain patch faces" << abort(FatalError);
01923         }
01924 
01925         const face& f = mesh.faces()[faceI];
01926 
01927 
01928         forAll(f, fp)
01929         {
01930             if (meshPointMap.found(f[fp]))
01931             {
01932                 label patchPointI = meshPointMap[f[fp]];
01933 
01934                 if (extrudeStatus[patchPointI] != NOEXTRUDE)
01935                 {
01936                     unmarkExtrusion
01937                     (
01938                         patchPointI,
01939                         patchDisp,
01940                         patchNLayers,
01941                         extrudeStatus
01942                     );
01943                     nChanged++;
01944                 }
01945             }
01946         }
01947     }
01948 
01949     forAll(patchDisp, patchPointI)
01950     {
01951         if (mag(patchDisp[patchPointI]) < minThickness[patchPointI])
01952         {
01953             if
01954             (
01955                 unmarkExtrusion
01956                 (
01957                     patchPointI,
01958                     patchDisp,
01959                     patchNLayers,
01960                     extrudeStatus
01961                 )
01962             )
01963             {
01964                 nChanged++;
01965             }
01966         }
01967         else if (extrudeStatus[patchPointI] == NOEXTRUDE)
01968         {
01969             // Make sure displacement is 0. Should already be so but ...
01970             patchDisp[patchPointI] = vector::zero;
01971             patchNLayers[patchPointI] = 0;
01972         }
01973     }
01974 
01975 
01976     const faceList& localFaces = pp.localFaces();
01977 
01978     while (true)
01979     {
01980         syncPatchDisplacement
01981         (
01982             meshMover,
01983             minThickness,
01984             patchDisp,
01985             patchNLayers,
01986             extrudeStatus
01987         );
01988 
01989         // Make sure that a face doesn't have two non-consecutive areas
01990         // not extruded (e.g. quad where vertex 0 and 2 are not extruded
01991         // but 1 and 3 are) since this gives topological errors.
01992 
01993         label nPinched = 0;
01994 
01995         forAll(localFaces, i)
01996         {
01997             const face& localF = localFaces[i];
01998 
01999             // Count number of transitions from unsnapped to snapped.
02000             label nTrans = 0;
02001 
02002             extrudeMode prevMode = extrudeStatus[localF.prevLabel(0)];
02003 
02004             forAll(localF, fp)
02005             {
02006                 extrudeMode fpMode = extrudeStatus[localF[fp]];
02007 
02008                 if (prevMode == NOEXTRUDE && fpMode != NOEXTRUDE)
02009                 {
02010                     nTrans++;
02011                 }
02012                 prevMode = fpMode;
02013             }
02014 
02015             if (nTrans > 1)
02016             {
02017                 // Multiple pinches. Reset whole face as unextruded.
02018                 if
02019                 (
02020                     unmarkExtrusion
02021                     (
02022                         localF,
02023                         patchDisp,
02024                         patchNLayers,
02025                         extrudeStatus
02026                     )
02027                 )
02028                 {
02029                     nPinched++;
02030                     nChanged++;
02031                 }
02032             }
02033         }
02034 
02035         reduce(nPinched, sumOp<label>());
02036 
02037         Info<< "truncateDisplacement : Unextruded " << nPinched
02038             << " faces due to non-consecutive vertices being extruded." << endl;
02039 
02040 
02041         // Make sure that a face has consistent number of layers for all
02042         // its vertices.
02043 
02044         label nDiffering = 0;
02045 
02046         //forAll(localFaces, i)
02047         //{
02048         //    const face& localF = localFaces[i];
02049         //
02050         //    label numLayers = -1;
02051         //
02052         //    forAll(localF, fp)
02053         //    {
02054         //        if (patchNLayers[localF[fp]] > 0)
02055         //        {
02056         //            if (numLayers == -1)
02057         //            {
02058         //                numLayers = patchNLayers[localF[fp]];
02059         //            }
02060         //            else if (numLayers != patchNLayers[localF[fp]])
02061         //            {
02062         //                // Differing number of layers
02063         //                if
02064         //                (
02065         //                    unmarkExtrusion
02066         //                    (
02067         //                        localF,
02068         //                        patchDisp,
02069         //                        patchNLayers,
02070         //                        extrudeStatus
02071         //                    )
02072         //                )
02073         //                {
02074         //                    nDiffering++;
02075         //                    nChanged++;
02076         //                }
02077         //                break;
02078         //            }
02079         //        }
02080         //    }
02081         //}
02082         //
02083         //reduce(nDiffering, sumOp<label>());
02084         //
02085         //Info<< "truncateDisplacement : Unextruded " << nDiffering
02086         //    << " faces due to having differing number of layers." << endl;
02087 
02088         if (nPinched+nDiffering == 0)
02089         {
02090             break;
02091         }
02092     }
02093 
02094     return nChanged;
02095 }
02096 
02097 
02098 // Setup layer information (at points and faces) to modify mesh topology in
02099 // regions where layer mesh terminates.
02100 void Foam::autoLayerDriver::setupLayerInfoTruncation
02101 (
02102     const motionSmoother& meshMover,
02103     const labelList& patchNLayers,
02104     const List<extrudeMode>& extrudeStatus,
02105     const label nBufferCellsNoExtrude,
02106     labelList& nPatchPointLayers,
02107     labelList& nPatchFaceLayers
02108 ) const
02109 {
02110     Info<< nl << "Setting up information for layer truncation ..." << endl;
02111 
02112     const indirectPrimitivePatch& pp = meshMover.patch();
02113     const polyMesh& mesh = meshMover.mesh();
02114 
02115     if (nBufferCellsNoExtrude < 0)
02116     {
02117         Info<< nl << "Performing no layer truncation."
02118             << " nBufferCellsNoExtrude set to less than 0  ..." << endl;
02119 
02120         // Face layers if any point get extruded
02121         forAll(pp.localFaces(), patchFaceI)
02122         {
02123             const face& f = pp.localFaces()[patchFaceI];
02124 
02125             forAll(f, fp)
02126             {
02127                 if (patchNLayers[f[fp]] > 0)
02128                 {
02129                     nPatchFaceLayers[patchFaceI] = patchNLayers[f[fp]];
02130                     break;
02131                 }
02132             }
02133         }
02134         nPatchPointLayers = patchNLayers;
02135     }
02136     else
02137     {
02138         // Determine max point layers per face.
02139         labelList maxLevel(pp.size(), 0);
02140 
02141         forAll(pp.localFaces(), patchFaceI)
02142         {
02143             const face& f = pp.localFaces()[patchFaceI];
02144 
02145             // find patch faces where layer terminates (i.e contains extrude
02146             // and noextrude points).
02147 
02148             bool noExtrude = false;
02149             label mLevel = 0;
02150 
02151             forAll(f, fp)
02152             {
02153                 if (extrudeStatus[f[fp]] == NOEXTRUDE)
02154                 {
02155                     noExtrude = true;
02156                 }
02157                 mLevel = max(mLevel, patchNLayers[f[fp]]);
02158             }
02159 
02160             if (mLevel > 0)
02161             {
02162                 // So one of the points is extruded. Check if all are extruded
02163                 // or is a mix.
02164 
02165                 if (noExtrude)
02166                 {
02167                     nPatchFaceLayers[patchFaceI] = 1;
02168                     maxLevel[patchFaceI] = mLevel;
02169                 }
02170                 else
02171                 {
02172                     maxLevel[patchFaceI] = mLevel;
02173                 }
02174             }
02175         }
02176 
02177         // We have the seed faces (faces with nPatchFaceLayers != maxLevel)
02178         // Now do a meshwave across the patch where we pick up neighbours
02179         // of seed faces.
02180         // Note: quite inefficient. Could probably be coded better.
02181 
02182         const labelListList& pointFaces = pp.pointFaces();
02183 
02184         label nLevels = gMax(patchNLayers);
02185 
02186         // flag neighbouring patch faces with number of layers to grow
02187         for (label ilevel = 1; ilevel < nLevels; ilevel++)
02188         {
02189             label nBuffer;
02190 
02191             if (ilevel == 1)
02192             {
02193                 nBuffer = nBufferCellsNoExtrude - 1;
02194             }
02195             else
02196             {
02197                 nBuffer = nBufferCellsNoExtrude;
02198             }
02199 
02200             for (label ibuffer = 0; ibuffer < nBuffer + 1; ibuffer++)
02201             {
02202                 labelList tempCounter(nPatchFaceLayers);
02203 
02204                 boolList foundNeighbour(pp.nPoints(), false);
02205 
02206                 forAll(pp.meshPoints(), patchPointI)
02207                 {
02208                     forAll(pointFaces[patchPointI], pointFaceI)
02209                     {
02210                         label faceI = pointFaces[patchPointI][pointFaceI];
02211 
02212                         if
02213                         (
02214                             nPatchFaceLayers[faceI] != -1
02215                          && maxLevel[faceI] > 0
02216                         )
02217                         {
02218                             foundNeighbour[patchPointI] = true;
02219                             break;
02220                         }
02221                     }
02222                 }
02223 
02224                 syncTools::syncPointList
02225                 (
02226                     mesh,
02227                     pp.meshPoints(),
02228                     foundNeighbour,
02229                     orEqOp<bool>(),
02230                     false,              // null value
02231                     false               // no separation
02232                 );
02233 
02234                 forAll(pp.meshPoints(), patchPointI)
02235                 {
02236                     if (foundNeighbour[patchPointI])
02237                     {
02238                         forAll(pointFaces[patchPointI], pointFaceI)
02239                         {
02240                             label faceI = pointFaces[patchPointI][pointFaceI];
02241                             if
02242                             (
02243                                 nPatchFaceLayers[faceI] == -1
02244                              && maxLevel[faceI] > 0
02245                              && ilevel < maxLevel[faceI]
02246                             )
02247                             {
02248                                 tempCounter[faceI] = ilevel;
02249                             }
02250                         }
02251                     }
02252                 }
02253                 nPatchFaceLayers = tempCounter;
02254             }
02255         }
02256 
02257         forAll(pp.localFaces(), patchFaceI)
02258         {
02259             if (nPatchFaceLayers[patchFaceI] == -1)
02260             {
02261                 nPatchFaceLayers[patchFaceI] = maxLevel[patchFaceI];
02262             }
02263         }
02264 
02265         forAll(pp.meshPoints(), patchPointI)
02266         {
02267             if (extrudeStatus[patchPointI] != NOEXTRUDE)
02268             {
02269                 forAll(pointFaces[patchPointI], pointFaceI)
02270                 {
02271                     label face = pointFaces[patchPointI][pointFaceI];
02272                     nPatchPointLayers[patchPointI] = max
02273                     (
02274                         nPatchPointLayers[patchPointI],
02275                         nPatchFaceLayers[face]
02276                     );
02277                 }
02278             }
02279             else
02280             {
02281                 nPatchPointLayers[patchPointI] = 0;
02282             }
02283         }
02284         syncTools::syncPointList
02285         (
02286             mesh,
02287             pp.meshPoints(),
02288             nPatchPointLayers,
02289             maxEqOp<label>(),
02290             0,                  // null value
02291             false               // no separation
02292         );
02293     }
02294 }
02295 
02296 
02297 // Does any of the cells use a face from faces?
02298 bool Foam::autoLayerDriver::cellsUseFace
02299 (
02300     const polyMesh& mesh,
02301     const labelList& cellLabels,
02302     const labelHashSet& faces
02303 )
02304 {
02305     forAll(cellLabels, i)
02306     {
02307         const cell& cFaces = mesh.cells()[cellLabels[i]];
02308 
02309         forAll(cFaces, cFaceI)
02310         {
02311             if (faces.found(cFaces[cFaceI]))
02312             {
02313                 return true;
02314             }
02315         }
02316     }
02317     return false;
02318 }
02319 
02320 
02321 // Checks the newly added cells and locally unmarks points so they
02322 // will not get extruded next time round. Returns global number of unmarked
02323 // points (0 if all was fine)
02324 Foam::label Foam::autoLayerDriver::checkAndUnmark
02325 (
02326     const addPatchCellLayer& addLayer,
02327     const dictionary& meshQualityDict,
02328     const indirectPrimitivePatch& pp,
02329     const fvMesh& newMesh,
02330 
02331     pointField& patchDisp,
02332     labelList& patchNLayers,
02333     List<extrudeMode>& extrudeStatus
02334 )
02335 {
02336     // Check the resulting mesh for errors
02337     Info<< nl << "Checking mesh with layer ..." << endl;
02338     faceSet wrongFaces(newMesh, "wrongFaces", newMesh.nFaces()/1000);
02339     motionSmoother::checkMesh(false, newMesh, meshQualityDict, wrongFaces);
02340     Info<< "Detected " << returnReduce(wrongFaces.size(), sumOp<label>())
02341         << " illegal faces"
02342         << " (concave, zero area or negative cell pyramid volume)"
02343         << endl;
02344 
02345     // Undo local extrusion if
02346     // - any of the added cells in error
02347 
02348     label nChanged = 0;
02349 
02350     // Get all cells in the layer.
02351     labelListList addedCells
02352     (
02353         addPatchCellLayer::addedCells
02354         (
02355             newMesh,
02356             addLayer.layerFaces()
02357         )
02358     );
02359 
02360     // Check if any of the faces in error uses any face of an added cell
02361     forAll(addedCells, oldPatchFaceI)
02362     {
02363         // Get the cells (in newMesh labels) per old patch face (in mesh
02364         // labels)
02365         const labelList& fCells = addedCells[oldPatchFaceI];
02366 
02367         if (cellsUseFace(newMesh, fCells, wrongFaces))
02368         {
02369             // Unmark points on old mesh
02370             if
02371             (
02372                 unmarkExtrusion
02373                 (
02374                     pp.localFaces()[oldPatchFaceI],
02375                     patchDisp,
02376                     patchNLayers,
02377                     extrudeStatus
02378                 )
02379             )
02380             {
02381                 nChanged++;
02382             }
02383         }
02384     }
02385 
02386     return returnReduce(nChanged, sumOp<label>());
02387 }
02388 
02389 
02390 //- Count global number of extruded faces
02391 Foam::label Foam::autoLayerDriver::countExtrusion
02392 (
02393     const indirectPrimitivePatch& pp,
02394     const List<extrudeMode>& extrudeStatus
02395 )
02396 {
02397     // Count number of extruded patch faces
02398     label nExtruded = 0;
02399     {
02400         const faceList& localFaces = pp.localFaces();
02401 
02402         forAll(localFaces, i)
02403         {
02404             const face& localFace = localFaces[i];
02405 
02406             forAll(localFace, fp)
02407             {
02408                 if (extrudeStatus[localFace[fp]] != NOEXTRUDE)
02409                 {
02410                     nExtruded++;
02411                     break;
02412                 }
02413             }
02414         }
02415     }
02416 
02417     return returnReduce(nExtruded, sumOp<label>());
02418 }
02419 
02420 
02421 // Collect layer faces and layer cells into bools for ease of handling
02422 void Foam::autoLayerDriver::getLayerCellsFaces
02423 (
02424     const polyMesh& mesh,
02425     const addPatchCellLayer& addLayer,
02426     boolList& flaggedCells,
02427     boolList& flaggedFaces
02428 )
02429 {
02430     flaggedCells.setSize(mesh.nCells());
02431     flaggedCells = false;
02432     flaggedFaces.setSize(mesh.nFaces());
02433     flaggedFaces = false;
02434 
02435     // Mark all faces in the layer
02436     const labelListList& layerFaces = addLayer.layerFaces();
02437 
02438     // Mark all cells in the layer.
02439     labelListList addedCells(addPatchCellLayer::addedCells(mesh, layerFaces));
02440 
02441     forAll(addedCells, oldPatchFaceI)
02442     {
02443         const labelList& added = addedCells[oldPatchFaceI];
02444 
02445         forAll(added, i)
02446         {
02447             flaggedCells[added[i]] = true;
02448         }
02449     }
02450 
02451     forAll(layerFaces, oldPatchFaceI)
02452     {
02453         const labelList& layer = layerFaces[oldPatchFaceI];
02454 
02455         if (layer.size())
02456         {
02457             for (label i = 1; i < layer.size()-1; i++)
02458             {
02459                 flaggedFaces[layer[i]] = true;
02460             }
02461         }
02462     }
02463 }
02464 
02465 
02466 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
02467 
02468 Foam::autoLayerDriver::autoLayerDriver(meshRefinement& meshRefiner)
02469 :
02470     meshRefiner_(meshRefiner)
02471 {}
02472 
02473 
02474 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
02475 
02476 void Foam::autoLayerDriver::mergePatchFacesUndo
02477 (
02478     const layerParameters& layerParams,
02479     const dictionary& motionDict
02480 )
02481 {
02482     scalar minCos = Foam::cos
02483     (
02484         layerParams.featureAngle()
02485       * mathematicalConstant::pi/180.0
02486     );
02487 
02488     scalar concaveCos = Foam::cos
02489     (
02490         layerParams.concaveAngle()
02491       * mathematicalConstant::pi/180.0
02492     );
02493 
02494     Info<< nl
02495         << "Merging all faces of a cell" << nl
02496         << "---------------------------" << nl
02497         << "    - which are on the same patch" << nl
02498         << "    - which make an angle < " << layerParams.featureAngle()
02499         << " degrees"
02500         << nl
02501         << "      (cos:" << minCos << ')' << nl
02502         << "    - as long as the resulting face doesn't become concave"
02503         << " by more than "
02504         << layerParams.concaveAngle() << " degrees" << nl
02505         << "      (0=straight, 180=fully concave)" << nl
02506         << endl;
02507 
02508     label nChanged = mergePatchFacesUndo(minCos, concaveCos, motionDict);
02509 
02510     nChanged += mergeEdgesUndo(minCos, motionDict);
02511 }
02512 
02513 
02514 void Foam::autoLayerDriver::addLayers
02515 (
02516     const layerParameters& layerParams,
02517     const dictionary& motionDict,
02518     const label nAllowableErrors,
02519     motionSmoother& meshMover,
02520     decompositionMethod& decomposer,
02521     fvMeshDistribute& distributor
02522 )
02523 {
02524     fvMesh& mesh = meshRefiner_.mesh();
02525     const indirectPrimitivePatch& pp = meshMover.patch();
02526     const labelList& meshPoints = pp.meshPoints();
02527 
02528     // Precalculate mesh edge labels for patch edges
02529     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
02530 
02531     labelList meshEdges(pp.nEdges());
02532     forAll(pp.edges(), edgeI)
02533     {
02534         const edge& ppEdge = pp.edges()[edgeI];
02535         label v0 = meshPoints[ppEdge[0]];
02536         label v1 = meshPoints[ppEdge[1]];
02537         meshEdges[edgeI] = meshTools::findEdge
02538         (
02539             mesh.edges(),
02540             mesh.pointEdges()[v0],
02541             v0,
02542             v1
02543         );
02544     }
02545 
02546     // Displacement for all pp.localPoints.
02547     vectorField patchDisp(pp.nPoints(), vector::one);
02548 
02549     // Number of layers for all pp.localPoints. Note: only valid if
02550     // extrudeStatus = EXTRUDE.
02551     labelList patchNLayers(pp.nPoints(), 0);
02552 
02553     // Whether to add edge for all pp.localPoints.
02554     List<extrudeMode> extrudeStatus(pp.nPoints(), EXTRUDE);
02555 
02556 
02557     // Get number of layer per point from number of layers per patch
02558     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
02559 
02560     setNumLayers
02561     (
02562         layerParams.numLayers(),    // per patch the num layers
02563         meshMover.adaptPatchIDs(),  // patches that are being moved
02564         pp,                         // indirectpatch for all faces moving
02565 
02566         patchDisp,
02567         patchNLayers,
02568         extrudeStatus
02569     );
02570 
02571     // Disable extrusion on non-manifold points
02572     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
02573 
02574     handleNonManifolds
02575     (
02576         pp,
02577         meshEdges,
02578 
02579         patchDisp,
02580         patchNLayers,
02581         extrudeStatus
02582     );
02583 
02584     // Disable extrusion on feature angles
02585     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
02586 
02587     handleFeatureAngle
02588     (
02589         pp,
02590         meshEdges,
02591         layerParams.featureAngle()*mathematicalConstant::pi/180.0,
02592 
02593         patchDisp,
02594         patchNLayers,
02595         extrudeStatus
02596     );
02597 
02598     // Disable extrusion on warped faces
02599     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
02600 
02601     // Undistorted edge length
02602     const scalar edge0Len = meshRefiner_.meshCutter().level0EdgeLength();
02603     const labelList& cellLevel = meshRefiner_.meshCutter().cellLevel();
02604 
02605     handleWarpedFaces
02606     (
02607         pp,
02608         layerParams.maxFaceThicknessRatio(),
02609         edge0Len,
02610         cellLevel,
02611 
02612         patchDisp,
02613         patchNLayers,
02614         extrudeStatus
02615     );
02616 
02619     //
02620     //handleMultiplePatchFaces
02621     //(
02622     //    pp,
02623     //
02624     //    patchDisp,
02625     //    patchNLayers,
02626     //    extrudeStatus
02627     //);
02628 
02629 
02630     // Grow out region of non-extrusion
02631     for (label i = 0; i < layerParams.nGrow(); i++)
02632     {
02633         growNoExtrusion
02634         (
02635             pp,
02636             patchDisp,
02637             patchNLayers,
02638             extrudeStatus
02639         );
02640     }
02641 
02642     // Determine (wanted) point-wise layer thickness and expansion ratio
02643     scalarField thickness(pp.nPoints());
02644     scalarField minThickness(pp.nPoints());
02645     scalarField expansionRatio(pp.nPoints());
02646     calculateLayerThickness
02647     (
02648         pp,
02649         meshMover.adaptPatchIDs(),
02650         layerParams.expansionRatio(),
02651 
02652         layerParams.relativeSizes(),        // thickness relative to cellsize?
02653         layerParams.finalLayerThickness(),  // wanted thicknes
02654         layerParams.minThickness(),         // minimum thickness
02655 
02656         cellLevel,
02657         patchNLayers,
02658         edge0Len,
02659 
02660         thickness,
02661         minThickness,
02662         expansionRatio
02663     );
02664 
02665 
02666     // Print a bit
02667     {
02668         const polyBoundaryMesh& patches = mesh.boundaryMesh();
02669 
02670         Info<< nl
02671             << "patch               faces    layers avg thickness[m]" << nl
02672             << "                                    near-wall overall" << nl
02673             << "-----               -----    ------ --------- -------" << endl;
02674 
02675         forAll(meshMover.adaptPatchIDs(), i)
02676         {
02677             label patchI = meshMover.adaptPatchIDs()[i];
02678 
02679             const labelList& meshPoints = patches[patchI].meshPoints();
02680 
02681             //scalar maxThickness = -VGREAT;
02682             //scalar minThickness = VGREAT;
02683             scalar sumThickness = 0;
02684             scalar sumNearWallThickness = 0;
02685 
02686             forAll(meshPoints, patchPointI)
02687             {
02688                 label ppPointI = pp.meshPointMap()[meshPoints[patchPointI]];
02689 
02690                 //maxThickness = max(maxThickness, thickness[ppPointI]);
02691                 //minThickness = min(minThickness, thickness[ppPointI]);
02692                 sumThickness += thickness[ppPointI];
02693 
02694                 label nLay = patchNLayers[ppPointI];
02695                 if (nLay > 0)
02696                 {
02697                     if (expansionRatio[ppPointI] == 1)
02698                     {
02699                         sumNearWallThickness += thickness[ppPointI]/nLay;
02700                     }
02701                     else
02702                     {
02703                         scalar s =
02704                             (1.0-pow(expansionRatio[ppPointI], nLay))
02705                           / (1.0-expansionRatio[ppPointI]);
02706                         sumNearWallThickness += thickness[ppPointI]/s;
02707                     }
02708                 }
02709             }
02710 
02711             label totNPoints = returnReduce(meshPoints.size(), sumOp<label>());
02712 
02713             //reduce(maxThickness, maxOp<scalar>());
02714             //reduce(minThickness, minOp<scalar>());
02715             scalar avgThickness =
02716                 returnReduce(sumThickness, sumOp<scalar>())
02717               / totNPoints;
02718             scalar avgNearWallThickness =
02719                 returnReduce(sumNearWallThickness, sumOp<scalar>())
02720               / totNPoints;
02721 
02722             Info<< setf(ios_base::left) << setw(19) << patches[patchI].name();
02723             //Sout.unsetf(ios_base::left);
02724             Info<< setprecision(3)
02725                 << " " << setw(8)
02726                 << returnReduce(patches[patchI].size(), sumOp<scalar>())
02727                 << " " << setw(6) << layerParams.numLayers()[patchI]
02728                 << " " << setw(8) << avgNearWallThickness
02729                 << "  " << setw(8) << avgThickness
02730                 //<< " " << setw(8) << minThickness
02731                 //<< " " << setw(8) << maxThickness
02732                 << endl;
02733         }
02734         Info<< endl;
02735     }
02736 
02737 
02738     // Calculate wall to medial axis distance for smoothing displacement
02739     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
02740 
02741     pointScalarField pointMedialDist
02742     (
02743         IOobject
02744         (
02745             "pointMedialDist",
02746             meshRefiner_.timeName(),
02747             mesh,
02748             IOobject::NO_READ,
02749             IOobject::NO_WRITE,
02750             false
02751         ),
02752         meshMover.pMesh(),
02753         dimensionedScalar("pointMedialDist", dimless, 0.0)
02754     );
02755 
02756     pointVectorField dispVec
02757     (
02758         IOobject
02759         (
02760             "dispVec",
02761             meshRefiner_.timeName(),
02762             mesh,
02763             IOobject::NO_READ,
02764             IOobject::NO_WRITE,
02765             false
02766         ),
02767         meshMover.pMesh(),
02768         dimensionedVector("dispVec", dimless, vector::zero)
02769     );
02770 
02771     pointScalarField medialRatio
02772     (
02773         IOobject
02774         (
02775             "medialRatio",
02776             meshRefiner_.timeName(),
02777             mesh,
02778             IOobject::NO_READ,
02779             IOobject::NO_WRITE,
02780             false
02781         ),
02782         meshMover.pMesh(),
02783         dimensionedScalar("medialRatio", dimless, 0.0)
02784     );
02785 
02786     // Setup information for medial axis smoothing. Calculates medial axis
02787     // and a smoothed displacement direction.
02788     // - pointMedialDist : distance to medial axis
02789     // - dispVec : normalised direction of nearest displacement
02790     // - medialRatio : ratio of medial distance to wall distance.
02791     //   (1 at wall, 0 at medial axis)
02792     medialAxisSmoothingInfo
02793     (
02794         meshMover,
02795         layerParams.nSmoothNormals(),
02796         layerParams.nSmoothSurfaceNormals(),
02797         layerParams.minMedianAxisAngleCos(),
02798 
02799         dispVec,
02800         medialRatio,
02801         pointMedialDist
02802     );
02803 
02804 
02805 
02806     // Saved old points
02807     pointField oldPoints(mesh.points());
02808 
02809     // Last set of topology changes. (changing mesh clears out polyTopoChange)
02810     polyTopoChange savedMeshMod(mesh.boundaryMesh().size());
02811 
02812     boolList flaggedCells;
02813     boolList flaggedFaces;
02814 
02815     for (label iteration = 0; iteration < layerParams.nLayerIter(); iteration++)
02816     {
02817         Info<< nl
02818             << "Layer addition iteration " << iteration << nl
02819             << "--------------------------" << endl;
02820 
02821 
02822         // Unset the extrusion at the pp.
02823         const dictionary& meshQualityDict =
02824         (
02825             iteration < layerParams.nRelaxedIter()
02826           ? motionDict
02827           : motionDict.subDict("relaxed")
02828         );
02829 
02830         if (iteration >= layerParams.nRelaxedIter())
02831         {
02832             Info<< "Switched to relaxed meshQuality constraints." << endl;
02833         }
02834 
02835 
02836 
02837         // Make sure displacement is equal on both sides of coupled patches.
02838         syncPatchDisplacement
02839         (
02840             meshMover,
02841             minThickness,
02842             patchDisp,
02843             patchNLayers,
02844             extrudeStatus
02845         );
02846 
02847         // Displacement acc. to pointnormals
02848         getPatchDisplacement
02849         (
02850             meshMover,
02851             thickness,
02852             minThickness,
02853             patchDisp,
02854             patchNLayers,
02855             extrudeStatus
02856         );
02857 
02858         // Shrink mesh by displacement value first.
02859         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
02860 
02861         {
02862             pointField oldPatchPos(pp.localPoints());
02863 
02865             //shrinkMeshDistance
02866             //(
02867             //    debug,
02868             //    meshMover,
02869             //    -patchDisp,     // Shrink in opposite direction of addedPoints
02870             //    layerParams.nSmoothDisp(),
02871             //    layerParams.nSnap()
02872             //);
02873 
02874             // Medial axis based shrinking
02875             shrinkMeshMedialDistance
02876             (
02877                 meshMover,
02878                 meshQualityDict,
02879 
02880                 layerParams.nSmoothThickness(),
02881                 layerParams.maxThicknessToMedialRatio(),
02882                 nAllowableErrors,
02883                 layerParams.nSnap(),
02884                 layerParams.layerTerminationCos(),
02885 
02886                 thickness,
02887                 minThickness,
02888 
02889                 dispVec,
02890                 medialRatio,
02891                 pointMedialDist,
02892 
02893                 extrudeStatus,
02894                 patchDisp,
02895                 patchNLayers
02896             );
02897 
02898             // Update patchDisp (since not all might have been honoured)
02899             patchDisp = oldPatchPos - pp.localPoints();
02900         }
02901 
02902         // Truncate displacements that are too small (this will do internal
02903         // ones, coupled ones have already been truncated by syncPatch)
02904         faceSet dummySet(mesh, "wrongPatchFaces", 0);
02905         truncateDisplacement
02906         (
02907             meshMover,
02908             minThickness,
02909             dummySet,
02910             patchDisp,
02911             patchNLayers,
02912             extrudeStatus
02913         );
02914 
02915 
02916         // Dump to .obj file for debugging.
02917         if (debug)
02918         {
02919             dumpDisplacement
02920             (
02921                 mesh.time().path()/"layer",
02922                 pp,
02923                 patchDisp,
02924                 extrudeStatus
02925             );
02926 
02927             const_cast<Time&>(mesh.time())++;
02928             Info<< "Writing shrunk mesh to " << meshRefiner_.timeName() << endl;
02929 
02930             // See comment in autoSnapDriver why we should not remove meshPhi
02931             // using mesh.clearPout().
02932 
02933             mesh.write();
02934         }
02935 
02936 
02937         // Mesh topo change engine
02938         polyTopoChange meshMod(mesh);
02939 
02940         // Grow layer of cells on to patch. Handles zero sized displacement.
02941         addPatchCellLayer addLayer(mesh);
02942 
02943         // Determine per point/per face number of layers to extrude. Also
02944         // handles the slow termination of layers when going switching layers
02945 
02946         labelList nPatchPointLayers(pp.nPoints(),-1);
02947         labelList nPatchFaceLayers(pp.localFaces().size(),-1);
02948         setupLayerInfoTruncation
02949         (
02950             meshMover,
02951             patchNLayers,
02952             extrudeStatus,
02953             layerParams.nBufferCellsNoExtrude(),
02954             nPatchPointLayers,
02955             nPatchFaceLayers
02956         );
02957 
02958         // Calculate displacement for first layer for addPatchLayer.
02959         // (first layer = layer of cells next to the original mesh)
02960         vectorField firstDisp(patchNLayers.size(), vector::zero);
02961 
02962         forAll(patchNLayers, i)
02963         {
02964             if (patchNLayers[i] > 0)
02965             {
02966                 if (expansionRatio[i] == 1.0)
02967                 {
02968                     firstDisp[i] = patchDisp[i]/nPatchPointLayers[i];
02969                 }
02970                 else
02971                 {
02972                     label nLay = nPatchPointLayers[i];
02973                     scalar h =
02974                         pow(expansionRatio[i], nLay - 1)
02975                       * (1.0 - expansionRatio[i])
02976                       / (1.0 - pow(expansionRatio[i], nLay));
02977                     firstDisp[i] = h*patchDisp[i];
02978                 }
02979             }
02980         }
02981 
02982         scalarField invExpansionRatio = 1.0 / expansionRatio;
02983 
02984         // Add topo regardless of whether extrudeStatus is extruderemove.
02985         // Not add layer if patchDisp is zero.
02986         addLayer.setRefinement
02987         (
02988             invExpansionRatio,
02989             pp,
02990             nPatchFaceLayers,   // layers per face
02991             nPatchPointLayers,  // layers per point
02992             firstDisp,          // thickness of layer nearest internal mesh
02993             meshMod
02994         );
02995 
02996         if (debug)
02997         {
02998             const_cast<Time&>(mesh.time())++;
02999         }
03000 
03001         // Store mesh changes for if mesh is correct.
03002         savedMeshMod = meshMod;
03003 
03004 
03005         // With the stored topo changes we create a new mesh so we can
03006         // undo if neccesary.
03007 
03008         autoPtr<fvMesh> newMeshPtr;
03009         autoPtr<mapPolyMesh> map = meshMod.makeMesh
03010         (
03011             newMeshPtr,
03012             IOobject
03013             (
03014                 //mesh.name()+"_layer",
03015                 mesh.name(),
03016                 static_cast<polyMesh&>(mesh).instance(),
03017                 mesh.time(),  // register with runTime
03018                 static_cast<polyMesh&>(mesh).readOpt(),
03019                 static_cast<polyMesh&>(mesh).writeOpt()
03020             ),              // io params from original mesh but new name
03021             mesh,           // original mesh
03022             true            // parallel sync
03023         );
03024         fvMesh& newMesh = newMeshPtr();
03025 
03026         //?neccesary? Update fields
03027         newMesh.updateMesh(map);
03028 
03029         if (meshRefiner_.overwrite())
03030         {
03031             newMesh.setInstance(meshRefiner_.oldInstance());
03032         }
03033 
03034         // Update numbering on addLayer:
03035         // - cell/point labels to be newMesh.
03036         // - patchFaces to remain in oldMesh order.
03037         addLayer.updateMesh
03038         (
03039             map,
03040             identity(pp.size()),
03041             identity(pp.nPoints())
03042         );
03043 
03044         // Collect layer faces and cells for outside loop.
03045         getLayerCellsFaces
03046         (
03047             newMesh,
03048             addLayer,
03049             flaggedCells,
03050             flaggedFaces
03051         );
03052 
03053 
03054         if (debug)
03055         {
03056             Info<< "Writing layer mesh to " << meshRefiner_.timeName() << endl;
03057             newMesh.write();
03058             cellSet addedCellSet
03059             (
03060                 newMesh,
03061                 "addedCells",
03062                 findIndices(flaggedCells, true)
03063             );
03064             Info<< "Writing "
03065                 << returnReduce(addedCellSet.size(), sumOp<label>())
03066                 << " added cells to cellSet "
03067                 << addedCellSet.name() << endl;
03068             addedCellSet.write();
03069 
03070             faceSet layerFacesSet
03071             (
03072                 newMesh,
03073                 "layerFaces",
03074                 findIndices(flaggedCells, true)
03075             );
03076             Info<< "Writing "
03077                 << returnReduce(layerFacesSet.size(), sumOp<label>())
03078                 << " faces inside added layer to faceSet "
03079                 << layerFacesSet.name() << endl;
03080             layerFacesSet.write();
03081         }
03082 
03083 
03084         label nTotChanged = checkAndUnmark
03085         (
03086             addLayer,
03087             meshQualityDict,
03088             pp,
03089             newMesh,
03090 
03091             patchDisp,
03092             patchNLayers,
03093             extrudeStatus
03094         );
03095 
03096         Info<< "Extruding " << countExtrusion(pp, extrudeStatus)
03097             << " out of " << returnReduce(pp.size(), sumOp<label>())
03098             << " faces. Removed extrusion at " << nTotChanged << " faces."
03099             << endl;
03100 
03101         if (nTotChanged == 0)
03102         {
03103             break;
03104         }
03105 
03106         // Reset mesh points and start again
03107         meshMover.movePoints(oldPoints);
03108         meshMover.correct();
03109 
03110         Info<< endl;
03111     }
03112 
03113 
03114     // At this point we have a (shrunk) mesh and a set of topology changes
03115     // which will make a valid mesh with layer. Apply these changes to the
03116     // current mesh.
03117 
03118     // Apply the stored topo changes to the current mesh.
03119     autoPtr<mapPolyMesh> map = savedMeshMod.changeMesh(mesh, false);
03120 
03121     // Update fields
03122     mesh.updateMesh(map);
03123 
03124     // Move mesh (since morphing does not do this)
03125     if (map().hasMotionPoints())
03126     {
03127         mesh.movePoints(map().preMotionPoints());
03128     }
03129     else
03130     {
03131         // Delete mesh volumes.
03132         mesh.clearOut();
03133     }
03134 
03135     if (meshRefiner_.overwrite())
03136     {
03137         mesh.setInstance(meshRefiner_.oldInstance());
03138     }
03139 
03140     meshRefiner_.updateMesh(map, labelList(0));
03141 
03142 
03143     // Do final balancing
03144     // ~~~~~~~~~~~~~~~~~~
03145 
03146     if (Pstream::parRun())
03147     {
03148         Info<< nl
03149             << "Doing final balancing" << nl
03150             << "---------------------" << nl
03151             << endl;
03152 
03153         if (debug)
03154         {
03155             const_cast<Time&>(mesh.time())++;
03156         }
03157 
03158         // Balance. No restriction on face zones and baffles.
03159         autoPtr<mapDistributePolyMesh> map = meshRefiner_.balance
03160         (
03161             false,
03162             false,
03163             decomposer,
03164             distributor
03165         );
03166 
03167         // Re-distribute flag of layer faces and cells
03168         map().distributeCellData(flaggedCells);
03169         map().distributeFaceData(flaggedFaces);
03170     }
03171 
03172 
03173     // Write mesh
03174     // ~~~~~~~~~~
03175 
03176     cellSet addedCellSet(mesh, "addedCells", findIndices(flaggedCells, true));
03177     Info<< "Writing "
03178         << returnReduce(addedCellSet.size(), sumOp<label>())
03179         << " added cells to cellSet "
03180         << addedCellSet.name() << endl;
03181     addedCellSet.write();
03182 
03183     faceSet layerFacesSet(mesh, "layerFaces", findIndices(flaggedFaces, true));
03184     Info<< "Writing "
03185         << returnReduce(layerFacesSet.size(), sumOp<label>())
03186         << " faces inside added layer to faceSet "
03187         << layerFacesSet.name() << endl;
03188     layerFacesSet.write();
03189 }
03190 
03191 
03192 void Foam::autoLayerDriver::doLayers
03193 (
03194     const dictionary& shrinkDict,
03195     const dictionary& motionDict,
03196     const layerParameters& layerParams,
03197     decompositionMethod& decomposer,
03198     fvMeshDistribute& distributor
03199 )
03200 {
03201     fvMesh& mesh = meshRefiner_.mesh();
03202 
03203     Info<< nl
03204         << "Shrinking and layer addition phase" << nl
03205         << "----------------------------------" << nl
03206         << endl;
03207 
03208     Info<< "Using mesh parameters " << motionDict << nl << endl;
03209 
03210     // Merge coplanar boundary faces
03211     mergePatchFacesUndo(layerParams, motionDict);
03212 
03213     // Per patch the number of layers (0 if no layer)
03214     const labelList& numLayers = layerParams.numLayers();
03215 
03216     // Patches that need to get a layer
03217     DynamicList<label> patchIDs(numLayers.size());
03218     label nFacesWithLayers = 0;
03219     forAll(numLayers, patchI)
03220     {
03221         if (numLayers[patchI] > 0)
03222         {
03223             patchIDs.append(patchI);
03224             nFacesWithLayers += mesh.boundaryMesh()[patchI].size();
03225         }
03226     }
03227     patchIDs.shrink();
03228 
03229     if (returnReduce(nFacesWithLayers, sumOp<label>()) == 0)
03230     {
03231         Info<< nl << "No layers to generate ..." << endl;
03232     }
03233     else
03234     {
03235         autoPtr<indirectPrimitivePatch> ppPtr
03236         (
03237             meshRefinement::makePatch
03238             (
03239                 mesh,
03240                 patchIDs
03241             )
03242         );
03243         indirectPrimitivePatch& pp = ppPtr();
03244 
03245         // Construct iterative mesh mover.
03246         Info<< "Constructing mesh displacer ..." << endl;
03247 
03248         {
03249             const pointMesh& pMesh = pointMesh::New(mesh);
03250 
03251             motionSmoother meshMover
03252             (
03253                 mesh,
03254                 pp,
03255                 patchIDs,
03256                 meshRefinement::makeDisplacementField(pMesh, patchIDs),
03257                 motionDict
03258             );
03259 
03260             // Check that outside of mesh is not multiply connected.
03261             checkMeshManifold();
03262 
03263             // Check initial mesh
03264             Info<< "Checking initial mesh ..." << endl;
03265             labelHashSet wrongFaces(mesh.nFaces()/100);
03266             motionSmoother::checkMesh(false, mesh, motionDict, wrongFaces);
03267             const label nInitErrors = returnReduce
03268             (
03269                 wrongFaces.size(),
03270                 sumOp<label>()
03271             );
03272 
03273             Info<< "Detected " << nInitErrors << " illegal faces"
03274                 << " (concave, zero area or negative cell pyramid volume)"
03275                 << endl;
03276 
03277             // Do all topo changes
03278             addLayers
03279             (
03280                 layerParams,
03281                 motionDict,
03282                 nInitErrors,
03283                 meshMover,
03284                 decomposer,
03285                 distributor
03286             );
03287         }
03288     }
03289 }
03290 
03291 
03292 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd