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

mergeCoupleFacePoints.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     Merge duplicate points created by arbitrary face coupling and remove unused
00027     points.
00028 
00029 \*---------------------------------------------------------------------------*/
00030 
00031 #include "starMesh.H"
00032 #include "demandDrivenData.H"
00033 
00034 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00035 
00036 void starMesh::mergeCoupleFacePoints()
00037 {
00038     // mark all used points by looping through all faces in two goes.
00039     // First, go into every cell and find min edge length.  Use a
00040     // fraction of that as a merge tolerance.  Loop through all the
00041     // points of the cell and query a merge against every other point
00042     // of the same cell based on the current tolerance.  If two points
00043     // merge, find out if any of them already belongs to a "merge set"
00044     // (group of points that merge together.  If so, add the current
00045     // points into the merge set and make sure that the merge label
00046     // for the whole set is the lowest of all encountered vertices.
00047     // This is stored in a mergeSetID list, under the index of the
00048     // merge set.  Once all cells (and thus points) are visited, go
00049     // through the renumbering list and for each merging point use the
00050     // label of the merge set as the new point label.
00051     // This is VERY fancy. Use care if/when changing. 
00052 
00053     Info << endl << "Creating merge sets" << endl;
00054 
00055     // Create a renumbering list for points
00056     // In the first instance the renumbering list is used as a
00057     // mergeSetID storage
00058     labelList renumberPoints(points_.size(), -1);
00059 
00060     // create storage of mergeSetIDs
00061     label mergeIncrement = points_.size()/10;
00062     labelList mergeSetID(mergeIncrement, -1);
00063 
00064     label nMergeSets = 0;
00065 
00066     forAll (cellFaces_, cellI)
00067     {
00068         const faceList& curFaces = cellFaces_[cellI];
00069 
00070         // create a list of all points for the cell with duplicates
00071         // and find the shortest edge length
00072 
00073         label nPointsInCell = 0;
00074 
00075         scalar pointMergeTol = GREAT;
00076 
00077         forAll (curFaces, faceI)
00078         {
00079             nPointsInCell += curFaces[faceI].size();
00080 
00081             edgeList curEdges = curFaces[faceI].edges();
00082 
00083             forAll (curEdges, edgeI)
00084             {
00085                 scalar length = curEdges[edgeI].mag(points_);
00086 
00087                 if (length < pointMergeTol)
00088                 {
00089                     pointMergeTol = length;
00090                 }
00091             }
00092         }
00093 
00094         // set merge tolerance as a fraction of the shortest edge
00095         pointMergeTol /= 100.0;
00096 
00097         // create a list of points
00098         labelList cellPoints(nPointsInCell);
00099         label nAddedPoints = 0;
00100 
00101         forAll (curFaces, faceI)
00102         {
00103             const face& f = curFaces[faceI];
00104 
00105             forAll (f, fI)
00106             {
00107                 cellPoints[nAddedPoints] = f[fI];
00108                 nAddedPoints++;
00109             }
00110         }
00111 
00112         // loop n-squared through the local points and merge them up
00113         for (label firstPI = 0; firstPI < cellPoints.size() - 1; firstPI++)
00114         {
00115             for
00116             (
00117                 label otherPI = firstPI;
00118                 otherPI < cellPoints.size();
00119                 otherPI++
00120             )
00121             {
00122                 if (cellPoints[firstPI] != cellPoints[otherPI])
00123                 {
00124                     label a = cellPoints[firstPI];
00125                     label b = cellPoints[otherPI];
00126 
00127                     if (edge (a, b).mag(points_) < pointMergeTol)
00128                     {
00129                         // found a pair of points to merge
00130 #                       ifdef DEBUG_MERGE
00131                         Info<< "Merging points " << a << " and " << b << endl;
00132 #                       endif
00133 
00134                         // are the two points in a merge group?
00135                         label mergeSetA = -1;
00136                         label mergeSetB = -1;
00137 
00138                         if (renumberPoints[a] > -1)
00139                         {
00140                             mergeSetA = renumberPoints[a];
00141                         }
00142 
00143                         if (renumberPoints[b] > -1)
00144                         {
00145                             mergeSetB = renumberPoints[b];
00146                         }
00147 
00148                         if (mergeSetA == -1 && mergeSetB == -1)
00149                         {
00150                             // add new merge group
00151 #                           ifdef DEBUG_MERGE
00152                             Info<< "adding new merge group " << nMergeSets
00153                                 << endl;
00154 #                           endif
00155 
00156                             // mark points as belonging to a new merge set
00157                             renumberPoints[a] = nMergeSets;
00158                             renumberPoints[b] = nMergeSets;
00159 
00160                             mergeSetID[nMergeSets] = min(a, b);
00161                             nMergeSets++;
00162 
00163                             if (nMergeSets >= mergeSetID.size())
00164                             {
00165                                 Info << "Resizing mergeSetID" << endl;
00166 
00167                                 mergeSetID.setSize
00168                                     (mergeSetID.size() + mergeIncrement);
00169                             }
00170                         }
00171                         else if (mergeSetA == -1 && mergeSetB != -1)
00172                         {
00173 #                           ifdef DEBUG_MERGE
00174                             Info<< "adding point a into the merge set of b. "
00175                                 << "a: " << a << endl;
00176 #                           endif
00177 
00178                             // add point a into the merge set of b
00179                             renumberPoints[a] = mergeSetB;
00180 
00181                             // reset the min label of the mergeSet
00182                             mergeSetID[mergeSetB] =
00183                                 min(a, mergeSetID[mergeSetB]);
00184                         }
00185                         else if  (mergeSetA != -1 && mergeSetB == -1)
00186                         {
00187 #                           ifdef DEBUG_MERGE
00188                             Info<< "adding point b into the merge set of a. "
00189                                 << "b: " << b << endl;
00190 #                           endif
00191 
00192                             // add point b into the merge set of a
00193                             renumberPoints[b] = mergeSetA;
00194 
00195                             // reset the min label of the mergeSet
00196                             mergeSetID[mergeSetA] =
00197                                 min(b, mergeSetID[mergeSetA]);
00198                         }
00199                         else if (mergeSetA != mergeSetB)
00200                         {
00201                             // Points already belong to two different merge
00202                             // sets. Eliminate the higher merge set
00203                             label minMerge = min(mergeSetA, mergeSetB);
00204                             label maxMerge = max(mergeSetA, mergeSetB);
00205 
00206 #                           ifdef DEBUG_MERGE
00207                             Info<< "Points already belong to two "
00208                                 << "different merge sets. "
00209                                 << "Eliminate the higher merge set. Sets: "
00210                                 << minMerge << " and " << maxMerge << endl;
00211 #                           endif
00212 
00213                             forAll (renumberPoints, elimI)
00214                             {
00215                                 if (renumberPoints[elimI] == maxMerge)
00216                                 {
00217                                     renumberPoints[elimI] = minMerge;
00218                                 }
00219                             }
00220 
00221                             // set the lower label
00222                             mergeSetID[minMerge] =
00223                                 min(mergeSetID[minMerge], mergeSetID[maxMerge]);
00224                         }
00225                     }
00226                 }
00227             }
00228         }
00229     }
00230 
00231     mergeSetID.setSize(nMergeSets);
00232 
00233     Info<< "Finished creating merge sets.  Number of merge sets: "
00234         << nMergeSets << "." << endl;
00235 
00236     // Insert the primary point renumbering into the list
00237     // Take care of possibly unused points in the list
00238     forAll (renumberPoints, pointI)
00239     {
00240         if (renumberPoints[pointI] < 0)
00241         {
00242             // point not merged
00243             renumberPoints[pointI] = pointI;
00244         }
00245         else
00246         {
00247             renumberPoints[pointI] = mergeSetID[renumberPoints[pointI]];
00248         }
00249     }
00250 
00251     // Now every point carries either its own label or the lowest of
00252     // the labels of the points it is merged with. Now do PRELIMINARY
00253     // renumbering of all faces.  This will only be used to see which
00254     // points are still used!
00255 
00256     forAll (cellFaces_, cellI)
00257     {
00258         faceList& prelimFaces = cellFaces_[cellI];
00259 
00260         forAll (prelimFaces, faceI)
00261         {
00262             face oldFacePoints = prelimFaces[faceI];
00263 
00264             face& prelimFacePoints = prelimFaces[faceI];
00265 
00266             forAll (prelimFacePoints, pointI)
00267             {
00268                 if (renumberPoints[oldFacePoints[pointI]] < 0)
00269                 {
00270                     FatalErrorIn("starMesh::mergeCoupleFacePoints()")
00271                         << "Error in point renumbering. Old face: "
00272                         << oldFacePoints << endl
00273                         << "prelim face: " << prelimFacePoints
00274                         << abort(FatalError);
00275                 }
00276 
00277                 prelimFacePoints[pointI] =
00278                     renumberPoints[oldFacePoints[pointI]];
00279             }
00280         }
00281     }
00282 
00283     // First step complete. Reset the renumbering list, mark all used points,
00284     // re-create the point list and renumber the whole lot
00285     renumberPoints = 0;
00286 
00287     forAll (cellFaces_, cellI)
00288     {
00289         const faceList& curFaces = cellFaces_[cellI];
00290 
00291         forAll (curFaces, faceI)
00292         {
00293             const face& curFacePoints = curFaces[faceI];
00294 
00295             forAll (curFacePoints, pointI)
00296             {
00297                 renumberPoints[curFacePoints[pointI]]++;
00298             }
00299         }
00300     }
00301 
00302     forAll (cellShapes_, cellI)
00303     {
00304         const labelList& curLabels = cellShapes_[cellI];
00305 
00306         forAll (curLabels, pointI)
00307         {
00308             if (renumberPoints[curLabels[pointI]] == 0)
00309             {
00310                 FatalErrorIn("starMesh::mergeCoupleFacePoints()")
00311                     << "Error in point merging for cell "
00312                     << cellI << ". STAR index: " << starCellID_[cellI]
00313                     << ". " << endl
00314                     << "Point index: " << curLabels[pointI] << " STAR index "
00315                     << starPointID_[curLabels[pointI]] << endl
00316                     << "Please check the geometry for the cell." << endl;
00317             }
00318         }
00319     }
00320 
00321     label nUsedPoints = 0;
00322 
00323     forAll (renumberPoints, pointI)
00324     {
00325         if (renumberPoints[pointI] > 0)
00326         {
00327             // This should be OK as the compressed points list will always
00328             // have less points that the original lists.  Even if there is
00329             // no points removed, this will copy the list back onto itself
00330             // 
00331             renumberPoints[pointI] = nUsedPoints;
00332             points_[nUsedPoints] = points_[pointI];
00333 
00334             nUsedPoints++;
00335         }
00336         else
00337         {
00338             renumberPoints[pointI] = -1;
00339         }
00340     }
00341 
00342     Info<< "Total number of points: " << points_.size() << endl
00343         << "Number of used points: " << nUsedPoints << endl;
00344 
00345     // reset number of points which need to be sorted
00346     points_.setSize(nUsedPoints);
00347 
00348     Info << "Renumbering all faces" << endl;
00349 
00350     forAll (cellFaces_, cellI)
00351     {
00352         faceList& newFaces = cellFaces_[cellI];
00353 
00354         forAll (newFaces, faceI)
00355         {
00356             face oldFacePoints = newFaces[faceI];
00357 
00358             face& newFacePoints = newFaces[faceI];
00359 
00360             forAll (newFacePoints, pointI)
00361             {
00362                 if (renumberPoints[oldFacePoints[pointI]] < 0)
00363                 {
00364                     FatalErrorIn("starMesh::mergeCoupleFacePoints()")
00365                         << "Error in point renumbering for point "
00366                         << oldFacePoints[pointI]
00367                         << ". Renumbering index is -1." << endl
00368                         << "Old face: " << oldFacePoints << endl
00369                         << "New face: " << newFacePoints << abort(FatalError);
00370                 }
00371 
00372                 newFacePoints[pointI] = renumberPoints[oldFacePoints[pointI]];
00373             }
00374         }
00375     }
00376 
00377     Info << "Renumbering all cell shapes" << endl;
00378 
00379     forAll (cellShapes_, cellI)
00380     {
00381         labelList oldLabels = cellShapes_[cellI];
00382 
00383         labelList& curLabels = cellShapes_[cellI];
00384 
00385         forAll (curLabels, pointI)
00386         {
00387             if (renumberPoints[curLabels[pointI]] < 0)
00388             {
00389                 FatalErrorIn("starMesh::mergeCoupleFacePoints()")
00390                     << "Error in point renumbering for cell "
00391                     << cellI << ". STAR index: " << starCellID_[cellI]
00392                     << ". " << endl
00393                     << "Point index: " << curLabels[pointI] << " STAR index "
00394                     << starPointID_[curLabels[pointI]] << " returns invalid "
00395                     << "renumbering index: "
00396                     << renumberPoints[curLabels[pointI]] << "." << endl
00397                     << "Old cellShape:  " << oldLabels << endl
00398                     << "New cell shape: " << curLabels << abort(FatalError);
00399                 }
00400 
00401             curLabels[pointI] = renumberPoints[oldLabels[pointI]];
00402         }
00403     }
00404 
00405     Info << "Renumbering STAR point lookup" << endl;
00406 
00407     labelList oldStarPointID = starPointID_;
00408 
00409     starPointID_ = -1;
00410 
00411     forAll (starPointID_, pointI)
00412     {
00413         if (renumberPoints[pointI] > -1)
00414         {
00415             starPointID_[renumberPoints[pointI]] = oldStarPointID[pointI];
00416         }
00417     }
00418 
00419     // point-cell addressing has changed. Force it to be re-created
00420     deleteDemandDrivenData(pointCellsPtr_);
00421 }
00422 
00423 
00424 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd