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

undoableMeshCutter.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 "undoableMeshCutter.H"
00028 #include "polyMesh.H"
00029 #include "polyTopoChange.H"
00030 #include "DynamicList.H"
00031 #include "meshCutter.H"
00032 #include "cellCuts.H"
00033 #include "splitCell.H"
00034 #include "mapPolyMesh.H"
00035 #include "mathematicalConstants.H"
00036 #include "meshTools.H"
00037 
00038 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00039 
00040 namespace Foam
00041 {
00042 
00043 defineTypeNameAndDebug(undoableMeshCutter, 0);
00044 
00045 }
00046 
00047 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00048 
00049 // For debugging
00050 void Foam::undoableMeshCutter::printCellRefTree
00051 (
00052     Ostream& os,
00053     const word& indent,
00054     const splitCell* splitCellPtr
00055 ) const
00056 {
00057     if (splitCellPtr)
00058     {
00059         os << indent << splitCellPtr->cellLabel() << endl;
00060 
00061         word subIndent = indent + "--";
00062 
00063         printCellRefTree(os, subIndent, splitCellPtr->master());
00064 
00065         printCellRefTree(os, subIndent, splitCellPtr->slave());
00066     }
00067 }
00068 
00069 
00070 // For debugging
00071 void Foam::undoableMeshCutter::printRefTree(Ostream& os) const
00072 {
00073     for
00074     (
00075         Map<splitCell*>::const_iterator iter = liveSplitCells_.begin();
00076         iter != liveSplitCells_.end();
00077         ++iter
00078     )
00079     {
00080         const splitCell* splitPtr = iter();
00081 
00082         // Walk to top (master path only)
00083         while (splitPtr->parent())
00084         {
00085             if (!splitPtr->isMaster())
00086             {
00087                 splitPtr = NULL;
00088 
00089                 break;
00090             }
00091             else
00092             {
00093                 splitPtr = splitPtr->parent();
00094             }
00095         }
00096 
00097         // If we have reached top along master path start printing.
00098         if (splitPtr)
00099         {
00100             // Print from top down
00101             printCellRefTree(os, word(""), splitPtr);
00102         }
00103     }
00104 }
00105 
00106 
00107 // Update all (cell) labels on splitCell structure.
00108 // Do in two passes to prevent allocation if nothing changed.
00109 void Foam::undoableMeshCutter::updateLabels
00110 (
00111     const labelList& map,
00112     Map<splitCell*>& liveSplitCells
00113 )
00114 {
00115     // Pass1 : check if changed
00116 
00117     bool changed = false;
00118 
00119     forAllConstIter(Map<splitCell*>,liveSplitCells, iter)
00120     {
00121         const splitCell* splitPtr = iter();
00122 
00123         if (!splitPtr)
00124         {
00125             FatalErrorIn
00126             (
00127                 "undoableMeshCutter::updateLabels"
00128                 "(const labelList&, Map<splitCell*>&)"
00129             )   << "Problem: null pointer on liveSplitCells list"
00130                 << abort(FatalError);
00131         }
00132 
00133         label cellI = splitPtr->cellLabel();
00134 
00135         if (cellI != map[cellI])
00136         {
00137             changed = true;
00138 
00139             break;
00140         }
00141     }
00142 
00143 
00144     // Pass2: relabel
00145 
00146     if (changed)
00147     {
00148         // Build new liveSplitCells
00149         // since new labels (= keys in Map) might clash with existing ones.
00150         Map<splitCell*> newLiveSplitCells(2*liveSplitCells.size());
00151 
00152         forAllIter(Map<splitCell*>, liveSplitCells, iter)
00153         {
00154             splitCell* splitPtr = iter();
00155 
00156             label cellI = splitPtr->cellLabel();
00157 
00158             label newCellI = map[cellI];
00159 
00160             if (debug && (cellI != newCellI))
00161             {
00162                 Pout<< "undoableMeshCutter::updateLabels :"
00163                     << " Updating live (split)cell from " << cellI
00164                     << " to " << newCellI << endl;
00165             }
00166 
00167             if (newCellI >= 0)
00168             {
00169                 // Update splitCell. Can do inplace since only one cellI will
00170                 // refer to this structure.
00171                 splitPtr->cellLabel() = newCellI;
00172 
00173                 // Update liveSplitCells
00174                 newLiveSplitCells.insert(newCellI, splitPtr);
00175             }
00176         }
00177         liveSplitCells = newLiveSplitCells;
00178     }
00179 }
00180 
00181 
00182 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00183 
00184 // Construct from components
00185 Foam::undoableMeshCutter::undoableMeshCutter
00186 (
00187     const polyMesh& mesh,
00188     const bool undoable
00189 )
00190 :
00191     meshCutter(mesh),
00192     undoable_(undoable),
00193     liveSplitCells_(mesh.nCells()/100 + 100),
00194     faceRemover_
00195     (
00196         mesh,   
00197         Foam::cos(30./180. * mathematicalConstant::pi)
00198     )
00199 {}
00200 
00201 
00202 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00203 
00204 Foam::undoableMeshCutter::~undoableMeshCutter()
00205 {
00206     // Clean split cell tree.
00207 
00208     forAllIter(Map<splitCell*>, liveSplitCells_, iter)
00209     {
00210         splitCell* splitPtr = iter();
00211 
00212         while (splitPtr)
00213         {
00214             splitCell* parentPtr = splitPtr->parent();
00215 
00216             // Sever ties with parent. Also of other side of refinement since
00217             // we are handling rest of tree so other side will not have to.
00218             if (parentPtr)
00219             {
00220                 splitCell* otherSidePtr = splitPtr->getOther();
00221 
00222                 otherSidePtr->parent() = NULL;
00223 
00224                 splitPtr->parent() = NULL;
00225             }
00226 
00227             // Delete splitCell (updates pointer on parent to itself)
00228             delete splitPtr;
00229 
00230             splitPtr = parentPtr;
00231         }
00232     }
00233 }
00234 
00235 
00236 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00237 
00238 void Foam::undoableMeshCutter::setRefinement
00239 (
00240     const cellCuts& cuts,
00241     polyTopoChange& meshMod
00242 )
00243 {
00244     // Insert commands to actually cut cells
00245     meshCutter::setRefinement(cuts, meshMod);
00246 
00247     if (undoable_)
00248     {
00249         // Use cells cut in this iteration to update splitCell tree.
00250         forAllConstIter(Map<label>, addedCells(), iter)
00251         {
00252             label cellI = iter.key();
00253 
00254             label addedCellI = iter();
00255 
00256 
00257             // Newly created split cell. (cellI ->  cellI + addedCellI)
00258 
00259             // Check if cellI already part of split.
00260             Map<splitCell*>::iterator findCell =
00261                 liveSplitCells_.find(cellI);
00262 
00263             if (findCell == liveSplitCells_.end())
00264             {
00265                 // CellI not yet split. It cannot be unlive split cell
00266                 // since that would be illegal to split in the first
00267                 // place.
00268 
00269                 // Create 0th level. Null parent to denote this.
00270                 splitCell* parentPtr = new splitCell(cellI, NULL);
00271 
00272                 splitCell* masterPtr = new splitCell(cellI, parentPtr);
00273 
00274                 splitCell* slavePtr = new splitCell(addedCellI, parentPtr);
00275 
00276                 // Store newly created cells on parent together with face
00277                 // that splits them
00278                 parentPtr->master() = masterPtr;
00279                 parentPtr->slave() = slavePtr;
00280 
00281                 // Insert master and slave into live splitcell list
00282 
00283                 if (liveSplitCells_.found(addedCellI))
00284                 {
00285                     FatalErrorIn("undoableMeshCutter::setRefinement")
00286                         << "problem addedCell:" << addedCellI
00287                         << abort(FatalError);
00288                 }
00289 
00290                 liveSplitCells_.insert(cellI, masterPtr);
00291                 liveSplitCells_.insert(addedCellI, slavePtr);
00292             }
00293             else
00294             {
00295                 // Cell that was split has been split again.
00296                 splitCell* parentPtr = findCell();
00297 
00298                 // It is no longer live
00299                 liveSplitCells_.erase(findCell);
00300 
00301                 splitCell* masterPtr = new splitCell(cellI, parentPtr);
00302 
00303                 splitCell* slavePtr = new splitCell(addedCellI, parentPtr);
00304 
00305                 // Store newly created cells on parent together with face
00306                 // that splits them
00307                 parentPtr->master() = masterPtr;
00308                 parentPtr->slave() = slavePtr;
00309 
00310                 // Insert master and slave into live splitcell list
00311 
00312                 if (liveSplitCells_.found(addedCellI))
00313                 {
00314                     FatalErrorIn("undoableMeshCutter::setRefinement")
00315                         << "problem addedCell:" << addedCellI
00316                         << abort(FatalError);
00317                 }
00318 
00319                 liveSplitCells_.insert(cellI, masterPtr);
00320                 liveSplitCells_.insert(addedCellI, slavePtr);
00321             }
00322         }
00323 
00324         if (debug & 2)
00325         {
00326             Pout<< "** After refinement: liveSplitCells_:" << endl;
00327 
00328             printRefTree(Pout);
00329         }
00330     }
00331 }
00332 
00333 
00334 void Foam::undoableMeshCutter::updateMesh(const mapPolyMesh& morphMap)
00335 {
00336     // Update mesh cutter for new labels.
00337     meshCutter::updateMesh(morphMap);
00338 
00339     // No need to update cell walker for new labels since does not store any.
00340 
00341     // Update faceRemover for new labels
00342     faceRemover_.updateMesh(morphMap);
00343 
00344     if (undoable_)
00345     {
00346         // Update all live split cells for mesh mapper.
00347         updateLabels(morphMap.reverseCellMap(), liveSplitCells_);
00348     }
00349 }
00350 
00351 
00352 Foam::labelList Foam::undoableMeshCutter::getSplitFaces() const
00353 {
00354     if (!undoable_)
00355     {
00356         FatalErrorIn("undoableMeshCutter::getSplitFaces()")
00357             << "Only call if constructed with unrefinement capability"
00358             << abort(FatalError);
00359     }
00360 
00361     DynamicList<label> liveSplitFaces(liveSplitCells_.size());
00362 
00363     forAllConstIter(Map<splitCell*>, liveSplitCells_, iter)
00364     {
00365         const splitCell* splitPtr = iter();
00366 
00367         if (!splitPtr->parent())
00368         {
00369             FatalErrorIn("undoableMeshCutter::getSplitFaces()")
00370                 << "Live split cell without parent" << endl
00371                 << "splitCell:" << splitPtr->cellLabel()
00372                 << abort(FatalError);
00373         }
00374 
00375         // Check if not top of refinement and whether it is the master side
00376         if (splitPtr->isMaster())
00377         {
00378             splitCell* slavePtr = splitPtr->getOther();
00379 
00380             if
00381             (
00382                 liveSplitCells_.found(slavePtr->cellLabel())
00383              && splitPtr->isUnrefined()
00384              && slavePtr->isUnrefined()
00385             )
00386             {
00387                 // Both master and slave are live and are not refined.
00388                 // Find common face.
00389 
00390                 label cellI = splitPtr->cellLabel();
00391 
00392                 label slaveCellI = slavePtr->cellLabel();
00393 
00394                 label commonFaceI =
00395                     meshTools::getSharedFace
00396                     (
00397                         mesh(),
00398                         cellI,
00399                         slaveCellI
00400                     );
00401 
00402                 liveSplitFaces.append(commonFaceI);
00403             }
00404         }
00405     }
00406 
00407     return liveSplitFaces.shrink();
00408 }
00409 
00410 
00411 Foam::Map<Foam::label> Foam::undoableMeshCutter::getAddedCells() const
00412 {
00413     // (code copied from getSplitFaces)
00414 
00415     if (!undoable_)
00416     {
00417         FatalErrorIn("undoableMeshCutter::getAddedCells()")
00418             << "Only call if constructed with unrefinement capability"
00419             << abort(FatalError);
00420     }
00421 
00422     Map<label> addedCells(liveSplitCells_.size());
00423 
00424     forAllConstIter(Map<splitCell*>, liveSplitCells_, iter)
00425     {
00426         const splitCell* splitPtr = iter();
00427 
00428         if (!splitPtr->parent())
00429         {
00430             FatalErrorIn("undoableMeshCutter::getAddedCells()")
00431                 << "Live split cell without parent" << endl
00432                 << "splitCell:" << splitPtr->cellLabel()
00433                 << abort(FatalError);
00434         }
00435 
00436         // Check if not top of refinement and whether it is the master side
00437         if (splitPtr->isMaster())
00438         {
00439             splitCell* slavePtr = splitPtr->getOther();
00440 
00441             if
00442             (
00443                 liveSplitCells_.found(slavePtr->cellLabel())
00444              && splitPtr->isUnrefined()
00445              && slavePtr->isUnrefined()
00446             )
00447             {
00448                 // Both master and slave are live and are not refined.
00449                 addedCells.insert(splitPtr->cellLabel(), slavePtr->cellLabel());
00450             }
00451         }
00452     }
00453     return addedCells;
00454 }
00455 
00456 
00457 Foam::labelList Foam::undoableMeshCutter::removeSplitFaces
00458 (
00459     const labelList& splitFaces,
00460     polyTopoChange& meshMod
00461 )
00462 {
00463     if (!undoable_)
00464     {
00465         FatalErrorIn("undoableMeshCutter::removeSplitFaces(const labelList&)")
00466             << "Only call if constructed with unrefinement capability"
00467             << abort(FatalError);
00468     }
00469 
00470     // Check with faceRemover what faces will get removed. Note that this can
00471     // be more (but never less) than splitFaces provided.
00472     labelList cellRegion;
00473     labelList cellRegionMaster;
00474     labelList facesToRemove;
00475 
00476     faceRemover().compatibleRemoves
00477     (
00478         splitFaces,         // pierced faces
00479         cellRegion,         // per cell -1 or region it is merged into
00480         cellRegionMaster,   // per region the master cell
00481         facesToRemove       // new faces to be removed.
00482     );
00483 
00484     if (facesToRemove.size() != splitFaces.size())
00485     {
00486         Pout<< "cellRegion:" << cellRegion << endl;
00487         Pout<< "cellRegionMaster:" << cellRegionMaster << endl;
00488 
00489         FatalErrorIn
00490         (
00491             "undoableMeshCutter::removeSplitFaces(const labelList&)"
00492         )   << "Faces to remove:" << splitFaces << endl
00493             << "to be removed:" << facesToRemove
00494             << abort(FatalError);
00495     }
00496 
00497 
00498     // Every face removed will result in neighbour and owner being merged
00499     // into owner.
00500     forAll(facesToRemove, facesToRemoveI)
00501     {
00502         label faceI = facesToRemove[facesToRemoveI];
00503 
00504         if (!mesh().isInternalFace(faceI))
00505         {
00506             FatalErrorIn
00507             (
00508                 "undoableMeshCutter::removeSplitFaces(const labelList&)"
00509             )   << "Trying to remove face that is not internal"
00510                 << abort(FatalError);
00511         }
00512 
00513         label own = mesh().faceOwner()[faceI];
00514 
00515         label nbr = mesh().faceNeighbour()[faceI];
00516 
00517         Map<splitCell*>::iterator ownFind = liveSplitCells_.find(own);
00518 
00519         Map<splitCell*>::iterator nbrFind = liveSplitCells_.find(nbr);
00520 
00521         if
00522         (
00523             (ownFind == liveSplitCells_.end())
00524          || (nbrFind == liveSplitCells_.end())
00525         )
00526         {
00527             // Can happen because of removeFaces adding extra faces to
00528             // original splitFaces
00529         }
00530         else
00531         {
00532             // Face is original splitFace.
00533 
00534             splitCell* ownPtr = ownFind();
00535 
00536             splitCell* nbrPtr = nbrFind();
00537 
00538             splitCell* parentPtr = ownPtr->parent();
00539 
00540             // Various checks on sanity.
00541 
00542             if (debug)
00543             {
00544                 Pout<< "Updating for removed splitFace " << faceI
00545                     << " own:" << own <<  " nbr:" << nbr
00546                     << " ownPtr:" << ownPtr->cellLabel()
00547                     << " nbrPtr:" << nbrPtr->cellLabel()
00548                     << endl;
00549             }
00550             if (!parentPtr)
00551             {
00552                 FatalErrorIn
00553                 (
00554                     "undoableMeshCutter::removeSplitFaces(const labelList&)"
00555                 )   << "No parent for owner " << ownPtr->cellLabel()
00556                     << abort(FatalError);
00557             }
00558 
00559             if (!nbrPtr->parent())
00560             {
00561                 FatalErrorIn
00562                 (
00563                     "undoableMeshCutter::removeSplitFaces(const labelList&)"
00564                 )   << "No parent for neighbour " << nbrPtr->cellLabel()
00565                     << abort(FatalError);
00566             }
00567 
00568             if (parentPtr != nbrPtr->parent())
00569             {
00570                 FatalErrorIn
00571                 (
00572                     "undoableMeshCutter::removeSplitFaces(const labelList&)"
00573                 )   << "Owner and neighbour liveSplitCell entries do not have"
00574                     << " same parent. faceI:" << faceI << "  owner:" << own
00575                     << "  ownparent:" << parentPtr->cellLabel()
00576                     << " neighbour:" << nbr
00577                     << "  nbrparent:" << nbrPtr->parent()->cellLabel()
00578                     << abort(FatalError);
00579             }
00580 
00581             if
00582             (
00583                 !ownPtr->isUnrefined()
00584              || !nbrPtr->isUnrefined()
00585              || parentPtr->isUnrefined()
00586             )
00587             {
00588                 // Live owner and neighbour are refined themselves.
00589                 FatalErrorIn
00590                 (
00591                     "undoableMeshCutter::removeSplitFaces(const labelList&)"
00592                 )   << "Owner and neighbour liveSplitCell entries are"
00593                     << " refined themselves or the parent is not refined"
00594                     << endl
00595                     << "owner unrefined:" << ownPtr->isUnrefined()
00596                     << "  neighbour unrefined:" << nbrPtr->isUnrefined()
00597                     << "  master unrefined:" << parentPtr->isUnrefined()
00598                     << abort(FatalError);
00599             }
00600 
00601             // Delete from liveSplitCell
00602             liveSplitCells_.erase(ownFind);
00603 
00605             liveSplitCells_.erase(liveSplitCells_.find(nbr));
00606 
00607             // Delete entries themselves
00608             delete ownPtr;
00609             delete nbrPtr;
00610 
00611             //
00612             // Update parent:
00613             //   - has parent itself: is part of split cell. Update cellLabel
00614             //     with merged cell one.
00615             //   - has no parent: is start of tree. Completely remove.
00616 
00617             if (parentPtr->parent())
00618             {
00619                 // Update parent cell label to be new merged cell label
00620                 // (will be owner)
00621                 parentPtr->cellLabel() = own;
00622 
00623                 // And insert into live cells (is ok since old entry with
00624                 // own as key has been removed above)
00625                 liveSplitCells_.insert(own, parentPtr);
00626             }
00627             else
00628             {
00629                 // No parent so is start of tree. No need to keep splitCell
00630                 // tree.
00631                 delete parentPtr;
00632             }
00633         }
00634     }
00635 
00636     // Insert all commands to combine cells. Never fails so don't have to
00637     // test for success.
00638     faceRemover().setRefinement
00639     (
00640         facesToRemove,
00641         cellRegion,
00642         cellRegionMaster,
00643         meshMod
00644     );
00645 
00646     return facesToRemove;
00647 }
00648 
00649 
00650 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd