00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00039
00040 namespace Foam
00041 {
00042
00043 defineTypeNameAndDebug(undoableMeshCutter, 0);
00044
00045 }
00046
00047
00048
00049
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
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
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
00098 if (splitPtr)
00099 {
00100
00101 printCellRefTree(os, word(""), splitPtr);
00102 }
00103 }
00104 }
00105
00106
00107
00108
00109 void Foam::undoableMeshCutter::updateLabels
00110 (
00111 const labelList& map,
00112 Map<splitCell*>& liveSplitCells
00113 )
00114 {
00115
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
00145
00146 if (changed)
00147 {
00148
00149
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
00170
00171 splitPtr->cellLabel() = newCellI;
00172
00173
00174 newLiveSplitCells.insert(newCellI, splitPtr);
00175 }
00176 }
00177 liveSplitCells = newLiveSplitCells;
00178 }
00179 }
00180
00181
00182
00183
00184
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
00203
00204 Foam::undoableMeshCutter::~undoableMeshCutter()
00205 {
00206
00207
00208 forAllIter(Map<splitCell*>, liveSplitCells_, iter)
00209 {
00210 splitCell* splitPtr = iter();
00211
00212 while (splitPtr)
00213 {
00214 splitCell* parentPtr = splitPtr->parent();
00215
00216
00217
00218 if (parentPtr)
00219 {
00220 splitCell* otherSidePtr = splitPtr->getOther();
00221
00222 otherSidePtr->parent() = NULL;
00223
00224 splitPtr->parent() = NULL;
00225 }
00226
00227
00228 delete splitPtr;
00229
00230 splitPtr = parentPtr;
00231 }
00232 }
00233 }
00234
00235
00236
00237
00238 void Foam::undoableMeshCutter::setRefinement
00239 (
00240 const cellCuts& cuts,
00241 polyTopoChange& meshMod
00242 )
00243 {
00244
00245 meshCutter::setRefinement(cuts, meshMod);
00246
00247 if (undoable_)
00248 {
00249
00250 forAllConstIter(Map<label>, addedCells(), iter)
00251 {
00252 label cellI = iter.key();
00253
00254 label addedCellI = iter();
00255
00256
00257
00258
00259
00260 Map<splitCell*>::iterator findCell =
00261 liveSplitCells_.find(cellI);
00262
00263 if (findCell == liveSplitCells_.end())
00264 {
00265
00266
00267
00268
00269
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
00277
00278 parentPtr->master() = masterPtr;
00279 parentPtr->slave() = slavePtr;
00280
00281
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
00296 splitCell* parentPtr = findCell();
00297
00298
00299 liveSplitCells_.erase(findCell);
00300
00301 splitCell* masterPtr = new splitCell(cellI, parentPtr);
00302
00303 splitCell* slavePtr = new splitCell(addedCellI, parentPtr);
00304
00305
00306
00307 parentPtr->master() = masterPtr;
00308 parentPtr->slave() = slavePtr;
00309
00310
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
00337 meshCutter::updateMesh(morphMap);
00338
00339
00340
00341
00342 faceRemover_.updateMesh(morphMap);
00343
00344 if (undoable_)
00345 {
00346
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
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
00388
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
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
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
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
00471
00472 labelList cellRegion;
00473 labelList cellRegionMaster;
00474 labelList facesToRemove;
00475
00476 faceRemover().compatibleRemoves
00477 (
00478 splitFaces,
00479 cellRegion,
00480 cellRegionMaster,
00481 facesToRemove
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
00499
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
00528
00529 }
00530 else
00531 {
00532
00533
00534 splitCell* ownPtr = ownFind();
00535
00536 splitCell* nbrPtr = nbrFind();
00537
00538 splitCell* parentPtr = ownPtr->parent();
00539
00540
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
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
00602 liveSplitCells_.erase(ownFind);
00603
00605 liveSplitCells_.erase(liveSplitCells_.find(nbr));
00606
00607
00608 delete ownPtr;
00609 delete nbrPtr;
00610
00611
00612
00613
00614
00615
00616
00617 if (parentPtr->parent())
00618 {
00619
00620
00621 parentPtr->cellLabel() = own;
00622
00623
00624
00625 liveSplitCells_.insert(own, parentPtr);
00626 }
00627 else
00628 {
00629
00630
00631 delete parentPtr;
00632 }
00633 }
00634 }
00635
00636
00637
00638 faceRemover().setRefinement
00639 (
00640 facesToRemove,
00641 cellRegion,
00642 cellRegionMaster,
00643 meshMod
00644 );
00645
00646 return facesToRemove;
00647 }
00648
00649
00650