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

autoLayerDriverShrink.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     Shrinking mesh (part of adding cell layers)
00027 
00028 \*----------------------------------------------------------------------------*/
00029 
00030 #include "autoLayerDriver.H"
00031 #include "fvMesh.H"
00032 #include "Time.H"
00033 #include "pointFields.H"
00034 #include "motionSmoother.H"
00035 #include "pointData.H"
00036 #include "PointEdgeWave.H"
00037 
00038 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00039 
00040 
00041 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00042 
00043 // Calculate inverse sum of edge weights (currently always 1.0)
00044 void Foam::autoLayerDriver::sumWeights
00045 (
00046     const PackedBoolList& isMasterEdge,
00047     const labelList& meshEdges,
00048     const labelList& meshPoints,
00049     const edgeList& edges,
00050     scalarField& invSumWeight
00051 ) const
00052 {
00053     invSumWeight = 0;
00054 
00055     forAll(edges, edgeI)
00056     {
00057         if (isMasterEdge.get(meshEdges[edgeI]) == 1)
00058         {
00059             const edge& e = edges[edgeI];
00060             //scalar eWeight = edgeWeights[edgeI];
00061             scalar eWeight = 1.0;
00062 
00063             invSumWeight[e[0]] += eWeight;
00064             invSumWeight[e[1]] += eWeight;
00065         }
00066     }
00067 
00068     syncTools::syncPointList
00069     (
00070         meshRefiner_.mesh(),
00071         meshPoints,
00072         invSumWeight,
00073         plusEqOp<scalar>(),
00074         scalar(0.0),        // null value
00075         false               // no separation
00076     );
00077 
00078     forAll(invSumWeight, pointI)
00079     {
00080         scalar w = invSumWeight[pointI];
00081 
00082         if (w > 0.0)
00083         {
00084             invSumWeight[pointI] = 1.0/w;
00085         }
00086     }
00087 }
00088 
00089 
00090 // Smooth field on moving patch
00091 void Foam::autoLayerDriver::smoothField
00092 (
00093     const motionSmoother& meshMover,
00094     const PackedBoolList& isMasterEdge,
00095     const labelList& meshEdges,
00096     const scalarField& fieldMin,
00097     const label& nSmoothDisp,
00098     scalarField& field
00099 ) const
00100 {
00101     const indirectPrimitivePatch& pp = meshMover.patch();
00102     const edgeList& edges = pp.edges();
00103     const labelList& meshPoints = pp.meshPoints();
00104 
00105     scalarField invSumWeight(pp.nPoints());
00106     sumWeights
00107     (
00108         isMasterEdge,
00109         meshEdges,
00110         meshPoints,
00111         edges,
00112         invSumWeight
00113     );
00114 
00115     // Get smoothly varying patch field.
00116     Info<< "shrinkMeshDistance : Smoothing field ..." << endl;
00117 
00118     for (label iter = 0; iter < nSmoothDisp; iter++)
00119     {
00120         scalarField average(pp.nPoints());
00121         averageNeighbours
00122         (
00123             meshMover.mesh(),
00124             isMasterEdge,
00125             meshEdges,
00126             meshPoints,
00127             pp.edges(),
00128             invSumWeight,
00129             field,
00130             average
00131         );
00132 
00133         // Transfer to field
00134         forAll(field, pointI)
00135         {
00136             //full smoothing neighbours + point value
00137             average[pointI] = 0.5*(field[pointI]+average[pointI]);
00138 
00139             // perform monotonic smoothing
00140             if
00141             (
00142                 average[pointI] < field[pointI]
00143              && average[pointI] >= fieldMin[pointI]
00144             )
00145             {
00146                 field[pointI] = average[pointI];
00147             }
00148         }
00149 
00150         // Do residual calculation every so often.
00151         if ((iter % 10) == 0)
00152         {
00153             Info<< "    Iteration " << iter << "   residual "
00154                 <<  gSum(mag(field-average))
00155                    /returnReduce(average.size(), sumOp<label>())
00156                 << endl;
00157         }
00158     }
00159 }
00160 
00161 
00162 // Smooth normals on moving patch.
00163 void Foam::autoLayerDriver::smoothPatchNormals
00164 (
00165     const motionSmoother& meshMover,
00166     const PackedBoolList& isMasterEdge,
00167     const labelList& meshEdges,
00168     const label nSmoothDisp,
00169     pointField& normals
00170 ) const
00171 {
00172     const indirectPrimitivePatch& pp = meshMover.patch();
00173     const edgeList& edges = pp.edges();
00174     const labelList& meshPoints = pp.meshPoints();
00175 
00176     // Calculate inverse sum of weights
00177 
00178     scalarField invSumWeight(pp.nPoints());
00179     sumWeights
00180     (
00181         isMasterEdge,
00182         meshEdges,
00183         meshPoints,
00184         edges,
00185         invSumWeight
00186     );
00187 
00188     // Get smoothly varying internal normals field.
00189     Info<< "shrinkMeshDistance : Smoothing normals ..." << endl;
00190 
00191     for (label iter = 0; iter < nSmoothDisp; iter++)
00192     {
00193         vectorField average(pp.nPoints());
00194         averageNeighbours
00195         (
00196             meshMover.mesh(),
00197             isMasterEdge,
00198             meshEdges,
00199             meshPoints,
00200             pp.edges(),
00201             invSumWeight,
00202             normals,
00203             average
00204         );
00205 
00206         // Do residual calculation every so often.
00207         if ((iter % 10) == 0)
00208         {
00209             Info<< "    Iteration " << iter << "   residual "
00210                 <<  gSum(mag(normals-average))
00211                    /returnReduce(average.size(), sumOp<label>())
00212                 << endl;
00213         }
00214 
00215         // Transfer to normals vector field
00216         forAll(average, pointI)
00217         {
00218             // full smoothing neighbours + point value
00219             average[pointI] = 0.5*(normals[pointI]+average[pointI]);
00220             normals[pointI] = average[pointI];
00221             normals[pointI] /= mag(normals[pointI]) + VSMALL;
00222         }
00223     }
00224 }
00225 
00226 
00227 // Smooth normals in interior.
00228 void Foam::autoLayerDriver::smoothNormals
00229 (
00230     const label nSmoothDisp,
00231     const PackedBoolList& isMasterEdge,
00232     const labelList& fixedPoints,
00233     pointVectorField& normals
00234 ) const
00235 {
00236     // Get smoothly varying internal normals field.
00237     Info<< "shrinkMeshDistance : Smoothing normals ..." << endl;
00238 
00239     const fvMesh& mesh = meshRefiner_.mesh();
00240     const edgeList& edges = mesh.edges();
00241 
00242     // Points that do not change.
00243     PackedBoolList isFixedPoint(mesh.nPoints(), 0);
00244 
00245     // Internal points that are fixed
00246     forAll(fixedPoints, i)
00247     {
00248         label meshPointI = fixedPoints[i];
00249         isFixedPoint.set(meshPointI, 1);
00250     }
00251 
00252     // Correspondence between local edges/points and mesh edges/points
00253     const labelList meshEdges(identity(mesh.nEdges()));
00254     const labelList meshPoints(identity(mesh.nPoints()));
00255 
00256     // Calculate inverse sum of weights
00257 
00258     scalarField invSumWeight(mesh.nPoints(), 0);
00259     sumWeights
00260     (
00261         isMasterEdge,
00262         meshEdges,
00263         meshPoints,
00264         edges,
00265         invSumWeight
00266     );
00267 
00268     Info<< "shrinkMeshDistance : Smoothing normals in interior ..." << endl;
00269 
00270     for (label iter = 0; iter < nSmoothDisp; iter++)
00271     {
00272         vectorField average(mesh.nPoints());
00273         averageNeighbours
00274         (
00275             mesh,
00276             isMasterEdge,
00277             meshEdges,
00278             meshPoints,
00279             edges,
00280             invSumWeight,
00281             normals,
00282             average
00283         );
00284 
00285         // Do residual calculation every so often.
00286         if ((iter % 10) == 0)
00287         {
00288             Info<< "    Iteration " << iter << "   residual "
00289                 <<  gSum(mag(normals-average))
00290                    /returnReduce(average.size(), sumOp<label>())
00291                 << endl;
00292         }
00293 
00294 
00295         // Transfer to normals vector field
00296         forAll(average, pointI)
00297         {
00298             if (isFixedPoint.get(pointI) == 0)
00299             {
00300                 //full smoothing neighbours + point value
00301                 average[pointI] = 0.5*(normals[pointI]+average[pointI]);
00302                 normals[pointI] = average[pointI];
00303                 normals[pointI] /= mag(normals[pointI]) + VSMALL;
00304             }
00305         }
00306     }
00307 }
00308 
00309 
00310 // Tries and find a medial axis point. Done by comparing vectors to nearest
00311 // wall point for both vertices of edge.
00312 bool Foam::autoLayerDriver::isMaxEdge
00313 (
00314     const List<pointData>& pointWallDist,
00315     const label edgeI,
00316     const scalar minCos
00317 ) const
00318 {
00319     const fvMesh& mesh = meshRefiner_.mesh();
00320     const pointField& points = mesh.points();
00321 
00322     // Do not mark edges with one side on moving wall.
00323 
00324     const edge& e = mesh.edges()[edgeI];
00325 
00326     vector v0(points[e[0]] - pointWallDist[e[0]].origin());
00327     scalar magV0(mag(v0));
00328 
00329     if (magV0 < SMALL)
00330     {
00331         return false;
00332     }
00333 
00334     vector v1(points[e[1]] - pointWallDist[e[1]].origin());
00335     scalar magV1(mag(v1));
00336 
00337     if (magV1 < SMALL)
00338     {
00339         return false;
00340     }
00341 
00342     v0 /= magV0;
00343     v1 /= magV1;
00344 
00345     // Test angle.
00346     if ((v0 & v1) < minCos)
00347     {
00348         return true;
00349     }
00350     else
00351     {
00352         return false;
00353     }
00354 }
00355 
00356 
00357 // Stop layer growth where mesh wraps around edge with a
00358 // large feature angle
00359 void Foam::autoLayerDriver::handleFeatureAngleLayerTerminations
00360 (
00361     const indirectPrimitivePatch& pp,
00362     const scalar minCos,
00363     List<extrudeMode>& extrudeStatus,
00364     pointField& patchDisp,
00365     labelList& patchNLayers,
00366     label& nPointCounter
00367 ) const
00368 {
00369     // Mark faces that have all points extruded
00370     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00371 
00372     boolList extrudedFaces(pp.size(), true);
00373 
00374     forAll(pp.localFaces(), faceI)
00375     {
00376         const face& f = pp.localFaces()[faceI];
00377 
00378         forAll(f, fp)
00379         {
00380             if (extrudeStatus[f[fp]] == NOEXTRUDE)
00381             {
00382                 extrudedFaces[faceI] = false;
00383                 break;
00384             }
00385         }
00386     }
00387 
00388 
00389     // Detect situation where two featureedge-neighbouring faces are partly or
00390     // not extruded and the edge itself is extruded. In this case unmark the
00391     // edge for extrusion.
00392 
00393     forAll(pp.edgeFaces(), edgeI)
00394     {
00395         const labelList& eFaces = pp.edgeFaces()[edgeI];
00396 
00397         if (eFaces.size() == 2)
00398         {
00399             const edge& e = pp.edges()[edgeI];
00400             label v0 = e[0];
00401             label v1 = e[1];
00402 
00403             if
00404             (
00405                 extrudeStatus[v0] != NOEXTRUDE
00406              || extrudeStatus[v1] != NOEXTRUDE
00407             )
00408             {
00409                 if (!extrudedFaces[eFaces[0]] || !extrudedFaces[eFaces[1]])
00410                 {
00411                     const vector& n0 = pp.faceNormals()[eFaces[0]];
00412                     const vector& n1 = pp.faceNormals()[eFaces[1]];
00413 
00414                     if ((n0 & n1) < minCos)
00415                     {
00416                         if
00417                         (
00418                             unmarkExtrusion
00419                             (
00420                                 v0,
00421                                 patchDisp,
00422                                 patchNLayers,
00423                                 extrudeStatus
00424                             )
00425                         )
00426                         {
00427                             nPointCounter++;
00428                         }
00429                         if
00430                         (
00431                             unmarkExtrusion
00432                             (
00433                                 v1,
00434                                 patchDisp,
00435                                 patchNLayers,
00436                                 extrudeStatus
00437                             )
00438                         )
00439                         {
00440                             nPointCounter++;
00441                         }
00442                     }
00443                 }
00444             }
00445         }
00446     }
00447 }
00448 
00449 
00450 // Find isolated islands (points, edges and faces and layer terminations)
00451 // in the layer mesh and stop any layer growth at these points.
00452 void Foam::autoLayerDriver::findIsolatedRegions
00453 (
00454     const indirectPrimitivePatch& pp,
00455     const PackedBoolList& isMasterEdge,
00456     const labelList& meshEdges,
00457     const scalar minCosLayerTermination,
00458     scalarField& field,
00459     List<extrudeMode>& extrudeStatus,
00460     pointField& patchDisp,
00461     labelList& patchNLayers
00462 ) const
00463 {
00464     const fvMesh& mesh = meshRefiner_.mesh();
00465 
00466     Info<< "shrinkMeshDistance : Removing isolated regions ..." << endl;
00467 
00468     // Keep count of number of points unextruded
00469     label nPointCounter = 0;
00470 
00471     while(true)
00472     {
00473         // Stop layer growth where mesh wraps around edge with a
00474         // large feature angle
00475         handleFeatureAngleLayerTerminations
00476         (
00477             pp,
00478             minCosLayerTermination,
00479 
00480             extrudeStatus,
00481             patchDisp,
00482             patchNLayers,
00483             nPointCounter
00484         );
00485 
00486 
00487         // Do not extrude from point where all neighbouring
00488         // faces are not grown
00489         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00490         boolList extrudedFaces(pp.size(), true);
00491         forAll(pp.localFaces(), faceI)
00492         {
00493             const face& f = pp.localFaces()[faceI];
00494             forAll(f, fp)
00495             {
00496                 if (extrudeStatus[f[fp]] == NOEXTRUDE)
00497                 {
00498                     extrudedFaces[faceI] = false;
00499                     break;
00500                 }
00501             }
00502         }
00503 
00504         const labelListList& pointFaces = pp.pointFaces();
00505 
00506         boolList keptPoints(pp.nPoints(), false);
00507         forAll(keptPoints, patchPointI)
00508         {
00509             const labelList& pFaces = pointFaces[patchPointI];
00510 
00511             forAll(pFaces, i)
00512             {
00513                 label faceI = pFaces[i];
00514                 if (extrudedFaces[faceI])
00515                 {
00516                     keptPoints[patchPointI] = true;
00517                     break;
00518                 }
00519             }
00520         }
00521 
00522         syncTools::syncPointList
00523         (
00524             mesh,
00525             pp.meshPoints(),
00526             keptPoints,
00527             orEqOp<bool>(),
00528             false,              // null value
00529             false               // no separation
00530         );
00531 
00532         label nChanged = 0;
00533 
00534         forAll(keptPoints, patchPointI)
00535         {
00536             if (!keptPoints[patchPointI])
00537             {
00538                 if
00539                 (
00540                     unmarkExtrusion
00541                     (
00542                         patchPointI,
00543                         patchDisp,
00544                         patchNLayers,
00545                         extrudeStatus
00546                     )
00547                 )
00548                 {
00549                    nPointCounter++;
00550                    nChanged++;
00551                 }
00552            }
00553        }
00554 
00555 
00556        if (returnReduce(nChanged, sumOp<label>()) == 0)
00557        {
00558            break;
00559        }
00560    }
00561 
00562     const edgeList& edges = pp.edges();
00563 
00564 
00565     // Count number of mesh edges using a point
00566     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00567 
00568     labelList isolatedPoint(pp.nPoints(),0);
00569 
00570     forAll(edges, edgeI)
00571     {
00572         if (isMasterEdge.get(meshEdges[edgeI]) == 1)
00573         {
00574             const edge& e = edges[edgeI];
00575 
00576             label v0 = e[0];
00577             label v1 = e[1];
00578 
00579             if (extrudeStatus[v1] != NOEXTRUDE)
00580             {
00581                 isolatedPoint[v0] += 1;
00582             }
00583             if (extrudeStatus[v0] != NOEXTRUDE)
00584             {
00585                 isolatedPoint[v1] += 1;
00586             }
00587         }
00588     }
00589 
00590     syncTools::syncPointList
00591     (
00592         mesh,
00593         pp.meshPoints(),
00594         isolatedPoint,
00595         plusEqOp<label>(),
00596         0,       // null value
00597         false    // no separation
00598     );
00599 
00600     // stop layer growth on isolated faces
00601     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00602     forAll(pp, faceI)
00603     {
00604         const face& f = pp.localFaces()[faceI];
00605         bool failed = false;
00606         forAll(f, fp)
00607         {
00608             if (isolatedPoint[f[fp]] > 2)
00609             {
00610                 failed = true;
00611                 break;
00612             }
00613         }
00614         bool allPointsExtruded = true;
00615         if (!failed)
00616         {
00617             forAll(f, fp)
00618             {
00619                 if (extrudeStatus[f[fp]] == NOEXTRUDE)
00620                 {
00621                     allPointsExtruded = false;
00622                     break;
00623                 }
00624             }
00625 
00626             if (allPointsExtruded)
00627             {
00628                 forAll(f, fp)
00629                 {
00630                     if
00631                     (
00632                         unmarkExtrusion
00633                         (
00634                             f[fp],
00635                             patchDisp,
00636                             patchNLayers,
00637                             extrudeStatus
00638                         )
00639                     )
00640                     {
00641                         field[f[fp]] = 0.0;
00642                         nPointCounter++;
00643                     }
00644                 }
00645             }
00646         }
00647     }
00648 
00649     reduce(nPointCounter, sumOp<label>());
00650     Info<< "Number isolated points extrusion stopped : "<< nPointCounter<< endl;
00651 }
00652 
00653 
00654 // Calculates medial axis fields:
00655 // dispVec     : normal of nearest wall. Where this normal changes direction
00656 //               defines the medial axis
00657 // medialDist  : distance to medial axis
00658 // medialRatio : ratio of medial distance to wall distance.
00659 //               (1 at wall, 0 at medial axis)
00660 void Foam::autoLayerDriver::medialAxisSmoothingInfo
00661 (
00662     const motionSmoother& meshMover,
00663     const label nSmoothNormals,
00664     const label nSmoothSurfaceNormals,
00665     const scalar minMedianAxisAngleCos,
00666 
00667     pointVectorField& dispVec,
00668     pointScalarField& medialRatio,
00669     pointScalarField& medialDist
00670 ) const
00671 {
00672 
00673     Info<< "medialAxisSmoothingInfo :"
00674         << " Calculate distance to Medial Axis ..." << endl;
00675 
00676     const polyMesh& mesh = meshMover.mesh();
00677     const pointField& points = mesh.points();
00678 
00679     const indirectPrimitivePatch& pp = meshMover.patch();
00680     const vectorField& faceNormals = pp.faceNormals();
00681     const labelList& meshPoints = pp.meshPoints();
00682 
00683     // Predetermine mesh edges
00684     // ~~~~~~~~~~~~~~~~~~~~~~~
00685 
00686     // Precalulate master edge (only relevant for shared edges)
00687     PackedBoolList isMasterEdge(syncTools::getMasterEdges(mesh));
00688     // Precalculate meshEdge per pp edge
00689     labelList meshEdges(pp.nEdges());
00690 
00691     forAll(meshEdges, patchEdgeI)
00692     {
00693         const edge& e = pp.edges()[patchEdgeI];
00694 
00695         label v0 = pp.meshPoints()[e[0]];
00696         label v1 = pp.meshPoints()[e[1]];
00697         meshEdges[patchEdgeI] = meshTools::findEdge
00698         (
00699             mesh.edges(),
00700             mesh.pointEdges()[v0],
00701             v0,
00702             v1
00703         );
00704     }
00705 
00706 
00707     // Determine pointNormal
00708     // ~~~~~~~~~~~~~~~~~~~~~
00709 
00710     pointField pointNormals(pp.nPoints(), vector::zero);
00711     {
00712         labelList nPointFaces(pp.nPoints(), 0);
00713 
00714         forAll(faceNormals, faceI)
00715         {
00716             const face& f = pp.localFaces()[faceI];
00717 
00718             forAll(f, fp)
00719             {
00720                 pointNormals[f[fp]] += faceNormals[faceI];
00721                 nPointFaces[f[fp]] ++;
00722             }
00723         }
00724 
00725         syncTools::syncPointList
00726         (
00727             mesh,
00728             meshPoints,
00729             pointNormals,
00730             plusEqOp<vector>(),
00731             vector::zero,       // null value
00732             false               // no separation
00733         );
00734 
00735         syncTools::syncPointList
00736         (
00737             mesh,
00738             meshPoints,
00739             nPointFaces,
00740             plusEqOp<label>(),
00741             0,                  // null value
00742             false               // no separation
00743         );
00744 
00745         forAll(pointNormals, i)
00746         {
00747             pointNormals[i] /= nPointFaces[i];
00748         }
00749     }
00750 
00751     // Smooth patch normal vectors
00752     smoothPatchNormals
00753     (
00754         meshMover,
00755         isMasterEdge,
00756         meshEdges,
00757         nSmoothSurfaceNormals,
00758         pointNormals
00759     );
00760 
00761 
00762     // Calculate distance to pp points
00763     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00764 
00765     // Distance to wall
00766     List<pointData> pointWallDist(mesh.nPoints());
00767 
00768 
00769     // 1. Calculate distance to points where displacement is specified.
00770     {
00771         // Seed data.
00772         List<pointData> wallInfo(meshPoints.size());
00773 
00774         forAll(meshPoints, patchPointI)
00775         {
00776             label pointI = meshPoints[patchPointI];
00777             wallInfo[patchPointI] = pointData
00778             (
00779                 points[pointI],
00780                 0.0,
00781                 pointI,                       // passive scalar
00782                 pointNormals[patchPointI]     // surface normals
00783             );
00784         }
00785 
00786         // Do all calculations
00787         List<pointData> edgeWallDist(mesh.nEdges());
00788         PointEdgeWave<pointData> wallDistCalc
00789         (
00790             mesh,
00791             meshPoints,
00792             wallInfo,
00793             pointWallDist,
00794             edgeWallDist,
00795             mesh.globalData().nTotalPoints()    // max iterations
00796         );
00797     }
00798 
00799     // 2. Find points with max distance and transport information back to
00800     //    wall.
00801     {
00802         List<pointData> pointMedialDist(mesh.nPoints());
00803         List<pointData> edgeMedialDist(mesh.nEdges());
00804 
00805         // Seed point data.
00806         DynamicList<pointData> maxInfo(meshPoints.size());
00807         DynamicList<label> maxPoints(meshPoints.size());
00808 
00809         // 1. Medial axis points
00810 
00811         const edgeList& edges = mesh.edges();
00812 
00813         forAll(edges, edgeI)
00814         {
00815             if (isMaxEdge(pointWallDist, edgeI, minMedianAxisAngleCos))
00816             {
00817                 // Both end points of edge have very different nearest wall
00818                 // point. Mark both points as medial axis points.
00819                 const edge& e = edges[edgeI];
00820 
00821                 forAll(e, ep)
00822                 {
00823                     label pointI = e[ep];
00824 
00825                     if (!pointMedialDist[pointI].valid())
00826                     {
00827                         maxPoints.append(pointI);
00828                         maxInfo.append
00829                         (
00830                             pointData
00831                             (
00832                                 points[pointI],
00833                                 0.0,
00834                                 pointI,         // passive data
00835                                 vector::zero    // passive data
00836                             )
00837                         );
00838                         pointMedialDist[pointI] = maxInfo[maxInfo.size()-1];
00839                     }
00840                 }
00841             }
00842         }
00843 
00844 
00845         // 2. Seed non-adapt patches
00846         const polyBoundaryMesh& patches = mesh.boundaryMesh();
00847 
00848         labelHashSet adaptPatches(meshMover.adaptPatchIDs());
00849 
00850         forAll(patches, patchI)
00851         {
00852             const polyPatch& pp = patches[patchI];
00853 
00854             if
00855             (
00856                 !pp.coupled()
00857              && !isA<emptyPolyPatch>(pp)
00858              && !adaptPatches.found(patchI)
00859             )
00860             {
00861                 Info<< "Inserting points on patch " << pp.name() << endl;
00862 
00863                 const labelList& meshPoints = pp.meshPoints();
00864 
00865                 forAll(meshPoints, i)
00866                 {
00867                     label pointI = meshPoints[i];
00868 
00869                     if (!pointMedialDist[pointI].valid())
00870                     {
00871                         maxPoints.append(pointI);
00872                         maxInfo.append
00873                         (
00874                             pointData
00875                             (
00876                                 points[pointI],
00877                                 0.0,
00878                                 pointI,         // passive data
00879                                 vector::zero    // passive data
00880                             )
00881                         );
00882                         pointMedialDist[pointI] = maxInfo[maxInfo.size()-1];
00883                     }
00884                 }
00885             }
00886         }
00887 
00888         maxInfo.shrink();
00889         maxPoints.shrink();
00890 
00891         // Do all calculations
00892         PointEdgeWave<pointData> medialDistCalc
00893         (
00894             mesh,
00895             maxPoints,
00896             maxInfo,
00897 
00898             pointMedialDist,
00899             edgeMedialDist,
00900             mesh.globalData().nTotalPoints()    // max iterations
00901         );
00902 
00903         // Extract medial axis distance as pointScalarField
00904         forAll(pointMedialDist, pointI)
00905         {
00906             medialDist[pointI] = Foam::sqrt(pointMedialDist[pointI].distSqr());
00907         }
00908     }
00909 
00910     // Extract transported surface normals as pointVectorField
00911     forAll(dispVec, i)
00912     {
00913         dispVec[i] = pointWallDist[i].v();
00914     }
00915 
00916     // Smooth normal vectors. Do not change normals on pp.meshPoints
00917     smoothNormals(nSmoothNormals, isMasterEdge, meshPoints, dispVec);
00918 
00919     // Calculate ratio point medial distance to point wall distance
00920     forAll(medialRatio, pointI)
00921     {
00922         scalar wDist2 = pointWallDist[pointI].distSqr();
00923         scalar mDist = medialDist[pointI];
00924 
00925         if (wDist2 < sqr(SMALL) && mDist < SMALL)
00926         {
00927             medialRatio[pointI] = 0.0;
00928         }
00929         else
00930         {
00931             medialRatio[pointI] = mDist / (Foam::sqrt(wDist2) + mDist);
00932         }
00933     }
00934 
00935     if (debug)
00936     {
00937         Info<< "medialAxisSmoothingInfo :"
00938             << " Writing:" << nl
00939             << "    " << dispVec.name()
00940             << " : normalised direction of nearest displacement" << nl
00941             << "    " << medialDist.name()
00942             << " : distance to medial axis" << nl
00943             << "    " << medialRatio.name()
00944             << " : ratio of medial distance to wall distance" << nl
00945             << endl;
00946         dispVec.write();
00947         medialDist.write();
00948         medialRatio.write();
00949     }
00950 }
00951 
00952 
00953 void Foam::autoLayerDriver::shrinkMeshMedialDistance
00954 (
00955     motionSmoother& meshMover,
00956     const dictionary& meshQualityDict,
00957     const label nSmoothThickness,
00958     const scalar maxThicknessToMedialRatio,
00959     const label nAllowableErrors,
00960     const label nSnap,
00961     const scalar minCosLayerTermination,
00962 
00963     const scalarField& layerThickness,
00964     const scalarField& minThickness,
00965 
00966     const pointVectorField& dispVec,
00967     const pointScalarField& medialRatio,
00968     const pointScalarField& medialDist,
00969 
00970     List<extrudeMode>& extrudeStatus,
00971     pointField& patchDisp,
00972     labelList& patchNLayers
00973 ) const
00974 {
00975     Info<< "shrinkMeshMedialDistance : Smoothing using Medial Axis ..." << endl;
00976 
00977     const polyMesh& mesh = meshMover.mesh();
00978 
00979     const indirectPrimitivePatch& pp = meshMover.patch();
00980     const labelList& meshPoints = pp.meshPoints();
00981 
00982     // Precalulate master edge (only relevant for shared edges)
00983     PackedBoolList isMasterEdge(syncTools::getMasterEdges(mesh));
00984     // Precalculate meshEdge per pp edge
00985     labelList meshEdges(pp.nEdges());
00986 
00987     forAll(meshEdges, patchEdgeI)
00988     {
00989         const edge& e = pp.edges()[patchEdgeI];
00990 
00991         label v0 = pp.meshPoints()[e[0]];
00992         label v1 = pp.meshPoints()[e[1]];
00993         meshEdges[patchEdgeI] = meshTools::findEdge
00994         (
00995             mesh.edges(),
00996             mesh.pointEdges()[v0],
00997             v0,
00998             v1
00999         );
01000     }
01001 
01002 
01003     scalarField thickness(layerThickness.size());
01004 
01005     thickness = mag(patchDisp);
01006 
01007     forAll(thickness, patchPointI)
01008     {
01009         if (extrudeStatus[patchPointI] == NOEXTRUDE)
01010         {
01011             thickness[patchPointI] = 0.0;
01012         }
01013     }
01014 
01015     label numThicknessRatioExclude = 0;
01016 
01017     // reduce thickness where thickness/medial axis distance large
01018     forAll(meshPoints, patchPointI)
01019     {
01020         if (extrudeStatus[patchPointI] != NOEXTRUDE)
01021         {
01022             label pointI = meshPoints[patchPointI];
01023 
01024             scalar mDist = medialDist[pointI];
01025 
01026             scalar thicknessRatio = thickness[patchPointI]/(mDist+VSMALL);
01027 
01028             if (thicknessRatio > maxThicknessToMedialRatio)
01029             {
01030                 // Truncate thickness.
01031                 thickness[patchPointI] =
01032                     0.5*(minThickness[patchPointI]+thickness[patchPointI]);
01033 
01034                 patchDisp[patchPointI] =
01035                     thickness[patchPointI]
01036                   * patchDisp[patchPointI]
01037                   / (mag(patchDisp[patchPointI]) + VSMALL);
01038                 numThicknessRatioExclude++;
01039             }
01040         }
01041     }
01042 
01043     reduce(numThicknessRatioExclude, sumOp<label>());
01044     Info<< "shrinkMeshMedialDistance : Reduce layer thickness at "
01045         << numThicknessRatioExclude
01046         << " nodes where thickness to medial axis distance is large " << endl;
01047 
01048     // find points where layer growth isolated to a lone point, edge or face
01049 
01050     findIsolatedRegions
01051     (
01052         pp,
01053         isMasterEdge,
01054         meshEdges,
01055         minCosLayerTermination,
01056         thickness,
01057         extrudeStatus,
01058         patchDisp,
01059         patchNLayers
01060     );
01061 
01062     // smooth layer thickness on moving patch
01063     smoothField
01064     (
01065         meshMover,
01066         isMasterEdge,
01067         meshEdges,
01068         minThickness,
01069         nSmoothThickness,
01070         thickness
01071     );
01072 
01073     List<pointData> pointWallDist(mesh.nPoints());
01074 
01075     const pointField& points = mesh.points();
01076     // 1. Calculate distance to points where displacement is specified.
01077     // This wave is used to transport layer thickness
01078     {
01079         // Distance to wall and medial axis on edges.
01080         List<pointData> edgeWallDist(mesh.nEdges());
01081         labelList wallPoints(meshPoints.size());
01082 
01083         // Seed data.
01084         List<pointData> wallInfo(meshPoints.size());
01085 
01086         forAll(meshPoints, patchPointI)
01087         {
01088             label pointI = meshPoints[patchPointI];
01089             wallPoints[patchPointI] = pointI;
01090             wallInfo[patchPointI] = pointData
01091             (
01092                 points[pointI],
01093                 0.0,
01094                 thickness[patchPointI],       // transport layer thickness
01095                 vector::zero                  // passive vector
01096             );
01097         }
01098 
01099         // Do all calculations
01100         PointEdgeWave<pointData> wallDistCalc
01101         (
01102             mesh,
01103             wallPoints,
01104             wallInfo,
01105             pointWallDist,
01106             edgeWallDist,
01107             mesh.globalData().nTotalPoints()    // max iterations
01108         );
01109     }
01110 
01111     // Calculate scaled displacement vector
01112     pointVectorField& displacement = meshMover.displacement();
01113 
01114     forAll(displacement, pointI)
01115     {
01116         // 1) displacement on nearest wall point, scaled by medialRatio
01117         //    (wall distance / medial distance)
01118         // 2) pointWallDist[pointI].s() is layer thickness transported
01119         //    from closest wall point.
01120         // 3) shrink in opposite direction of addedPoints
01121         displacement[pointI] =
01122             -medialRatio[pointI]
01123             *pointWallDist[pointI].s()
01124             *dispVec[pointI];
01125     }
01126 
01127     // Current faces to check. Gets modified in meshMover.scaleMesh
01128     labelList checkFaces(identity(mesh.nFaces()));
01129 
01130     Info<< "shrinkMeshMedialDistance : Moving mesh ..." << endl;
01131     scalar oldErrorReduction = -1;
01132 
01133     for (label iter = 0; iter < 2*nSnap ; iter++)
01134     {
01135         Info<< "Iteration " << iter << endl;
01136         if (iter == nSnap)
01137         {
01138             Info<< "Displacement scaling for error reduction set to 0." << endl;
01139             oldErrorReduction = meshMover.setErrorReduction(0.0);
01140         }
01141 
01142         if
01143         (
01144             meshMover.scaleMesh
01145             (
01146                 checkFaces,
01147                 List<labelPair>(0),
01148                 meshMover.paramDict(),
01149                 meshQualityDict,
01150                 true,
01151                 nAllowableErrors
01152             )
01153         )
01154         {
01155             Info<< "shrinkMeshMedialDistance : Successfully moved mesh" << endl;
01156             break;
01157         }
01158     }
01159 
01160     if (oldErrorReduction >= 0)
01161     {
01162         meshMover.setErrorReduction(oldErrorReduction);
01163     }
01164 
01165     Info<< "shrinkMeshMedialDistance : Finished moving mesh ..." << endl;
01166 }
01167 
01168 
01169 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd