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

polyTopoChange.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 "polyTopoChange.H"
00028 #include "SortableList.H"
00029 #include "polyMesh.H"
00030 #include "polyAddPoint.H"
00031 #include "polyModifyPoint.H"
00032 #include "polyRemovePoint.H"
00033 #include "polyAddFace.H"
00034 #include "polyModifyFace.H"
00035 #include "polyRemoveFace.H"
00036 #include "polyAddCell.H"
00037 #include "polyModifyCell.H"
00038 #include "polyRemoveCell.H"
00039 #include "objectMap.H"
00040 #include "processorPolyPatch.H"
00041 #include "fvMesh.H"
00042 
00043 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00044 
00045 namespace Foam
00046 {
00047     defineTypeNameAndDebug(polyTopoChange, 0);
00048 }
00049 
00050 
00051 const Foam::point Foam::polyTopoChange::greatPoint
00052 (
00053     GREAT,
00054     GREAT,
00055     GREAT
00056 );
00057 
00058 
00059 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00060 
00061 // Renumber with special handling for merged items (marked with <-1)
00062 void Foam::polyTopoChange::renumberReverseMap
00063 (
00064     const labelList& map,
00065     DynamicList<label>& elems
00066 )
00067 {
00068     forAll(elems, elemI)
00069     {
00070         label val = elems[elemI];
00071 
00072         if (val >= 0)
00073         {
00074             elems[elemI] = map[val];
00075         }
00076         else if (val < -1)
00077         {
00078             label mergedVal = -val-2;
00079             elems[elemI] = -map[mergedVal]-2;
00080         }
00081     }
00082 }
00083 
00084 
00085 void Foam::polyTopoChange::renumber
00086 (
00087     const labelList& map,
00088     labelHashSet& elems
00089 )
00090 {
00091     labelHashSet newElems(elems.size());
00092 
00093     forAllConstIter(labelHashSet, elems, iter)
00094     {
00095         label newElem = map[iter.key()];
00096 
00097         if (newElem >= 0)
00098         {
00099             newElems.insert(newElem);
00100         }
00101     }
00102 
00103     elems.transfer(newElems);
00104 }
00105 
00106 
00107 // Renumber and remove -1 elements.
00108 void Foam::polyTopoChange::renumberCompact
00109 (
00110     const labelList& map,
00111     labelList& elems
00112 )
00113 {
00114     label newElemI = 0;
00115 
00116     forAll(elems, elemI)
00117     {
00118         label newVal = map[elems[elemI]];
00119 
00120         if (newVal != -1)
00121         {
00122             elems[newElemI++] = newVal;
00123         }
00124     }
00125     elems.setSize(newElemI);
00126 }
00127 
00128 
00129 void Foam::polyTopoChange::countMap
00130 (
00131     const labelList& map,
00132     const labelList& reverseMap,
00133     label& nAdd,
00134     label& nInflate,
00135     label& nMerge,
00136     label& nRemove
00137 )
00138 {
00139     nAdd = 0;
00140     nInflate = 0;
00141     nMerge = 0;
00142     nRemove = 0;
00143 
00144     forAll(map, newCellI)
00145     {
00146         label oldCellI = map[newCellI];
00147 
00148         if (oldCellI >= 0)
00149         {
00150             if (reverseMap[oldCellI] == newCellI)
00151             {
00152                 // unchanged
00153             }
00154             else
00155             {
00156                 // Added (from another cell v.s. inflated from face/point)
00157                 nAdd++;
00158             }
00159         }
00160         else if (oldCellI == -1)
00161         {
00162             // Created from nothing
00163             nInflate++;
00164         }
00165         else
00166         {
00167             FatalErrorIn("countMap") << "old:" << oldCellI
00168                 << " new:" << newCellI << abort(FatalError);
00169         }
00170     }
00171 
00172     forAll(reverseMap, oldCellI)
00173     {
00174         label newCellI = reverseMap[oldCellI];
00175 
00176         if (newCellI >= 0)
00177         {
00178             // unchanged
00179         }
00180         else if (newCellI == -1)
00181         {
00182             // removed
00183             nRemove++;
00184         }
00185         else
00186         {
00187             // merged into -newCellI-2
00188             nMerge++;
00189         }
00190     }
00191 }
00192 
00193 
00194 Foam::labelHashSet Foam::polyTopoChange::getSetIndices
00195 (
00196     const PackedBoolList& lst
00197 )
00198 {
00199     labelHashSet values(lst.count());
00200     forAll(lst, i)
00201     {
00202         if (lst[i])
00203         {
00204             values.insert(i);
00205         }
00206     }
00207     return values;
00208 }
00209 
00210 
00211 void Foam::polyTopoChange::writeMeshStats(const polyMesh& mesh, Ostream& os)
00212 {
00213     const polyBoundaryMesh& patches = mesh.boundaryMesh();
00214 
00215     labelList patchSizes(patches.size());
00216     labelList patchStarts(patches.size());
00217     forAll(patches, patchI)
00218     {
00219         patchSizes[patchI] = patches[patchI].size();
00220         patchStarts[patchI] = patches[patchI].start();
00221     }
00222 
00223     os  << "    Points      : " << mesh.nPoints() << nl
00224         << "    Faces       : " << mesh.nFaces() << nl
00225         << "    Cells       : " << mesh.nCells() << nl
00226         << "    PatchSizes  : " << patchSizes << nl
00227         << "    PatchStarts : " << patchStarts << nl
00228         << endl;
00229 }
00230 
00231 
00232 void Foam::polyTopoChange::getMergeSets
00233 (
00234     const labelList& reverseCellMap,
00235     const labelList& cellMap,
00236     List<objectMap>& cellsFromCells
00237 )
00238 {
00239     // Per new cell the number of old cells that have been merged into it
00240     labelList nMerged(cellMap.size(), 1);
00241 
00242     forAll(reverseCellMap, oldCellI)
00243     {
00244         label newCellI = reverseCellMap[oldCellI];
00245 
00246         if (newCellI < -1)
00247         {
00248             label mergeCellI = -newCellI-2;
00249 
00250             nMerged[mergeCellI]++;
00251         }
00252     }
00253 
00254     // From merged cell to set index
00255     labelList cellToMergeSet(cellMap.size(), -1);
00256 
00257     label nSets = 0;
00258 
00259     forAll(nMerged, cellI)
00260     {
00261         if (nMerged[cellI] > 1)
00262         {
00263             cellToMergeSet[cellI] = nSets++;
00264         }
00265     }
00266 
00267     // Collect cell labels.
00268     // Each objectMap will have
00269     // - index : new mesh cell label
00270     // - masterObjects : list of old cells that have been merged. Element 0
00271     //                   will be the original destination cell label.
00272 
00273     cellsFromCells.setSize(nSets);
00274 
00275     forAll(reverseCellMap, oldCellI)
00276     {
00277         label newCellI = reverseCellMap[oldCellI];
00278 
00279         if (newCellI < -1)
00280         {
00281             label mergeCellI = -newCellI-2;
00282 
00283             // oldCellI was merged into mergeCellI
00284 
00285             label setI = cellToMergeSet[mergeCellI];
00286 
00287             objectMap& mergeSet = cellsFromCells[setI];
00288 
00289             if (mergeSet.masterObjects().empty())
00290             {
00291                 // First occurrence of master cell mergeCellI
00292 
00293                 mergeSet.index() = mergeCellI;
00294                 mergeSet.masterObjects().setSize(nMerged[mergeCellI]);
00295 
00296                 // old master label
00297                 mergeSet.masterObjects()[0] = cellMap[mergeCellI];
00298 
00299                 // old slave label
00300                 mergeSet.masterObjects()[1] = oldCellI;
00301 
00302                 nMerged[mergeCellI] = 2;
00303             }
00304             else
00305             {
00306                 mergeSet.masterObjects()[nMerged[mergeCellI]++] = oldCellI;
00307             }
00308         }
00309     }
00310 }
00311 
00312 
00313 void Foam::polyTopoChange::checkFace
00314 (
00315     const face& f,
00316     const label faceI,
00317     const label own,
00318     const label nei,
00319     const label patchI,
00320     const label zoneI
00321 ) const
00322 {
00323     if (nei == -1)
00324     {
00325         if (own == -1 && zoneI != -1)
00326         {
00327             // retired face
00328         }
00329         else if (patchI == -1 || patchI >= nPatches_)
00330         {
00331             FatalErrorIn
00332             (
00333                 "polyTopoChange::checkFace(const face&, const label"
00334                 ", const label, const label, const label)"
00335             )   << "Face has no neighbour (so external) but does not have"
00336                 << " a valid patch" << nl
00337                 << "f:" << f
00338                 << " faceI(-1 if added face):" << faceI
00339                 << " own:" << own << " nei:" << nei
00340                 << " patchI:" << patchI << abort(FatalError);
00341         }
00342     }
00343     else
00344     {
00345         if (patchI != -1)
00346         {
00347             FatalErrorIn
00348             (
00349                 "polyTopoChange::checkFace(const face&, const label"
00350                 ", const label, const label, const label)"
00351             )   << "Cannot both have valid patchI and neighbour" << nl
00352                 << "f:" << f
00353                 << " faceI(-1 if added face):" << faceI
00354                 << " own:" << own << " nei:" << nei
00355                 << " patchI:" << patchI << abort(FatalError);
00356         }
00357 
00358         if (nei <= own)
00359         {
00360             FatalErrorIn
00361             (
00362                 "polyTopoChange::checkFace(const face&, const label"
00363                 ", const label, const label, const label)"
00364             )   << "Owner cell label should be less than neighbour cell label"
00365                 << nl
00366                 << "f:" << f
00367                 << " faceI(-1 if added face):" << faceI
00368                 << " own:" << own << " nei:" << nei
00369                 << " patchI:" << patchI << abort(FatalError);
00370         }
00371     }
00372 
00373     if (f.size() < 3 || findIndex(f, -1) != -1)
00374     {
00375         FatalErrorIn
00376         (
00377             "polyTopoChange::checkFace(const face&, const label"
00378             ", const label, const label, const label)"
00379         )   << "Illegal vertices in face"
00380             << nl
00381             << "f:" << f
00382             << " faceI(-1 if added face):" << faceI
00383             << " own:" << own << " nei:" << nei
00384             << " patchI:" << patchI << abort(FatalError);
00385     }
00386     if (faceI >= 0 && faceI < faces_.size() && faceRemoved(faceI))
00387     {
00388         FatalErrorIn
00389         (
00390             "polyTopoChange::checkFace(const face&, const label"
00391             ", const label, const label, const label)"
00392         )   << "Face already marked for removal"
00393             << nl
00394             << "f:" << f
00395             << " faceI(-1 if added face):" << faceI
00396             << " own:" << own << " nei:" << nei
00397             << " patchI:" << patchI << abort(FatalError);
00398     }
00399     forAll(f, fp)
00400     {
00401         if (f[fp] < points_.size() && pointRemoved(f[fp]))
00402         {
00403             FatalErrorIn
00404             (
00405                 "polyTopoChange::checkFace(const face&, const label"
00406                 ", const label, const label, const label)"
00407             )   << "Face uses removed vertices"
00408                 << nl
00409                 << "f:" << f
00410                 << " faceI(-1 if added face):" << faceI
00411                 << " own:" << own << " nei:" << nei
00412                 << " patchI:" << patchI << abort(FatalError);
00413         }
00414     }
00415 }
00416 
00417 
00418 void Foam::polyTopoChange::makeCells
00419 (
00420     const label nActiveFaces,
00421     labelList& cellFaces,
00422     labelList& cellFaceOffsets
00423 ) const
00424 {
00425     cellFaces.setSize(2*nActiveFaces);
00426     cellFaceOffsets.setSize(cellMap_.size() + 1);
00427 
00428     // Faces per cell
00429     labelList nNbrs(cellMap_.size(), 0);
00430 
00431     // 1. Count faces per cell
00432 
00433     for (label faceI = 0; faceI < nActiveFaces; faceI++)
00434     {
00435         nNbrs[faceOwner_[faceI]]++;
00436     }
00437     for (label faceI = 0; faceI < nActiveFaces; faceI++)
00438     {
00439         if (faceNeighbour_[faceI] >= 0)
00440         {
00441             nNbrs[faceNeighbour_[faceI]]++;
00442         }
00443     }
00444 
00445     // 2. Calculate offsets
00446 
00447     cellFaceOffsets[0] = 0;
00448     forAll(nNbrs, cellI)
00449     {
00450         cellFaceOffsets[cellI+1] = cellFaceOffsets[cellI] + nNbrs[cellI];
00451     }
00452 
00453     // 3. Fill faces per cell
00454 
00455     // reset the whole list to use as counter
00456     nNbrs = 0;
00457 
00458     for (label faceI = 0; faceI < nActiveFaces; faceI++)
00459     {
00460         label cellI = faceOwner_[faceI];
00461 
00462         cellFaces[cellFaceOffsets[cellI] + nNbrs[cellI]++] = faceI;
00463     }
00464 
00465     for (label faceI = 0; faceI < nActiveFaces; faceI++)
00466     {
00467         label cellI = faceNeighbour_[faceI];
00468 
00469         if (cellI >= 0)
00470         {
00471             cellFaces[cellFaceOffsets[cellI] + nNbrs[cellI]++] = faceI;
00472         }
00473     }
00474 
00475     // Last offset points to beyond end of cellFaces.
00476     cellFaces.setSize(cellFaceOffsets[cellMap_.size()]);
00477 }
00478 
00479 
00480 // Create cell-cell addressing. Called after compaction (but before ordering)
00481 // of faces
00482 void Foam::polyTopoChange::makeCellCells
00483 (
00484     const label nActiveFaces,
00485     CompactListList<label>& cellCells
00486 ) const
00487 {
00488     // Neighbours per cell
00489     labelList nNbrs(cellMap_.size(), 0);
00490 
00491     // Overall number of cellCells
00492     label nCellCells = 0;
00493 
00494     // 1. Count neighbours (through internal faces) per cell
00495 
00496     for (label faceI = 0; faceI < nActiveFaces; faceI++)
00497     {
00498         if (faceNeighbour_[faceI] >= 0)
00499         {
00500             nNbrs[faceOwner_[faceI]]++;
00501             nNbrs[faceNeighbour_[faceI]]++;
00502             nCellCells += 2;
00503         }
00504     }
00505 
00506     cellCells.setSize(cellMap_.size(), nCellCells);
00507 
00508     // 2. Calculate offsets
00509 
00510     labelList& offsets = cellCells.offsets();
00511 
00512     label sumSize = 0;
00513     forAll(nNbrs, cellI)
00514     {
00515         sumSize += nNbrs[cellI];
00516         offsets[cellI] = sumSize;
00517     }
00518 
00519     // 3. Fill faces per cell
00520 
00521     // reset the whole list to use as counter
00522     nNbrs = 0;
00523 
00524     for (label faceI = 0; faceI < nActiveFaces; faceI++)
00525     {
00526         label nei = faceNeighbour_[faceI];
00527 
00528         if (nei >= 0)
00529         {
00530             label own = faceOwner_[faceI];
00531             cellCells.m()[cellCells.index(own, nNbrs[own]++)] = nei;
00532             cellCells.m()[cellCells.index(nei, nNbrs[nei]++)] = own;
00533         }
00534     }
00535 }
00536 
00537 
00538 // Cell ordering (based on bandCompression).
00539 // Handles removed cells. Returns number of remaining cells.
00540 Foam::label Foam::polyTopoChange::getCellOrder
00541 (
00542     const CompactListList<label>& cellCellAddressing,
00543     labelList& oldToNew
00544 ) const
00545 {
00546     const labelList& offsets = cellCellAddressing.offsets();
00547 
00548     labelList newOrder(cellCellAddressing.size());
00549 
00550     // Fifo buffer for string of cells
00551     SLList<label> nextCell;
00552 
00553     // Whether cell has been done already
00554     PackedBoolList visited(cellCellAddressing.size());
00555 
00556     label cellInOrder = 0;
00557 
00558 
00559     // loop over the cells
00560     forAll (visited, cellI)
00561     {
00562         // find the first non-removed cell that has not been visited yet
00563         if (!cellRemoved(cellI) && visited.get(cellI) == 0)
00564         {
00565             // use this cell as a start
00566             nextCell.append(cellI);
00567 
00568             // loop through the nextCell list. Add the first cell into the
00569             // cell order if it has not already been visited and ask for its
00570             // neighbours. If the neighbour in question has not been visited,
00571             // add it to the end of the nextCell list
00572 
00573             do
00574             {
00575                 label currentCell = nextCell.removeHead();
00576 
00577                 if (visited.get(currentCell) == 0)
00578                 {
00579                     visited.set(currentCell, 1);
00580 
00581                     // add into cellOrder
00582                     newOrder[cellInOrder] = currentCell;
00583                     cellInOrder++;
00584 
00585                     // find if the neighbours have been visited
00586                     label i0 = (currentCell == 0 ? 0 : offsets[currentCell-1]);
00587                     label i1 = offsets[currentCell];
00588 
00589                     for (label i = i0; i < i1; i++)
00590                     {
00591                         label nbr = cellCellAddressing.m()[i];
00592 
00593                         if (!cellRemoved(nbr) && visited.get(nbr) == 0)
00594                         {
00595                             // not visited, add to the list
00596                             nextCell.append(nbr);
00597                         }
00598                     }
00599                 }
00600             }
00601             while (nextCell.size());
00602         }
00603     }
00604 
00605 
00606     // Now we have new-to-old in newOrder.
00607     newOrder.setSize(cellInOrder);
00608 
00609     // Invert to get old-to-new. Make sure removed (i.e. unmapped) cells are -1.
00610     oldToNew = invert(cellCellAddressing.size(), newOrder);
00611 
00612     return cellInOrder;
00613 }
00614 
00615 
00616 // Determine order for faces:
00617 // - upper-triangular order for internal faces
00618 // - external faces after internal faces and in patch order.
00619 void Foam::polyTopoChange::getFaceOrder
00620 (
00621     const label nActiveFaces,
00622     const labelList& cellFaces,
00623     const labelList& cellFaceOffsets,
00624 
00625     labelList& oldToNew,
00626     labelList& patchSizes,
00627     labelList& patchStarts
00628 ) const
00629 {
00630     oldToNew.setSize(faceOwner_.size());
00631     oldToNew = -1;
00632 
00633     // First unassigned face
00634     label newFaceI = 0;
00635 
00636     forAll(cellMap_, cellI)
00637     {
00638         label startOfCell = cellFaceOffsets[cellI];
00639         label nFaces = cellFaceOffsets[cellI+1] - startOfCell;
00640 
00641         // Neighbouring cells
00642         SortableList<label> nbr(nFaces);
00643 
00644         for (label i = 0; i < nFaces; i++)
00645         {
00646             label faceI = cellFaces[startOfCell + i];
00647 
00648             label nbrCellI = faceNeighbour_[faceI];
00649 
00650             if (faceI >= nActiveFaces)
00651             {
00652                 // Retired face.
00653                 nbr[i] = -1;
00654             }
00655             else if (nbrCellI != -1)
00656             {
00657                 // Internal face. Get cell on other side.
00658                 if (nbrCellI == cellI)
00659                 {
00660                     nbrCellI = faceOwner_[faceI];
00661                 }
00662 
00663                 if (cellI < nbrCellI)
00664                 {
00665                     // CellI is master
00666                     nbr[i] = nbrCellI;
00667                 }
00668                 else
00669                 {
00670                     // nbrCell is master. Let it handle this face.
00671                     nbr[i] = -1;
00672                 }
00673             }
00674             else
00675             {
00676                 // External face. Do later.
00677                 nbr[i] = -1;
00678             }
00679         }
00680 
00681         nbr.sort();
00682 
00683         forAll(nbr, i)
00684         {
00685             if (nbr[i] != -1)
00686             {
00687                 oldToNew[cellFaces[startOfCell + nbr.indices()[i]]] =
00688                     newFaceI++;
00689             }
00690         }
00691     }
00692 
00693 
00694     // Pick up all patch faces in patch face order.
00695     patchStarts.setSize(nPatches_);
00696     patchStarts = 0;
00697     patchSizes.setSize(nPatches_);
00698     patchSizes = 0;
00699 
00700     patchStarts[0] = newFaceI;
00701 
00702     for (label faceI = 0; faceI < nActiveFaces; faceI++)
00703     {
00704         if (region_[faceI] >= 0)
00705         {
00706             patchSizes[region_[faceI]]++;
00707         }
00708     }
00709 
00710     label faceI = patchStarts[0];
00711 
00712     forAll(patchStarts, patchI)
00713     {
00714         patchStarts[patchI] = faceI;
00715         faceI += patchSizes[patchI];
00716     }
00717 
00718     //if (debug)
00719     //{
00720     //    Pout<< "patchSizes:" << patchSizes << nl
00721     //        << "patchStarts:" << patchStarts << endl;
00722     //}
00723 
00724     labelList workPatchStarts(patchStarts);
00725 
00726     for (label faceI = 0; faceI < nActiveFaces; faceI++)
00727     {
00728         if (region_[faceI] >= 0)
00729         {
00730             oldToNew[faceI] = workPatchStarts[region_[faceI]]++;
00731         }
00732     }
00733 
00734     // Retired faces.
00735     for (label faceI = nActiveFaces; faceI < oldToNew.size(); faceI++)
00736     {
00737         oldToNew[faceI] = faceI;
00738     }
00739 
00740     // Check done all faces.
00741     forAll(oldToNew, faceI)
00742     {
00743         if (oldToNew[faceI] == -1)
00744         {
00745             FatalErrorIn
00746             (
00747                 "polyTopoChange::getFaceOrder"
00748                 "(const label, const labelList&, const labelList&)"
00749                 " const"
00750             )   << "Did not determine new position"
00751                 << " for face " << faceI
00752                 << abort(FatalError);
00753         }
00754     }
00755 }
00756 
00757 
00758 // Reorder and compact faces according to map.
00759 void Foam::polyTopoChange::reorderCompactFaces
00760 (
00761     const label newSize,
00762     const labelList& oldToNew
00763 )
00764 {
00765     reorder(oldToNew, faces_);
00766     faces_.setCapacity(newSize);
00767 
00768     reorder(oldToNew, region_);
00769     region_.setCapacity(newSize);
00770 
00771     reorder(oldToNew, faceOwner_);
00772     faceOwner_.setCapacity(newSize);
00773 
00774     reorder(oldToNew, faceNeighbour_);
00775     faceNeighbour_.setCapacity(newSize);
00776 
00777     // Update faceMaps.
00778     reorder(oldToNew, faceMap_);
00779     faceMap_.setCapacity(newSize);
00780 
00781     renumberReverseMap(oldToNew, reverseFaceMap_);
00782 
00783     renumberKey(oldToNew, faceFromPoint_);
00784     renumberKey(oldToNew, faceFromEdge_);
00785     inplaceReorder(oldToNew, flipFaceFlux_);
00786     flipFaceFlux_.setCapacity(newSize);
00787     renumberKey(oldToNew, faceZone_);
00788     inplaceReorder(oldToNew, faceZoneFlip_);
00789     faceZoneFlip_.setCapacity(newSize);
00790 }
00791 
00792 
00793 // Compact all and orders points and faces:
00794 // - points into internal followed by external points
00795 // - internalfaces upper-triangular
00796 // - externalfaces after internal ones.
00797 void Foam::polyTopoChange::compact
00798 (
00799     const bool orderCells,
00800     const bool orderPoints,
00801     label& nInternalPoints,
00802     labelList& patchSizes,
00803     labelList& patchStarts
00804 )
00805 {
00806     points_.shrink();
00807     pointMap_.shrink();
00808     reversePointMap_.shrink();
00809 
00810     faces_.shrink();
00811     region_.shrink();
00812     faceOwner_.shrink();
00813     faceNeighbour_.shrink();
00814     faceMap_.shrink();
00815     reverseFaceMap_.shrink();
00816 
00817     cellMap_.shrink();
00818     reverseCellMap_.shrink();
00819     cellZone_.shrink();
00820 
00821 
00822     // Compact points
00823     label nActivePoints = 0;
00824     {
00825         labelList localPointMap(points_.size(), -1);
00826         label newPointI = 0;
00827 
00828         if (!orderPoints)
00829         {
00830             nInternalPoints = -1;
00831 
00832             forAll(points_, pointI)
00833             {
00834                 if (!pointRemoved(pointI) && !retiredPoints_.found(pointI))
00835                 {
00836                     localPointMap[pointI] = newPointI++;
00837                 }
00838             }
00839             nActivePoints = newPointI;
00840         }
00841         else
00842         {
00843             forAll(points_, pointI)
00844             {
00845                 if (!pointRemoved(pointI) && !retiredPoints_.found(pointI))
00846                 {
00847                     nActivePoints++;
00848                 }
00849             }
00850 
00851             // Mark boundary points
00852             forAll(faceOwner_, faceI)
00853             {
00854                 if
00855                 (
00856                    !faceRemoved(faceI)
00857                  && faceOwner_[faceI] >= 0
00858                  && faceNeighbour_[faceI] < 0
00859                 )
00860                 {
00861                     // Valid boundary face
00862                     const face& f = faces_[faceI];
00863 
00864                     forAll(f, fp)
00865                     {
00866                         label pointI = f[fp];
00867 
00868                         if (localPointMap[pointI] == -1)
00869                         {
00870                             if
00871                             (
00872                                 pointRemoved(pointI)
00873                              || retiredPoints_.found(pointI)
00874                             )
00875                             {
00876                                 FatalErrorIn("polyTopoChange::compact(..)")
00877                                     << "Removed or retired point " << pointI
00878                                     << " in face " << f
00879                                     << " at position " << faceI << endl
00880                                     << "Probably face has not been adapted for"
00881                                     << " removed points." << abort(FatalError);
00882                             }
00883                             localPointMap[pointI] = newPointI++;
00884                         }
00885                     }
00886                 }
00887             }
00888 
00889             label nBoundaryPoints = newPointI;
00890             nInternalPoints = nActivePoints - nBoundaryPoints;
00891 
00892             // Move the boundary addressing up
00893             forAll(localPointMap, pointI)
00894             {
00895                 if (localPointMap[pointI] != -1)
00896                 {
00897                     localPointMap[pointI] += nInternalPoints;
00898                 }
00899             }
00900 
00901             newPointI = 0;
00902 
00903             // Mark internal points
00904             forAll(faceOwner_, faceI)
00905             {
00906                 if
00907                 (
00908                    !faceRemoved(faceI)
00909                  && faceOwner_[faceI] >= 0
00910                  && faceNeighbour_[faceI] >= 0
00911                 )
00912                 {
00913                     // Valid internal face
00914                     const face& f = faces_[faceI];
00915 
00916                     forAll(f, fp)
00917                     {
00918                         label pointI = f[fp];
00919 
00920                         if (localPointMap[pointI] == -1)
00921                         {
00922                             if
00923                             (
00924                                 pointRemoved(pointI)
00925                              || retiredPoints_.found(pointI)
00926                             )
00927                             {
00928                                 FatalErrorIn("polyTopoChange::compact(..)")
00929                                     << "Removed or retired point " << pointI
00930                                     << " in face " << f
00931                                     << " at position " << faceI << endl
00932                                     << "Probably face has not been adapted for"
00933                                     << " removed points." << abort(FatalError);
00934                             }
00935                             localPointMap[pointI] = newPointI++;
00936                         }
00937                     }
00938                 }
00939             }
00940 
00941             if (newPointI != nInternalPoints)
00942             {
00943                 FatalErrorIn("polyTopoChange::compact(..)")
00944                     << "Problem." << abort(FatalError);
00945             }
00946             newPointI = nActivePoints;
00947         }
00948 
00949         forAllConstIter(labelHashSet, retiredPoints_, iter)
00950         {
00951             localPointMap[iter.key()] = newPointI++;
00952         }
00953 
00954 
00955         if (debug)
00956         {
00957             Pout<< "Points : active:" << nActivePoints
00958                 << "  removed:" << points_.size()-newPointI << endl;
00959         }
00960 
00961         reorder(localPointMap, points_);
00962         points_.setCapacity(newPointI);
00963 
00964         // Update pointMaps
00965         reorder(localPointMap, pointMap_);
00966         pointMap_.setCapacity(newPointI);
00967         renumberReverseMap(localPointMap, reversePointMap_);
00968 
00969         renumberKey(localPointMap, pointZone_);
00970         renumber(localPointMap, retiredPoints_);
00971 
00972         // Use map to relabel face vertices
00973         forAll(faces_, faceI)
00974         {
00975             face& f = faces_[faceI];
00976 
00977             //labelList oldF(f);
00978             renumberCompact(localPointMap, f);
00979 
00980             if (!faceRemoved(faceI) && f.size() < 3)
00981             {
00982                 FatalErrorIn("polyTopoChange::compact(..)")
00983                     << "Created illegal face " << f
00984                     //<< " from face " << oldF
00985                     << " at position:" << faceI
00986                     << " when filtering removed points"
00987                     << abort(FatalError);
00988             }
00989         }
00990     }
00991 
00992 
00993     // Compact faces.
00994     {
00995         labelList localFaceMap(faces_.size(), -1);
00996         label newFaceI = 0;
00997 
00998         forAll(faces_, faceI)
00999         {
01000             if (!faceRemoved(faceI) && faceOwner_[faceI] >= 0)
01001             {
01002                 localFaceMap[faceI] = newFaceI++;
01003             }
01004         }
01005         nActiveFaces_ = newFaceI;
01006 
01007         forAll(faces_, faceI)
01008         {
01009             if (!faceRemoved(faceI) && faceOwner_[faceI] < 0)
01010             {
01011                 // Retired face
01012                 localFaceMap[faceI] = newFaceI++;
01013             }
01014         }
01015 
01016         if (debug)
01017         {
01018             Pout<< "Faces : active:" << nActiveFaces_
01019                 << "  removed:" << faces_.size()-newFaceI << endl;
01020         }
01021 
01022         // Reorder faces.
01023         reorderCompactFaces(newFaceI, localFaceMap);
01024     }
01025 
01026     // Compact cells.
01027     {
01028         labelList localCellMap;
01029         label newCellI;
01030 
01031         if (orderCells)
01032         {
01033             // Construct cellCell addressing
01034             CompactListList<label> cellCells;
01035             makeCellCells(nActiveFaces_, cellCells);
01036 
01037             // Cell ordering (based on bandCompression). Handles removed cells.
01038             newCellI = getCellOrder(cellCells, localCellMap);
01039         }
01040         else
01041         {
01042             // Compact out removed cells
01043             localCellMap.setSize(cellMap_.size());
01044             localCellMap = -1;
01045 
01046             newCellI = 0;
01047             forAll(cellMap_, cellI)
01048             {
01049                 if (!cellRemoved(cellI))
01050                 {
01051                     localCellMap[cellI] = newCellI++;
01052                 }
01053             }
01054         }
01055 
01056         if (debug)
01057         {
01058             Pout<< "Cells : active:" << newCellI
01059                 << "  removed:" << cellMap_.size()-newCellI << endl;
01060         }
01061 
01062         // Renumber -if cells reordered or -if cells removed
01063         if (orderCells || (newCellI != cellMap_.size()))
01064         {
01065             reorder(localCellMap, cellMap_);
01066             cellMap_.setCapacity(newCellI);
01067             renumberReverseMap(localCellMap, reverseCellMap_);
01068 
01069             reorder(localCellMap, cellZone_);
01070             cellZone_.setCapacity(newCellI);
01071 
01072             renumberKey(localCellMap, cellFromPoint_);
01073             renumberKey(localCellMap, cellFromEdge_);
01074             renumberKey(localCellMap, cellFromFace_);
01075 
01076             // Renumber owner/neighbour. Take into account if neighbour suddenly
01077             // gets lower cell than owner.
01078             forAll(faceOwner_, faceI)
01079             {
01080                 label own = faceOwner_[faceI];
01081                 label nei = faceNeighbour_[faceI];
01082 
01083                 if (own >= 0)
01084                 {
01085                     // Update owner
01086                     faceOwner_[faceI] = localCellMap[own];
01087 
01088                     if (nei >= 0)
01089                     {
01090                         // Update neighbour.
01091                         faceNeighbour_[faceI] = localCellMap[nei];
01092 
01093                         // Check if face needs reversing.
01094                         if
01095                         (
01096                             faceNeighbour_[faceI] >= 0
01097                          && faceNeighbour_[faceI] < faceOwner_[faceI]
01098                         )
01099                         {
01100                             faces_[faceI] = faces_[faceI].reverseFace();
01101                             Swap(faceOwner_[faceI], faceNeighbour_[faceI]);
01102                             flipFaceFlux_[faceI] =
01103                             (
01104                                 flipFaceFlux_[faceI]
01105                               ? 0
01106                               : 1
01107                             );
01108                             faceZoneFlip_[faceI] =
01109                             (
01110                                 faceZoneFlip_[faceI]
01111                               ? 0
01112                               : 1
01113                             );
01114                         }
01115                     }
01116                 }
01117                 else if (nei >= 0)
01118                 {
01119                     // Update neighbour.
01120                     faceNeighbour_[faceI] = localCellMap[nei];
01121                 }
01122             }
01123         }
01124     }
01125 
01126     // Reorder faces into upper-triangular and patch ordering
01127     {
01128         // Create cells (packed storage)
01129         labelList cellFaces;
01130         labelList cellFaceOffsets;
01131         makeCells(nActiveFaces_, cellFaces, cellFaceOffsets);
01132 
01133         // Do upper triangular order and patch sorting
01134         labelList localFaceMap;
01135         getFaceOrder
01136         (
01137             nActiveFaces_,
01138             cellFaces,
01139             cellFaceOffsets,
01140 
01141             localFaceMap,
01142             patchSizes,
01143             patchStarts
01144         );
01145 
01146         // Reorder faces.
01147         reorderCompactFaces(localFaceMap.size(), localFaceMap);
01148     }
01149 }
01150 
01151 
01152 // Find faces to interpolate to create value for new face. Only used if
01153 // face was inflated from edge or point. Internal faces should only be
01154 // created from internal faces, external faces only from external faces
01155 // (and ideally the same patch)
01156 // Is bit problematic if there are no faces to select, i.e. in polyDualMesh
01157 // an internal face can be created from a boundary edge with no internal
01158 // faces connected to it.
01159 Foam::labelList Foam::polyTopoChange::selectFaces
01160 (
01161     const primitiveMesh& mesh,
01162     const labelList& faceLabels,
01163     const bool internalFacesOnly
01164 )
01165 {
01166     label nFaces = 0;
01167 
01168     forAll(faceLabels, i)
01169     {
01170         label faceI = faceLabels[i];
01171 
01172         if (internalFacesOnly == mesh.isInternalFace(faceI))
01173         {
01174             nFaces++;
01175         }
01176     }
01177 
01178     labelList collectedFaces;
01179 
01180     if (nFaces == 0)
01181     {
01182         // Did not find any faces of the correct type so just use any old
01183         // face.
01184         collectedFaces = faceLabels;
01185     }
01186     else
01187     {
01188         collectedFaces.setSize(nFaces);
01189 
01190         nFaces = 0;
01191 
01192         forAll(faceLabels, i)
01193         {
01194             label faceI = faceLabels[i];
01195 
01196             if (internalFacesOnly == mesh.isInternalFace(faceI))
01197             {
01198                 collectedFaces[nFaces++] = faceI;
01199             }
01200         }
01201     }
01202 
01203     return collectedFaces;
01204 }
01205 
01206 
01207 // Calculate pointMap per patch (so from patch point label to old patch point
01208 // label)
01209 void Foam::polyTopoChange::calcPatchPointMap
01210 (
01211     const List<Map<label> >& oldPatchMeshPointMaps,
01212     const polyBoundaryMesh& boundary,
01213     labelListList& patchPointMap
01214 ) const
01215 {
01216     patchPointMap.setSize(boundary.size());
01217 
01218     forAll(boundary, patchI)
01219     {
01220         const labelList& meshPoints = boundary[patchI].meshPoints();
01221 
01222         const Map<label>& oldMeshPointMap = oldPatchMeshPointMaps[patchI];
01223 
01224         labelList& curPatchPointRnb = patchPointMap[patchI];
01225 
01226         curPatchPointRnb.setSize(meshPoints.size());
01227 
01228         forAll(meshPoints, i)
01229         {
01230             if (meshPoints[i] < pointMap_.size())
01231             {
01232                 // Check if old point was part of same patch
01233                 Map<label>::const_iterator ozmpmIter = oldMeshPointMap.find
01234                 (
01235                     pointMap_[meshPoints[i]]
01236                 );
01237 
01238                 if (ozmpmIter != oldMeshPointMap.end())
01239                 {
01240                     curPatchPointRnb[i] = ozmpmIter();
01241                 }
01242                 else
01243                 {
01244                     curPatchPointRnb[i] = -1;
01245                 }
01246             }
01247             else
01248             {
01249                 curPatchPointRnb[i] = -1;
01250             }
01251         }
01252     }
01253 }
01254 
01255 
01256 void Foam::polyTopoChange::calcFaceInflationMaps
01257 (
01258     const polyMesh& mesh,
01259     List<objectMap>& facesFromPoints,
01260     List<objectMap>& facesFromEdges,
01261     List<objectMap>& facesFromFaces
01262 ) const
01263 {
01264     // Faces inflated from points
01265     // ~~~~~~~~~~~~~~~~~~~~~~~~~~
01266 
01267     facesFromPoints.setSize(faceFromPoint_.size());
01268 
01269     if (faceFromPoint_.size())
01270     {
01271         label nFacesFromPoints = 0;
01272 
01273         // Collect all still existing faces connected to this point.
01274         forAllConstIter(Map<label>, faceFromPoint_, iter)
01275         {
01276             label newFaceI = iter.key();
01277 
01278             if (region_[newFaceI] == -1)
01279             {
01280                 // Get internal faces using point on old mesh
01281                 facesFromPoints[nFacesFromPoints++] = objectMap
01282                 (
01283                     newFaceI,
01284                     selectFaces
01285                     (
01286                         mesh,
01287                         mesh.pointFaces()[iter()],
01288                         true
01289                     )
01290                 );
01291             }
01292             else
01293             {
01294                 // Get patch faces using point on old mesh
01295                 facesFromPoints[nFacesFromPoints++] = objectMap
01296                 (
01297                     newFaceI,
01298                     selectFaces
01299                     (
01300                         mesh,
01301                         mesh.pointFaces()[iter()],
01302                         false
01303                     )
01304                 );
01305             }
01306         }
01307     }
01308 
01309 
01310     // Faces inflated from edges
01311     // ~~~~~~~~~~~~~~~~~~~~~~~~~
01312 
01313     facesFromEdges.setSize(faceFromEdge_.size());
01314 
01315     if (faceFromEdge_.size())
01316     {
01317         label nFacesFromEdges = 0;
01318 
01319         // Collect all still existing faces connected to this edge.
01320         forAllConstIter(Map<label>, faceFromEdge_, iter)
01321         {
01322             label newFaceI = iter.key();
01323 
01324             if (region_[newFaceI] == -1)
01325             {
01326                 // Get internal faces using edge on old mesh
01327                 facesFromEdges[nFacesFromEdges++] = objectMap
01328                 (
01329                     newFaceI,
01330                     selectFaces
01331                     (
01332                         mesh,
01333                         mesh.edgeFaces(iter()),
01334                         true
01335                     )
01336                 );
01337             }
01338             else
01339             {
01340                 // Get patch faces using edge on old mesh
01341                 facesFromEdges[nFacesFromEdges++] = objectMap
01342                 (
01343                     newFaceI,
01344                     selectFaces
01345                     (
01346                         mesh,
01347                         mesh.edgeFaces(iter()),
01348                         false
01349                     )
01350                 );
01351             }
01352         }
01353     }
01354 
01355 
01356     // Faces from face merging
01357     // ~~~~~~~~~~~~~~~~~~~~~~~
01358 
01359     getMergeSets
01360     (
01361         reverseFaceMap_,
01362         faceMap_,
01363         facesFromFaces
01364     );
01365 }
01366 
01367 
01368 void Foam::polyTopoChange::calcCellInflationMaps
01369 (
01370     const polyMesh& mesh,
01371     List<objectMap>& cellsFromPoints,
01372     List<objectMap>& cellsFromEdges,
01373     List<objectMap>& cellsFromFaces,
01374     List<objectMap>& cellsFromCells
01375 ) const
01376 {
01377     cellsFromPoints.setSize(cellFromPoint_.size());
01378 
01379     if (cellFromPoint_.size())
01380     {
01381         label nCellsFromPoints = 0;
01382 
01383         // Collect all still existing faces connected to this point.
01384         forAllConstIter(Map<label>, cellFromPoint_, iter)
01385         {
01386             cellsFromPoints[nCellsFromPoints++] = objectMap
01387             (
01388                 iter.key(),
01389                 mesh.pointCells()[iter()]
01390             );
01391         }
01392     }
01393 
01394 
01395     cellsFromEdges.setSize(cellFromEdge_.size());
01396 
01397     if (cellFromEdge_.size())
01398     {
01399         label nCellsFromEdges = 0;
01400 
01401         // Collect all still existing faces connected to this point.
01402         forAllConstIter(Map<label>, cellFromEdge_, iter)
01403         {
01404             cellsFromEdges[nCellsFromEdges++] = objectMap
01405             (
01406                 iter.key(),
01407                 mesh.edgeCells()[iter()]
01408             );
01409         }
01410     }
01411 
01412 
01413     cellsFromFaces.setSize(cellFromFace_.size());
01414 
01415     if (cellFromFace_.size())
01416     {
01417         label nCellsFromFaces = 0;
01418 
01419         labelList cellsAroundFace(2, -1);
01420 
01421         // Collect all still existing faces connected to this point.
01422         forAllConstIter(Map<label>, cellFromFace_, iter)
01423         {
01424             label oldFaceI = iter();
01425 
01426             if (mesh.isInternalFace(oldFaceI))
01427             {
01428                 cellsAroundFace[0] = mesh.faceOwner()[oldFaceI];
01429                 cellsAroundFace[1] = mesh.faceNeighbour()[oldFaceI];
01430             }
01431             else
01432             {
01433                 cellsAroundFace[0] = mesh.faceOwner()[oldFaceI];
01434                 cellsAroundFace[1] = -1;
01435             }
01436 
01437             cellsFromFaces[nCellsFromFaces++] = objectMap
01438             (
01439                 iter.key(),
01440                 cellsAroundFace
01441             );
01442         }
01443     }
01444 
01445 
01446     // Cells from cell merging
01447     // ~~~~~~~~~~~~~~~~~~~~~~~
01448 
01449     getMergeSets
01450     (
01451         reverseCellMap_,
01452         cellMap_,
01453         cellsFromCells
01454     );
01455 }
01456 
01457 
01458 void Foam::polyTopoChange::resetZones
01459 (
01460     const polyMesh& mesh,
01461     polyMesh& newMesh,
01462     labelListList& pointZoneMap,
01463     labelListList& faceZoneFaceMap,
01464     labelListList& cellZoneMap
01465 ) const
01466 {
01467     // pointZones
01468     // ~~~~~~~~~~
01469 
01470     pointZoneMap.setSize(mesh.pointZones().size());
01471     {
01472         const pointZoneMesh& pointZones = mesh.pointZones();
01473 
01474         // Count points per zone
01475 
01476         labelList nPoints(pointZones.size(), 0);
01477 
01478         forAllConstIter(Map<label>, pointZone_, iter)
01479         {
01480             label zoneI = iter();
01481 
01482             if (zoneI < 0 || zoneI >= pointZones.size())
01483             {
01484                 FatalErrorIn
01485                 (
01486                     "resetZones(const polyMesh&, polyMesh&, labelListList&"
01487                     "labelListList&, labelListList&)"
01488                 )   << "Illegal zoneID " << zoneI << " for point "
01489                     << iter.key() << " coord " << mesh.points()[iter.key()]
01490                     << abort(FatalError);
01491             }
01492             nPoints[zoneI]++;
01493         }
01494 
01495         // Distribute points per zone
01496 
01497         labelListList addressing(pointZones.size());
01498         forAll(addressing, zoneI)
01499         {
01500             addressing[zoneI].setSize(nPoints[zoneI]);
01501         }
01502         nPoints = 0;
01503 
01504         forAllConstIter(Map<label>, pointZone_, iter)
01505         {
01506             label zoneI = iter();
01507 
01508             addressing[zoneI][nPoints[zoneI]++] = iter.key();
01509         }
01510         // Sort the addressing
01511         forAll(addressing, zoneI)
01512         {
01513             stableSort(addressing[zoneI]);
01514         }
01515 
01516         // So now we both have old zones and the new addressing.
01517         // Invert the addressing to get pointZoneMap.
01518         forAll(addressing, zoneI)
01519         {
01520             const pointZone& oldZone = pointZones[zoneI];
01521             const labelList& newZoneAddr = addressing[zoneI];
01522 
01523             labelList& curPzRnb = pointZoneMap[zoneI];
01524             curPzRnb.setSize(newZoneAddr.size());
01525 
01526             forAll(newZoneAddr, i)
01527             {
01528                 if (newZoneAddr[i] < pointMap_.size())
01529                 {
01530                     curPzRnb[i] = oldZone.whichPoint(pointMap_[newZoneAddr[i]]);
01531                 }
01532                 else
01533                 {
01534                     curPzRnb[i] = -1;
01535                 }
01536             }
01537         }
01538 
01539         // Reset the addresing on the zone
01540         newMesh.pointZones().clearAddressing();
01541         forAll(newMesh.pointZones(), zoneI)
01542         {
01543             if (debug)
01544             {
01545                 Pout<< "pointZone:" << zoneI
01546                     << "  name:" << newMesh.pointZones()[zoneI].name()
01547                     << "  size:" << addressing[zoneI].size()
01548                     << endl;
01549             }
01550 
01551             newMesh.pointZones()[zoneI] = addressing[zoneI];
01552         }
01553     }
01554 
01555 
01556     // faceZones
01557     // ~~~~~~~~~
01558 
01559     faceZoneFaceMap.setSize(mesh.faceZones().size());
01560     {
01561         const faceZoneMesh& faceZones = mesh.faceZones();
01562 
01563         labelList nFaces(faceZones.size(), 0);
01564 
01565         forAllConstIter(Map<label>, faceZone_, iter)
01566         {
01567             label zoneI = iter();
01568 
01569             if (zoneI < 0 || zoneI >= faceZones.size())
01570             {
01571                 FatalErrorIn
01572                 (
01573                     "resetZones(const polyMesh&, polyMesh&, labelListList&"
01574                     "labelListList&, labelListList&)"
01575                 )   << "Illegal zoneID " << zoneI << " for face "
01576                     << iter.key()
01577                     << abort(FatalError);
01578             }
01579             nFaces[zoneI]++;
01580         }
01581 
01582         labelListList addressing(faceZones.size());
01583         boolListList flipMode(faceZones.size());
01584 
01585         forAll(addressing, zoneI)
01586         {
01587             addressing[zoneI].setSize(nFaces[zoneI]);
01588             flipMode[zoneI].setSize(nFaces[zoneI]);
01589         }
01590         nFaces = 0;
01591 
01592         forAllConstIter(Map<label>, faceZone_, iter)
01593         {
01594             label zoneI = iter();
01595             label faceI = iter.key();
01596 
01597             label index = nFaces[zoneI]++;
01598 
01599             addressing[zoneI][index] = faceI;
01600             flipMode[zoneI][index] = faceZoneFlip_[faceI];
01601         }
01602         // Sort the addressing
01603         forAll(addressing, zoneI)
01604         {
01605             labelList newToOld;
01606             sortedOrder(addressing[zoneI], newToOld);
01607             {
01608                 labelList newAddressing(addressing[zoneI].size());
01609                 forAll(newAddressing, i)
01610                 {
01611                     newAddressing[i] = addressing[zoneI][newToOld[i]];
01612                 }
01613                 addressing[zoneI].transfer(newAddressing);
01614             }
01615             {
01616                 boolList newFlipMode(flipMode[zoneI].size());
01617                 forAll(newFlipMode, i)
01618                 {
01619                     newFlipMode[i] = flipMode[zoneI][newToOld[i]];
01620                 }
01621                 flipMode[zoneI].transfer(newFlipMode);
01622             }
01623         }
01624 
01625         // So now we both have old zones and the new addressing.
01626         // Invert the addressing to get faceZoneFaceMap.
01627         forAll(addressing, zoneI)
01628         {
01629             const faceZone& oldZone = faceZones[zoneI];
01630             const labelList& newZoneAddr = addressing[zoneI];
01631 
01632             labelList& curFzFaceRnb = faceZoneFaceMap[zoneI];
01633 
01634             curFzFaceRnb.setSize(newZoneAddr.size());
01635 
01636             forAll(newZoneAddr, i)
01637             {
01638                 if (newZoneAddr[i] < faceMap_.size())
01639                 {
01640                     curFzFaceRnb[i] =
01641                         oldZone.whichFace(faceMap_[newZoneAddr[i]]);
01642                 }
01643                 else
01644                 {
01645                     curFzFaceRnb[i] = -1;
01646                 }
01647             }
01648         }
01649 
01650 
01651         // Reset the addresing on the zone
01652         newMesh.faceZones().clearAddressing();
01653         forAll(newMesh.faceZones(), zoneI)
01654         {
01655             if (debug)
01656             {
01657                 Pout<< "faceZone:" << zoneI
01658                     << "  name:" << newMesh.faceZones()[zoneI].name()
01659                     << "  size:" << addressing[zoneI].size()
01660                     << endl;
01661             }
01662 
01663             newMesh.faceZones()[zoneI].resetAddressing
01664             (
01665                 addressing[zoneI],
01666                 flipMode[zoneI]
01667             );
01668         }
01669     }
01670 
01671 
01672     // cellZones
01673     // ~~~~~~~~~
01674 
01675     cellZoneMap.setSize(mesh.cellZones().size());
01676     {
01677         const cellZoneMesh& cellZones = mesh.cellZones();
01678 
01679         labelList nCells(cellZones.size(), 0);
01680 
01681         forAll(cellZone_, cellI)
01682         {
01683             label zoneI = cellZone_[cellI];
01684 
01685             if (zoneI >= cellZones.size())
01686             {
01687                 FatalErrorIn
01688                 (
01689                     "resetZones(const polyMesh&, polyMesh&, labelListList&"
01690                     "labelListList&, labelListList&)"
01691                 )   << "Illegal zoneID " << zoneI << " for cell "
01692                     << cellI << abort(FatalError);
01693             }
01694 
01695             if (zoneI >= 0)
01696             {
01697                 nCells[zoneI]++;
01698             }
01699         }
01700 
01701         labelListList addressing(cellZones.size());
01702         forAll(addressing, zoneI)
01703         {
01704             addressing[zoneI].setSize(nCells[zoneI]);
01705         }
01706         nCells = 0;
01707 
01708         forAll(cellZone_, cellI)
01709         {
01710             label zoneI = cellZone_[cellI];
01711 
01712             if (zoneI >= 0)
01713             {
01714                 addressing[zoneI][nCells[zoneI]++] = cellI;
01715             }
01716         }
01717         // Sort the addressing
01718         forAll(addressing, zoneI)
01719         {
01720             stableSort(addressing[zoneI]);
01721         }
01722 
01723         // So now we both have old zones and the new addressing.
01724         // Invert the addressing to get cellZoneMap.
01725         forAll(addressing, zoneI)
01726         {
01727             const cellZone& oldZone = cellZones[zoneI];
01728             const labelList& newZoneAddr = addressing[zoneI];
01729 
01730             labelList& curCellRnb = cellZoneMap[zoneI];
01731 
01732             curCellRnb.setSize(newZoneAddr.size());
01733 
01734             forAll(newZoneAddr, i)
01735             {
01736                 if (newZoneAddr[i] < cellMap_.size())
01737                 {
01738                     curCellRnb[i] =
01739                         oldZone.whichCell(cellMap_[newZoneAddr[i]]);
01740                 }
01741                 else
01742                 {
01743                     curCellRnb[i] = -1;
01744                 }
01745             }
01746         }
01747 
01748         // Reset the addresing on the zone
01749         newMesh.cellZones().clearAddressing();
01750         forAll(newMesh.cellZones(), zoneI)
01751         {
01752             if (debug)
01753             {
01754                 Pout<< "cellZone:" << zoneI
01755                     << "  name:" << newMesh.cellZones()[zoneI].name()
01756                     << "  size:" << addressing[zoneI].size()
01757                     << endl;
01758             }
01759 
01760             newMesh.cellZones()[zoneI] = addressing[zoneI];
01761         }
01762     }
01763 }
01764 
01765 
01766 void Foam::polyTopoChange::calcFaceZonePointMap
01767 (
01768     const polyMesh& mesh,
01769     const List<Map<label> >& oldFaceZoneMeshPointMaps,
01770     labelListList& faceZonePointMap
01771 ) const
01772 {
01773     const faceZoneMesh& faceZones = mesh.faceZones();
01774 
01775     faceZonePointMap.setSize(faceZones.size());
01776 
01777     forAll(faceZones, zoneI)
01778     {
01779         const faceZone& newZone = faceZones[zoneI];
01780 
01781         const labelList& newZoneMeshPoints = newZone().meshPoints();
01782 
01783         const Map<label>& oldZoneMeshPointMap = oldFaceZoneMeshPointMaps[zoneI];
01784 
01785         labelList& curFzPointRnb = faceZonePointMap[zoneI];
01786 
01787         curFzPointRnb.setSize(newZoneMeshPoints.size());
01788 
01789         forAll(newZoneMeshPoints, pointI)
01790         {
01791             if (newZoneMeshPoints[pointI] < pointMap_.size())
01792             {
01793                 Map<label>::const_iterator ozmpmIter =
01794                     oldZoneMeshPointMap.find
01795                     (
01796                         pointMap_[newZoneMeshPoints[pointI]]
01797                     );
01798 
01799                 if (ozmpmIter != oldZoneMeshPointMap.end())
01800                 {
01801                     curFzPointRnb[pointI] = ozmpmIter();
01802                 }
01803                 else
01804                 {
01805                     curFzPointRnb[pointI] = -1;
01806                 }
01807             }
01808             else
01809             {
01810                 curFzPointRnb[pointI] = -1;
01811             }
01812         }
01813     }
01814 }
01815 
01816 
01817 Foam::face Foam::polyTopoChange::rotateFace
01818 (
01819     const face& f,
01820     const label nPos
01821 )
01822 {
01823     face newF(f.size());
01824 
01825     forAll(f, fp)
01826     {
01827         label fp1 = (fp + nPos) % f.size();
01828 
01829         if (fp1 < 0)
01830         {
01831             fp1 += f.size();
01832         }
01833 
01834         newF[fp1] = f[fp];
01835     }
01836 
01837     return newF;
01838 }
01839 
01840 
01841 void Foam::polyTopoChange::reorderCoupledFaces
01842 (
01843     const bool syncParallel,
01844     const polyBoundaryMesh& boundary,
01845     const labelList& patchStarts,
01846     const labelList& patchSizes,
01847     const pointField& points
01848 )
01849 {
01850     // Mapping for faces (old to new). Extends over all mesh faces for
01851     // convenience (could be just the external faces)
01852     labelList oldToNew(identity(faces_.size()));
01853 
01854     // Rotation on new faces.
01855     labelList rotation(faces_.size(), 0);
01856 
01857     // Send ordering
01858     forAll(boundary, patchI)
01859     {
01860         if (syncParallel || !isA<processorPolyPatch>(boundary[patchI]))
01861         {
01862             boundary[patchI].initOrder
01863             (
01864                 primitivePatch
01865                 (
01866                     SubList<face>
01867                     (
01868                         faces_,
01869                         patchSizes[patchI],
01870                         patchStarts[patchI]
01871                     ),
01872                     points
01873                 )
01874             );
01875         }
01876     }
01877 
01878     // Receive and calculate ordering
01879 
01880     bool anyChanged = false;
01881 
01882     forAll(boundary, patchI)
01883     {
01884         if (syncParallel || !isA<processorPolyPatch>(boundary[patchI]))
01885         {
01886             labelList patchFaceMap(patchSizes[patchI], -1);
01887             labelList patchFaceRotation(patchSizes[patchI], 0);
01888 
01889             bool changed = boundary[patchI].order
01890             (
01891                 primitivePatch
01892                 (
01893                     SubList<face>
01894                     (
01895                         faces_,
01896                         patchSizes[patchI],
01897                         patchStarts[patchI]
01898                     ),
01899                     points
01900                 ),
01901                 patchFaceMap,
01902                 patchFaceRotation
01903             );
01904 
01905             if (changed)
01906             {
01907                 // Merge patch face reordering into mesh face reordering table
01908                 label start = patchStarts[patchI];
01909 
01910                 forAll(patchFaceMap, patchFaceI)
01911                 {
01912                     oldToNew[patchFaceI + start] =
01913                         start + patchFaceMap[patchFaceI];
01914                 }
01915 
01916                 forAll(patchFaceRotation, patchFaceI)
01917                 {
01918                     rotation[patchFaceI + start] =
01919                         patchFaceRotation[patchFaceI];
01920                 }
01921 
01922                 anyChanged = true;
01923             }
01924         }
01925     }
01926 
01927     if (syncParallel)
01928     {
01929         reduce(anyChanged, orOp<bool>());
01930     }
01931 
01932     if (anyChanged)
01933     {
01934         // Reorder faces according to oldToNew.
01935         reorderCompactFaces(oldToNew.size(), oldToNew);
01936 
01937         // Rotate faces (rotation is already in new face indices).
01938         forAll(rotation, faceI)
01939         {
01940             if (rotation[faceI] != 0)
01941             {
01942                 faces_[faceI] = rotateFace(faces_[faceI], rotation[faceI]);
01943             }
01944         }
01945     }
01946 }
01947 
01948 
01949 void Foam::polyTopoChange::compactAndReorder
01950 (
01951     const polyMesh& mesh,
01952     const bool syncParallel,
01953     const bool orderCells,
01954     const bool orderPoints,
01955 
01956     label& nInternalPoints,
01957     pointField& newPoints,
01958     labelList& patchSizes,
01959     labelList& patchStarts,
01960     List<objectMap>& pointsFromPoints,
01961     List<objectMap>& facesFromPoints,
01962     List<objectMap>& facesFromEdges,
01963     List<objectMap>& facesFromFaces,
01964     List<objectMap>& cellsFromPoints,
01965     List<objectMap>& cellsFromEdges,
01966     List<objectMap>& cellsFromFaces,
01967     List<objectMap>& cellsFromCells,
01968     List<Map<label> >& oldPatchMeshPointMaps,
01969     labelList& oldPatchNMeshPoints,
01970     labelList& oldPatchStarts,
01971     List<Map<label> >& oldFaceZoneMeshPointMaps
01972 )
01973 {
01974     if (mesh.boundaryMesh().size() != nPatches_)
01975     {
01976         FatalErrorIn("polyTopoChange::compactAndReorder(..)")
01977             << "polyTopoChange was constructed with a mesh with "
01978             << nPatches_ << " patches." << endl
01979             << "The mesh now provided has a different number of patches "
01980             << mesh.boundaryMesh().size()
01981             << " which is illegal" << endl
01982             << abort(FatalError);
01983     }
01984 
01985     // Remove any holes from points/faces/cells and sort faces.
01986     // Sets nActiveFaces_.
01987     compact(orderCells, orderPoints, nInternalPoints, patchSizes, patchStarts);
01988 
01989     // Transfer points to pointField. points_ are now cleared!
01990     // Only done since e.g. reorderCoupledFaces requires pointField.
01991     newPoints.transfer(points_);
01992 
01993     // Reorder any coupled faces
01994     reorderCoupledFaces
01995     (
01996         syncParallel,
01997         mesh.boundaryMesh(),
01998         patchStarts,
01999         patchSizes,
02000         newPoints
02001     );
02002 
02003 
02004     // Calculate inflation/merging maps
02005     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
02006     // These are for the new face(/point/cell) the old faces whose value
02007     // needs to be
02008     // averaged/summed to get the new value. These old faces come either from
02009     // merged old faces (face remove into other face),
02010     // the old edgeFaces (inflate from edge) or the old pointFaces (inflate
02011     // from point). As an additional complexity will use only internal faces
02012     // to create new value for internal face and vice versa only patch
02013     // faces to to create patch face value.
02014 
02015     // For point only point merging
02016     getMergeSets
02017     (
02018         reversePointMap_,
02019         pointMap_,
02020         pointsFromPoints
02021     );
02022 
02023     calcFaceInflationMaps
02024     (
02025         mesh,
02026         facesFromPoints,
02027         facesFromEdges,
02028         facesFromFaces
02029     );
02030 
02031     calcCellInflationMaps
02032     (
02033         mesh,
02034         cellsFromPoints,
02035         cellsFromEdges,
02036         cellsFromFaces,
02037         cellsFromCells
02038     );
02039 
02040     // Clear inflation info
02041     {
02042         faceFromPoint_.clearStorage();
02043         faceFromEdge_.clearStorage();
02044 
02045         cellFromPoint_.clearStorage();
02046         cellFromEdge_.clearStorage();
02047         cellFromFace_.clearStorage();
02048     }
02049 
02050 
02051     const polyBoundaryMesh& boundary = mesh.boundaryMesh();
02052 
02053     // Grab patch mesh point maps
02054     oldPatchMeshPointMaps.setSize(boundary.size());
02055     oldPatchNMeshPoints.setSize(boundary.size());
02056     oldPatchStarts.setSize(boundary.size());
02057 
02058     forAll(boundary, patchI)
02059     {
02060         // Copy old face zone mesh point maps
02061         oldPatchMeshPointMaps[patchI] = boundary[patchI].meshPointMap();
02062         oldPatchNMeshPoints[patchI] = boundary[patchI].meshPoints().size();
02063         oldPatchStarts[patchI] = boundary[patchI].start();
02064     }
02065 
02066     // Grab old face zone mesh point maps.
02067     // These need to be saved before resetting the mesh and are used
02068     // later on to calculate the faceZone pointMaps.
02069     oldFaceZoneMeshPointMaps.setSize(mesh.faceZones().size());
02070 
02071     forAll(mesh.faceZones(), zoneI)
02072     {
02073         const faceZone& oldZone = mesh.faceZones()[zoneI];
02074 
02075         oldFaceZoneMeshPointMaps[zoneI] = oldZone().meshPointMap();
02076     }
02077 }
02078 
02079 
02080 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
02081 
02082 // Construct from components
02083 Foam::polyTopoChange::polyTopoChange(const label nPatches, const bool strict)
02084 :
02085     strict_(strict),
02086     nPatches_(nPatches),
02087     points_(0),
02088     pointMap_(0),
02089     reversePointMap_(0),
02090     pointZone_(0),
02091     retiredPoints_(0),
02092     faces_(0),
02093     region_(0),
02094     faceOwner_(0),
02095     faceNeighbour_(0),
02096     faceMap_(0),
02097     reverseFaceMap_(0),
02098     faceFromPoint_(0),
02099     faceFromEdge_(0),
02100     flipFaceFlux_(0),
02101     faceZone_(0),
02102     faceZoneFlip_(0),
02103     nActiveFaces_(0),
02104     cellMap_(0),
02105     reverseCellMap_(0),
02106     cellFromPoint_(0),
02107     cellFromEdge_(0),
02108     cellFromFace_(0),
02109     cellZone_(0)
02110 {}
02111 
02112 
02113 // Construct from components
02114 Foam::polyTopoChange::polyTopoChange
02115 (
02116     const polyMesh& mesh,
02117     const bool strict
02118 )
02119 :
02120     strict_(strict),
02121     nPatches_(0),
02122     points_(0),
02123     pointMap_(0),
02124     reversePointMap_(0),
02125     pointZone_(0),
02126     retiredPoints_(0),
02127     faces_(0),
02128     region_(0),
02129     faceOwner_(0),
02130     faceNeighbour_(0),
02131     faceMap_(0),
02132     reverseFaceMap_(0),
02133     faceFromPoint_(0),
02134     faceFromEdge_(0),
02135     flipFaceFlux_(0),
02136     faceZone_(0),
02137     faceZoneFlip_(0),
02138     nActiveFaces_(0),
02139     cellMap_(0),
02140     reverseCellMap_(0),
02141     cellFromPoint_(0),
02142     cellFromEdge_(0),
02143     cellFromFace_(0),
02144     cellZone_(0)
02145 {
02146     addMesh
02147     (
02148         mesh,
02149         identity(mesh.boundaryMesh().size()),
02150         identity(mesh.pointZones().size()),
02151         identity(mesh.faceZones().size()),
02152         identity(mesh.cellZones().size())
02153     );
02154 }
02155 
02156 
02157 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
02158 
02159 void Foam::polyTopoChange::clear()
02160 {
02161     points_.clearStorage();
02162     pointMap_.clearStorage();
02163     reversePointMap_.clearStorage();
02164     pointZone_.clearStorage();
02165     retiredPoints_.clearStorage();
02166 
02167     faces_.clearStorage();
02168     region_.clearStorage();
02169     faceOwner_.clearStorage();
02170     faceNeighbour_.clearStorage();
02171     faceMap_.clearStorage();
02172     reverseFaceMap_.clearStorage();
02173     faceFromPoint_.clearStorage();
02174     faceFromEdge_.clearStorage();
02175     flipFaceFlux_.clearStorage();
02176     faceZone_.clearStorage();
02177     faceZoneFlip_.clearStorage();
02178     nActiveFaces_ = 0;
02179 
02180     cellMap_.clearStorage();
02181     reverseCellMap_.clearStorage();
02182     cellZone_.clearStorage();
02183     cellFromPoint_.clearStorage();
02184     cellFromEdge_.clearStorage();
02185     cellFromFace_.clearStorage();
02186 }
02187 
02188 
02189 void Foam::polyTopoChange::addMesh
02190 (
02191     const polyMesh& mesh,
02192     const labelList& patchMap,
02193     const labelList& pointZoneMap,
02194     const labelList& faceZoneMap,
02195     const labelList& cellZoneMap
02196 )
02197 {
02198     label maxRegion = nPatches_ - 1;
02199     forAll(patchMap, i)
02200     {
02201         maxRegion = max(maxRegion, patchMap[i]);
02202     }
02203     nPatches_ = maxRegion + 1;
02204 
02205 
02206     // Add points
02207     {
02208         const pointField& points = mesh.points();
02209         const pointZoneMesh& pointZones = mesh.pointZones();
02210 
02211         // Extend
02212         points_.setCapacity(points_.size() + points.size());
02213         pointMap_.setCapacity(pointMap_.size() + points.size());
02214         pointZone_.resize(pointZone_.size() + points.size()/100);
02215 
02216         // Precalc offset zones
02217         labelList newZoneID(points.size(), -1);
02218 
02219         forAll(pointZones, zoneI)
02220         {
02221             const labelList& pointLabels = pointZones[zoneI];
02222 
02223             forAll(pointLabels, j)
02224             {
02225                 newZoneID[pointLabels[j]] = pointZoneMap[zoneI];
02226             }
02227         }
02228 
02229         // Add points in mesh order
02230         for (label pointI = 0; pointI < mesh.nPoints(); pointI++)
02231         {
02232             addPoint
02233             (
02234                 points[pointI],
02235                 pointI,
02236                 newZoneID[pointI],
02237                 true
02238             );
02239         }
02240     }
02241 
02242     // Add cells
02243     {
02244         const cellZoneMesh& cellZones = mesh.cellZones();
02245 
02246         // Resize
02247 
02248         // Note: polyMesh does not allow retired cells anymore. So allCells
02249         // always equals nCells
02250         label nAllCells = mesh.nCells();
02251 
02252         cellMap_.setCapacity(cellMap_.size() + nAllCells);
02253         cellFromPoint_.resize(cellFromPoint_.size() + nAllCells/100);
02254         cellFromEdge_.resize(cellFromEdge_.size() + nAllCells/100);
02255         cellFromFace_.resize(cellFromFace_.size() + nAllCells/100);
02256         cellZone_.setCapacity(cellZone_.size() + nAllCells);
02257 
02258 
02259         // Precalc offset zones
02260         labelList newZoneID(nAllCells, -1);
02261 
02262         forAll(cellZones, zoneI)
02263         {
02264             const labelList& cellLabels = cellZones[zoneI];
02265 
02266             forAll(cellLabels, j)
02267             {
02268                 label cellI = cellLabels[j];
02269 
02270                 if (newZoneID[cellI] != -1)
02271                 {
02272                     WarningIn
02273                     (
02274                         "polyTopoChange::addMesh"
02275                         "(const polyMesh&, const labelList&,"
02276                         "const labelList&, const labelList&,"
02277                         "const labelList&)"
02278                     )   << "Cell:" << cellI
02279                         << " centre:" << mesh.cellCentres()[cellI]
02280                         << " is in two zones:"
02281                         << cellZones[newZoneID[cellI]].name()
02282                         << " and " << cellZones[zoneI].name() << endl
02283                         << "    This is not supported."
02284                         << " Continuing with first zone only." << endl;
02285                 }
02286                 else
02287                 {
02288                     newZoneID[cellI] = cellZoneMap[zoneI];
02289                 }
02290             }
02291         }
02292 
02293         // Add cells in mesh order
02294         for (label cellI = 0; cellI < nAllCells; cellI++)
02295         {
02296             // Add cell from cell
02297             addCell(-1, -1, -1, cellI, newZoneID[cellI]);
02298         }
02299     }
02300 
02301     // Add faces
02302     {
02303         const polyBoundaryMesh& patches = mesh.boundaryMesh();
02304         const faceList& faces = mesh.faces();
02305         const labelList& faceOwner = mesh.faceOwner();
02306         const labelList& faceNeighbour = mesh.faceNeighbour();
02307         const faceZoneMesh& faceZones = mesh.faceZones();
02308 
02309         // Resize
02310         label nAllFaces = mesh.faces().size();
02311 
02312         faces_.setCapacity(faces_.size() + nAllFaces);
02313         region_.setCapacity(region_.size() + nAllFaces);
02314         faceOwner_.setCapacity(faceOwner_.size() + nAllFaces);
02315         faceNeighbour_.setCapacity(faceNeighbour_.size() + nAllFaces);
02316         faceMap_.setCapacity(faceMap_.size() + nAllFaces);
02317         faceFromPoint_.resize(faceFromPoint_.size() + nAllFaces/100);
02318         faceFromEdge_.resize(faceFromEdge_.size() + nAllFaces/100);
02319         flipFaceFlux_.setCapacity(faces_.size() + nAllFaces);
02320         faceZone_.resize(faceZone_.size() + nAllFaces/100);
02321         faceZoneFlip_.setCapacity(faces_.size() + nAllFaces);
02322 
02323 
02324         // Precalc offset zones
02325         labelList newZoneID(nAllFaces, -1);
02326         boolList zoneFlip(nAllFaces, false);
02327 
02328         forAll(faceZones, zoneI)
02329         {
02330             const labelList& faceLabels = faceZones[zoneI];
02331             const boolList& flipMap = faceZones[zoneI].flipMap();
02332 
02333             forAll(faceLabels, j)
02334             {
02335                 newZoneID[faceLabels[j]] = faceZoneMap[zoneI];
02336                 zoneFlip[faceLabels[j]] = flipMap[j];
02337             }
02338         }
02339 
02340         // Add faces in mesh order
02341 
02342         // 1. Internal faces
02343         for (label faceI = 0; faceI < mesh.nInternalFaces(); faceI++)
02344         {
02345             addFace
02346             (
02347                 faces[faceI],
02348                 faceOwner[faceI],
02349                 faceNeighbour[faceI],
02350                 -1,                         // masterPointID
02351                 -1,                         // masterEdgeID
02352                 faceI,                      // masterFaceID
02353                 false,                      // flipFaceFlux
02354                 -1,                         // patchID
02355                 newZoneID[faceI],           // zoneID
02356                 zoneFlip[faceI]             // zoneFlip
02357             );
02358         }
02359 
02360         // 2. Patch faces
02361         forAll(patches, patchI)
02362         {
02363             const polyPatch& pp = patches[patchI];
02364 
02365             if (pp.start() != faces_.size())
02366             {
02367                 FatalErrorIn
02368                 (
02369                     "polyTopoChange::polyTopoChange"
02370                     "(const polyMesh& mesh, const bool strict)"
02371                 )   << "Problem : "
02372                     << "Patch " << pp.name() << " starts at " << pp.start()
02373                     << endl
02374                     << "Current face counter at " << faces_.size() << endl
02375                     << "Are patches in incremental order?"
02376                     << abort(FatalError);
02377             }
02378             forAll(pp, patchFaceI)
02379             {
02380                 label faceI = pp.start() + patchFaceI;
02381 
02382                 addFace
02383                 (
02384                     faces[faceI],
02385                     faceOwner[faceI],
02386                     -1,                         // neighbour
02387                     -1,                         // masterPointID
02388                     -1,                         // masterEdgeID
02389                     faceI,                      // masterFaceID
02390                     false,                      // flipFaceFlux
02391                     patchMap[patchI],           // patchID
02392                     newZoneID[faceI],           // zoneID
02393                     zoneFlip[faceI]             // zoneFlip
02394                 );
02395             }
02396         }
02397     }
02398 }
02399 
02400 
02401 Foam::label Foam::polyTopoChange::setAction(const topoAction& action)
02402 {
02403     if (isType<polyAddPoint>(action))
02404     {
02405         const polyAddPoint& pap = refCast<const polyAddPoint>(action);
02406 
02407         return addPoint
02408         (
02409             pap.newPoint(),
02410             pap.masterPointID(),
02411             pap.zoneID(),
02412             pap.inCell()
02413         );
02414     }
02415     else if (isType<polyModifyPoint>(action))
02416     {
02417         const polyModifyPoint& pmp = refCast<const polyModifyPoint>(action);
02418 
02419         modifyPoint
02420         (
02421             pmp.pointID(),
02422             pmp.newPoint(),
02423             pmp.zoneID(),
02424             pmp.inCell()
02425         );
02426 
02427         return -1;
02428     }
02429     else if (isType<polyRemovePoint>(action))
02430     {
02431         const polyRemovePoint& prp = refCast<const polyRemovePoint>(action);
02432 
02433         removePoint(prp.pointID(), prp.mergePointID());
02434 
02435         return -1;
02436     }
02437     else if (isType<polyAddFace>(action))
02438     {
02439         const polyAddFace& paf = refCast<const polyAddFace>(action);
02440 
02441         return addFace
02442         (
02443             paf.newFace(),
02444             paf.owner(),
02445             paf.neighbour(),
02446             paf.masterPointID(),
02447             paf.masterEdgeID(),
02448             paf.masterFaceID(),
02449             paf.flipFaceFlux(),
02450             paf.patchID(),
02451             paf.zoneID(),
02452             paf.zoneFlip()
02453         );
02454     }
02455     else if (isType<polyModifyFace>(action))
02456     {
02457         const polyModifyFace& pmf = refCast<const polyModifyFace>(action);
02458 
02459         modifyFace
02460         (
02461             pmf.newFace(),
02462             pmf.faceID(),
02463             pmf.owner(),
02464             pmf.neighbour(),
02465             pmf.flipFaceFlux(),
02466             pmf.patchID(),
02467             pmf.zoneID(),
02468             pmf.zoneFlip()
02469         );
02470 
02471         return -1;
02472     }
02473     else if (isType<polyRemoveFace>(action))
02474     {
02475         const polyRemoveFace& prf = refCast<const polyRemoveFace>(action);
02476 
02477         removeFace(prf.faceID(), prf.mergeFaceID());
02478 
02479         return -1;
02480     }
02481     else if (isType<polyAddCell>(action))
02482     {
02483         const polyAddCell& pac = refCast<const polyAddCell>(action);
02484 
02485         return addCell
02486         (
02487             pac.masterPointID(),
02488             pac.masterEdgeID(),
02489             pac.masterFaceID(),
02490             pac.masterCellID(),
02491             pac.zoneID()
02492         );
02493     }
02494     else if (isType<polyModifyCell>(action))
02495     {
02496         const polyModifyCell& pmc = refCast<const polyModifyCell>(action);
02497 
02498         if (pmc.removeFromZone())
02499         {
02500             modifyCell(pmc.cellID(), -1);
02501         }
02502         else
02503         {
02504             modifyCell(pmc.cellID(), pmc.zoneID());
02505         }
02506 
02507         return -1;
02508     }
02509     else if (isType<polyRemoveCell>(action))
02510     {
02511         const polyRemoveCell& prc = refCast<const polyRemoveCell>(action);
02512 
02513         removeCell(prc.cellID(), prc.mergeCellID());
02514 
02515         return -1;
02516     }
02517     else
02518     {
02519         FatalErrorIn
02520         (
02521             "label polyTopoChange::setAction(const topoAction& action)"
02522         )   << "Unknown type of topoChange: " << action.type()
02523             << abort(FatalError);
02524 
02525         // Dummy return to keep compiler happy
02526         return -1;
02527     }
02528 }
02529 
02530 
02531 Foam::label Foam::polyTopoChange::addPoint
02532 (
02533     const point& pt,
02534     const label masterPointID,
02535     const label zoneID,
02536     const bool inCell
02537 )
02538 {
02539     label pointI = points_.size();
02540 
02541     points_.append(pt);
02542     pointMap_.append(masterPointID);
02543     reversePointMap_.append(pointI);
02544 
02545     if (zoneID >= 0)
02546     {
02547         pointZone_.insert(pointI, zoneID);
02548     }
02549 
02550     if (!inCell)
02551     {
02552         retiredPoints_.insert(pointI);
02553     }
02554 
02555     return pointI;
02556 }
02557 
02558 
02559 void Foam::polyTopoChange::modifyPoint
02560 (
02561     const label pointI,
02562     const point& pt,
02563     const label newZoneID,
02564     const bool inCell
02565 )
02566 {
02567     if (pointI < 0 || pointI >= points_.size())
02568     {
02569         FatalErrorIn
02570         (
02571             "polyTopoChange::modifyPoint(const label, const point&)"
02572         )   << "illegal point label " << pointI << endl
02573             << "Valid point labels are 0 .. " << points_.size()-1
02574             << abort(FatalError);
02575     }
02576     if (pointRemoved(pointI) || pointMap_[pointI] == -1)
02577     {
02578         FatalErrorIn
02579         (
02580             "polyTopoChange::modifyPoint(const label, const point&)"
02581         )   << "point " << pointI << " already marked for removal"
02582             << abort(FatalError);
02583     }
02584     points_[pointI] = pt;
02585 
02586     Map<label>::iterator pointFnd = pointZone_.find(pointI);
02587 
02588     if (pointFnd != pointZone_.end())
02589     {
02590         if (newZoneID >= 0)
02591         {
02592             pointFnd() = newZoneID;
02593         }
02594         else
02595         {
02596             pointZone_.erase(pointFnd);
02597         }
02598     }
02599     else if (newZoneID >= 0)
02600     {
02601         pointZone_.insert(pointI, newZoneID);
02602     }
02603 
02604     if (inCell)
02605     {
02606         retiredPoints_.erase(pointI);
02607     }
02608     else
02609     {
02610         retiredPoints_.insert(pointI);
02611     }
02612 }
02613 
02614 
02615 void Foam::polyTopoChange::movePoints(const pointField& newPoints)
02616 {
02617     if (newPoints.size() != points_.size())
02618     {
02619         FatalErrorIn("polyTopoChange::movePoints(const pointField&)")
02620             << "illegal pointField size." << endl
02621             << "Size:" << newPoints.size() << endl
02622             << "Points in mesh:" << points_.size()
02623             << abort(FatalError);
02624     }
02625 
02626     forAll(points_, pointI)
02627     {
02628         points_[pointI] = newPoints[pointI];
02629     }
02630 }
02631 
02632 
02633 void Foam::polyTopoChange::removePoint
02634 (
02635     const label pointI,
02636     const label mergePointI
02637 )
02638 {
02639     if (pointI < 0 || pointI >= points_.size())
02640     {
02641         FatalErrorIn("polyTopoChange::removePoint(const label, const label)")
02642             << "illegal point label " << pointI << endl
02643             << "Valid point labels are 0 .. " << points_.size()-1
02644             << abort(FatalError);
02645     }
02646 
02647     if
02648     (
02649         strict_
02650      && (pointRemoved(pointI) || pointMap_[pointI] == -1)
02651     )
02652     {
02653         FatalErrorIn("polyTopoChange::removePoint(const label, const label)")
02654             << "point " << pointI << " already marked for removal" << nl
02655             << "Point:" << points_[pointI] << " pointMap:" << pointMap_[pointI]
02656             << abort(FatalError);
02657     }
02658 
02659     if (pointI == mergePointI)
02660     {
02661         FatalErrorIn("polyTopoChange::removePoint(const label, const label)")
02662             << "Cannot remove/merge point " << pointI << " onto itself."
02663             << abort(FatalError);
02664     }
02665 
02666     points_[pointI] = greatPoint;
02667     pointMap_[pointI] = -1;
02668     if (mergePointI >= 0)
02669     {
02670         reversePointMap_[pointI] = -mergePointI-2;
02671     }
02672     else
02673     {
02674         reversePointMap_[pointI] = -1;
02675     }
02676     pointZone_.erase(pointI);
02677     retiredPoints_.erase(pointI);
02678 }
02679 
02680 
02681 Foam::label Foam::polyTopoChange::addFace
02682 (
02683     const face& f,
02684     const label own,
02685     const label nei,
02686     const label masterPointID,
02687     const label masterEdgeID,
02688     const label masterFaceID,
02689     const bool flipFaceFlux,
02690     const label patchID,
02691     const label zoneID,
02692     const bool zoneFlip
02693 )
02694 {
02695     // Check validity
02696     if (debug)
02697     {
02698         checkFace(f, -1, own, nei, patchID, zoneID);
02699     }
02700 
02701     label faceI = faces_.size();
02702 
02703     faces_.append(f);
02704     region_.append(patchID);
02705     faceOwner_.append(own);
02706     faceNeighbour_.append(nei);
02707 
02708     if (masterPointID >= 0)
02709     {
02710         faceMap_.append(-1);
02711         faceFromPoint_.insert(faceI, masterPointID);
02712     }
02713     else if (masterEdgeID >= 0)
02714     {
02715         faceMap_.append(-1);
02716         faceFromEdge_.insert(faceI, masterEdgeID);
02717     }
02718     else if (masterFaceID >= 0)
02719     {
02720         faceMap_.append(masterFaceID);
02721     }
02722     else
02723     {
02724         // Allow inflate-from-nothing?
02725         //FatalErrorIn("polyTopoChange::addFace")
02726         //    << "Need to specify a master point, edge or face"
02727         //    << "face:" << f << " own:" << own << " nei:" << nei
02728         //    << abort(FatalError);
02729         faceMap_.append(-1);
02730     }
02731     reverseFaceMap_.append(faceI);
02732 
02733     flipFaceFlux_[faceI] = (flipFaceFlux ? 1 : 0);
02734 
02735     if (zoneID >= 0)
02736     {
02737         faceZone_.insert(faceI, zoneID);
02738     }
02739     faceZoneFlip_[faceI] = (zoneFlip ? 1 : 0);
02740 
02741     return faceI;
02742 }
02743 
02744 
02745 void Foam::polyTopoChange::modifyFace
02746 (
02747     const face& f,
02748     const label faceI,
02749     const label own,
02750     const label nei,
02751     const bool flipFaceFlux,
02752     const label patchID,
02753     const label zoneID,
02754     const bool zoneFlip
02755 )
02756 {
02757     // Check validity
02758     if (debug)
02759     {
02760         checkFace(f, faceI, own, nei, patchID, zoneID);
02761     }
02762 
02763     faces_[faceI] = f;
02764     faceOwner_[faceI] = own;
02765     faceNeighbour_[faceI] = nei;
02766     region_[faceI] = patchID;
02767 
02768     flipFaceFlux_[faceI] = (flipFaceFlux ? 1 : 0);
02769 
02770     Map<label>::iterator faceFnd = faceZone_.find(faceI);
02771 
02772     if (faceFnd != faceZone_.end())
02773     {
02774         if (zoneID >= 0)
02775         {
02776             faceFnd() = zoneID;
02777         }
02778         else
02779         {
02780             faceZone_.erase(faceFnd);
02781         }
02782     }
02783     else if (zoneID >= 0)
02784     {
02785         faceZone_.insert(faceI, zoneID);
02786     }
02787     faceZoneFlip_[faceI] = (zoneFlip ? 1 : 0);
02788 }
02789 
02790 
02791 void Foam::polyTopoChange::removeFace(const label faceI, const label mergeFaceI)
02792 {
02793     if (faceI < 0 || faceI >= faces_.size())
02794     {
02795         FatalErrorIn("polyTopoChange::removeFace(const label, const label)")
02796             << "illegal face label " << faceI << endl
02797             << "Valid face labels are 0 .. " << faces_.size()-1
02798             << abort(FatalError);
02799     }
02800 
02801     if
02802     (
02803         strict_
02804      && (faceRemoved(faceI) || faceMap_[faceI] == -1)
02805     )
02806     {
02807         FatalErrorIn("polyTopoChange::removeFace(const label, const label)")
02808             << "face " << faceI
02809             << " already marked for removal"
02810             << abort(FatalError);
02811     }
02812 
02813     faces_[faceI].setSize(0);
02814     region_[faceI] = -1;
02815     faceOwner_[faceI] = -1;
02816     faceNeighbour_[faceI] = -1;
02817     faceMap_[faceI] = -1;
02818     if (mergeFaceI >= 0)
02819     {
02820         reverseFaceMap_[faceI] = -mergeFaceI-2;
02821     }
02822     else
02823     {
02824         reverseFaceMap_[faceI] = -1;
02825     }
02826     faceFromEdge_.erase(faceI);
02827     faceFromPoint_.erase(faceI);
02828     flipFaceFlux_[faceI] = 0;
02829     faceZone_.erase(faceI);
02830     faceZoneFlip_[faceI] = 0;
02831 }
02832 
02833 
02834 Foam::label Foam::polyTopoChange::addCell
02835 (
02836     const label masterPointID,
02837     const label masterEdgeID,
02838     const label masterFaceID,
02839     const label masterCellID,
02840     const label zoneID
02841 )
02842 {
02843     label cellI = cellMap_.size();
02844 
02845     if (masterPointID >= 0)
02846     {
02847         cellMap_.append(-1);
02848         cellFromPoint_.insert(cellI, masterPointID);
02849     }
02850     else if (masterEdgeID >= 0)
02851     {
02852         cellMap_.append(-1);
02853         cellFromEdge_.insert(cellI, masterEdgeID);
02854     }
02855     else if (masterFaceID >= 0)
02856     {
02857         cellMap_.append(-1);
02858         cellFromFace_.insert(cellI, masterFaceID);
02859     }
02860     else
02861     {
02862         cellMap_.append(masterCellID);
02863     }
02864     reverseCellMap_.append(cellI);
02865     cellZone_.append(zoneID);
02866 
02867     return cellI;
02868 }
02869 
02870 
02871 void Foam::polyTopoChange::modifyCell
02872 (
02873     const label cellI,
02874     const label zoneID
02875 )
02876 {
02877     cellZone_[cellI] = zoneID;
02878 }
02879 
02880 
02881 void Foam::polyTopoChange::removeCell(const label cellI, const label mergeCellI)
02882 {
02883     if (cellI < 0 || cellI >= cellMap_.size())
02884     {
02885         FatalErrorIn("polyTopoChange::removeCell(const label, const label)")
02886             << "illegal cell label " << cellI << endl
02887             << "Valid cell labels are 0 .. " << cellMap_.size()-1
02888             << abort(FatalError);
02889     }
02890 
02891     if (strict_ && cellMap_[cellI] == -2)
02892     {
02893         FatalErrorIn("polyTopoChange::removeCell(const label, const label)")
02894             << "cell " << cellI
02895             << " already marked for removal"
02896             << abort(FatalError);
02897     }
02898 
02899     cellMap_[cellI] = -2;
02900     if (mergeCellI >= 0)
02901     {
02902         reverseCellMap_[cellI] = -mergeCellI-2;
02903     }
02904     else
02905     {
02906         reverseCellMap_[cellI] = -1;
02907     }
02908     cellFromPoint_.erase(cellI);
02909     cellFromEdge_.erase(cellI);
02910     cellFromFace_.erase(cellI);
02911     cellZone_[cellI] = -1;
02912 }
02913 
02914 
02915 Foam::autoPtr<Foam::mapPolyMesh> Foam::polyTopoChange::changeMesh
02916 (
02917     polyMesh& mesh,
02918     const bool inflate,
02919     const bool syncParallel,
02920     const bool orderCells,
02921     const bool orderPoints
02922 )
02923 {
02924     if (debug)
02925     {
02926         Pout<< "polyTopoChange::changeMesh"
02927             << "(polyMesh&, const bool, const bool, const bool, const bool)"
02928             << endl;
02929     }
02930 
02931     if (debug)
02932     {
02933         Pout<< "Old mesh:" << nl;
02934         writeMeshStats(mesh, Pout);
02935     }
02936 
02937     // new mesh points
02938     pointField newPoints;
02939     // number of internal points
02940     label nInternalPoints;
02941     // patch slicing
02942     labelList patchSizes;
02943     labelList patchStarts;
02944     // inflate maps
02945     List<objectMap> pointsFromPoints;
02946     List<objectMap> facesFromPoints;
02947     List<objectMap> facesFromEdges;
02948     List<objectMap> facesFromFaces;
02949     List<objectMap> cellsFromPoints;
02950     List<objectMap> cellsFromEdges;
02951     List<objectMap> cellsFromFaces;
02952     List<objectMap> cellsFromCells;
02953     // old mesh info
02954     List<Map<label> > oldPatchMeshPointMaps;
02955     labelList oldPatchNMeshPoints;
02956     labelList oldPatchStarts;
02957     List<Map<label> > oldFaceZoneMeshPointMaps;
02958 
02959     // Compact, reorder patch faces and calculate mesh/patch maps.
02960     compactAndReorder
02961     (
02962         mesh,
02963         syncParallel,
02964         orderCells,
02965         orderPoints,
02966 
02967         nInternalPoints,
02968         newPoints,
02969         patchSizes,
02970         patchStarts,
02971         pointsFromPoints,
02972         facesFromPoints,
02973         facesFromEdges,
02974         facesFromFaces,
02975         cellsFromPoints,
02976         cellsFromEdges,
02977         cellsFromFaces,
02978         cellsFromCells,
02979         oldPatchMeshPointMaps,
02980         oldPatchNMeshPoints,
02981         oldPatchStarts,
02982         oldFaceZoneMeshPointMaps
02983     );
02984 
02985     const label nOldPoints(mesh.nPoints());
02986     const label nOldFaces(mesh.nFaces());
02987     const label nOldCells(mesh.nCells());
02988 
02989 
02990     // Change the mesh
02991     // ~~~~~~~~~~~~~~~
02992     // This will invalidate any addressing so better make sure you have
02993     // all the information you need!!!
02994 
02995     if (inflate)
02996     {
02997         // Keep (renumbered) mesh points, store new points in map for inflation
02998         // (appended points (i.e. from nowhere) get value zero)
02999         pointField renumberedMeshPoints(newPoints.size());
03000 
03001         forAll(pointMap_, newPointI)
03002         {
03003             label oldPointI = pointMap_[newPointI];
03004 
03005             if (oldPointI >= 0)
03006             {
03007                 renumberedMeshPoints[newPointI] = mesh.points()[oldPointI];
03008             }
03009             else
03010             {
03011                 renumberedMeshPoints[newPointI] = vector::zero;
03012             }
03013         }
03014 
03015         mesh.resetPrimitives
03016         (
03017             xferMove(renumberedMeshPoints),
03018             faces_.xfer(),
03019             faceOwner_.xfer(),
03020             faceNeighbour_.xfer(),
03021             patchSizes,
03022             patchStarts,
03023             syncParallel
03024         );
03025 
03026         mesh.changing(true);
03027     }
03028     else
03029     {
03030         // Set new points.
03031         mesh.resetPrimitives
03032         (
03033             xferMove(newPoints),
03034             faces_.xfer(),
03035             faceOwner_.xfer(),
03036             faceNeighbour_.xfer(),
03037             patchSizes,
03038             patchStarts,
03039             syncParallel
03040         );
03041         mesh.changing(true);
03042     }
03043 
03044     // Clear out primitives
03045     {
03046         retiredPoints_.clearStorage();
03047         region_.clearStorage();
03048     }
03049 
03050 
03051     if (debug)
03052     {
03053         // Some stats on changes
03054         label nAdd, nInflate, nMerge, nRemove;
03055         countMap(pointMap_, reversePointMap_, nAdd, nInflate, nMerge, nRemove);
03056         Pout<< "Points:"
03057             << "  added(from point):" << nAdd
03058             << "  added(from nothing):" << nInflate
03059             << "  merged(into other point):" << nMerge
03060             << "  removed:" << nRemove
03061             << nl;
03062 
03063         countMap(faceMap_, reverseFaceMap_, nAdd, nInflate, nMerge, nRemove);
03064         Pout<< "Faces:"
03065             << "  added(from face):" << nAdd
03066             << "  added(inflated):" << nInflate
03067             << "  merged(into other face):" << nMerge
03068             << "  removed:" << nRemove
03069             << nl;
03070 
03071         countMap(cellMap_, reverseCellMap_, nAdd, nInflate, nMerge, nRemove);
03072         Pout<< "Cells:"
03073             << "  added(from cell):" << nAdd
03074             << "  added(inflated):" << nInflate
03075             << "  merged(into other cell):" << nMerge
03076             << "  removed:" << nRemove
03077             << nl
03078             << endl;
03079     }
03080 
03081     if (debug)
03082     {
03083         Pout<< "New mesh:" << nl;
03084         writeMeshStats(mesh, Pout);
03085     }
03086 
03087 
03088     // Zones
03089     // ~~~~~
03090 
03091     // Inverse of point/face/cell zone addressing.
03092     // For every preserved point/face/cells in zone give the old position.
03093     // For added points, the index is set to -1
03094     labelListList pointZoneMap(mesh.pointZones().size());
03095     labelListList faceZoneFaceMap(mesh.faceZones().size());
03096     labelListList cellZoneMap(mesh.cellZones().size());
03097 
03098     resetZones(mesh, mesh, pointZoneMap, faceZoneFaceMap, cellZoneMap);
03099 
03100     // Clear zone info
03101     {
03102         pointZone_.clearStorage();
03103         faceZone_.clearStorage();
03104         faceZoneFlip_.clearStorage();
03105         cellZone_.clearStorage();
03106     }
03107 
03108 
03109     // Patch point renumbering
03110     // For every preserved point on a patch give the old position.
03111     // For added points, the index is set to -1
03112     labelListList patchPointMap(mesh.boundaryMesh().size());
03113     calcPatchPointMap
03114     (
03115         oldPatchMeshPointMaps,
03116         mesh.boundaryMesh(),
03117         patchPointMap
03118     );
03119 
03120     // Create the face zone mesh point renumbering
03121     labelListList faceZonePointMap(mesh.faceZones().size());
03122     calcFaceZonePointMap(mesh, oldFaceZoneMeshPointMaps, faceZonePointMap);
03123 
03124     labelHashSet flipFaceFluxSet(getSetIndices(flipFaceFlux_));
03125 
03126     return autoPtr<mapPolyMesh>
03127     (
03128         new mapPolyMesh
03129         (
03130             mesh,
03131             nOldPoints,
03132             nOldFaces,
03133             nOldCells,
03134 
03135             pointMap_,
03136             pointsFromPoints,
03137 
03138             faceMap_,
03139             facesFromPoints,
03140             facesFromEdges,
03141             facesFromFaces,
03142 
03143             cellMap_,
03144             cellsFromPoints,
03145             cellsFromEdges,
03146             cellsFromFaces,
03147             cellsFromCells,
03148 
03149             reversePointMap_,
03150             reverseFaceMap_,
03151             reverseCellMap_,
03152 
03153             flipFaceFluxSet,
03154 
03155             patchPointMap,
03156 
03157             pointZoneMap,
03158 
03159             faceZonePointMap,
03160             faceZoneFaceMap,
03161             cellZoneMap,
03162 
03163             newPoints,          // if empty signals no inflation.
03164             oldPatchStarts,
03165             oldPatchNMeshPoints,
03166             true                // steal storage.
03167         )
03168     );
03169 
03170     // At this point all member DynamicList (pointMap_, cellMap_ etc.) will
03171     // be invalid.
03172 }
03173 
03174 
03175 Foam::autoPtr<Foam::mapPolyMesh> Foam::polyTopoChange::makeMesh
03176 (
03177     autoPtr<fvMesh>& newMeshPtr,
03178     const IOobject& io,
03179     const fvMesh& mesh,
03180     const bool syncParallel,
03181     const bool orderCells,
03182     const bool orderPoints
03183 )
03184 {
03185     if (debug)
03186     {
03187         Pout<< "polyTopoChange::changeMesh"
03188             << "(autoPtr<fvMesh>&, const IOobject&, const fvMesh&"
03189             << ", const bool, const bool, const bool)"
03190             << endl;
03191     }
03192 
03193     if (debug)
03194     {
03195         Pout<< "Old mesh:" << nl;
03196         writeMeshStats(mesh, Pout);
03197     }
03198 
03199     // new mesh points
03200     pointField newPoints;
03201     // number of internal points
03202     label nInternalPoints;
03203     // patch slicing
03204     labelList patchSizes;
03205     labelList patchStarts;
03206     // inflate maps
03207     List<objectMap> pointsFromPoints;
03208     List<objectMap> facesFromPoints;
03209     List<objectMap> facesFromEdges;
03210     List<objectMap> facesFromFaces;
03211     List<objectMap> cellsFromPoints;
03212     List<objectMap> cellsFromEdges;
03213     List<objectMap> cellsFromFaces;
03214     List<objectMap> cellsFromCells;
03215 
03216     // old mesh info
03217     List<Map<label> > oldPatchMeshPointMaps;
03218     labelList oldPatchNMeshPoints;
03219     labelList oldPatchStarts;
03220     List<Map<label> > oldFaceZoneMeshPointMaps;
03221 
03222     // Compact, reorder patch faces and calculate mesh/patch maps.
03223     compactAndReorder
03224     (
03225         mesh,
03226         syncParallel,
03227         orderCells,
03228         orderPoints,
03229 
03230         nInternalPoints,
03231         newPoints,
03232         patchSizes,
03233         patchStarts,
03234         pointsFromPoints,
03235         facesFromPoints,
03236         facesFromEdges,
03237         facesFromFaces,
03238         cellsFromPoints,
03239         cellsFromEdges,
03240         cellsFromFaces,
03241         cellsFromCells,
03242         oldPatchMeshPointMaps,
03243         oldPatchNMeshPoints,
03244         oldPatchStarts,
03245         oldFaceZoneMeshPointMaps
03246     );
03247 
03248     const label nOldPoints(mesh.nPoints());
03249     const label nOldFaces(mesh.nFaces());
03250     const label nOldCells(mesh.nCells());
03251 
03252 
03253     // Create the mesh
03254     // ~~~~~~~~~~~~~~~
03255 
03256     newMeshPtr.reset
03257     (
03258         new fvMesh
03259         (
03260             io,
03261             xferMove(newPoints),
03262             faces_.xfer(),
03263             faceOwner_.xfer(),
03264             faceNeighbour_.xfer()
03265         )
03266     );
03267     fvMesh& newMesh = newMeshPtr();
03268 
03269     // Clear out primitives
03270     {
03271         retiredPoints_.clearStorage();
03272         region_.clearStorage();
03273     }
03274 
03275 
03276     if (debug)
03277     {
03278         // Some stats on changes
03279         label nAdd, nInflate, nMerge, nRemove;
03280         countMap(pointMap_, reversePointMap_, nAdd, nInflate, nMerge, nRemove);
03281         Pout<< "Points:"
03282             << "  added(from point):" << nAdd
03283             << "  added(from nothing):" << nInflate
03284             << "  merged(into other point):" << nMerge
03285             << "  removed:" << nRemove
03286             << nl;
03287 
03288         countMap(faceMap_, reverseFaceMap_, nAdd, nInflate, nMerge, nRemove);
03289         Pout<< "Faces:"
03290             << "  added(from face):" << nAdd
03291             << "  added(inflated):" << nInflate
03292             << "  merged(into other face):" << nMerge
03293             << "  removed:" << nRemove
03294             << nl;
03295 
03296         countMap(cellMap_, reverseCellMap_, nAdd, nInflate, nMerge, nRemove);
03297         Pout<< "Cells:"
03298             << "  added(from cell):" << nAdd
03299             << "  added(inflated):" << nInflate
03300             << "  merged(into other cell):" << nMerge
03301             << "  removed:" << nRemove
03302             << nl
03303             << endl;
03304     }
03305 
03306 
03307     {
03308         const polyBoundaryMesh& oldPatches = mesh.boundaryMesh();
03309 
03310         List<polyPatch*> newBoundary(oldPatches.size());
03311 
03312         forAll(oldPatches, patchI)
03313         {
03314             newBoundary[patchI] = oldPatches[patchI].clone
03315             (
03316                 newMesh.boundaryMesh(),
03317                 patchI,
03318                 patchSizes[patchI],
03319                 patchStarts[patchI]
03320             ).ptr();
03321         }
03322         newMesh.addFvPatches(newBoundary);
03323     }
03324 
03325 
03326     // Zones
03327     // ~~~~~
03328 
03329     // Start off from empty zones.
03330     const pointZoneMesh& oldPointZones = mesh.pointZones();
03331     List<pointZone*> pZonePtrs(oldPointZones.size());
03332     {
03333         forAll(oldPointZones, i)
03334         {
03335             pZonePtrs[i] = new pointZone
03336             (
03337                 oldPointZones[i].name(),
03338                 labelList(0),
03339                 i,
03340                 newMesh.pointZones()
03341             );
03342         }
03343     }
03344 
03345     const faceZoneMesh& oldFaceZones = mesh.faceZones();
03346     List<faceZone*> fZonePtrs(oldFaceZones.size());
03347     {
03348         forAll(oldFaceZones, i)
03349         {
03350             fZonePtrs[i] = new faceZone
03351             (
03352                 oldFaceZones[i].name(),
03353                 labelList(0),
03354                 boolList(0),
03355                 i,
03356                 newMesh.faceZones()
03357             );
03358         }
03359     }
03360 
03361     const cellZoneMesh& oldCellZones = mesh.cellZones();
03362     List<cellZone*> cZonePtrs(oldCellZones.size());
03363     {
03364         forAll(oldCellZones, i)
03365         {
03366             cZonePtrs[i] = new cellZone
03367             (
03368                 oldCellZones[i].name(),
03369                 labelList(0),
03370                 i,
03371                 newMesh.cellZones()
03372             );
03373         }
03374     }
03375 
03376     newMesh.addZones(pZonePtrs, fZonePtrs, cZonePtrs);
03377 
03378     // Inverse of point/face/cell zone addressing.
03379     // For every preserved point/face/cells in zone give the old position.
03380     // For added points, the index is set to -1
03381     labelListList pointZoneMap(mesh.pointZones().size());
03382     labelListList faceZoneFaceMap(mesh.faceZones().size());
03383     labelListList cellZoneMap(mesh.cellZones().size());
03384 
03385     resetZones(mesh, newMesh, pointZoneMap, faceZoneFaceMap, cellZoneMap);
03386 
03387     // Clear zone info
03388     {
03389         pointZone_.clearStorage();
03390         faceZone_.clearStorage();
03391         faceZoneFlip_.clearStorage();
03392         cellZone_.clearStorage();
03393     }
03394 
03395     // Patch point renumbering
03396     // For every preserved point on a patch give the old position.
03397     // For added points, the index is set to -1
03398     labelListList patchPointMap(newMesh.boundaryMesh().size());
03399     calcPatchPointMap
03400     (
03401         oldPatchMeshPointMaps,
03402         newMesh.boundaryMesh(),
03403         patchPointMap
03404     );
03405 
03406     // Create the face zone mesh point renumbering
03407     labelListList faceZonePointMap(newMesh.faceZones().size());
03408     calcFaceZonePointMap(newMesh, oldFaceZoneMeshPointMaps, faceZonePointMap);
03409 
03410     if (debug)
03411     {
03412         Pout<< "New mesh:" << nl;
03413         writeMeshStats(mesh, Pout);
03414     }
03415 
03416     labelHashSet flipFaceFluxSet(getSetIndices(flipFaceFlux_));
03417 
03418     return autoPtr<mapPolyMesh>
03419     (
03420         new mapPolyMesh
03421         (
03422             newMesh,
03423             nOldPoints,
03424             nOldFaces,
03425             nOldCells,
03426 
03427             pointMap_,
03428             pointsFromPoints,
03429 
03430             faceMap_,
03431             facesFromPoints,
03432             facesFromEdges,
03433             facesFromFaces,
03434 
03435             cellMap_,
03436             cellsFromPoints,
03437             cellsFromEdges,
03438             cellsFromFaces,
03439             cellsFromCells,
03440 
03441             reversePointMap_,
03442             reverseFaceMap_,
03443             reverseCellMap_,
03444 
03445             flipFaceFluxSet,
03446 
03447             patchPointMap,
03448 
03449             pointZoneMap,
03450 
03451             faceZonePointMap,
03452             faceZoneFaceMap,
03453             cellZoneMap,
03454 
03455             newPoints,          // if empty signals no inflation.
03456             oldPatchStarts,
03457             oldPatchNMeshPoints,
03458             true                // steal storage.
03459         )
03460     );
03461 
03462     // At this point all member DynamicList (pointMap_, cellMap_ etc.) will
03463     // be invalid.
03464 }
03465 
03466 
03467 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd