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

refinementHistory.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 "DynamicList.H"
00028 #include "refinementHistory.H"
00029 #include "ListOps.H"
00030 #include "mapPolyMesh.H"
00031 #include "mapDistributePolyMesh.H"
00032 #include "polyMesh.H"
00033 
00034 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00035 
00036 namespace Foam
00037 {
00038 
00039 defineTypeNameAndDebug(refinementHistory, 0);
00040 
00041 }
00042 
00043 
00044 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00045 
00046 void Foam::refinementHistory::writeEntry
00047 (
00048     const List<splitCell8>& splitCells,
00049     const splitCell8& split
00050 )
00051 {
00052     // Write me:
00053     if (split.addedCellsPtr_.valid())
00054     {
00055         Pout<< "parent:" << split.parent_
00056             << " subCells:" << split.addedCellsPtr_()
00057             << endl;
00058     }
00059     else
00060     {
00061         Pout<< "parent:" << split.parent_
00062             << " no subcells"
00063             << endl;
00064     }
00065 
00066     if (split.parent_ >= 0)
00067     {
00068         Pout<< "parent data:" << endl;
00069         // Write my parent
00070         string oldPrefix = Pout.prefix();
00071         Pout.prefix() = "  " + oldPrefix;
00072         writeEntry(splitCells, splitCells[split.parent_]);
00073         Pout.prefix() = oldPrefix;
00074     }
00075 }
00076 
00077 
00078 void Foam::refinementHistory::writeDebug
00079 (
00080     const labelList& visibleCells,
00081     const List<splitCell8>& splitCells
00082 )
00083 {
00084     string oldPrefix = Pout.prefix();
00085     Pout.prefix() = "";
00086 
00087     forAll(visibleCells, cellI)
00088     {
00089         label index = visibleCells[cellI];
00090 
00091         if (index >= 0)
00092         {
00093             Pout<< "Cell from refinement:" << cellI << " index:" << index
00094                 << endl;
00095 
00096             string oldPrefix = Pout.prefix();
00097             Pout.prefix() = "  " + oldPrefix;
00098             writeEntry(splitCells, splitCells[index]);
00099             Pout.prefix() = oldPrefix;
00100         }
00101         else
00102         {
00103             Pout<< "Unrefined cell:" << cellI << " index:" << index << endl;
00104         }
00105     }
00106     Pout.prefix() = oldPrefix;
00107 }
00108 
00109 
00110 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00111 
00112 //- Construct null
00113 Foam::refinementHistory::splitCell8::splitCell8()
00114 :
00115     parent_(-1),
00116     addedCellsPtr_(NULL)
00117 {}
00118 
00119 
00120 //- Construct as child element of parent
00121 Foam::refinementHistory::splitCell8::splitCell8(const label parent)
00122 :
00123     parent_(parent),
00124     addedCellsPtr_(NULL)
00125 {}
00126 
00127 
00128 //- Construct from Istream
00129 Foam::refinementHistory::splitCell8::splitCell8(Istream& is)
00130 {
00131     is >> *this;
00132 }
00133 
00134 
00135 //- Construct as (deep) copy.
00136 Foam::refinementHistory::splitCell8::splitCell8(const splitCell8& sc)
00137 :
00138     parent_(sc.parent_),
00139     addedCellsPtr_
00140     (
00141         sc.addedCellsPtr_.valid()
00142       ? new FixedList<label, 8>(sc.addedCellsPtr_())
00143       : NULL
00144     )
00145 {}
00146 
00147 
00148 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00149 
00150 Foam::Istream& Foam::operator>>(Istream& is, refinementHistory::splitCell8& sc)
00151 {
00152     labelList addedCells;
00153 
00154     is >> sc.parent_ >> addedCells;
00155 
00156     if (addedCells.size())
00157     {
00158         sc.addedCellsPtr_.reset(new FixedList<label, 8>(addedCells));
00159     }
00160     else
00161     {
00162         sc.addedCellsPtr_.reset(NULL);
00163     }
00164 
00165     return is;
00166 }
00167 
00168 
00169 Foam::Ostream& Foam::operator<<
00170 (
00171     Ostream& os,
00172     const refinementHistory::splitCell8& sc
00173 )
00174 {
00175     // Output as labelList so we can have 0 sized lists. Alternative is to
00176     // output as fixedlist with e.g. -1 elements and check for this upon
00177     // reading. However would cause much more data to be transferred.
00178 
00179     if (sc.addedCellsPtr_.valid())
00180     {
00181         return os
00182             << sc.parent_
00183             << token::SPACE
00184             << labelList(sc.addedCellsPtr_());
00185     }
00186     else
00187     {
00188         return os << sc.parent_ << token::SPACE << labelList(0);
00189     }
00190 }
00191 
00192 
00193 void Foam::refinementHistory::checkIndices() const
00194 {
00195     // Check indices.
00196     forAll(visibleCells_, i)
00197     {
00198         if (visibleCells_[i] < 0 && visibleCells_[i] >= splitCells_.size())
00199         {
00200             FatalErrorIn("refinementHistory::checkIndices() const")
00201                 << "Illegal entry " << visibleCells_[i]
00202                 << " in visibleCells at location" << i << nl
00203                 << "It points outside the range of splitCells : 0.."
00204                 << splitCells_.size()-1
00205                 << abort(FatalError);
00206         }
00207     }
00208 }
00209 
00210 
00211 Foam::label Foam::refinementHistory::allocateSplitCell
00212 (
00213     const label parent,
00214     const label i
00215 )
00216 {
00217     label index = -1;
00218 
00219     if (freeSplitCells_.size())
00220     {
00221         index = freeSplitCells_.remove();
00222 
00223         splitCells_[index] = splitCell8(parent);
00224     }
00225     else
00226     {
00227         index = splitCells_.size();
00228 
00229         splitCells_.append(splitCell8(parent));
00230     }
00231 
00232 
00233     // Update the parent field
00234     if (parent >= 0)
00235     {
00236         splitCell8& parentSplit = splitCells_[parent];
00237 
00238         if (parentSplit.addedCellsPtr_.empty())
00239         {
00240             // Allocate storage on parent for the 8 subcells.
00241             parentSplit.addedCellsPtr_.reset(new FixedList<label, 8>(-1));
00242         }
00243 
00244 
00245         // Store me on my parent
00246         FixedList<label, 8>& parentSplits = parentSplit.addedCellsPtr_();
00247 
00248         parentSplits[i] = index;
00249     }
00250 
00251     return index;
00252 }
00253 
00254 
00255 void Foam::refinementHistory::freeSplitCell(const label index)
00256 {
00257     splitCell8& split = splitCells_[index];
00258 
00259     // Make sure parent does not point to me anymore.
00260     if (split.parent_ >= 0)
00261     {
00262         autoPtr<FixedList<label, 8> >& subCellsPtr =
00263             splitCells_[split.parent_].addedCellsPtr_;
00264 
00265         if (subCellsPtr.valid())
00266         {
00267             FixedList<label, 8>& subCells = subCellsPtr();
00268 
00269             label myPos = findIndex(subCells, index);
00270 
00271             if (myPos == -1)
00272             {
00273                 FatalErrorIn("refinementHistory::freeSplitCell")
00274                     << "Problem: cannot find myself in"
00275                     << " parents' children" << abort(FatalError);
00276             }
00277             else
00278             {
00279                 subCells[myPos] = -1;
00280             }
00281         }
00282     }
00283 
00284     // Mark splitCell as free
00285     split.parent_ = -2;
00286 
00287     // Add to cache of free splitCells
00288     freeSplitCells_.append(index);
00289 }
00290 
00291 
00292 // Mark entry in splitCells. Recursively mark its parent and subs.
00293 void Foam::refinementHistory::markSplit
00294 (
00295     const label index,
00296     labelList& oldToNew,
00297     DynamicList<splitCell8>& newSplitCells
00298 ) const
00299 {
00300     if (oldToNew[index] == -1)
00301     {
00302         // Not yet compacted.
00303 
00304         const splitCell8& split = splitCells_[index];
00305 
00306         oldToNew[index] = newSplitCells.size();
00307         newSplitCells.append(split);
00308 
00309         if (split.parent_ >= 0)
00310         {
00311             markSplit(split.parent_, oldToNew, newSplitCells);
00312         }
00313         if (split.addedCellsPtr_.valid())
00314         {
00315             const FixedList<label, 8>& splits = split.addedCellsPtr_();
00316 
00317             forAll(splits, i)
00318             {
00319                 if (splits[i] >= 0)
00320                 {
00321                     markSplit(splits[i], oldToNew, newSplitCells);
00322                 }
00323             }
00324         }
00325     }
00326 }
00327 
00328 
00329 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00330 
00331 Foam::refinementHistory::refinementHistory(const IOobject& io)
00332 :
00333     regIOobject(io)
00334 {
00335     if
00336     (
00337         io.readOpt() == IOobject::MUST_READ
00338      || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
00339     )
00340     {
00341         readStream(typeName) >> *this;
00342         close();
00343     }
00344 
00345     if (debug)
00346     {
00347         Pout<< "refinementHistory::refinementHistory :"
00348             << " constructed history from IOobject :"
00349             << " splitCells:" << splitCells_.size()
00350             << " visibleCells:" << visibleCells_.size()
00351             << endl;
00352     }
00353 }
00354 
00355 
00356 //- Read or construct
00357 Foam::refinementHistory::refinementHistory
00358 (
00359     const IOobject& io,
00360     const List<splitCell8>& splitCells,
00361     const labelList& visibleCells
00362 )
00363 :
00364     regIOobject(io),
00365     splitCells_(splitCells),
00366     freeSplitCells_(0),
00367     visibleCells_(visibleCells)
00368 {
00369     if
00370     (
00371         io.readOpt() == IOobject::MUST_READ
00372      || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
00373     )
00374     {
00375         readStream(typeName) >> *this;
00376         close();
00377     }
00378 
00379     // Check indices.
00380     checkIndices();
00381 
00382     if (debug)
00383     {
00384         Pout<< "refinementHistory::refinementHistory :"
00385             << " constructed history from IOobject or components :"
00386             << " splitCells:" << splitCells_.size()
00387             << " visibleCells:" << visibleCells_.size()
00388             << endl;
00389     }
00390 }
00391 
00392 
00393 // Construct from initial number of cells (all visible)
00394 Foam::refinementHistory::refinementHistory
00395 (
00396     const IOobject& io,
00397     const label nCells
00398 )
00399 :
00400     regIOobject(io),
00401     freeSplitCells_(0)
00402 {
00403     if
00404     (
00405         io.readOpt() == IOobject::MUST_READ
00406      || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
00407     )
00408     {
00409         readStream(typeName) >> *this;
00410         close();
00411     }
00412     else
00413     {
00414         visibleCells_.setSize(nCells);
00415         splitCells_.setCapacity(nCells);
00416 
00417         for (label cellI = 0; cellI < nCells; cellI++)
00418         {
00419             visibleCells_[cellI] = cellI;
00420             splitCells_.append(splitCell8());
00421         }
00422     }
00423 
00424     // Check indices.
00425     checkIndices();
00426 
00427     if (debug)
00428     {
00429         Pout<< "refinementHistory::refinementHistory :"
00430             << " constructed history from IOobject or initial size :"
00431             << " splitCells:" << splitCells_.size()
00432             << " visibleCells:" << visibleCells_.size()
00433             << endl;
00434     }
00435 }
00436 
00437 
00438 // Construct as copy
00439 Foam::refinementHistory::refinementHistory
00440 (
00441     const IOobject& io,
00442     const refinementHistory& rh
00443 )
00444 :
00445     regIOobject(io),
00446     splitCells_(rh.splitCells()),
00447     freeSplitCells_(rh.freeSplitCells()),
00448     visibleCells_(rh.visibleCells())
00449 {
00450     if (debug)
00451     {
00452         Pout<< "refinementHistory::refinementHistory : constructed initial"
00453             << " history." << endl;
00454     }
00455 }
00456 
00457 
00458 // Construct from Istream
00459 Foam::refinementHistory::refinementHistory(const IOobject& io, Istream& is)
00460 :
00461     regIOobject(io),
00462     splitCells_(is),
00463     freeSplitCells_(0),
00464     visibleCells_(is)
00465 {
00466     // Check indices.
00467     checkIndices();
00468 
00469     if (debug)
00470     {
00471         Pout<< "refinementHistory::refinementHistory :"
00472             << " constructed history from Istream"
00473             << " splitCells:" << splitCells_.size()
00474             << " visibleCells:" << visibleCells_.size()
00475             << endl;
00476     }
00477 }
00478 
00479 
00480 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00481 
00482 void Foam::refinementHistory::resize(const label size)
00483 {
00484     label oldSize = visibleCells_.size();
00485 
00486     if (debug)
00487     {
00488         Pout<< "refinementHistory::resize from " << oldSize << " to " << size
00489             << " cells" << endl;
00490     }
00491 
00492     visibleCells_.setSize(size);
00493 
00494     // Set additional elements to -1.
00495     for (label i = oldSize; i < visibleCells_.size(); i++)
00496     {
00497         visibleCells_[i] = -1;
00498     }
00499 }
00500 
00501 
00502 void Foam::refinementHistory::updateMesh(const mapPolyMesh& map)
00503 {
00504     if (active())
00505     {
00506         const labelList& reverseCellMap = map.reverseCellMap();
00507 
00508         // Note that only the live cells need to be renumbered.
00509 
00510         labelList newVisibleCells(map.cellMap().size(), -1);
00511 
00512         forAll(visibleCells_, cellI)
00513         {
00514             if (visibleCells_[cellI] != -1)
00515             {
00516                 label index = visibleCells_[cellI];
00517 
00518                 // Check not already set
00519                 if (splitCells_[index].addedCellsPtr_.valid())
00520                 {
00521                     FatalErrorIn
00522                     (
00523                         "refinementHistory::updateMesh(const mapPolyMesh&)"
00524                     )   << "Problem" << abort(FatalError);
00525                 }
00526 
00527                 label newCellI = reverseCellMap[cellI];
00528 
00529                 if (newCellI >= 0)
00530                 {
00531                     newVisibleCells[newCellI] = index;
00532                 }
00533             }
00534         }
00535 
00536         if (debug)
00537         {
00538             Pout<< "refinementHistory::updateMesh : from "
00539                 << visibleCells_.size()
00540                 << " to " << newVisibleCells.size()
00541                 << " cells" << endl;
00542         }
00543 
00544         visibleCells_.transfer(newVisibleCells);
00545     }
00546 }
00547 
00548 
00549 // Update numbering for subsetting
00550 void Foam::refinementHistory::subset
00551 (
00552     const labelList& pointMap,
00553     const labelList& faceMap,
00554     const labelList& cellMap
00555 )
00556 {
00557     if (active())
00558     {
00559         labelList newVisibleCells(cellMap.size(), -1);
00560 
00561         forAll(newVisibleCells, cellI)
00562         {
00563             label oldCellI = cellMap[cellI];
00564 
00565             label index = visibleCells_[oldCellI];
00566 
00567             // Check that cell is live (so its parent has no refinement)
00568             if (index >= 0 && splitCells_[index].addedCellsPtr_.valid())
00569             {
00570                 FatalErrorIn
00571                 (
00572                     "refinementHistory::subset"
00573                     "(const labelList&, const labelList&, const labelList&)"
00574                 )   << "Problem" << abort(FatalError);
00575             }
00576 
00577             newVisibleCells[cellI] = index;
00578         }
00579 
00580         if (debug)
00581         {
00582             Pout<< "refinementHistory::updateMesh : from "
00583                 << visibleCells_.size()
00584                 << " to " << newVisibleCells.size()
00585                 << " cells" << endl;
00586         }
00587 
00588         visibleCells_.transfer(newVisibleCells);
00589     }
00590 }
00591 
00592 
00593 void Foam::refinementHistory::countProc
00594 (
00595     const label index,
00596     const label newProcNo,
00597     labelList& splitCellProc,
00598     labelList& splitCellNum
00599 ) const
00600 {
00601     if (splitCellProc[index] != newProcNo)
00602     {
00603         // Different destination processor from other cells using this
00604         // parent. Reset count.
00605         splitCellProc[index] = newProcNo;
00606         splitCellNum[index] = 1;
00607     }
00608     else
00609     {
00610         splitCellNum[index]++;
00611 
00612         // Increment parent if whole splitCell moves to same processor
00613         if (splitCellNum[index] == 8)
00614         {
00615             Pout<< "Moving " << splitCellNum[index]
00616                 << " cells originating from cell " << index
00617                 << " from processor " << Pstream::myProcNo()
00618                 << " to processor " << splitCellProc[index]
00619                 << endl;
00620 
00621             label parent = splitCells_[index].parent_;
00622 
00623             if (parent >= 0)
00624             {
00625                 string oldPrefix = Pout.prefix();
00626                 Pout.prefix() = "  " + oldPrefix;
00627 
00628                 countProc(parent, newProcNo, splitCellProc, splitCellNum);
00629 
00630                 Pout.prefix() = oldPrefix;
00631             }
00632         }
00633     }
00634 }
00635 
00636 
00637 void Foam::refinementHistory::distribute(const mapDistributePolyMesh& map)
00638 {
00639     if (!active())
00640     {
00641         FatalErrorIn
00642         (
00643             "refinementHistory::distribute(const mapDistributePolyMesh&)"
00644         )   << "Calling distribute on inactive history" << abort(FatalError);
00645     }
00646 
00647 
00648     if (!Pstream::parRun())
00649     {
00650         return;
00651     }
00652 
00653     // Remove unreferenced history.
00654     compact();
00655 
00656     Pout<< nl << "--BEFORE:" << endl;
00657     writeDebug();
00658     Pout<< "---------" << nl << endl;
00659 
00660 
00661     // Distribution is only partially functional.
00662     // If all 8 cells resulting from a single parent are sent across in one
00663     // go it will also send across that part of the refinement history.
00664     // If however e.g. first 1 and then the other 7 are sent across the
00665     // history will not be reconstructed.
00666 
00667     // Determine clusters. This is per every entry in splitCells_ (that is
00668     // a parent of some refinement) a label giving the processor it goes to
00669     // if all its children are going to the same processor.
00670 
00671     // Per visible cell the processor it goes to.
00672     labelList destination(visibleCells_.size());
00673 
00674     const labelListList& subCellMap = map.cellMap().subMap();
00675 
00676     forAll(subCellMap, procI)
00677     {
00678         const labelList& newToOld = subCellMap[procI];
00679 
00680         forAll(newToOld, i)
00681         {
00682             label oldCellI = newToOld[i];
00683 
00684             destination[oldCellI] = procI;
00685         }
00686     }
00687 
00688 //Pout<< "refinementHistory::distribute :"
00689 //    << " destination:" << destination << endl;
00690 
00691     // Per splitCell entry the processor it moves to
00692     labelList splitCellProc(splitCells_.size(), -1);
00693     // Per splitCell entry the number of live cells that move to that processor
00694     labelList splitCellNum(splitCells_.size(), 0);
00695 
00696     forAll(visibleCells_, cellI)
00697     {
00698         label index = visibleCells_[cellI];
00699 
00700         if (index >= 0)
00701         {
00702             countProc
00703             (
00704                 splitCells_[index].parent_,
00705                 destination[cellI],
00706                 splitCellProc,
00707                 splitCellNum
00708             );
00709         }
00710     }
00711 
00712 Pout<< "refinementHistory::distribute :"
00713     << " splitCellProc:" << splitCellProc << endl;
00714 
00715 Pout<< "refinementHistory::distribute :"
00716     << " splitCellNum:" << splitCellNum << endl;
00717 
00718 
00719     // Create subsetted refinement tree consisting of all parents that
00720     // move in their whole to other processor.
00721     for (label procI = 0; procI < Pstream::nProcs(); procI++)
00722     {
00723         Pout<< "-- Subetting for processor " << procI << endl;
00724 
00725         // From uncompacted to compacted splitCells.
00726         labelList oldToNew(splitCells_.size(), -1);
00727 
00728         // Compacted splitCells. Similar to subset routine below.
00729         DynamicList<splitCell8> newSplitCells(splitCells_.size());
00730 
00731         // Loop over all entries. Note: could recurse like countProc so only
00732         // visit used entries but is probably not worth it.
00733 
00734         forAll(splitCells_, index)
00735         {
00736 //            Pout<< "oldCell:" << index
00737 //                << " proc:" << splitCellProc[index]
00738 //                << " nCells:" << splitCellNum[index]
00739 //                << endl;
00740 
00741             if (splitCellProc[index] == procI && splitCellNum[index] == 8)
00742             {
00743                 // Entry moves in its whole to procI
00744                 oldToNew[index] = newSplitCells.size();
00745                 newSplitCells.append(splitCells_[index]);
00746 
00747                 Pout<< "Added oldCell " << index
00748                     << " info " << newSplitCells[newSplitCells.size()-1]
00749                     << " at position " << newSplitCells.size()-1
00750                     << endl;
00751             }
00752         }
00753 
00754         // Add live cells that are subsetted.
00755         forAll(visibleCells_, cellI)
00756         {
00757             label index = visibleCells_[cellI];
00758 
00759             if (index >= 0 && destination[cellI] == procI)
00760             {
00761                 label parent = splitCells_[index].parent_;
00762 
00763                 Pout<< "Adding refined cell " << cellI
00764                     << " since moves to "
00765                     << procI << " old parent:" << parent << endl;
00766 
00767                 // Create new splitCell with parent
00768                 oldToNew[index] = newSplitCells.size();
00769                 newSplitCells.append(splitCell8(parent));
00770             }
00771         }
00772 
00773         //forAll(oldToNew, index)
00774         //{
00775         //    Pout<< "old:" << index << " new:" << oldToNew[index]
00776         //        << endl;
00777         //}
00778 
00779         newSplitCells.shrink();
00780 
00781         // Renumber contents of newSplitCells
00782         forAll(newSplitCells, index)
00783         {
00784             splitCell8& split = newSplitCells[index];
00785 
00786             if (split.parent_ >= 0)
00787             {
00788                 split.parent_ = oldToNew[split.parent_];
00789             }
00790             if (split.addedCellsPtr_.valid())
00791             {
00792                 FixedList<label, 8>& splits = split.addedCellsPtr_();
00793 
00794                 forAll(splits, i)
00795                 {
00796                     if (splits[i] >= 0)
00797                     {
00798                         splits[i] = oldToNew[splits[i]];
00799                     }
00800                 }
00801             }
00802         }
00803 
00804 
00805         const labelList& subMap = subCellMap[procI];
00806 
00807         // New visible cells.
00808         labelList newVisibleCells(subMap.size(), -1);
00809 
00810         forAll(subMap, newCellI)
00811         {
00812             label oldCellI = subMap[newCellI];
00813 
00814             label oldIndex = visibleCells_[oldCellI];
00815 
00816             if (oldIndex >= 0)
00817             {
00818                 newVisibleCells[newCellI] = oldToNew[oldIndex];
00819             }
00820         }
00821 
00822         //Pout<< nl << "--Subset for domain:" << procI << endl;
00823         //writeDebug(newVisibleCells, newSplitCells);
00824         //Pout<< "---------" << nl << endl;
00825 
00826 
00827         // Send to neighbours
00828         OPstream toNbr(Pstream::blocking, procI);
00829         toNbr << newSplitCells << newVisibleCells;
00830     }
00831 
00832 
00833     // Receive from neighbours and merge
00834     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00835 
00836     // Remove all entries. Leave storage intact.
00837     splitCells_.clear();
00838 
00839     visibleCells_.setSize(map.mesh().nCells());
00840     visibleCells_ = -1;
00841 
00842     for (label procI = 0; procI < Pstream::nProcs(); procI++)
00843     {
00844         IPstream fromNbr(Pstream::blocking, procI);
00845         List<splitCell8> newSplitCells(fromNbr);
00846         labelList newVisibleCells(fromNbr);
00847 
00848         //Pout<< nl << "--Received from domain:" << procI << endl;
00849         //writeDebug(newVisibleCells, newSplitCells);
00850         //Pout<< "---------" << nl << endl;
00851 
00852 
00853         // newSplitCells contain indices only into newSplitCells so
00854         // renumbering can be done here.
00855         label offset = splitCells_.size();
00856 
00857         Pout<< "**Renumbering data from proc " << procI << " with offset "
00858             << offset << endl;
00859 
00860         forAll(newSplitCells, index)
00861         {
00862             splitCell8& split = newSplitCells[index];
00863 
00864             if (split.parent_ >= 0)
00865             {
00866                 split.parent_ += offset;
00867             }
00868             if (split.addedCellsPtr_.valid())
00869             {
00870                 FixedList<label, 8>& splits = split.addedCellsPtr_();
00871 
00872                 forAll(splits, i)
00873                 {
00874                     if (splits[i] >= 0)
00875                     {
00876                         splits[i] += offset;
00877                     }
00878                 }
00879             }
00880 
00881             splitCells_.append(split);
00882         }
00883 
00884 
00885         // Combine visibleCell.
00886         const labelList& constructMap = map.cellMap().constructMap()[procI];
00887 
00888         forAll(newVisibleCells, i)
00889         {
00890             visibleCells_[constructMap[i]] = newVisibleCells[i] + offset;
00891         }
00892     }
00893     splitCells_.shrink();
00894 
00895     Pout<< nl << "--AFTER:" << endl;
00896     writeDebug();
00897     Pout<< "---------" << nl << endl;
00898 }
00899 
00900 
00901 void Foam::refinementHistory::compact()
00902 {
00903     if (debug)
00904     {
00905         Pout<< "refinementHistory::compact() Entering with:"
00906             << " freeSplitCells_:" << freeSplitCells_.size()
00907             << " splitCells_:" << splitCells_.size()
00908             << " visibleCells_:" << visibleCells_.size()
00909             << endl;
00910 
00911         // Check all free splitCells are marked as such
00912         forAll(freeSplitCells_, i)
00913         {
00914             label index = freeSplitCells_[i];
00915 
00916             if (splitCells_[index].parent_ != -2)
00917             {
00918                 FatalErrorIn("refinementHistory::compact()")
00919                     << "Problem index:" << index
00920                     << abort(FatalError);
00921             }
00922         }
00923 
00924         // Check none of the visible cells are marked as free
00925         forAll(visibleCells_, cellI)
00926         {
00927             if
00928             (
00929                 visibleCells_[cellI] >= 0
00930              && splitCells_[visibleCells_[cellI]].parent_ == -2
00931             )
00932             {
00933                 FatalErrorIn("refinementHistory::compact()")
00934                     << "Problem : visible cell:" << cellI
00935                     << " is marked as being free." << abort(FatalError);
00936             }
00937         }
00938     }
00939 
00940     DynamicList<splitCell8> newSplitCells(splitCells_.size());
00941 
00942     // From uncompacted to compacted splitCells.
00943     labelList oldToNew(splitCells_.size(), -1);
00944 
00945     // Mark all used splitCell entries. These are either indexed by visibleCells
00946     // or indexed from other splitCell entries.
00947 
00948     // Mark from visibleCells
00949     forAll(visibleCells_, cellI)
00950     {
00951         label index = visibleCells_[cellI];
00952 
00953         if (index >= 0)
00954         {
00955             // Make sure we only mark visible indices if they either have a
00956             // parent or subsplits.
00957             if
00958             (
00959                 splitCells_[index].parent_ != -1
00960              || splitCells_[index].addedCellsPtr_.valid()
00961             )
00962             {
00963                 markSplit(index, oldToNew, newSplitCells);
00964             }
00965         }
00966     }
00967 
00968     // Mark from splitCells
00969     forAll(splitCells_, index)
00970     {
00971         if (splitCells_[index].parent_ == -2)
00972         {
00973             // freed cell.
00974         }
00975         else if
00976         (
00977             splitCells_[index].parent_ == -1
00978          && splitCells_[index].addedCellsPtr_.empty()
00979         )
00980         {
00981             // recombined cell. No need to keep since no parent and no subsplits
00982             // Note that gets marked if reachable from other index!
00983         }
00984         else
00985         {
00986             // Is used element.
00987             markSplit(index, oldToNew, newSplitCells);
00988         }
00989     }
00990 
00991 
00992     // Now oldToNew is fully complete and compacted elements are in
00993     // newSplitCells.
00994     // Renumber contents of newSplitCells and visibleCells.
00995     forAll(newSplitCells, index)
00996     {
00997         splitCell8& split = newSplitCells[index];
00998 
00999         if (split.parent_ >= 0)
01000         {
01001             split.parent_ = oldToNew[split.parent_];
01002         }
01003         if (split.addedCellsPtr_.valid())
01004         {
01005             FixedList<label, 8>& splits = split.addedCellsPtr_();
01006 
01007             forAll(splits, i)
01008             {
01009                 if (splits[i] >= 0)
01010                 {
01011                     splits[i] = oldToNew[splits[i]];
01012                 }
01013             }
01014         }
01015     }
01016 
01017 
01018     if (debug)
01019     {
01020         Pout<< "refinementHistory::compact : compacted splitCells from "
01021             << splitCells_.size() << " to " << newSplitCells.size() << endl;
01022     }
01023 
01024     splitCells_.transfer(newSplitCells);
01025     freeSplitCells_.clearStorage();
01026 
01027 
01028     if (debug)
01029     {
01030         Pout<< "refinementHistory::compact() NOW:"
01031             << " freeSplitCells_:" << freeSplitCells_.size()
01032             << " splitCells_:" << splitCells_.size()
01033             << " newSplitCells:" << newSplitCells.size()
01034             << " visibleCells_:" << visibleCells_.size()
01035             << endl;
01036     }
01037 
01038 
01039     // Adapt indices in visibleCells_
01040     forAll(visibleCells_, cellI)
01041     {
01042         label index = visibleCells_[cellI];
01043 
01044         if (index >= 0)
01045         {
01046             // Note that oldToNew can be -1 so it resets newVisibleCells.
01047             visibleCells_[cellI] = oldToNew[index];
01048         }
01049         else
01050         {
01051             // Keep -1 value.
01052         }
01053     }
01054 }
01055 
01056 
01057 void Foam::refinementHistory::writeDebug() const
01058 {
01059     writeDebug(visibleCells_, splitCells_);
01060 }
01061 
01062 
01063 void Foam::refinementHistory::storeSplit
01064 (
01065     const label cellI,
01066     const labelList& addedCells
01067 )
01068 {
01069     label parentIndex = -1;
01070 
01071     if (visibleCells_[cellI] != -1)
01072     {
01073         // Was already live. The current live cell becomes the
01074         // parent of the cells split off from it.
01075 
01076         parentIndex = visibleCells_[cellI];
01077 
01078         // It is no longer live (note that actually cellI gets alive
01079         // again below since is addedCells[0])
01080         visibleCells_[cellI] = -1;
01081     }
01082     else
01083     {
01084         // Create 0th level. -1 parent to denote this.
01085         parentIndex = allocateSplitCell(-1, -1);
01086     }
01087 
01088     // Create live entries for added cells that point to the
01089     // cell they were created from (parentIndex)
01090     forAll(addedCells, i)
01091     {
01092         label addedCellI = addedCells[i];
01093 
01094         // Create entries for the split off cells. All of them
01095         // are visible.
01096         visibleCells_[addedCellI] = allocateSplitCell(parentIndex, i);
01097     }
01098 }
01099 
01100 
01101 void Foam::refinementHistory::combineCells
01102 (
01103     const label masterCellI,
01104     const labelList& combinedCells
01105 )
01106 {
01107     // Save the parent structure
01108     label parentIndex = splitCells_[visibleCells_[masterCellI]].parent_;
01109 
01110     // Remove the information for the combined cells
01111     forAll(combinedCells, i)
01112     {
01113         label cellI = combinedCells[i];
01114 
01115         freeSplitCell(visibleCells_[cellI]);
01116         visibleCells_[cellI] = -1;
01117     }
01118 
01119     splitCell8& parentSplit = splitCells_[parentIndex];
01120     parentSplit.addedCellsPtr_.reset(NULL);
01121     visibleCells_[masterCellI] = parentIndex;
01122 }
01123 
01124 
01125 bool Foam::refinementHistory::readData(Istream& is)
01126 {
01127     is >> *this;
01128     return !is.bad();
01129 }
01130 
01131 
01132 bool Foam::refinementHistory::writeData(Ostream& os) const
01133 {
01134     os << *this;
01135 
01136     return os.good();
01137 }
01138 
01139 
01140 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
01141 
01142 Foam::Istream& Foam::operator>>(Istream& is, refinementHistory& rh)
01143 {
01144     rh.freeSplitCells_.clearStorage();
01145 
01146     is >> rh.splitCells_ >> rh.visibleCells_;
01147 
01148     // Check indices.
01149     rh.checkIndices();
01150 
01151     return is;
01152 }
01153 
01154 
01155 Foam::Ostream& Foam::operator<<(Ostream& os, const refinementHistory& rh)
01156 {
01157     const_cast<refinementHistory&>(rh).compact();
01158 
01159     return os   << "// splitCells" << nl
01160                 << rh.splitCells_ << nl
01161                 << "// visibleCells" << nl
01162                 << rh.visibleCells_;
01163 }
01164 
01165 
01166 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd