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

slidingInterfaceProjectPoints.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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "slidingInterface.H"
00028 #include "polyMesh.H"
00029 #include "primitiveMesh.H"
00030 #include "line.H"
00031 #include "triPointRef.H"
00032 #include "plane.H"
00033 #include "polyTopoChanger.H"
00034 
00035 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00036 
00037 const Foam::scalar Foam::slidingInterface::pointMergeTol_ = 0.05;
00038 const Foam::scalar Foam::slidingInterface::edgeMergeTol_ = 0.01;
00039 const Foam::label Foam::slidingInterface::nFacesPerSlaveEdge_ = 5;
00040 const Foam::label Foam::slidingInterface::edgeFaceEscapeLimit_ = 10;
00041 
00042 const Foam::scalar Foam::slidingInterface::integralAdjTol_ = 0.05;
00043 const Foam::scalar Foam::slidingInterface::edgeMasterCatchFraction_ = 0.4;
00044 const Foam::scalar Foam::slidingInterface::edgeEndCutoffTol_ = 0.0001;
00045 
00046 
00047 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00048 
00049 // Index of debug signs:
00050 // a - integral match adjustment: point adjusted
00051 // n - integral match adjustment: point not adjusted
00052 // . - loop of the edge-to-face interaction detection
00053 // x - reversal of direction in edge-to-face interaction detection
00054 // + - complete edge-to-face interaction detection
00055 // z - incomplete edge-to-face interaction detection.  This may be OK for edges
00056 //     crossing from one to the other side of multiply connected master patch
00057 // * - colinear triangle: adjusting projection with slave face normal
00058 // m - master point inserted into the edge
00059 
00060 bool Foam::slidingInterface::projectPoints() const
00061 {
00062     if (debug)
00063     {
00064         Pout<< "bool slidingInterface::projectPoints() : "
00065             << " for object " << name() << " : "
00066             << "Projecting slave points onto master surface." << endl;
00067     }
00068 
00069     // Algorithm:
00070     // 1) Go through all the points of the master and slave patch and calculate
00071     //    minimum edge length coming from the point.  Calculate the point
00072     //    merge tolerance as the fraction of mimimum edge length.
00073     // 2) Project all the slave points onto the master patch
00074     //    in the normal direction.
00075     // 3) If some points have missed and the match is integral, the
00076     //    points in question will be adjusted.  Find the nearest face for
00077     //    those points and continue with the procedure.
00078     // 4) For every point, check all the points of the face it has hit.
00079     //    For every pair of points find if their distance is smaller than
00080     //    both the master and slave merge tolerance.  If so, the slave point
00081     //    is moved to the location of the master point.  Remember the master
00082     //    point index.
00083     // 5) For every unmerged slave point, check its distance to all the
00084     //    edges of the face it has hit.  If the distance is smaller than the
00085     //    edge merge tolerance, the point will be moved onto the edge.
00086     //    Remember the master edge index.
00087     // 6) The remaning slave points will be projected into faces.  Remember the
00088     //    master face index.
00089     // 7) For the points that miss the master patch, grab the nearest face
00090     //    on the master and leave the slave point where it started
00091     //    from and the miss is recorded.
00092 
00093     const polyMesh& mesh = topoChanger().mesh();
00094 
00095     const primitiveFacePatch& masterPatch =
00096         mesh.faceZones()[masterFaceZoneID_.index()]();
00097 
00098     const primitiveFacePatch& slavePatch =
00099         mesh.faceZones()[slaveFaceZoneID_.index()]();
00100 
00101     // Get references to local points, local edges and local faces
00102     // for master and slave patch
00103 //     const labelList& masterMeshPoints = masterPatch.meshPoints();
00104     const pointField& masterLocalPoints = masterPatch.localPoints();
00105     const faceList& masterLocalFaces = masterPatch.localFaces();
00106     const edgeList& masterEdges = masterPatch.edges();
00107     const labelListList& masterEdgeFaces = masterPatch.edgeFaces();
00108     const labelListList& masterFaceEdges = masterPatch.faceEdges();
00109     const labelListList& masterFaceFaces = masterPatch.faceFaces();
00110 //     Pout<< "Master patch.  Local points: " << masterLocalPoints << nl
00111 //         << "Master patch.  Mesh points: " << masterPatch.meshPoints() << nl
00112 //         << "Local faces: " << masterLocalFaces << nl
00113 //         << "local edges: " << masterEdges << endl;
00114 
00115 //     const labelList& slaveMeshPoints = slavePatch.meshPoints();
00116     const pointField& slaveLocalPoints = slavePatch.localPoints();
00117     const edgeList& slaveEdges = slavePatch.edges();
00118     const labelListList& slaveEdgeFaces = slavePatch.edgeFaces();
00119     const vectorField& slavePointNormals = slavePatch.pointNormals();
00120 //     const vectorField& slaveFaceNormals = slavePatch.faceNormals();
00121 //     Pout<< "Slave patch.  Local points: " << slaveLocalPoints << nl
00122 //         << "Slave patch.  Mesh points: " << slavePatch.meshPoints() << nl
00123 //         << "Local faces: " << slavePatch.localFaces() << nl
00124 //         << "local edges: " << slaveEdges << endl;
00125 
00126     // Calculate min edge distance for points and faces
00127 
00128     // Calculate min edge length for the points and faces of master patch
00129     scalarField minMasterPointLength(masterLocalPoints.size(), GREAT);
00130     scalarField minMasterFaceLength(masterPatch.size(), GREAT);
00131 
00132     forAll (masterEdges, edgeI)
00133     {
00134         const edge& curEdge = masterEdges[edgeI];
00135 
00136         const scalar curLength =
00137             masterEdges[edgeI].mag(masterLocalPoints);
00138 
00139         // Do points
00140         minMasterPointLength[curEdge.start()] =
00141             min
00142             (
00143                 minMasterPointLength[curEdge.start()],
00144                 curLength
00145             );
00146 
00147         minMasterPointLength[curEdge.end()] =
00148             min
00149             (
00150                 minMasterPointLength[curEdge.end()],
00151                 curLength
00152             );
00153 
00154         // Do faces
00155         const labelList& curFaces = masterEdgeFaces[edgeI];
00156 
00157         forAll (curFaces, faceI)
00158         {
00159             minMasterFaceLength[curFaces[faceI]] =
00160                 min
00161                 (
00162                     minMasterFaceLength[curFaces[faceI]],
00163                     curLength
00164                 );
00165         }
00166     }
00167 
00168 //     Pout << "min length for master points: " << minMasterPointLength << endl
00169 //         << "min length for master faces: " << minMasterFaceLength << endl;
00170 
00171     // Calculate min edge length for the points and faces of slave patch
00172     scalarField minSlavePointLength(slaveLocalPoints.size(), GREAT);
00173     scalarField minSlaveFaceLength(slavePatch.size(), GREAT);
00174 
00175     forAll (slaveEdges, edgeI)
00176     {
00177         const edge& curEdge = slaveEdges[edgeI];
00178 
00179         const scalar curLength =
00180             slaveEdges[edgeI].mag(slaveLocalPoints);
00181 
00182         // Do points
00183         minSlavePointLength[curEdge.start()] = 
00184             min
00185             (
00186                 minSlavePointLength[curEdge.start()],
00187                 curLength
00188             );
00189 
00190         minSlavePointLength[curEdge.end()] = 
00191             min
00192             (
00193                 minSlavePointLength[curEdge.end()],
00194                 curLength
00195             );
00196 
00197         // Do faces
00198         const labelList& curFaces = slaveEdgeFaces[edgeI];
00199 
00200         forAll (curFaces, faceI)
00201         {
00202             minSlaveFaceLength[curFaces[faceI]] =
00203                 min
00204                 (
00205                     minSlaveFaceLength[curFaces[faceI]],
00206                     curLength
00207                 );
00208         }
00209     }
00210 
00211 //     Pout << "min length for slave points: " << minSlavePointLength << endl
00212 //         << "min length for slave faces: " << minSlaveFaceLength << endl;
00213 
00214     // Project slave points onto the master patch
00215 
00216     // Face hit by the slave point
00217     List<objectHit> slavePointFaceHits =
00218         slavePatch.projectPoints
00219         (
00220             masterPatch,
00221             slavePointNormals,
00222             projectionAlgo_
00223         );
00224 
00225 //     Pout << "USING N-SQAURED!!!" << endl;
00226 //     List<objectHit> slavePointFaceHits =
00227 //         projectPointsNSquared<face, List, const pointField&>
00228 //         (
00229 //             slavePatch,
00230 //             masterPatch,
00231 //             slavePointNormals,
00232 //             projectionAlgo_
00233 //         );
00234 
00235     if (debug)
00236     {
00237         label nHits = 0;
00238 
00239         forAll (slavePointFaceHits, pointI)
00240         {
00241             if (slavePointFaceHits[pointI].hit())
00242             {
00243                 nHits++;
00244             }
00245         }
00246 
00247         Pout<< "Number of hits in point projection: " << nHits
00248             << " out of " << slavePointFaceHits.size() << " points."
00249             << endl;
00250     }
00251 
00252     // Projected slave points are stored for mesh motion correction
00253     if (projectedSlavePointsPtr_) delete projectedSlavePointsPtr_;
00254 
00255     projectedSlavePointsPtr_ =
00256         new pointField(slavePointFaceHits.size(), vector::zero);
00257     pointField& projectedSlavePoints = *projectedSlavePointsPtr_;
00258 
00259     // Adjust projection to type of match
00260 
00261     label nAdjustedPoints = 0;
00262 
00263     // If the match is integral and some points have missed,
00264     // find nearest master face
00265     if (matchType_ == INTEGRAL)
00266     {
00267         if (debug)
00268         {
00269             Pout<< "bool slidingInterface::projectPoints() for object "
00270                 << name() << " : "
00271                 << "Adjusting point projection for integral match: ";
00272         }
00273 
00274         forAll (slavePointFaceHits, pointI)
00275         {
00276             if (slavePointFaceHits[pointI].hit())
00277             {
00278                 // Grab the hit point
00279                 projectedSlavePoints[pointI] =
00280                     masterLocalFaces
00281                         [slavePointFaceHits[pointI].hitObject()].ray
00282                         (
00283                             slaveLocalPoints[pointI],
00284                             slavePointNormals[pointI],
00285                             masterLocalPoints,
00286                             projectionAlgo_
00287                         ).hitPoint();
00288             }
00289             else
00290             {
00291                 // Grab the nearest point on the face (edge)
00292                 pointHit missAdjust =
00293                     masterLocalFaces[slavePointFaceHits[pointI].hitObject()].ray
00294                     (
00295                         slaveLocalPoints[pointI],
00296                         slavePointNormals[pointI],
00297                         masterLocalPoints,
00298                         projectionAlgo_
00299                     );
00300 
00301                 const point nearPoint = missAdjust.missPoint();
00302                 const point missPoint =
00303                     slaveLocalPoints[pointI]
00304                   + slavePointNormals[pointI]*missAdjust.distance();
00305 
00306                 // Calculate the tolerance
00307                 const scalar mergeTol =
00308                     integralAdjTol_*minSlavePointLength[pointI];
00309 
00310                 // Adjust the hit
00311                 if (mag(nearPoint - missPoint) < mergeTol)
00312                 {
00313                     if (debug)
00314                     {
00315                         Pout << "a";
00316                     }
00317 
00318 //                     Pout<< "Moving slave point in integral adjustment "
00319 //                         << pointI << " miss point: " << missPoint
00320 //                         << " near point: " << nearPoint
00321 //                         << " mergeTol: " << mergeTol
00322 //                         << " dist: " << mag(nearPoint - missPoint) << endl;
00323 
00324                     projectedSlavePoints[pointI] = nearPoint;
00325 
00326                     slavePointFaceHits[pointI] =
00327                         objectHit(true, slavePointFaceHits[pointI].hitObject());
00328 
00329                     nAdjustedPoints++;
00330                 }
00331                 else
00332                 {
00333                     projectedSlavePoints[pointI] = slaveLocalPoints[pointI];
00334 
00335                     if (debug)
00336                     {
00337                         Pout << "n";
00338                     }
00339                 }
00340             }
00341         }
00342 
00343         if (debug)
00344         {
00345             Pout << " done." << endl;
00346         }
00347     }
00348     else if (matchType_ == PARTIAL)
00349     {
00350         forAll (slavePointFaceHits, pointI)
00351         {
00352             if (slavePointFaceHits[pointI].hit())
00353             {
00354                 // Grab the hit point
00355                 projectedSlavePoints[pointI] =
00356                     masterLocalFaces
00357                         [slavePointFaceHits[pointI].hitObject()].ray
00358                         (
00359                             slaveLocalPoints[pointI],
00360                             slavePointNormals[pointI],
00361                             masterLocalPoints,
00362                             projectionAlgo_
00363                         ).hitPoint();
00364             }
00365             else
00366             {
00367                 // The point remains where it started from
00368                 projectedSlavePoints[pointI] = slaveLocalPoints[pointI];
00369             }
00370         }
00371     }
00372     else
00373     {
00374         FatalErrorIn
00375         (
00376             "bool slidingInterface::projectPoints() const"
00377         )   << "Problem in point projection.  Unknown sliding match type "
00378             << " for object " << name()
00379             << abort(FatalError);
00380     }
00381 
00382     if (debug)
00383     {
00384         Pout<< "Number of adjusted points in projection: "
00385             << nAdjustedPoints << endl;
00386 
00387         // Check for zero-length edges in slave projection
00388         scalar minEdgeLength = GREAT;
00389         scalar el = 0;
00390         label nShortEdges = 0;
00391 
00392         forAll (slaveEdges, edgeI)
00393         {
00394             el = slaveEdges[edgeI].mag(projectedSlavePoints);
00395 
00396             if (el < SMALL)
00397             {
00398                 Pout<< "Point projection problems for edge: "
00399                     << slaveEdges[edgeI] << ". Length = " << el
00400                     << endl;
00401 
00402                 nShortEdges++;
00403             }
00404 
00405             minEdgeLength = min(minEdgeLength, el);
00406         }
00407 
00408         if (nShortEdges > 0)
00409         {
00410             FatalErrorIn
00411             (
00412                 "bool slidingInterface::projectPoints() const"
00413             )   << "Problem in point projection.  " << nShortEdges
00414                 << " short projected edges "
00415                 << "after adjustment for object " << name()
00416                 << abort(FatalError);
00417         }
00418         else
00419         {
00420             Pout << " ... projection OK." << endl;
00421         }
00422     }
00423 //     scalarField magDiffs(mag(slaveLocalPoints - projectedSlavePoints));
00424 //     Pout<< "Slave point face hits: " << slavePointFaceHits << nl
00425 //         << "slave points: " << slaveLocalPoints << nl
00426 //         << "Projected slave points: " << projectedSlavePoints 
00427 //         << "diffs: " << magDiffs << endl;
00428 
00429     // Merge projected points against master points
00430 
00431     labelList slavePointPointHits(slaveLocalPoints.size(), -1);
00432     labelList masterPointPointHits(masterLocalPoints.size(), -1);
00433 
00434     // Go through all the slave points and compare them against all the points
00435     // in the master face they hit.  If the distance between the projected point
00436     // and any of the master face points is smaller than both tolerances,
00437     // merge the projected point by:
00438     // 1) adjusting the projected point to coincide with the
00439     //    master point it merges with
00440     // 2) remembering the hit master point index in slavePointPointHits
00441     // 3) resetting the hit face to -1
00442     // 4) If a master point has been hit directly, it cannot be merged
00443     // into the edge. Mark it as used in the reverse list
00444 
00445     label nMergedPoints = 0;
00446 
00447     forAll (projectedSlavePoints, pointI)
00448     {
00449         if (slavePointFaceHits[pointI].hit())
00450         {
00451             // Taking a non-const reference so the point can be adjusted
00452             point& curPoint = projectedSlavePoints[pointI];
00453 
00454             // Get the hit face
00455             const face& hitFace =
00456                 masterLocalFaces[slavePointFaceHits[pointI].hitObject()];
00457 
00458             label mergePoint = -1;
00459             scalar mergeDist = GREAT;
00460 
00461             // Try all point before deciding on best fit.  
00462             forAll (hitFace, hitPointI)
00463             {
00464                 scalar dist =
00465                     mag(masterLocalPoints[hitFace[hitPointI]] - curPoint);
00466 
00467                 // Calculate the tolerance
00468                 const scalar mergeTol =
00469                     pointMergeTol_*
00470                     min
00471                     (
00472                         minSlavePointLength[pointI],
00473                         minMasterPointLength[hitFace[hitPointI]]
00474                     );
00475 
00476                 if (dist < mergeTol && dist < mergeDist)
00477                 {
00478                     mergePoint = hitFace[hitPointI];
00479                     mergeDist = dist;
00480 
00481 //                     Pout<< "Merging slave point "
00482 //                         << slavePatch.meshPoints()[pointI] << " at "
00483 //                         << slaveLocalPoints[pointI] << " with master "
00484 //                         << masterPatch.meshPoints()[mergePoint] << " at "
00485 //                         << masterLocalPoints[mergePoint]
00486 //                         << ". dist: " << mergeDist
00487 //                         << " mergeTol: " << mergeTol << endl;
00488                 }
00489             }
00490 
00491             if (mergePoint > -1)
00492             {
00493                 // Point is to be merged with master point
00494                 nMergedPoints++;
00495 
00496                 slavePointPointHits[pointI] = mergePoint;
00497                 curPoint = masterLocalPoints[mergePoint];
00498                 masterPointPointHits[mergePoint] = pointI;
00499             }
00500         }
00501     }
00502 
00503 //     Pout<< "slavePointPointHits: " << slavePointPointHits << nl
00504 //         << "masterPointPointHits: " << masterPointPointHits << endl;
00505 
00506     if (debug)
00507     {
00508         // Check for zero-length edges in slave projection
00509         scalar minEdgeLength = GREAT;
00510         scalar el = 0;
00511 
00512         forAll (slaveEdges, edgeI)
00513         {
00514             el = slaveEdges[edgeI].mag(projectedSlavePoints);
00515 
00516             if (el < SMALL)
00517             {
00518                 Pout<< "Point projection problems for edge: "
00519                     << slaveEdges[edgeI] << ". Length = " << el
00520                     << endl;
00521             }
00522 
00523             minEdgeLength = min(minEdgeLength, el);
00524         }
00525 
00526         if (minEdgeLength < SMALL)
00527         {
00528             FatalErrorIn
00529             (
00530                 "bool slidingInterface::projectPoints() const"
00531             )   << "Problem in point projection.  Short projected edge "
00532                 << " after point merge for object " << name()
00533                 << abort(FatalError);
00534         }
00535         else
00536         {
00537             Pout << " ... point merge OK." << endl;
00538         }
00539     }
00540 
00541     // Merge projected points against master edges
00542 
00543     labelList slavePointEdgeHits(slaveLocalPoints.size(), -1);
00544 
00545     label nMovedPoints = 0;
00546 
00547     forAll (projectedSlavePoints, pointI)
00548     {
00549         // Eliminate the points merged into points
00550         if (slavePointPointHits[pointI] < 0)
00551         {
00552             // Get current point position
00553             point& curPoint = projectedSlavePoints[pointI];
00554 
00555             // Get the hit face
00556             const labelList& hitFaceEdges =
00557                 masterFaceEdges[slavePointFaceHits[pointI].hitObject()];
00558 
00559             // Calculate the tolerance
00560             const scalar mergeLength = 
00561                 min
00562                 (
00563                     minSlavePointLength[pointI],
00564                     minMasterFaceLength[slavePointFaceHits[pointI].hitObject()]
00565                 );
00566 
00567             const scalar mergeTol = pointMergeTol_*mergeLength;
00568 
00569             scalar minDistance = GREAT;
00570 
00571             forAll (hitFaceEdges, edgeI)
00572             {
00573                 const edge& curEdge = masterEdges[hitFaceEdges[edgeI]];
00574 
00575                 pointHit edgeHit =
00576                     curEdge.line(masterLocalPoints).nearestDist(curPoint);
00577 
00578                 if (edgeHit.hit())
00579                 {
00580                     scalar dist =
00581                         mag(edgeHit.hitPoint() - projectedSlavePoints[pointI]);
00582 
00583                     if (dist < mergeTol && dist < minDistance)
00584                     {
00585                         // Point is to be moved onto master edge
00586                         nMovedPoints++;
00587 
00588                         slavePointEdgeHits[pointI] = hitFaceEdges[edgeI];
00589                         projectedSlavePoints[pointI] = edgeHit.hitPoint();
00590 
00591                         minDistance = dist;
00592 
00593 //                         Pout<< "Moving slave point "
00594 //                             << slavePatch.meshPoints()[pointI]
00595 //                             << " (" << pointI 
00596 //                             << ") at " << slaveLocalPoints[pointI]
00597 //                             << " onto master edge " << hitFaceEdges[edgeI]
00598 //                             << " or ("
00599 //                             << masterLocalPoints[curEdge.start()]
00600 //                             << masterLocalPoints[curEdge.end()]
00601 //                             << ") hit: " << edgeHit.hitPoint()
00602 //                             << ". dist: " << dist
00603 //                             << " mergeTol: " << mergeTol << endl;
00604                     }
00605                 }
00606             } // end of hit face edges
00607 
00608             if (slavePointEdgeHits[pointI] > -1)
00609             {
00610                 // Projected slave point has moved.  Re-attempt merge with
00611                 // master points of the edge
00612                 point& curPoint = projectedSlavePoints[pointI];
00613 
00614                 const edge& hitMasterEdge =
00615                     masterEdges[slavePointEdgeHits[pointI]];
00616 
00617                 label mergePoint = -1;
00618                 scalar mergeDist = GREAT;
00619 
00620                 forAll (hitMasterEdge, hmeI)
00621                 {
00622                     scalar hmeDist =
00623                         mag(masterLocalPoints[hitMasterEdge[hmeI]] - curPoint);
00624 
00625                     // Calculate the tolerance
00626                     const scalar mergeTol =
00627                         pointMergeTol_*
00628                         min
00629                         (
00630                             minSlavePointLength[pointI],
00631                             minMasterPointLength[hitMasterEdge[hmeI]]
00632                     );
00633 
00634                     if (hmeDist < mergeTol && hmeDist < mergeDist)
00635                     {
00636                         mergePoint = hitMasterEdge[hmeI];
00637                         mergeDist = hmeDist;
00638 
00639 //                     Pout<< "Merging slave point; SECOND TRY "
00640 //                         << slavePatch.meshPoints()[pointI] << " local "
00641 //                         << pointI << " at "
00642 //                         << slaveLocalPoints[pointI] << " with master "
00643 //                         << masterPatch.meshPoints()[mergePoint] << " at "
00644 //                         << masterLocalPoints[mergePoint]
00645 //                         << ". hmeDist: " << mergeDist
00646 //                         << " mergeTol: " << mergeTol << endl;
00647                     }
00648                 }
00649 
00650                 if (mergePoint > -1)
00651                 {
00652                     // Point is to be merged with master point
00653                     slavePointPointHits[pointI] = mergePoint;
00654                     curPoint = masterLocalPoints[mergePoint];
00655                     masterPointPointHits[mergePoint] = pointI;
00656 
00657                     slavePointFaceHits[pointI] =
00658                         objectHit(true, slavePointFaceHits[pointI].hitObject());
00659 
00660 
00661                     // Disable edge merge
00662                     slavePointEdgeHits[pointI] = -1;
00663                 }
00664             }
00665         }
00666     }
00667 
00668     if (debug)
00669     {
00670         Pout<< "Number of merged master points: " << nMergedPoints << nl
00671             << "Number of adjusted slave points: " << nMovedPoints << endl;
00672 
00673         // Check for zero-length edges in slave projection
00674         scalar minEdgeLength = GREAT;
00675         scalar el = 0;
00676 
00677         forAll (slaveEdges, edgeI)
00678         {
00679             el = slaveEdges[edgeI].mag(projectedSlavePoints);
00680 
00681             if (el < SMALL)
00682             {
00683                 Pout<< "Point projection problems for edge: "
00684                     << slaveEdges[edgeI] << ". Length = " << el
00685                     << endl;
00686             }
00687 
00688             minEdgeLength = min(minEdgeLength, el);
00689         }
00690 
00691         if (minEdgeLength < SMALL)
00692         {
00693             FatalErrorIn
00694             (
00695                 "bool slidingInterface::projectPoints() const"
00696             )   << "Problem in point projection.  Short projected edge "
00697             << " after correction for object " << name()
00698             << abort(FatalError);
00699         }
00700     }
00701 
00702 //     Pout << "slavePointEdgeHits: " << slavePointEdgeHits << endl;
00703 
00704     // Insert the master points into closest slave edge if appropriate
00705 
00706     // Algorithm:
00707     //    The face potentially interacts with all the points of the
00708     //    faces covering the path between its two ends.  Since we are
00709     //    examining an arbitrary shadow of the edge on a non-Euclidian
00710     //    surface, it is typically quite hard to do a geometric point
00711     //    search (under a shadow of a straight line).  Therefore, the
00712     //    search will be done topologically.
00713     //
00714     // I) Point collection
00715     // -------------------
00716     // 1) Grab the master faces to which the end points of the edge
00717     //    have been projected.
00718     // 2) Starting from the face containing the edge start, grab all
00719     //    its points and put them into the point lookup map.  Put the
00720     //    face onto the face lookup map.
00721     // 3) If the face of the end point is on the face lookup, complete
00722     //    the point collection step (this is checked during insertion.
00723     // 4) Start a new round of insertion.  Visit all faces in the face
00724     //    lookup and add their neighbours which are not already on the
00725     //    map.  Every time the new neighbour is found, add its points to
00726     //    the point lookup.  If the face of the end point is inserted,
00727     //    continue with the current roundof insertion and stop the
00728     //    algorithm.
00729     //
00730     // II) Point check
00731     // ---------------
00732     //    Grab all the points from the point collection and check them
00733     //    against the current edge.  If the edge-to-point distance is
00734     //    smaller than the smallest tolerance in the game (min of
00735     //    master point tolerance and left and right slave face around
00736     //    the edge tolerance) and the nearest point hits the edge, the
00737     //    master point will break the slave edge.  Check the actual
00738     //    distance of the original master position from the edge.  If
00739     //    the distance is smaller than a fraction of slave edge
00740     //    length, the hit is considered valid.  Store the slave edge
00741     //    index for every master point.
00742 
00743     labelList masterPointEdgeHits(masterLocalPoints.size(), -1);
00744     scalarField masterPointEdgeDist(masterLocalPoints.size(), GREAT);
00745 
00746     // Note.  "Processing slave edges" code is repeated twice in the
00747     // sliding intergace coupling in order to allow the point
00748     // projection to be done separately from the actual cutting.
00749     // Please change consistently with coupleSlidingInterface.C
00750     // 
00751 
00752     if (debug)
00753     {
00754         Pout << "Processing slave edges " << endl;
00755     }
00756 
00757     // Create a map of faces the edge can interact with
00758     labelHashSet curFaceMap
00759     (
00760         nFacesPerSlaveEdge_*primitiveMesh::edgesPerFace_
00761     );
00762 
00763     labelHashSet addedFaces(2*primitiveMesh::edgesPerFace_);
00764 
00765     forAll (slaveEdges, edgeI)
00766     {
00767         const edge& curEdge = slaveEdges[edgeI];
00768 
00769         //HJ: check for all edges even if both ends have missed
00770         //    Experimental.
00771 //         if
00772 //         (
00773 //             slavePointFaceHits[curEdge.start()].hit()
00774 //          || slavePointFaceHits[curEdge.end()].hit()
00775 //         )
00776         {
00777             // Clear the maps
00778             curFaceMap.clear();
00779             addedFaces.clear();
00780 
00781             // Grab the faces for start and end points
00782             const label startFace =
00783                 slavePointFaceHits[curEdge.start()].hitObject();
00784             const label endFace = slavePointFaceHits[curEdge.end()].hitObject();
00785 
00786             // Insert the start face into the list
00787             curFaceMap.insert(startFace);
00788             addedFaces.insert(startFace);
00789 
00790 //             Pout << "Doing edge " << edgeI << " or " << curEdge << " start: " << slavePointFaceHits[curEdge.start()].hitObject() << " end " << slavePointFaceHits[curEdge.end()].hitObject() << endl;
00791             // If the end face is on the list, the face collection is finished
00792             label nSweeps = 0;
00793             bool completed = false;
00794 
00795             while (nSweeps < edgeFaceEscapeLimit_)
00796             {
00797                 nSweeps++;
00798 
00799                 if (addedFaces.found(endFace))
00800                 {
00801                     completed = true;
00802                 }
00803 
00804                 // Add all face neighbours of face in the map
00805                 const labelList cf = addedFaces.toc();
00806                 addedFaces.clear();
00807 
00808                 forAll (cf, cfI)
00809                 {
00810                     const labelList& curNbrs = masterFaceFaces[cf[cfI]];
00811 
00812                     forAll (curNbrs, nbrI)
00813                     {
00814                         if (!curFaceMap.found(curNbrs[nbrI]))
00815                         {
00816                             curFaceMap.insert(curNbrs[nbrI]);
00817                             addedFaces.insert(curNbrs[nbrI]);
00818                         }
00819                     }
00820                 }
00821 
00822                 if (completed) break;
00823 
00824                 if (debug)
00825                 {
00826                     Pout << ".";
00827                 }
00828             }
00829 
00830             if (!completed)
00831             {
00832                 if (debug)
00833                 {
00834                     Pout << "x";
00835                 }
00836 
00837                 // It is impossible to reach the end from the start, probably
00838                 // due to disconnected domain.  Do search in opposite direction
00839 
00840                 label nReverseSweeps = 0;
00841 
00842                 addedFaces.clear();
00843                 curFaceMap.insert(endFace);
00844                 addedFaces.insert(endFace);
00845 
00846                 while (nReverseSweeps < edgeFaceEscapeLimit_)
00847                 {
00848                     nReverseSweeps++;
00849 
00850                     if (addedFaces.found(startFace))
00851                     {
00852                         completed = true;
00853                     }
00854 
00855                     // Add all face neighbours of face in the map
00856                     const labelList cf = addedFaces.toc();
00857                     addedFaces.clear();
00858 
00859                     forAll (cf, cfI)
00860                     {
00861                         const labelList& curNbrs = masterFaceFaces[cf[cfI]];
00862 
00863                         forAll (curNbrs, nbrI)
00864                         {
00865                             if (!curFaceMap.found(curNbrs[nbrI]))
00866                             {
00867                                 curFaceMap.insert(curNbrs[nbrI]);
00868                                 addedFaces.insert(curNbrs[nbrI]);
00869                             }
00870                         }
00871                     }
00872 
00873                     if (completed) break;
00874 
00875                     if (debug)
00876                     {
00877                         Pout << ".";
00878                     }
00879                 }
00880             }
00881 
00882             if (completed)
00883             {
00884                 if (debug)
00885                 {
00886                     Pout << "+ ";
00887                 }
00888             }
00889             else
00890             {
00891                 if (debug)
00892                 {
00893                     Pout << "z ";
00894                 }
00895             }
00896 
00897             // Collect the points
00898 
00899             // Create a map of points the edge can interact with
00900             labelHashSet curPointMap
00901             (
00902                 nFacesPerSlaveEdge_*primitiveMesh::pointsPerFace_
00903             );
00904 
00905             const labelList curFaces = curFaceMap.toc();
00906 //             Pout << "curFaces: " << curFaces << endl;
00907             forAll (curFaces, faceI)
00908             {
00909                 const face& f = masterLocalFaces[curFaces[faceI]];
00910 
00911                 forAll (f, pointI)
00912                 {
00913                     curPointMap.insert(f[pointI]);
00914                 }
00915             }
00916 
00917             const labelList curMasterPoints = curPointMap.toc();
00918 
00919             // Check all the points against the edge.
00920 
00921             linePointRef edgeLine = curEdge.line(projectedSlavePoints);
00922 
00923             const vector edgeVec = edgeLine.vec();
00924             const scalar edgeMag = edgeLine.mag();
00925 
00926             // Calculate actual distance involved in projection.  This
00927             // is used to reject master points out of reach.
00928             // Calculated as a combination of travel distance in projection and
00929             // edge length
00930             scalar slaveCatchDist =
00931                 edgeMasterCatchFraction_*edgeMag
00932               + 0.5*
00933                 (
00934                     mag
00935                     (
00936                         projectedSlavePoints[curEdge.start()]
00937                       - slaveLocalPoints[curEdge.start()]
00938                     )
00939                   + mag
00940                     (
00941                         projectedSlavePoints[curEdge.end()]
00942                       - slaveLocalPoints[curEdge.end()]
00943                     )
00944                 );
00945 
00946             // The point merge distance needs to be measured in the
00947             // plane of the slave edge.  The unit vector is calculated
00948             // as a cross product of the edge vector and the edge
00949             // projection direction.  When checking for the distance
00950             // in plane, a minimum of the master-to-edge and
00951             // projected-master-to-edge distance is used, to avoid
00952             // problems with badly defined master planes.  HJ,
00953             // 17/Oct/2004
00954             vector edgeNormalInPlane =
00955                 edgeVec
00956               ^ (
00957                     slavePointNormals[curEdge.start()]
00958                   + slavePointNormals[curEdge.end()]
00959                 );
00960 
00961             edgeNormalInPlane /= mag(edgeNormalInPlane);
00962 
00963             forAll (curMasterPoints, pointI)
00964             {
00965                 const label cmp = curMasterPoints[pointI];
00966 
00967                 // Skip the current point if the edge start or end has
00968                 // been adjusted onto in
00969                 if
00970                 (
00971                     slavePointPointHits[curEdge.start()] == cmp
00972                  || slavePointPointHits[curEdge.end()] == cmp
00973                  || masterPointPointHits[cmp] > -1
00974                 )
00975                 {
00976 // Pout << "Edge already snapped to point.  Skipping." << endl;
00977                     continue;
00978                 }
00979 
00980                 // Check if the point actually hits the edge within bounds
00981                 pointHit edgeLineHit =
00982                     edgeLine.nearestDist(masterLocalPoints[cmp]);
00983 
00984                 if (edgeLineHit.hit())
00985                 {
00986                     // If the distance to the line is smaller than
00987                     // the tolerance the master point needs to be
00988                     // inserted into the edge
00989 
00990                     // Strict checking of slave cut to avoid capturing
00991                     // end points.  
00992                     scalar cutOnSlave =
00993                         ((edgeLineHit.hitPoint() - edgeLine.start()) & edgeVec)
00994                         /sqr(edgeMag);
00995 
00996                     scalar distInEdgePlane =
00997                         min
00998                         (
00999                             edgeLineHit.distance(),
01000                             mag
01001                             (
01002                                 (
01003                                     masterLocalPoints[cmp]
01004                                   - edgeLineHit.hitPoint()
01005                                 )
01006                               & edgeNormalInPlane
01007                             )
01008                         );
01009 //                     Pout << "master point: " << cmp
01010 //                         << " cutOnSlave " << cutOnSlave
01011 //                         << " distInEdgePlane: " << distInEdgePlane
01012 //                         << " tol1: " << pointMergeTol_*edgeMag
01013 //                         << " hitDist: " << edgeLineHit.distance()
01014 //                         << " tol2: " <<
01015 //                         min
01016 //                         (
01017 //                             slaveCatchDist,
01018 //                             masterPointEdgeDist[cmp]
01019 //                         ) << endl;
01020 
01021                     // Not a point hit, check for edge
01022                     if
01023                     (
01024                         cutOnSlave > edgeEndCutoffTol_
01025                      && cutOnSlave < 1.0 - edgeEndCutoffTol_ // check edge cut
01026                      && distInEdgePlane < edgeMergeTol_*edgeMag // merge plane
01027                      && edgeLineHit.distance()
01028                       < min
01029                         (
01030                             slaveCatchDist,
01031                             masterPointEdgeDist[cmp]
01032                         )
01033                     )
01034                     {
01035                         if (debug)
01036                         {
01037                             if (masterPointEdgeHits[cmp] == -1)
01038                             {
01039                                 // First hit
01040                                 Pout << "m";
01041                             }
01042                             else
01043                             {
01044                                 // Repeat hit
01045                                 Pout << "M";
01046                             }
01047                         }
01048 
01049                         // Snap to point onto edge
01050                         masterPointEdgeHits[cmp] = edgeI;
01051                         masterPointEdgeDist[cmp] = edgeLineHit.distance();
01052 
01053 //                         Pout<< "Inserting master point "
01054 //                             << masterPatch.meshPoints()[cmp]
01055 //                             << " (" << cmp
01056 //                             << ") at " << masterLocalPoints[cmp]
01057 //                             << " into slave edge " << edgeI
01058 //                             << " " << curEdge
01059 //                             << " cutOnSlave: " << cutOnSlave
01060 //                             << " distInEdgePlane: " << distInEdgePlane
01061 //                             << ". dist: " << edgeLineHit.distance()
01062 //                             << " mergeTol: "
01063 //                             << edgeMergeTol_*edgeMag
01064 //                             << " other tol: " <<
01065 //                             min
01066 //                             (
01067 //                                 slaveCatchDist,
01068 //                                 masterPointEdgeDist[cmp]
01069 //                             )
01070 //                             << endl;
01071                     }
01072                 }
01073             }
01074         } // End if both ends missing
01075     } // End all slave edges
01076 
01077     if (debug)
01078     {
01079         Pout << endl;
01080     }
01081 //     Pout << "masterPointEdgeHits: " << masterPointEdgeHits << endl;
01082 
01083     if (debug)
01084     {
01085         Pout<< "bool slidingInterface::projectPoints() for object "
01086             << name() << " : "
01087             << "Finished projecting  points. Topology = ";
01088     }
01089 
01090 //     Pout<< "slavePointPointHits: " << slavePointPointHits << nl
01091 //         << "slavePointEdgeHits: " << slavePointEdgeHits << nl
01092 //         << "slavePointFaceHits: " << slavePointFaceHits << nl
01093 //         << "masterPointEdgeHits: " << masterPointEdgeHits << endl;
01094 
01095     // The four lists:
01096     // - slavePointPointHits
01097     // - slavePointEdgeHits
01098     // - slavePointFaceHits
01099     // - masterPointEdgeHits
01100     //   define how the two patches will be merged topologically.
01101     //   If the lists have not changed since the last merge, the
01102     //   sliding interface changes only geometrically and simple mesh
01103     //   motion will suffice.  Otherwise, a topological change is
01104     //   required.
01105 
01106     // Compare the result with the current state
01107     if (!attached_)
01108     {
01109         // The mesh needs to change topologically
01110         trigger_ = true;
01111 
01112         // Store the addressing arrays and projected points
01113         deleteDemandDrivenData(slavePointPointHitsPtr_);
01114         slavePointPointHitsPtr_ = new labelList(slavePointPointHits);
01115 
01116         deleteDemandDrivenData(slavePointEdgeHitsPtr_);
01117         slavePointEdgeHitsPtr_ = new labelList(slavePointEdgeHits);
01118 
01119         deleteDemandDrivenData(slavePointFaceHitsPtr_);
01120         slavePointFaceHitsPtr_ = new List<objectHit>(slavePointFaceHits);
01121 
01122         deleteDemandDrivenData(masterPointEdgeHitsPtr_);
01123         masterPointEdgeHitsPtr_ = new labelList(masterPointEdgeHits);
01124 
01125         if (debug)
01126         {
01127             Pout << "(Detached interface) changing." << endl;
01128         }
01129     }
01130     else
01131     {
01132         // Compare the lists against the stored lists.  If any of them
01133         // has changed, topological change will be executed.
01134         trigger_ = false;
01135 
01136         if
01137         (
01138             !slavePointPointHitsPtr_
01139          || !slavePointEdgeHitsPtr_
01140          || !slavePointFaceHitsPtr_
01141          || !masterPointEdgeHitsPtr_
01142         )
01143         {
01144             // Must be restart.  Force topological change.
01145             deleteDemandDrivenData(slavePointPointHitsPtr_);
01146             slavePointPointHitsPtr_ = new labelList(slavePointPointHits);
01147 
01148             deleteDemandDrivenData(slavePointEdgeHitsPtr_);
01149             slavePointEdgeHitsPtr_ = new labelList(slavePointEdgeHits);
01150 
01151             deleteDemandDrivenData(slavePointFaceHitsPtr_);
01152             slavePointFaceHitsPtr_ = new List<objectHit>(slavePointFaceHits);
01153 
01154             deleteDemandDrivenData(masterPointEdgeHitsPtr_);
01155             masterPointEdgeHitsPtr_ = new labelList(masterPointEdgeHits);
01156 
01157             if (debug)
01158             {
01159                 Pout << "(Attached interface restart) changing." << endl;
01160             }
01161 
01162             trigger_ = true;
01163             return trigger_;
01164         }
01165 
01166         if (slavePointPointHits != (*slavePointPointHitsPtr_))
01167         {
01168             if (debug)
01169             {
01170                 Pout << "(Point projection) ";
01171             }
01172 
01173             trigger_ = true;
01174         }
01175 
01176         if (slavePointEdgeHits != (*slavePointEdgeHitsPtr_))
01177         {
01178             if (debug)
01179             {
01180                 Pout << "(Edge projection) ";
01181             }
01182 
01183             trigger_ = true;
01184         }
01185 
01186         // Comparison for face hits needs to exclude points that have hit
01187         // another point or edge
01188         bool faceHitsDifferent = false;
01189 
01190         const List<objectHit>& oldPointFaceHits = *slavePointFaceHitsPtr_;
01191 
01192         forAll (slavePointFaceHits, pointI)
01193         {
01194             if
01195             (
01196                 slavePointPointHits[pointI] < 0
01197              && slavePointEdgeHits[pointI] < 0
01198             )
01199             {
01200                 // This is a straight face hit
01201                 if (slavePointFaceHits[pointI] != oldPointFaceHits[pointI])
01202                 {
01203                     // Two lists are different
01204                     faceHitsDifferent = true;
01205                     break;
01206                 }
01207             }
01208         }
01209 
01210         if (faceHitsDifferent)
01211         {
01212             if (debug)
01213             {
01214                 Pout << "(Face projection) ";
01215             }
01216 
01217             trigger_ = true;
01218 
01219         }
01220 
01221         if (masterPointEdgeHits != (*masterPointEdgeHitsPtr_))
01222         {
01223             if (debug)
01224             {
01225                 Pout << "(Master point projection) ";
01226             }
01227 
01228             trigger_ = true;
01229         }
01230 
01231 
01232         if (trigger_)
01233         {
01234             // Grab new data
01235             deleteDemandDrivenData(slavePointPointHitsPtr_);
01236             slavePointPointHitsPtr_ = new labelList(slavePointPointHits);
01237 
01238             deleteDemandDrivenData(slavePointEdgeHitsPtr_);
01239             slavePointEdgeHitsPtr_ = new labelList(slavePointEdgeHits);
01240 
01241             deleteDemandDrivenData(slavePointFaceHitsPtr_);
01242             slavePointFaceHitsPtr_ = new List<objectHit>(slavePointFaceHits);
01243 
01244             deleteDemandDrivenData(masterPointEdgeHitsPtr_);
01245             masterPointEdgeHitsPtr_ = new labelList(masterPointEdgeHits);
01246 
01247             if (debug)
01248             {
01249                 Pout << "changing." << endl;
01250             }
01251         }
01252         else
01253         {
01254             if (debug)
01255             {
01256                 Pout << "preserved." << endl;
01257             }
01258         }
01259     }
01260 
01261     return trigger_;
01262 }
01263 
01264 
01265 void Foam::slidingInterface::clearPointProjection() const
01266 {
01267     deleteDemandDrivenData(slavePointPointHitsPtr_);
01268     deleteDemandDrivenData(slavePointEdgeHitsPtr_);
01269     deleteDemandDrivenData(slavePointFaceHitsPtr_);
01270     deleteDemandDrivenData(masterPointEdgeHitsPtr_);
01271 
01272     deleteDemandDrivenData(projectedSlavePointsPtr_);
01273 }
01274 
01275 
01276 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd