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

multiDirRefinement.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 "multiDirRefinement.H"
00028 #include "polyMesh.H"
00029 #include "polyTopoChanger.H"
00030 #include "Time.H"
00031 #include "undoableMeshCutter.H"
00032 #include "hexCellLooper.H"
00033 #include "geomCellLooper.H"
00034 #include "topoSet.H"
00035 #include "directions.H"
00036 #include "hexRef8.H"
00037 #include "mapPolyMesh.H"
00038 #include "polyTopoChange.H"
00039 #include "ListOps.H"
00040 #include "cellModeller.H"
00041 
00042 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00043 
00044 namespace Foam
00045 {
00046     defineTypeNameAndDebug(multiDirRefinement, 0);
00047 }
00048 
00049 
00050 // * * * * * * * * * * * * * Private Statc Functions * * * * * * * * * * * * //
00051 
00052 // Update refCells pattern for split cells. Note refCells is current
00053 // list of cells to refine (these should all have been refined)
00054 void Foam::multiDirRefinement::addCells
00055 (
00056     const Map<label>& splitMap,
00057     List<refineCell>& refCells
00058 )
00059 {
00060     label newRefI = refCells.size();
00061 
00062     label oldSize = refCells.size();
00063 
00064     refCells.setSize(newRefI + splitMap.size());
00065 
00066     for (label refI = 0; refI < oldSize; refI++)
00067     {
00068         const refineCell& refCell = refCells[refI];
00069 
00070         Map<label>::const_iterator iter = splitMap.find(refCell.cellNo());
00071 
00072         if (iter == splitMap.end())
00073         {
00074             FatalErrorIn
00075             (
00076                 "multiDirRefinement::addCells(const Map<label>&"
00077                 ", List<refineCell>&)"
00078             )   << "Problem : cannot find added cell for cell "
00079                 << refCell.cellNo() << abort(FatalError);
00080         }
00081 
00082         refCells[newRefI++] = refineCell(iter(), refCell.direction());
00083     }
00084 }
00085 
00086 
00087 // Update vectorField for all the new cells. Takes over value of
00088 // original cell.
00089 void Foam::multiDirRefinement::update
00090 (
00091     const Map<label>& splitMap,
00092     vectorField& field
00093 )
00094 {
00095     field.setSize(field.size() + splitMap.size());
00096 
00097     for
00098     (
00099         Map<label>::const_iterator iter = splitMap.begin();
00100         iter != splitMap.end();
00101         ++iter
00102     )
00103     {
00104         field[iter()] = field[iter.key()];
00105     }
00106 }
00107 
00108 
00109 // Add added cells to labelList
00110 void Foam::multiDirRefinement::addCells
00111 (
00112     const Map<label>& splitMap,
00113     labelList& labels
00114 )
00115 {
00116     label newCellI = labels.size();
00117 
00118     labels.setSize(labels.size() + splitMap.size());
00119 
00120     for
00121     (
00122         Map<label>::const_iterator iter = splitMap.begin();
00123         iter != splitMap.end();
00124         ++iter
00125     )
00126     {
00127         labels[newCellI++] = iter();
00128     }
00129 }
00130 
00131 
00132 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00133 
00134 void Foam::multiDirRefinement::addCells
00135 (
00136     const primitiveMesh& mesh,
00137     const Map<label>& splitMap
00138 )
00139 {
00140     // Construct inverse addressing: from new to original cell.
00141     labelList origCell(mesh.nCells(), -1);
00142 
00143     forAll(addedCells_, cellI)
00144     {
00145         const labelList& added = addedCells_[cellI];
00146 
00147         forAll(added, i)
00148         {
00149             label slave = added[i];
00150 
00151             if (origCell[slave] == -1)
00152             {
00153                 origCell[slave] = cellI;
00154             }
00155             else if (origCell[slave] != cellI)
00156             {
00157                 FatalErrorIn
00158                 (
00159                     "multiDirRefinement::addCells(const primitiveMesh&"
00160                     ", const Map<label>&"
00161                 )   << "Added cell " << slave << " has two different masters:"
00162                     << origCell[slave] << " , " << cellI
00163                     << abort(FatalError);
00164             }
00165         }
00166     }
00167 
00168 
00169     for
00170     (
00171         Map<label>::const_iterator iter = splitMap.begin();
00172         iter != splitMap.end();
00173         ++iter
00174     )
00175     {
00176         label masterI = iter.key();
00177         label newCellI = iter();
00178 
00179         while (origCell[masterI] != -1 && origCell[masterI] != masterI)
00180         {
00181             masterI = origCell[masterI];
00182         }
00183 
00184         if (masterI >= addedCells_.size())
00185         {
00186             FatalErrorIn
00187             (
00188                 "multiDirRefinement::addCells(const primitiveMesh&"
00189                 ", const Map<label>&"
00190             )   << "Map of added cells contains master cell " << masterI
00191                 << " which is not a valid cell number" << endl
00192                 << "This means that the mesh is not consistent with the"
00193                 << " done refinement" << endl
00194                 << "newCell:" << newCellI << abort(FatalError);
00195         }
00196 
00197         labelList& added = addedCells_[masterI];
00198 
00199         if (added.empty())
00200         {
00201             added.setSize(2);
00202             added[0] = masterI;
00203             added[1] = newCellI;
00204         }
00205         else if (findIndex(added, newCellI) == -1)
00206         {
00207             label sz = added.size();
00208             added.setSize(sz + 1);
00209             added[sz] = newCellI;
00210         }
00211     }
00212 }
00213 
00214 
00215 Foam::labelList Foam::multiDirRefinement::splitOffHex(const primitiveMesh& mesh)
00216 {
00217     const cellModel& hex = *(cellModeller::lookup("hex"));
00218 
00219     const cellShapeList& cellShapes = mesh.cellShapes();
00220 
00221     // Split cellLabels_ into two lists.
00222 
00223     labelList nonHexLabels(cellLabels_.size());
00224     label nonHexI = 0;
00225 
00226     labelList hexLabels(cellLabels_.size());
00227     label hexI = 0;
00228 
00229     forAll(cellLabels_, i)
00230     {
00231         label cellI = cellLabels_[i];
00232 
00233         if (cellShapes[cellI].model() == hex)
00234         {
00235             hexLabels[hexI++] = cellI;
00236         }
00237         else
00238         {
00239             nonHexLabels[nonHexI++] = cellI;
00240         }
00241     }
00242 
00243     nonHexLabels.setSize(nonHexI);
00244 
00245     cellLabels_.transfer(nonHexLabels);
00246 
00247     hexLabels.setSize(hexI);
00248 
00249     return hexLabels;
00250 }
00251 
00252 
00253 void Foam::multiDirRefinement::refineHex8
00254 (
00255     polyMesh& mesh,
00256     const labelList& hexCells,
00257     const bool writeMesh
00258 )
00259 {
00260     if (debug)
00261     {
00262         Pout<< "multiDirRefinement : Refining hexes " << hexCells.size()
00263             << endl;
00264     }
00265 
00266     hexRef8 hexRefiner
00267     (
00268         mesh,
00269         labelList(mesh.nCells(), 0),    // cellLevel
00270         labelList(mesh.nPoints(), 0),   // pointLevel
00271         refinementHistory
00272         (
00273             IOobject
00274             (
00275                 "refinementHistory",
00276                 mesh.facesInstance(),
00277                 polyMesh::meshSubDir,
00278                 mesh,
00279                 IOobject::NO_READ,
00280                 IOobject::NO_WRITE,
00281                 false
00282             ),
00283             List<refinementHistory::splitCell8>(0),
00284             labelList(0)
00285         )                                   // refinement history
00286     );
00287 
00288     polyTopoChange meshMod(mesh);
00289 
00290     labelList consistentCells
00291     (
00292         hexRefiner.consistentRefinement
00293         (
00294             hexCells,
00295             true                  // buffer layer
00296         )
00297     );
00298 
00299     // Check that consistentRefinement hasn't added cells
00300     {
00301         // Create count 1 for original cells
00302         Map<label> hexCellSet(2*hexCells.size());
00303         forAll(hexCells, i)
00304         {
00305             hexCellSet.insert(hexCells[i], 1);
00306         }
00307 
00308         // Increment count 
00309         forAll(consistentCells, i)
00310         {
00311             label cellI = consistentCells[i];
00312 
00313             Map<label>::iterator iter = hexCellSet.find(cellI);
00314 
00315             if (iter == hexCellSet.end())
00316             {
00317                 FatalErrorIn
00318                 (
00319                     "multiDirRefinement::refineHex8"
00320                     "(polyMesh&, const labelList&, const bool)"
00321                 )   << "Resulting mesh would not satisfy 2:1 ratio"
00322                     << " when refining cell " << cellI << abort(FatalError);
00323             }
00324             else
00325             {
00326                 iter() = 2;
00327             }
00328         }
00329 
00330         // Check if all been visited (should always be since
00331         // consistentRefinement set up to extend set.
00332         forAllConstIter(Map<label>, hexCellSet, iter)
00333         {
00334             if (iter() != 2)
00335             {
00336                 FatalErrorIn
00337                 (
00338                     "multiDirRefinement::refineHex8"
00339                     "(polyMesh&, const labelList&, const bool)"
00340                 )   << "Resulting mesh would not satisfy 2:1 ratio"
00341                     << " when refining cell " << iter.key()
00342                     << abort(FatalError);
00343             }
00344         }
00345     }
00346 
00347 
00348     hexRefiner.setRefinement(consistentCells, meshMod);
00349 
00350     // Change mesh, no inflation
00351     autoPtr<mapPolyMesh> morphMapPtr = meshMod.changeMesh(mesh, false, true);
00352     const mapPolyMesh& morphMap = morphMapPtr();
00353 
00354     if (morphMap.hasMotionPoints())
00355     {
00356         mesh.movePoints(morphMap.preMotionPoints());
00357     }
00358 
00359     if (writeMesh)
00360     {
00361         mesh.write();
00362     }
00363 
00364     if (debug)
00365     {
00366         Pout<< "multiDirRefinement : updated mesh at time "
00367             << mesh.time().timeName() << endl;
00368     }
00369 
00370     hexRefiner.updateMesh(morphMap);
00371 
00372     // Collect all cells originating from same old cell (original + 7 extra)
00373 
00374     forAll(consistentCells, i)
00375     {
00376         addedCells_[consistentCells[i]].setSize(8);
00377     }
00378     labelList nAddedCells(addedCells_.size(), 0);
00379 
00380     const labelList& cellMap = morphMap.cellMap();
00381 
00382     forAll(cellMap, cellI)
00383     {
00384         label oldCellI = cellMap[cellI];
00385 
00386         if (addedCells_[oldCellI].size())
00387         {
00388             addedCells_[oldCellI][nAddedCells[oldCellI]++] = cellI;
00389         }
00390     }
00391 }
00392 
00393 
00394 void Foam::multiDirRefinement::refineAllDirs
00395 (
00396     polyMesh& mesh,
00397     List<vectorField>& cellDirections,
00398     const cellLooper& cellWalker,
00399     undoableMeshCutter& cutter,
00400     const bool writeMesh
00401 )
00402 {
00403     // Iterator
00404     refinementIterator refiner(mesh, cutter, cellWalker, writeMesh);
00405 
00406     forAll(cellDirections, dirI)
00407     {
00408         if (debug)
00409         {
00410             Pout<< "multiDirRefinement : Refining " << cellLabels_.size()
00411                 << " cells in direction " << dirI << endl
00412                 << endl;
00413         }
00414 
00415         const vectorField& dirField = cellDirections[dirI];
00416 
00417         // Combine cell to be cut with direction to cut in.
00418         // If dirField is only one element use this for all cells.
00419 
00420         List<refineCell> refCells(cellLabels_.size());
00421 
00422         if (dirField.size() == 1)
00423         {
00424             // Uniform directions.
00425             if (debug)
00426             {
00427                 Pout<< "multiDirRefinement : Uniform refinement:"
00428                     << dirField[0] << endl;
00429             }
00430 
00431             forAll(refCells, refI)
00432             {
00433                 label cellI = cellLabels_[refI];
00434 
00435                 refCells[refI] = refineCell(cellI, dirField[0]);
00436             }
00437         }
00438         else
00439         {
00440             // Non uniform directions.
00441             forAll(refCells, refI)
00442             {
00443                 label cellI = cellLabels_[refI];
00444 
00445                 refCells[refI] = refineCell(cellI, dirField[cellI]);
00446             }
00447         }
00448 
00449         // Do refine mesh (multiple iterations). Remember added cells.
00450         Map<label> splitMap = refiner.setRefinement(refCells);
00451 
00452         // Update overall map of added cells
00453         addCells(mesh, splitMap);
00454 
00455         // Add added cells to list of cells to refine in next iteration
00456         addCells(splitMap, cellLabels_);
00457 
00458         // Update refinement direction for added cells.
00459         if (dirField.size() != 1)
00460         {
00461             forAll(cellDirections, i)
00462             {
00463                 update(splitMap, cellDirections[i]);
00464             }
00465         }
00466 
00467         if (debug)
00468         {
00469             Pout<< "multiDirRefinement : Done refining direction " << dirI
00470                 << " resulting in " << cellLabels_.size() << " cells" << nl
00471                 << endl;
00472         }
00473     }
00474 }
00475 
00476 
00477 void Foam::multiDirRefinement::refineFromDict
00478 (
00479     polyMesh& mesh,
00480     List<vectorField>& cellDirections,
00481     const dictionary& dict,
00482     const bool writeMesh
00483 )
00484 {
00485     // How to walk cell circumference.
00486     Switch pureGeomCut(dict.lookup("geometricCut"));
00487 
00488     autoPtr<cellLooper> cellWalker(NULL);
00489     if (pureGeomCut)
00490     {
00491         cellWalker.reset(new geomCellLooper(mesh));
00492     }
00493     else
00494     {
00495         cellWalker.reset(new hexCellLooper(mesh));
00496     }
00497 
00498     // Construct undoable refinement topology modifier. 
00499     //Note: undoability switched off.
00500     // Might want to reconsider if needs to be possible. But then can always
00501     // use other constructor.
00502     undoableMeshCutter cutter(mesh, false);
00503 
00504     refineAllDirs(mesh, cellDirections, cellWalker(), cutter, writeMesh);
00505 }
00506 
00507 
00508 
00509 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00510 
00511 // Construct from dictionary
00512 Foam::multiDirRefinement::multiDirRefinement
00513 (
00514     polyMesh& mesh,
00515     const labelList& cellLabels,        // list of cells to refine
00516     const dictionary& dict
00517 )
00518 :
00519     cellLabels_(cellLabels),
00520     addedCells_(mesh.nCells())
00521 {
00522     Switch useHex(dict.lookup("useHexTopology"));
00523 
00524     Switch writeMesh(dict.lookup("writeMesh"));
00525 
00526     wordList dirNames(dict.lookup("directions"));
00527 
00528     if (useHex && dirNames.size() == 3)
00529     {
00530         // Filter out hexes from cellLabels_
00531         labelList hexCells(splitOffHex(mesh));
00532 
00533         refineHex8(mesh, hexCells, writeMesh);
00534     }
00535 
00536     label nRemainingCells = cellLabels_.size();
00537 
00538     reduce(nRemainingCells, sumOp<label>());
00539 
00540     if (nRemainingCells > 0)
00541     {
00542         // Any cells to refine using meshCutter
00543 
00544         // Determine directions for every cell. Can either be uniform
00545         // (size = 1) or non-uniform (one vector per cell)
00546         directions cellDirections(mesh, dict);
00547 
00548         refineFromDict(mesh, cellDirections, dict, writeMesh);
00549     }
00550 }
00551 
00552 
00553 // Construct from directionary and directions to refine.
00554 Foam::multiDirRefinement::multiDirRefinement
00555 (
00556     polyMesh& mesh,
00557     const labelList& cellLabels,        // list of cells to refine
00558     const List<vectorField>& cellDirs,  // Explicitly provided directions
00559     const dictionary& dict
00560 )
00561 :
00562     cellLabels_(cellLabels),
00563     addedCells_(mesh.nCells())
00564 {
00565     Switch useHex(dict.lookup("useHexTopology"));
00566 
00567     Switch writeMesh(dict.lookup("writeMesh"));
00568 
00569     wordList dirNames(dict.lookup("directions"));
00570 
00571     if (useHex && dirNames.size() == 3)
00572     {
00573         // Filter out hexes from cellLabels_
00574         labelList hexCells(splitOffHex(mesh));
00575 
00576         refineHex8(mesh, hexCells, writeMesh);
00577     }
00578 
00579     label nRemainingCells = cellLabels_.size();
00580 
00581     reduce(nRemainingCells, sumOp<label>());
00582 
00583     if (nRemainingCells > 0)
00584     {
00585         // Any cells to refine using meshCutter
00586 
00587         // Working copy of cells to refine
00588         List<vectorField> cellDirections(cellDirs);
00589 
00590         refineFromDict(mesh, cellDirections, dict, writeMesh);
00591     }
00592 }
00593 
00594 
00595 // Construct from components. Implies meshCutter since directions provided.
00596 Foam::multiDirRefinement::multiDirRefinement
00597 (
00598     polyMesh& mesh,
00599     undoableMeshCutter& cutter,     // actual mesh modifier
00600     const cellLooper& cellWalker,   // how to cut a single cell with
00601                                     // a plane
00602     const labelList& cellLabels,    // list of cells to refine
00603     const List<vectorField>& cellDirs,
00604     const bool writeMesh            // write intermediate meshes
00605 )
00606 :
00607     cellLabels_(cellLabels),
00608     addedCells_(mesh.nCells())
00609 {
00610     // Working copy of cells to refine
00611     List<vectorField> cellDirections(cellDirs);
00612 
00613     refineAllDirs(mesh, cellDirections, cellWalker, cutter, writeMesh);
00614 }
00615 
00616 
00617 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd