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

enrichedPatchCutFaces.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     Calculating cut faces of the enriched patch, together with the addressing
00027     into master and slave patch.
00028 
00029 \*---------------------------------------------------------------------------*/
00030 
00031 #include "enrichedPatch.H"
00032 #include "boolList.H"
00033 #include "DynamicList.H"
00034 #include "labelPair.H"
00035 #include "primitiveMesh.H"
00036 #include "HashSet.H"
00037 
00038 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00039 
00040 // If the cut face gets more than this number of points, it will be checked
00041 const Foam::label Foam::enrichedPatch::maxFaceSizeDebug_ = 100;
00042 
00043 
00044 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00045 
00046 // Index of debug signs:
00047 // x - skip a point
00048 // l - left turn
00049 // r - right turn
00050 
00051 void Foam::enrichedPatch::calcCutFaces() const
00052 {
00053     if (cutFacesPtr_ || cutFaceMasterPtr_ || cutFaceSlavePtr_)
00054     {
00055         FatalErrorIn("void enrichedPatch::calcCutFaces() const")
00056             << "Cut faces addressing already calculated."
00057             << abort(FatalError);
00058     }
00059 
00060     const faceList& enFaces = enrichedFaces();
00061     const labelList& mp = meshPoints();
00062     const faceList& lf = localFaces();
00063     const pointField& lp = localPoints();
00064     const labelListList& pp = pointPoints();
00065 //     Pout << "enFaces: " << enFaces << endl;
00066 //     Pout << "lf: " << lf << endl;
00067 //     Pout << "lp: " << lp << endl;
00068 //     Pout << "pp: " << pp << endl;
00069     const Map<labelList>& masterPointFaceAddr = masterPointFaces();
00070 
00071     // Prepare the storage
00072     DynamicList<face> cf(2*lf.size());
00073     DynamicList<label> cfMaster(2*lf.size());
00074     DynamicList<label> cfSlave(2*lf.size());
00075 
00076     // Algorithm
00077     // Go through all the faces
00078     // 1) For each face, start at point zero and grab the edge zero.
00079     //    Both points are added into the cut face.  Mark the first edge
00080     //    as used and position the current point as the end of the first
00081     //    edge and previous point as point zero.
00082     // 2) Grab all possible points from the current point.  Excluding
00083     //    the previous point find the point giving the biggest right
00084     //    turn. Add the point to the face and mark the edges as used.
00085     //    Continue doing this until the face is complete, i.e. the next point
00086     //    to add is the first point of the face.
00087     // 3) Once the facet is completed, register its owner from the face
00088     //    it has been created from (remember that the first lot of faces
00089     //    inserted into the enriched faces list are the slaves, to allow
00090     //    matching of the other side).
00091     // 4) If the facet is created from an original slave face, go
00092     //    through the master patch and try to identify the master face
00093     //    this facet belongs to.  This is recognised by the fact that the
00094     //    faces consists exclusively of the points of the master face and
00095     //    the points projected onto the face.
00096 
00097     // Create a set of edge usage parameters
00098     HashSet<edge, Hash<edge> > edgesUsedOnce(pp.size());
00099     HashSet<edge, Hash<edge> > edgesUsedTwice
00100         (pp.size()*primitiveMesh::edgesPerPoint_);
00101 
00102 
00103     forAll (lf, faceI)
00104     {
00105         const face& curLocalFace = lf[faceI];
00106         const face& curGlobalFace = enFaces[faceI];
00107 
00108 //         Pout<< "Doing face " << faceI << " local: " << curLocalFace << " or " << curGlobalFace << endl;
00109 //         if (faceI < slavePatch_.size())
00110 //         {
00111 //             Pout<< "original slave: " << slavePatch_[faceI]
00112 //                 << " local: " << slavePatch_.localFaces()[faceI] << endl;
00113 //         }
00114 //         else
00115 //         {
00116 //             Pout<< "original master: "
00117 //                 << masterPatch_[faceI - slavePatch_.size()] << " "
00118 //                 << masterPatch_.localFaces()[faceI - slavePatch_.size()]
00119 //                 << endl;
00120 //         }
00121 //         {
00122 //             pointField facePoints = curLocalFace.points(lp);
00123 //             forAll (curLocalFace, pointI)
00124 //             {
00125 //                 Pout << "v " << facePoints[pointI].x() << " "
00126 //                     << facePoints[pointI].y() << " "
00127 //                     << facePoints[pointI].z() << endl;
00128 //             }
00129 //         }
00130 
00131         // Track the usage of face edges.  When all edges are used, the
00132         // face decomposition is complete.
00133         // The seed edges include all the edges of the original face + all edges
00134         // of other faces that have been used in the construction of the
00135         // facet.  Edges from other faces can be considered as
00136         // internal to the current face if used only once.
00137 
00138         // Track the edge usage to avoid duplicate faces and reset it to unused
00139         boolList usedFaceEdges(curLocalFace.size(), false);
00140 
00141         SLList<edge> edgeSeeds;
00142 
00143         // Insert the edges of current face into the seed list.
00144         edgeList cfe = curLocalFace.edges();
00145         forAll (curLocalFace, edgeI)
00146         {
00147             edgeSeeds.append(cfe[edgeI]);
00148         }
00149 
00150         // Grab face normal
00151         vector normal = curLocalFace.normal(lp);
00152         normal /= mag(normal);
00153 
00154         while (edgeSeeds.size())
00155         {
00156 //             Pout << "edgeSeeds.size(): " << edgeSeeds.size() << endl;
00157             const edge curEdge = edgeSeeds.removeHead();
00158 
00159             // Locate the edge in current face
00160             const label curEdgeWhich = curLocalFace.which(curEdge.start());
00161 
00162             // Check if the edge is in current face and if it has been
00163             // used already.  If so, skip it
00164             if
00165             (
00166                 curEdgeWhich > -1
00167              && curLocalFace.nextLabel(curEdgeWhich) == curEdge.end()
00168             )
00169             {
00170                 // Edge is in the starting face.
00171                 // If unused, mark it as used; if used, skip it
00172                 if (usedFaceEdges[curEdgeWhich]) continue;
00173 
00174                 usedFaceEdges[curEdgeWhich] = true;
00175             }
00176 
00177             // If the edge has already been used twice, skip it
00178             if (edgesUsedTwice.found(curEdge)) continue;
00179 //             Pout << "Trying new edge (" << mp[curEdge.start()] << ", " << mp[curEdge.end()] << ") seed: " << curEdge << " used: " << edgesUsedTwice.found(curEdge) << endl;
00180 
00181             // Estimate the size of cut face as twice the size of original face
00182             DynamicList<label> cutFaceGlobalPoints(2*curLocalFace.size());
00183             DynamicList<label> cutFaceLocalPoints(2*curLocalFace.size());
00184 
00185             // Found unused edge.
00186             label prevPointLabel = curEdge.start();
00187             cutFaceGlobalPoints.append(mp[prevPointLabel]);
00188             cutFaceLocalPoints.append(prevPointLabel);
00189 //             Pout << "prevPointLabel: " << mp[prevPointLabel] << endl;
00190             // Grab current point and append it to the list
00191             label curPointLabel = curEdge.end();
00192             point curPoint = lp[curPointLabel];
00193             cutFaceGlobalPoints.append(mp[curPointLabel]);
00194             cutFaceLocalPoints.append(curPointLabel);
00195 
00196             bool completedCutFace = false;
00197 
00198             label faceSizeDebug = maxFaceSizeDebug_;
00199 
00200             do
00201             {
00202                 // Grab the next point options
00203 //                 Pout << "curPointLabel: " << mp[curPointLabel] << endl;
00204                 const labelList& nextPoints = pp[curPointLabel];
00205 //                 Pout << "nextPoints: " << UIndirectList<label>(mp, nextPoints) << endl;
00206                 // Get the vector along the edge and the right vector
00207                 vector ahead = curPoint - lp[prevPointLabel];
00208                 ahead -= normal*(normal & ahead);
00209                 ahead /= mag(ahead);
00210 
00211                 vector right = normal ^ ahead;
00212                 right /= mag(right);
00213 //                 Pout<< "normal: " << normal << " ahead: " << ahead << " right: " << right << endl;
00214 
00215                 scalar atanTurn = -GREAT;
00216                 label bestAtanPoint = -1;
00217 
00218                 forAll (nextPoints, nextI)
00219                 {
00220                     // Exclude the point we are coming from; there will always
00221                     // be more than one edge, so this is safe
00222                     if (nextPoints[nextI] != prevPointLabel)
00223                     {
00224 // Pout << "cur point: " << curPoint << " trying for point: " << mp[nextPoints[nextI]] << " " << lp[nextPoints[nextI]];
00225                         vector newDir = lp[nextPoints[nextI]] - curPoint;
00226 // Pout << " newDir: " << newDir << " mag: " << mag(newDir) << flush;
00227                         newDir -= normal*(normal & newDir);
00228                         scalar magNewDir = mag(newDir);
00229 // Pout << " corrected: " << newDir << " mag: " << mag(newDir) << flush;
00230 
00231                         if (magNewDir < SMALL)
00232                         {
00233                             FatalErrorIn
00234                             (
00235                                 "void enrichedPatch::"
00236                                 "calcCutFaces() const"
00237                             )   << "Zero length edge detected.  Probable "
00238                                 << "projection error: slave patch probably "
00239                                 << "does not project onto master.  "
00240                                 << "Please switch on "
00241                                 << "enriched patch debug for more info"
00242                                 << abort(FatalError);
00243                         }
00244 
00245                         newDir /= magNewDir;
00246 
00247                         scalar curAtanTurn =
00248                             atan2(newDir & right, newDir & ahead);
00249 
00250 //                         Pout << " atan: " << curAtanTurn << endl;
00251 
00252                         if (curAtanTurn > atanTurn)
00253                         {
00254                             bestAtanPoint = nextPoints[nextI];
00255                             atanTurn = curAtanTurn;
00256                         }
00257                     } // end of prev point skip
00258                 } // end of next point selection
00259 //                 Pout<< "   bestAtanPoint: " << bestAtanPoint << " or "
00260 //                     << mp[bestAtanPoint] << endl;
00261                 // Selected next best point.
00262 //                 Pout << "cutFaceGlobalPoints: " << cutFaceGlobalPoints << endl;
00263                 // Check if the edge about to be added has been used
00264                 // in the current face or twice in other faces.  If
00265                 // so, the face is bad.
00266                 const label whichNextPoint = curLocalFace.which(curPointLabel);
00267 
00268                 if
00269                 (
00270                     whichNextPoint > -1
00271                  && curLocalFace.nextLabel(whichNextPoint) == bestAtanPoint
00272                  && usedFaceEdges[whichNextPoint]
00273                 )
00274                 {
00275                     // This edge is already used in current face
00276                     // face cannot be good; start on a new one
00277 //                     Pout << "Double usage in current face, cannot be good" << endl;
00278                     completedCutFace = true;
00279                 }
00280 
00281 
00282                 if (edgesUsedTwice.found(edge(curPointLabel, bestAtanPoint)))
00283                 {
00284                     // This edge is already used -
00285                     // face cannot be good; start on a new one
00286                     completedCutFace = true;
00287 //                     Pout << "Double usage elsewhere, cannot be good" << endl;
00288                 }
00289 
00290                 if (completedCutFace) continue;
00291 
00292                 // Insert the next best point into the list
00293                 if (mp[bestAtanPoint] == cutFaceGlobalPoints[0])
00294                 {
00295                     // Face is completed.  Save it and move on to the next
00296                     // available edge
00297                     completedCutFace = true;
00298 
00299                     if (debug)
00300                     {
00301                         Pout<< " local: " << cutFaceLocalPoints
00302                             << " one side: " << faceI;
00303                     }
00304 
00305                     // Append the face
00306                     face cutFaceGlobal;
00307                     cutFaceGlobal.transfer(cutFaceGlobalPoints);
00308 
00309                     cf.append(cutFaceGlobal);
00310 
00311                     face cutFaceLocal;
00312                     cutFaceLocal.transfer(cutFaceLocalPoints);
00313 //                     Pout << "\ncutFaceLocal: " << cutFaceLocal.points(lp) << endl;
00314                     // Go through all edges of the cut faces.
00315                     // If the edge corresponds to a starting face edge,
00316                     // mark the starting face edge as true
00317 
00318                     forAll (cutFaceLocal, cutI)
00319                     {
00320                         const edge curCutFaceEdge
00321                         (
00322                             cutFaceLocal[cutI],
00323                             cutFaceLocal.nextLabel(cutI)
00324                         );
00325 
00326                         // Increment the usage count using two hash sets
00327                         HashSet<edge, Hash<edge> >::iterator euoIter =
00328                             edgesUsedOnce.find(curCutFaceEdge);
00329 
00330                         if (euoIter == edgesUsedOnce.end())
00331                         {
00332 //                             Pout << "Found edge not used before: "<< curCutFaceEdge << endl;
00333                             edgesUsedOnce.insert(curCutFaceEdge);
00334                         }
00335                         else
00336                         {
00337 //                             Pout << "Found edge used once: " << curCutFaceEdge << endl;
00338                             edgesUsedOnce.erase(euoIter);
00339                             edgesUsedTwice.insert(curCutFaceEdge);
00340                         }
00341 
00342                         const label curCutFaceEdgeWhich = curLocalFace.which(curCutFaceEdge.start());
00343 
00344                         if
00345                         (
00346                             curCutFaceEdgeWhich > -1
00347                          && curLocalFace.nextLabel(curCutFaceEdgeWhich) == curCutFaceEdge.end()
00348                         )
00349                         {
00350                             // Found edge in original face
00351 //                             Pout << "Found edge in orig face: " << curCutFaceEdge << ": " << curCutFaceEdgeWhich << endl;
00352                             usedFaceEdges[curCutFaceEdgeWhich] = true;
00353                         }
00354                         else
00355                         {
00356                             // Edge not in original face.  Add it to seeds
00357 //                             Pout << "Found new edge seed: " << curCutFaceEdge << endl;
00358                             edgeSeeds.append(curCutFaceEdge.reverseEdge());
00359                         }
00360                     }
00361 
00362 
00363                     // Find out what the other side is
00364 
00365                     // Algorithm
00366                     // If the face is in the slave half of the
00367                     // enrichedFaces lists, it may be matched against
00368                     // the master face.  It can be recognised by the
00369                     // fact that all its points belong to one master
00370                     // face.  For this purpose:
00371                     // 1) Grab the master faces around the point zero
00372                     // of the cut face and collect all master faces
00373                     // around it.
00374                     // 2) Loop through the rest of cut face points and
00375                     // if the point refers to one of the faces marked
00376                     // by point zero, increment its count.
00377                     // 3) When completed, the face whose count is
00378                     // equal to the number of points in the cut face
00379                     // is the other side.  If this is not the case, there is no
00380                     // face on the other side.
00381 
00382                     if (faceI < slavePatch_.size())
00383                     {
00384                         Map<labelList>::const_iterator mpfAddrIter =
00385                             masterPointFaceAddr.find(cutFaceGlobal[0]);
00386 
00387                         bool otherSideFound = false;
00388 
00389                         if (mpfAddrIter != masterPointFaceAddr.end())
00390                         {
00391                             bool miss = false;
00392 
00393                             // Create the label count using point zero
00394                             const labelList& masterFacesOfPZero = mpfAddrIter();
00395 
00396                             labelList hits(masterFacesOfPZero.size(), 1);
00397 
00398                             for
00399                             (
00400                                 label pointI = 1;
00401                                 pointI < cutFaceGlobal.size();
00402                                 pointI++
00403                             )
00404                             {
00405                                 Map<labelList>::const_iterator
00406                                     mpfAddrPointIter =
00407                                         masterPointFaceAddr.find
00408                                         (
00409                                             cutFaceGlobal[pointI]
00410                                         );
00411 
00412                                 if
00413                                 (
00414                                     mpfAddrPointIter
00415                                  == masterPointFaceAddr.end()
00416                                 )
00417                                 {
00418                                     // Point is off the master patch. Skip
00419                                     miss = true;
00420                                     break;
00421                                 }
00422 
00423                                 const labelList& curMasterFaces =
00424                                     mpfAddrPointIter();
00425 
00426                                 // For every current face, try to find it in the
00427                                 // zero-list
00428                                 forAll (curMasterFaces, i)
00429                                 {
00430                                     forAll (masterFacesOfPZero, j)
00431                                     {
00432                                         if
00433                                         (
00434                                             curMasterFaces[i]
00435                                          == masterFacesOfPZero[j]
00436                                         )
00437                                         {
00438                                             hits[j]++;
00439                                             break;
00440                                         }
00441                                     }
00442                                 }
00443                             }
00444 
00445                             // If all point are found attempt matching
00446                             if (!miss)
00447                             {
00448                                 forAll (hits, pointI)
00449                                 {
00450                                     if (hits[pointI] == cutFaceGlobal.size())
00451                                     {
00452                                         // Found other side.
00453                                         otherSideFound = true;
00454 
00455                                         cfMaster.append
00456                                         (
00457                                             masterFacesOfPZero[pointI]
00458                                         );
00459 
00460                                         cfSlave.append(faceI);
00461 
00462                                         // Reverse the face such that it
00463                                         // points out of the master patch
00464                                         cf[cf.size() - 1] =
00465                                             cf[cf.size() - 1].reverseFace();
00466 
00467                                         if (debug)
00468                                         {
00469                                             Pout<< " other side: "
00470                                                 << masterFacesOfPZero[pointI]
00471                                                 << endl;
00472                                         }
00473                                     } // end of hits
00474                                 } // end of for all hits
00475 
00476                             } // end of not miss
00477 
00478                             if (!otherSideFound || miss)
00479                             {
00480                                 if (debug)
00481                                 {
00482                                     Pout << " solo slave A" << endl;
00483                                 }
00484 
00485                                 cfMaster.append(-1);
00486                                 cfSlave.append(faceI);
00487                             }
00488                         }
00489                         else
00490                         {
00491                             // First point not in master patch
00492                             if (debug)
00493                             {
00494                                 Pout << " solo slave B" << endl;
00495                             }
00496 
00497                             cfMaster.append(-1);
00498                             cfSlave.append(faceI);
00499                         }
00500                     }
00501                     else
00502                     {
00503                         if (debug)
00504                         {
00505                             Pout << " master side" << endl;
00506                         }
00507 
00508                         cfMaster.append(faceI - slavePatch_.size());
00509                         cfSlave.append(-1);
00510                     }
00511                 }
00512                 else
00513                 {
00514                     // Face not completed.  Prepare for the next point search
00515                     prevPointLabel = curPointLabel;
00516                     curPointLabel = bestAtanPoint;
00517                     curPoint = lp[curPointLabel];
00518                     cutFaceGlobalPoints.append(mp[curPointLabel]);
00519                     cutFaceLocalPoints.append(curPointLabel);
00520 
00521                     if (debug || cutFaceGlobalPoints.size() > faceSizeDebug)
00522                     {
00523                         faceSizeDebug *= 2;
00524 
00525                         // Check for duplicate points in the face
00526                         forAll (cutFaceGlobalPoints, checkI)
00527                         {
00528                             for
00529                             (
00530                                 label checkJ = checkI + 1;
00531                                 checkJ < cutFaceGlobalPoints.size();
00532                                 checkJ++
00533                             )
00534                             {
00535                                 if
00536                                 (
00537                                     cutFaceGlobalPoints[checkI]
00538                                  == cutFaceGlobalPoints[checkJ]
00539                                 )
00540                                 {
00541                                     // Shrink local points for debugging
00542                                     cutFaceLocalPoints.shrink();
00543 
00544                                     face origFace;
00545                                     face origFaceLocal;
00546                                     if (faceI < slavePatch_.size())
00547                                     {
00548                                         origFace = slavePatch_[faceI];
00549                                         origFaceLocal =
00550                                             slavePatch_.localFaces()[faceI];
00551                                     }
00552                                     else
00553                                     {
00554                                         origFace =
00555                                             masterPatch_
00556                                             [faceI - slavePatch_.size()];
00557 
00558                                         origFaceLocal =
00559                                             masterPatch_.localFaces()
00560                                             [faceI - slavePatch_.size()];
00561                                     }
00562 
00563                                     FatalErrorIn
00564                                     (
00565                                         "void enrichedPatch::"
00566                                         "calcCutFaces() const"
00567                                     )   << "Duplicate point found in cut face. "
00568                                         << "Error in the face cutting "
00569                                         << "algorithm for global face "
00570                                         << origFace << " local face "
00571                                         << origFaceLocal << nl
00572                                         << "Slave size: " << slavePatch_.size()
00573                                         << " Master size: "
00574                                         << masterPatch_.size()
00575                                         << " index: " << faceI << ".\n"
00576                                         << "Face: " << curGlobalFace << nl
00577                                         << "Cut face: " << cutFaceGlobalPoints
00578                                         << " local: " << cutFaceLocalPoints
00579                                         << nl << "Points: "
00580                                         << face(cutFaceLocalPoints).points(lp)
00581                                         << abort(FatalError);
00582                                 }
00583                             }
00584                         }
00585                     } // end of debug
00586                 }
00587             } while (!completedCutFace);
00588         } // end of used edges
00589 
00590         if (debug)
00591         {
00592             Pout << " Finished face " << faceI << endl;
00593         }
00594 
00595     } // end of local faces
00596 
00597     // Re-pack the list into compact storage
00598     cutFacesPtr_ = new faceList();
00599     cutFacesPtr_->transfer(cf);
00600 
00601     cutFaceMasterPtr_ = new labelList();
00602     cutFaceMasterPtr_->transfer(cfMaster);
00603 
00604     cutFaceSlavePtr_ = new labelList();
00605     cutFaceSlavePtr_->transfer(cfSlave);
00606 }
00607 
00608 
00609 void Foam::enrichedPatch::clearCutFaces()
00610 {
00611     deleteDemandDrivenData(cutFacesPtr_);
00612     deleteDemandDrivenData(cutFaceMasterPtr_);
00613     deleteDemandDrivenData(cutFaceSlavePtr_);
00614 }
00615 
00616 
00617 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00618 
00619 const Foam::faceList& Foam::enrichedPatch::cutFaces() const
00620 {
00621     if (!cutFacesPtr_)
00622     {
00623         calcCutFaces();
00624     }
00625 
00626     return *cutFacesPtr_;
00627 }
00628 
00629 
00630 const Foam::labelList& Foam::enrichedPatch::cutFaceMaster() const
00631 {
00632     if (!cutFaceMasterPtr_)
00633     {
00634         calcCutFaces();
00635     }
00636 
00637     return *cutFaceMasterPtr_;
00638 }
00639 
00640 
00641 const Foam::labelList& Foam::enrichedPatch::cutFaceSlave() const
00642 {
00643     if (!cutFaceSlavePtr_)
00644     {
00645         calcCutFaces();
00646     }
00647 
00648     return *cutFaceSlavePtr_;
00649 }
00650 
00651 
00652 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd