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

combineFaces.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 "combineFaces.H"
00028 #include "polyMesh.H"
00029 #include "polyTopoChange.H"
00030 #include "polyRemoveFace.H"
00031 #include "polyAddFace.H"
00032 #include "polyModifyFace.H"
00033 #include "polyRemovePoint.H"
00034 #include "polyAddPoint.H"
00035 #include "syncTools.H"
00036 #include "meshTools.H"
00037 
00038 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00039 
00040 namespace Foam
00041 {
00042 
00043 defineTypeNameAndDebug(combineFaces, 0);
00044 
00045 }
00046 
00047 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00048 
00049 // Test face for (almost) convexeness. Allows certain convexity before
00050 // complaining.
00051 // For every two consecutive edges calculate the normal. If it differs too
00052 // much from the average face normal complain.
00053 bool Foam::combineFaces::convexFace
00054 (
00055     const scalar minConcaveCos,
00056     const pointField& points,
00057     const face& f
00058 )
00059 {
00060     // Get outwards pointing normal of f.
00061     vector n = f.normal(points);
00062     n /= mag(n);
00063 
00064     // Get edge from f[0] to f[size-1];
00065     vector ePrev(points[f[0]] - points[f[f.size()-1]]);
00066     scalar magEPrev = mag(ePrev);
00067     ePrev /= magEPrev + VSMALL;
00068 
00069     forAll(f, fp0)
00070     {
00071         // Get vertex after fp
00072         label fp1 = f.fcIndex(fp0);
00073 
00074         // Normalized vector between two consecutive points
00075         vector e10(points[f[fp1]] - points[f[fp0]]);
00076         scalar magE10 = mag(e10);
00077         e10 /= magE10 + VSMALL;
00078 
00079         if (magEPrev > SMALL && magE10 > SMALL)
00080         {
00081             vector edgeNormal = ePrev ^ e10;
00082 
00083             if ((edgeNormal & n) < 0)
00084             {
00085                 // Concave. Check angle.
00086                 if ((ePrev & e10) < minConcaveCos)
00087                 {
00088                     return false;
00089                 }
00090             }
00091         }
00092 
00093         ePrev = e10;
00094         magEPrev = magE10;
00095     }
00096 
00097     // Not a single internal angle is concave so face is convex.
00098     return true;
00099 }
00100 
00101 
00102 // Determines if set of faces is valid to collapse into single face.
00103 bool Foam::combineFaces::validFace
00104 (
00105     const scalar minConcaveCos,
00106     const indirectPrimitivePatch& bigFace
00107 )
00108 {
00109     // Get outside vertices (in local vertex numbering)
00110     const labelListList& edgeLoops = bigFace.edgeLoops();
00111 
00112     if (edgeLoops.size() > 1)
00113     {
00114         return false;
00115     }
00116 
00117     // Check for convexness
00118     face f(getOutsideFace(bigFace));
00119 
00120     return convexFace(minConcaveCos, bigFace.points(), f);
00121 }
00122 
00123 
00124 void Foam::combineFaces::regioniseFaces
00125 (
00126     const scalar minCos,
00127     const label cellI,
00128     const labelList& cEdges,
00129     Map<label>& faceRegion
00130 ) const
00131 {
00132     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00133 
00134     forAll(cEdges, i)
00135     {
00136         label edgeI = cEdges[i];
00137 
00138         label f0, f1;
00139         meshTools::getEdgeFaces(mesh_, cellI, edgeI, f0, f1);
00140 
00141         label p0 = patches.whichPatch(f0);
00142         label p1 = patches.whichPatch(f1);
00143 
00144         // Face can be merged if
00145         // - same non-coupled patch
00146         // - small angle
00147         if (p0 != -1 && p0 == p1 && !patches[p0].coupled())
00148         {
00149             vector f0Normal = mesh_.faceAreas()[f0];
00150             f0Normal /= mag(f0Normal);
00151             vector f1Normal = mesh_.faceAreas()[f1];
00152             f1Normal /= mag(f1Normal);
00153 
00154             if ((f0Normal&f1Normal) > minCos)
00155             {
00156                 Map<label>::const_iterator f0Fnd = faceRegion.find(f0);
00157 
00158                 label region0 = -1;
00159                 if (f0Fnd != faceRegion.end())
00160                 {
00161                     region0 = f0Fnd();
00162                 }
00163 
00164                 Map<label>::const_iterator f1Fnd = faceRegion.find(f1);
00165 
00166                 label region1 = -1;
00167                 if (f1Fnd != faceRegion.end())
00168                 {
00169                     region1 = f1Fnd();
00170                 }
00171 
00172                 if (region0 == -1)
00173                 {
00174                     if (region1 == -1)
00175                     {
00176                         label useRegion = faceRegion.size();
00177                         faceRegion.insert(f0, useRegion);
00178                         faceRegion.insert(f1, useRegion);
00179                     }
00180                     else
00181                     {
00182                         faceRegion.insert(f0, region1);
00183                     }
00184                 }
00185                 else
00186                 {
00187                     if (region1 == -1)
00188                     {
00189                         faceRegion.insert(f1, region0);
00190                     }
00191                     else if (region0 != region1)
00192                     {
00193                         // Merge the two regions
00194                         label useRegion = min(region0, region1);
00195                         label freeRegion = max(region0, region1);
00196 
00197                         forAllIter(Map<label>, faceRegion, iter)
00198                         {
00199                             if (iter() == freeRegion)
00200                             {
00201                                 iter() = useRegion;
00202                             }
00203                         }
00204                     }
00205                 }
00206             }
00207         }
00208     }
00209 }
00210 
00211 
00212 bool Foam::combineFaces::faceNeighboursValid
00213 (
00214     const label cellI,
00215     const Map<label>& faceRegion
00216 ) const
00217 {
00218     if (faceRegion.size() <= 1)
00219     {
00220         return true;
00221     }
00222 
00223     const cell& cFaces = mesh_.cells()[cellI];
00224 
00225     DynamicList<label> storage;
00226 
00227     // Test for face collapsing to edge since too many neighbours merged.
00228     forAll(cFaces, cFaceI)
00229     {
00230         label faceI = cFaces[cFaceI];
00231 
00232         if (!faceRegion.found(faceI))
00233         {
00234             const labelList& fEdges = mesh_.faceEdges(faceI, storage);
00235 
00236             // Count number of remaining faces neighbouring faceI. This has
00237             // to be 3 or more.
00238 
00239             // Unregioned neighbouring faces
00240             DynamicList<label> neighbourFaces(cFaces.size());
00241             // Regioned neighbouring faces
00242             labelHashSet neighbourRegions;
00243 
00244             forAll(fEdges, i)
00245             {
00246                 label edgeI = fEdges[i];
00247                 label nbrI = meshTools::otherFace(mesh_, cellI, faceI, edgeI);
00248 
00249                 Map<label>::const_iterator iter = faceRegion.find(nbrI);
00250 
00251                 if (iter == faceRegion.end())
00252                 {
00253                     if (findIndex(neighbourFaces, nbrI) == -1)
00254                     {
00255                         neighbourFaces.append(nbrI);
00256                     }
00257                 }
00258                 else
00259                 {
00260                     neighbourRegions.insert(iter());
00261                 }
00262             }
00263 
00264             if ((neighbourFaces.size()+neighbourRegions.size()) < 3)
00265             {
00266                 return false;
00267             }
00268         }
00269     }
00270     return true;
00271 }
00272 
00273 
00274 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00275 
00276 // Construct from mesh
00277 Foam::combineFaces::combineFaces
00278 (
00279     const polyMesh& mesh,
00280     const bool undoable
00281 )
00282 :
00283     mesh_(mesh),
00284     undoable_(undoable),
00285     masterFace_(0),
00286     faceSetsVertices_(0),
00287     savedPointLabels_(0),
00288     savedPoints_(0)
00289 {}
00290 
00291 
00292 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00293 
00294 Foam::labelListList Foam::combineFaces::getMergeSets
00295 (
00296     const scalar featureCos,
00297     const scalar minConcaveCos,
00298     const labelHashSet& boundaryCells
00299 ) const
00300 {
00301     // Lists of faces that can be merged.
00302     DynamicList<labelList> allFaceSets(boundaryCells.size() / 10);
00303     // Storage for on-the-fly cell-edge addressing.
00304     DynamicList<label> storage;
00305 
00306     // On all cells regionise the faces
00307     forAllConstIter(labelHashSet, boundaryCells, iter)
00308     {
00309         label cellI = iter.key();
00310 
00311         const cell& cFaces = mesh_.cells()[cellI];
00312 
00313         const labelList& cEdges = mesh_.cellEdges(cellI, storage);
00314 
00315         // Region per face
00316         Map<label> faceRegion(cFaces.size());
00317         regioniseFaces(featureCos, cellI, cEdges, faceRegion);
00318 
00319         // Now we have in faceRegion for every face the region with planar
00320         // face sharing the same region. We now check whether the resulting
00321         // sets cause a face
00322         // - to become a set of edges since too many faces are merged.
00323         // - to become convex
00324 
00325         if (faceNeighboursValid(cellI, faceRegion))
00326         {
00327             // Create region-to-faces addressing
00328             Map<labelList> regionToFaces(faceRegion.size());
00329 
00330             forAllConstIter(Map<label>, faceRegion, iter)
00331             {
00332                 label faceI = iter.key();
00333                 label region = iter();
00334 
00335                 Map<labelList>::iterator regionFnd = regionToFaces.find(region);
00336 
00337                 if (regionFnd != regionToFaces.end())
00338                 {
00339                     labelList& setFaces = regionFnd();
00340                     label sz = setFaces.size();
00341                     setFaces.setSize(sz+1);
00342                     setFaces[sz] = faceI;
00343                 }
00344                 else
00345                 {
00346                     regionToFaces.insert(region, labelList(1, faceI));
00347                 }
00348             }
00349 
00350             // For every set check if it forms a valid convex face
00351 
00352             forAllConstIter(Map<labelList>, regionToFaces, iter)
00353             {
00354                 // Make face out of setFaces
00355                 indirectPrimitivePatch bigFace
00356                 (
00357                     IndirectList<face>
00358                     (
00359                         mesh_.faces(),
00360                         iter()
00361                     ),
00362                     mesh_.points()
00363                 );
00364 
00365                 // Only store if -only one outside loop -which forms convex face
00366                 if (validFace(minConcaveCos, bigFace))
00367                 {
00368                     allFaceSets.append(iter());
00369                 }
00370             }
00371         }
00372     }
00373 
00374     return allFaceSets.shrink();
00375 }
00376 
00377 
00378 Foam::labelListList Foam::combineFaces::getMergeSets
00379 (
00380     const scalar featureCos,
00381     const scalar minConcaveCos
00382 ) const
00383 {
00384     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00385 
00386     // Pick up all cells on boundary
00387     labelHashSet boundaryCells(mesh_.nFaces()-mesh_.nInternalFaces());
00388 
00389     forAll(patches, patchI)
00390     {
00391         const polyPatch& patch = patches[patchI];
00392 
00393         if (!patch.coupled())
00394         {
00395             forAll(patch, i)
00396             {
00397                 boundaryCells.insert(mesh_.faceOwner()[patch.start()+i]);
00398             }
00399         }
00400     }
00401 
00402     return getMergeSets(featureCos, minConcaveCos, boundaryCells);
00403 }
00404 
00405 
00406 // Gets outside edgeloop as a face
00407 // - in same order as faces
00408 // - in mesh vertex labels
00409 Foam::face Foam::combineFaces::getOutsideFace
00410 (
00411     const indirectPrimitivePatch& fp
00412 )
00413 {
00414     if (fp.edgeLoops().size() != 1)
00415     {
00416         FatalErrorIn
00417         (
00418             "combineFaces::getOutsideFace(const indirectPrimitivePatch&)"
00419         )   << "Multiple outside loops:" << fp.edgeLoops()
00420             << abort(FatalError);
00421     }
00422 
00423     // Get first boundary edge. Since guaranteed one edgeLoop when in here this
00424     // edge must be on it.
00425     label bEdgeI = fp.nInternalEdges();
00426 
00427     const edge& e = fp.edges()[bEdgeI];
00428 
00429     const labelList& eFaces = fp.edgeFaces()[bEdgeI];
00430 
00431     if (eFaces.size() != 1)
00432     {
00433         FatalErrorIn
00434         (
00435             "combineFaces::getOutsideFace(const indirectPrimitivePatch&)"
00436         )   << "boundary edge:" << bEdgeI
00437             << " points:" << fp.meshPoints()[e[0]]
00438             << ' ' << fp.meshPoints()[e[1]]
00439             << " on indirectPrimitivePatch has " << eFaces.size()
00440             << " faces using it" << abort(FatalError);
00441     }
00442 
00443 
00444     // Outside loop
00445     const labelList& outsideLoop = fp.edgeLoops()[0];
00446 
00447 
00448     // Get order of edge e in outside loop
00449     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00450 
00451     // edgeLoopConsistent : true  = edge in same order as outsideloop
00452     //                      false = opposite order
00453     bool edgeLoopConsistent = false;
00454 
00455     {
00456         label index0 = findIndex(outsideLoop, e[0]);
00457         label index1 = findIndex(outsideLoop, e[1]);
00458 
00459         if (index0 == -1 || index1 == -1)
00460         {
00461             FatalErrorIn
00462             (
00463                 "combineFaces::getOutsideFace"
00464                 "(const indirectPrimitivePatch&)"
00465             )   << "Cannot find boundary edge:" << e
00466                 << " points:" << fp.meshPoints()[e[0]]
00467                 << ' ' << fp.meshPoints()[e[1]]
00468                 << " in edgeLoop:" << outsideLoop << abort(FatalError);
00469         }
00470         else if (index1 == outsideLoop.fcIndex(index0))
00471         {
00472             edgeLoopConsistent = true;
00473         }
00474         else if (index0 == outsideLoop.fcIndex(index1))
00475         {
00476             edgeLoopConsistent = false;
00477         }
00478         else
00479         {
00480             FatalErrorIn
00481             (
00482                 "combineFaces::getOutsideFace"
00483                 "(const indirectPrimitivePatch&)"
00484             )   << "Cannot find boundary edge:" << e
00485                 << " points:" << fp.meshPoints()[e[0]]
00486                 << ' ' << fp.meshPoints()[e[1]]
00487                 << " on consecutive points in edgeLoop:"
00488                 << outsideLoop << abort(FatalError);
00489         }
00490     }
00491 
00492 
00493     // Get face in local vertices
00494     const face& localF = fp.localFaces()[eFaces[0]];
00495 
00496     // Get order of edge in localF
00497     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
00498 
00499     // faceEdgeConsistent : true  = edge in same order as localF
00500     //                      false = opposite order
00501     bool faceEdgeConsistent = false;
00502 
00503     {
00504         // Find edge in face.
00505         label index = findIndex(fp.faceEdges()[eFaces[0]], bEdgeI);
00506 
00507         if (index == -1)
00508         {
00509             FatalErrorIn
00510             (
00511                 "combineFaces::getOutsideFace"
00512                 "(const indirectPrimitivePatch&)"
00513             )   << "Cannot find boundary edge:" << e
00514                 << " points:" << fp.meshPoints()[e[0]]
00515                 << ' ' << fp.meshPoints()[e[1]]
00516                 << " in face:" << eFaces[0]
00517                 << " edges:" << fp.faceEdges()[eFaces[0]]
00518                 << abort(FatalError);
00519         }
00520         else if (localF[index] == e[0] && localF.nextLabel(index) == e[1])
00521         {
00522             faceEdgeConsistent = true;
00523         }
00524         else if (localF[index] == e[1] && localF.nextLabel(index) == e[0])
00525         {
00526             faceEdgeConsistent = false;
00527         }
00528         else
00529         {
00530             FatalErrorIn
00531             (
00532                 "combineFaces::getOutsideFace"
00533                 "(const indirectPrimitivePatch&)"
00534             )   << "Cannot find boundary edge:" << e
00535                 << " points:" << fp.meshPoints()[e[0]]
00536                 << ' ' << fp.meshPoints()[e[1]]
00537                 << " in face:" << eFaces[0] << " verts:" << localF
00538                 << abort(FatalError);
00539         }
00540     }
00541 
00542     // Get face in mesh points.
00543     face meshFace(renumber(fp.meshPoints(), outsideLoop));
00544 
00545     if (faceEdgeConsistent != edgeLoopConsistent)
00546     {
00547         reverse(meshFace);
00548     }
00549     return meshFace;
00550 }
00551 
00552 
00553 void Foam::combineFaces::setRefinement
00554 (
00555     const labelListList& faceSets,
00556     polyTopoChange& meshMod
00557 )
00558 {
00559     if (undoable_)
00560     {
00561         masterFace_.setSize(faceSets.size());
00562         faceSetsVertices_.setSize(faceSets.size());
00563         forAll(faceSets, setI)
00564         {
00565             const labelList& setFaces = faceSets[setI];
00566 
00567             masterFace_[setI] = setFaces[0];
00568             faceSetsVertices_[setI] = UIndirectList<face>
00569             (
00570                 mesh_.faces(),
00571                 setFaces
00572             );
00573         }
00574     }
00575 
00576     // Running count of number of faces using a point
00577     labelList nPointFaces(mesh_.nPoints(), 0);
00578 
00579     const labelListList& pointFaces = mesh_.pointFaces();
00580 
00581     forAll(pointFaces, pointI)
00582     {
00583         nPointFaces[pointI] = pointFaces[pointI].size();
00584     }
00585 
00586     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00587 
00588     forAll(faceSets, setI)
00589     {
00590         const labelList& setFaces = faceSets[setI];
00591 
00592         // Check
00593         if (debug)
00594         {
00595             forAll(setFaces, i)
00596             {
00597                 label patchI = patches.whichPatch(setFaces[i]);
00598 
00599                 if (patchI == -1 || patches[patchI].coupled())
00600                 {
00601                     FatalErrorIn
00602                     (
00603                         "combineFaces::setRefinement"
00604                         "(const bool, const labelListList&"
00605                         ", polyTopoChange&)"
00606                     )   << "Can only merge non-coupled boundary faces"
00607                         << " but found internal or coupled face:"
00608                         << setFaces[i] << " in set " << setI
00609                         << abort(FatalError);
00610                 }
00611             }
00612         }
00613 
00614         // Make face out of setFaces
00615         indirectPrimitivePatch bigFace
00616         (
00617             IndirectList<face>
00618             (
00619                 mesh_.faces(),
00620                 setFaces
00621             ),
00622             mesh_.points()
00623         );
00624 
00625         // Get outside vertices (in local vertex numbering)
00626         const labelListList& edgeLoops = bigFace.edgeLoops();
00627 
00628         if (edgeLoops.size() != 1)
00629         {
00630             FatalErrorIn
00631             (
00632                 "combineFaces::setRefinement"
00633                 "(const bool, const labelListList&, polyTopoChange&)"
00634             )   << "Faces to-be-merged " << setFaces
00635                 << " do not form a single big face." << nl
00636                 << abort(FatalError);
00637         }
00638 
00639 
00640         // Delete all faces except master, whose face gets modified.
00641 
00642         // Modify master face
00643         // ~~~~~~~~~~~~~~~~~~
00644 
00645         label masterFaceI = setFaces[0];
00646 
00647         // Get outside face in mesh vertex labels
00648         face outsideFace(getOutsideFace(bigFace));
00649 
00650         label zoneID = mesh_.faceZones().whichZone(masterFaceI);
00651 
00652         bool zoneFlip = false;
00653 
00654         if (zoneID >= 0)
00655         {
00656             const faceZone& fZone = mesh_.faceZones()[zoneID];
00657 
00658             zoneFlip = fZone.flipMap()[fZone.whichFace(masterFaceI)];
00659         }
00660 
00661         label patchI = mesh_.boundaryMesh().whichPatch(masterFaceI);
00662 
00663         meshMod.setAction
00664         (
00665             polyModifyFace
00666             (
00667                 outsideFace,                    // modified face
00668                 masterFaceI,                    // label of face being modified
00669                 mesh_.faceOwner()[masterFaceI], // owner
00670                 -1,                             // neighbour
00671                 false,                          // face flip
00672                 patchI,                         // patch for face
00673                 false,                          // remove from zone
00674                 zoneID,                         // zone for face
00675                 zoneFlip                        // face flip in zone
00676             )
00677         );
00678 
00679 
00680         // Delete all non-master faces
00681         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
00682 
00683         for (label i = 1; i < setFaces.size(); i++)
00684         {
00685             meshMod.setAction(polyRemoveFace(setFaces[i]));
00686         }
00687 
00688 
00689         // Mark unused points for deletion
00690         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00691 
00692         // Remove count of all faces combined
00693         forAll(setFaces, i)
00694         {
00695             const face& f = mesh_.faces()[setFaces[i]];
00696 
00697             forAll(f, fp)
00698             {
00699                 nPointFaces[f[fp]]--;
00700             }
00701         }
00702         // But keep count on new outside face
00703         forAll(outsideFace, fp)
00704         {
00705             nPointFaces[outsideFace[fp]]++;
00706         }
00707     }
00708 
00709 
00710     // If one or more processors use the point it needs to be kept.
00711     syncTools::syncPointList
00712     (
00713         mesh_,
00714         nPointFaces,
00715         plusEqOp<label>(),
00716         0,                  // null value
00717         false               // no separation
00718     );
00719 
00720     // Remove all unused points. Store position if undoable.
00721     if (!undoable_)
00722     {
00723         forAll(nPointFaces, pointI)
00724         {
00725             if (nPointFaces[pointI] == 0)
00726             {
00727                 meshMod.setAction(polyRemovePoint(pointI));
00728             }
00729         }
00730     }
00731     else
00732     {
00733         // Count removed points
00734         label n = 0;
00735         forAll(nPointFaces, pointI)
00736         {
00737             if (nPointFaces[pointI] == 0)
00738             {
00739                 n++;
00740             }
00741         }
00742 
00743         savedPointLabels_.setSize(n);
00744         savedPoints_.setSize(n);
00745         Map<label> meshToSaved(2*n);
00746 
00747         // Remove points and store position
00748         n = 0;
00749         forAll(nPointFaces, pointI)
00750         {
00751             if (nPointFaces[pointI] == 0)
00752             {
00753                 meshMod.setAction(polyRemovePoint(pointI));
00754 
00755                 savedPointLabels_[n] = pointI;
00756                 savedPoints_[n] = mesh_.points()[pointI];
00757 
00758                 meshToSaved.insert(pointI, n);
00759                 n++;
00760             }
00761         }
00762 
00763         // Update stored vertex labels. Negative indices index into local points
00764         forAll(faceSetsVertices_, setI)
00765         {
00766             faceList& setFaces = faceSetsVertices_[setI];
00767 
00768             forAll(setFaces, i)
00769             {
00770                 face& f = setFaces[i];
00771 
00772                 forAll(f, fp)
00773                 {
00774                     label pointI = f[fp];
00775 
00776                     if (nPointFaces[pointI] == 0)
00777                     {
00778                         f[fp] = -meshToSaved[pointI]-1;
00779                     }
00780                 }
00781             }
00782         }
00783     }
00784 }
00785 
00786 
00787 void Foam::combineFaces::updateMesh(const mapPolyMesh& map)
00788 {
00789     if (undoable_)
00790     {
00791         // Master face just renumbering of point labels
00792         inplaceRenumber(map.reverseFaceMap(), masterFace_);
00793 
00794         // Stored faces refer to backed-up vertices (not changed)
00795         // and normal vertices (need to be renumbered)
00796         forAll(faceSetsVertices_, setI)
00797         {
00798             faceList& faces = faceSetsVertices_[setI];
00799 
00800             forAll(faces, i)
00801             {
00802                 // Note: faces[i] can have negative elements.
00803                 face& f = faces[i];
00804 
00805                 forAll(f, fp)
00806                 {
00807                     label pointI = f[fp];
00808 
00809                     if (pointI >= 0)
00810                     {
00811                         f[fp] = map.reversePointMap()[pointI];
00812 
00813                         if (f[fp] < 0)
00814                         {
00815                             FatalErrorIn
00816                             (
00817                                 "combineFaces::updateMesh"
00818                                 "(const mapPolyMesh&)"
00819                             )   << "In set " << setI << " at position " << i
00820                                 << " with master face "
00821                                 << masterFace_[setI] << nl
00822                                 << "the points of the slave face " << faces[i]
00823                                 << " don't exist anymore."
00824                                 << abort(FatalError);
00825                         }
00826                     }
00827                 }
00828             }
00829         }
00830     }
00831 }
00832 
00833 
00834 // Note that faces on coupled patches are never combined (or at least
00835 // getMergeSets never picks them up. Thus the points on them will never get
00836 // deleted since still used by the coupled faces. So never a risk of undoing
00837 // things (faces/points) on coupled patches.
00838 void Foam::combineFaces::setUnrefinement
00839 (
00840     const labelList& masterFaces,
00841     polyTopoChange& meshMod,
00842     Map<label>& restoredPoints,
00843     Map<label>& restoredFaces,
00844     Map<label>& restoredCells
00845 )
00846 {
00847     if (!undoable_)
00848     {
00849         FatalErrorIn
00850         (
00851             "combineFaces::setUnrefinement"
00852             "(const labelList&, polyTopoChange&"
00853             ", Map<label>&, Map<label>&, Map<label>&)"
00854         )   << "Can only call setUnrefinement if constructed with"
00855             << " unrefinement capability." << exit(FatalError);
00856     }
00857 
00858 
00859     // Restored points
00860     labelList addedPoints(savedPoints_.size(), -1);
00861 
00862     // Invert set-to-master-face
00863     Map<label> masterToSet(masterFace_.size());
00864 
00865     forAll(masterFace_, setI)
00866     {
00867         if (masterFace_[setI] >= 0)
00868         {
00869             masterToSet.insert(masterFace_[setI], setI);
00870         }
00871     }
00872 
00873     forAll(masterFaces, i)
00874     {
00875         label masterFaceI = masterFaces[i];
00876 
00877         Map<label>::const_iterator iter = masterToSet.find(masterFaceI);
00878 
00879         if (iter == masterToSet.end())
00880         {
00881             FatalErrorIn
00882             (
00883                 "combineFaces::setUnrefinement"
00884                 "(const labelList&, polyTopoChange&"
00885                 ", Map<label>&, Map<label>&, Map<label>&)"
00886             )   << "Master face " << masterFaceI
00887                 << " is not the master of one of the merge sets"
00888                 << " or has already been merged"
00889                 << abort(FatalError);
00890         }
00891 
00892         label setI = iter();
00893 
00894 
00895         // Update faces of the merge setI for reintroduced vertices
00896         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00897 
00898         faceList& faces = faceSetsVertices_[setI];
00899 
00900         if (faces.empty())
00901         {
00902             FatalErrorIn
00903             (
00904                 "combineFaces::setUnrefinement"
00905                 "(const labelList&, polyTopoChange&"
00906                 ", Map<label>&, Map<label>&, Map<label>&)"
00907             )   << "Set " << setI << " with master face " << masterFaceI
00908                 << " has already been merged." << abort(FatalError);
00909         }
00910 
00911         forAll(faces, i)
00912         {
00913             face& f = faces[i];
00914 
00915             forAll(f, fp)
00916             {
00917                 label pointI = f[fp];
00918 
00919                 if (pointI < 0)
00920                 {
00921                     label localI = -pointI-1;
00922 
00923                     if (addedPoints[localI] == -1)
00924                     {
00925                         // First occurrence of saved point. Reintroduce point
00926                         addedPoints[localI] = meshMod.setAction
00927                         (
00928                             polyAddPoint
00929                             (
00930                                 savedPoints_[localI],   // point
00931                                 -1,                     // master point
00932                                 -1,                     // zone for point
00933                                 true                    // supports a cell
00934                             )
00935                         );
00936                         restoredPoints.insert
00937                         (
00938                             addedPoints[localI],        // current point label
00939                             savedPointLabels_[localI]   // point label when it
00940                                                         // was stored
00941                         );
00942                     }
00943                     f[fp] = addedPoints[localI];
00944                 }
00945             }
00946         }
00947 
00948 
00949         // Restore
00950         // ~~~~~~~
00951 
00952         label own = mesh_.faceOwner()[masterFaceI];
00953         label zoneID = mesh_.faceZones().whichZone(masterFaceI);
00954         bool zoneFlip = false;
00955         if (zoneID >= 0)
00956         {
00957             const faceZone& fZone = mesh_.faceZones()[zoneID];
00958             zoneFlip = fZone.flipMap()[fZone.whichFace(masterFaceI)];
00959         }
00960         label patchI = mesh_.boundaryMesh().whichPatch(masterFaceI);
00961 
00962         if (mesh_.boundaryMesh()[patchI].coupled())
00963         {
00964             FatalErrorIn
00965             (
00966                 "combineFaces::setUnrefinement"
00967                 "(const labelList&, polyTopoChange&"
00968                 ", Map<label>&, Map<label>&, Map<label>&)"
00969             )   << "Master face " << masterFaceI << " is on coupled patch "
00970                 << mesh_.boundaryMesh()[patchI].name()
00971                 << abort(FatalError);
00972         }
00973 
00974         //Pout<< "Restoring new master face " << masterFaceI
00975         //    << " to vertices " << faces[0] << endl;
00976 
00977         // Modify the master face.
00978         meshMod.setAction
00979         (
00980             polyModifyFace
00981             (
00982                 faces[0],                       // original face
00983                 masterFaceI,                    // label of face
00984                 own,                            // owner
00985                 -1,                             // neighbour
00986                 false,                          // face flip
00987                 patchI,                         // patch for face
00988                 false,                          // remove from zone
00989                 zoneID,                         // zone for face
00990                 zoneFlip                        // face flip in zone
00991             )
00992         );
00993 
00994         // Add the previously removed faces
00995         for (label i = 1; i < faces.size(); i++)
00996         {
00997             //Pout<< "Restoring removed face with vertices " << faces[i]
00998             //    << endl;
00999 
01000             meshMod.setAction
01001             (
01002                 polyAddFace
01003                 (
01004                     faces[i],        // vertices
01005                     own,                    // owner,
01006                     -1,                     // neighbour,
01007                     -1,                     // masterPointID,
01008                     -1,                     // masterEdgeID,
01009                     masterFaceI,             // masterFaceID,
01010                     false,                  // flipFaceFlux,
01011                     patchI,                 // patchID,
01012                     zoneID,                 // zoneID,
01013                     zoneFlip                // zoneFlip
01014                 )
01015             );
01016         }
01017 
01018         // Clear out restored set
01019         faceSetsVertices_[setI].clear();
01020         masterFace_[setI] = -1;
01021     }
01022 }
01023 
01024 
01025 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd