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

cellSplitter.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 Description
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "cellSplitter.H"
00030 #include "polyMesh.H"
00031 #include "polyTopoChange.H"
00032 #include "polyAddCell.H"
00033 #include "polyAddFace.H"
00034 #include "polyAddPoint.H"
00035 #include "polyModifyFace.H"
00036 #include "mapPolyMesh.H"
00037 #include "meshTools.H"
00038 
00039 
00040 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00041 
00042 namespace Foam
00043 {
00044 
00045 defineTypeNameAndDebug(cellSplitter, 0);
00046 
00047 }
00048 
00049 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00050 
00051 void Foam::cellSplitter::getFaceInfo
00052 (
00053     const label faceI,
00054     label& patchID,
00055     label& zoneID,
00056     label& zoneFlip
00057 ) const
00058 {
00059     patchID = -1;
00060 
00061     if (!mesh_.isInternalFace(faceI))
00062     {
00063         patchID = mesh_.boundaryMesh().whichPatch(faceI);
00064     }
00065 
00066     zoneID = mesh_.faceZones().whichZone(faceI);
00067 
00068     zoneFlip = false;
00069 
00070     if (zoneID >= 0)
00071     {
00072         const faceZone& fZone = mesh_.faceZones()[zoneID];
00073 
00074         zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
00075     }
00076 }
00077 
00078 
00079 // Find the new owner of faceI (since the original cell has been split into
00080 // newCells
00081 Foam::label Foam::cellSplitter::newOwner
00082 (
00083     const label faceI,
00084     const Map<labelList>& cellToCells
00085 ) const
00086 {
00087     label oldOwn = mesh_.faceOwner()[faceI];
00088 
00089     Map<labelList>::const_iterator fnd = cellToCells.find(oldOwn);
00090 
00091     if (fnd == cellToCells.end())
00092     {
00093         // Unsplit cell
00094         return oldOwn;
00095     }
00096     else
00097     {
00098         // Look up index of face in the cells' faces.
00099 
00100         const labelList& newCells = fnd();
00101 
00102         const cell& cFaces = mesh_.cells()[oldOwn];
00103 
00104         return newCells[findIndex(cFaces, faceI)];
00105     }
00106 }
00107 
00108 
00109 Foam::label Foam::cellSplitter::newNeighbour
00110 (
00111     const label faceI,
00112     const Map<labelList>& cellToCells
00113 ) const
00114 {
00115     label oldNbr = mesh_.faceNeighbour()[faceI];
00116 
00117     Map<labelList>::const_iterator fnd = cellToCells.find(oldNbr);
00118 
00119     if (fnd == cellToCells.end())
00120     {
00121         // Unsplit cell
00122         return oldNbr;
00123     }
00124     else
00125     {
00126         // Look up index of face in the cells' faces.
00127 
00128         const labelList& newCells = fnd();
00129 
00130         const cell& cFaces = mesh_.cells()[oldNbr];
00131 
00132         return newCells[findIndex(cFaces, faceI)];
00133     }
00134 }
00135 
00136 
00137 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00138 
00139 // Construct from components
00140 Foam::cellSplitter::cellSplitter(const polyMesh& mesh)
00141 :
00142     mesh_(mesh),
00143     addedPoints_()
00144 {}
00145 
00146 
00147 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00148 
00149 Foam::cellSplitter::~cellSplitter()
00150 {}
00151 
00152 
00153 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00154 
00155 void Foam::cellSplitter::setRefinement
00156 (
00157     const Map<point>& cellToMidPoint,
00158     polyTopoChange& meshMod
00159 )
00160 {
00161     addedPoints_.clear();
00162     addedPoints_.resize(cellToMidPoint.size());
00163 
00164 
00165     //
00166     // Introduce cellToMidPoints.
00167     //
00168 
00169     forAllConstIter(Map<point>, cellToMidPoint, iter)
00170     {
00171         label cellI = iter.key();
00172 
00173         label anchorPoint = mesh_.cellPoints()[cellI][0];
00174 
00175         label addedPointI =
00176             meshMod.setAction
00177             (
00178                 polyAddPoint
00179                 (
00180                     iter(),         // point
00181                     anchorPoint,    // master point
00182                     -1,             // zone for point
00183                     true            // supports a cell
00184                 )
00185             );
00186         addedPoints_.insert(cellI, addedPointI);
00187 
00188         //Pout<< "Added point " << addedPointI
00189         //    << iter() << " in cell " << cellI << " with centre "
00190         //    << mesh_.cellCentres()[cellI] << endl;
00191     }
00192 
00193 
00194     //
00195     // Add cells (first one is modified original cell)
00196     //
00197 
00198     Map<labelList> cellToCells(cellToMidPoint.size());
00199 
00200     forAllConstIter(Map<point>, cellToMidPoint, iter)
00201     {
00202         label cellI = iter.key();
00203 
00204         const cell& cFaces = mesh_.cells()[cellI];
00205 
00206         // Cells created for this cell.
00207         labelList newCells(cFaces.size());
00208 
00209         // First pyramid is the original cell
00210         newCells[0] = cellI;
00211 
00212         // Add other pyramids
00213         for (label i = 1; i < cFaces.size(); i++)
00214         {
00215             label addedCellI =
00216                 meshMod.setAction
00217                 (
00218                     polyAddCell
00219                     (
00220                         -1,     // master point
00221                         -1,     // master edge
00222                         -1,     // master face
00223                         cellI,  // master cell
00224                         -1      // zone
00225                     )
00226                 );
00227 
00228             newCells[i] = addedCellI;
00229         }
00230 
00231         cellToCells.insert(cellI, newCells);
00232 
00233         //Pout<< "Split cell " << cellI
00234         //    << " with centre " << mesh_.cellCentres()[cellI] << nl
00235         //    << " faces:" << cFaces << nl
00236         //    << " into :" << newCells << endl;
00237     }
00238 
00239 
00240     //
00241     // Introduce internal faces. These go from edges of the cell to the mid
00242     // point.
00243     //
00244 
00245     forAllConstIter(Map<point>, cellToMidPoint, iter)
00246     {
00247         label cellI = iter.key();
00248 
00249         label midPointI = addedPoints_[cellI];
00250 
00251         const cell& cFaces = mesh_.cells()[cellI];
00252 
00253         const labelList& cEdges = mesh_.cellEdges()[cellI];
00254 
00255         forAll(cEdges, i)
00256         {
00257             label edgeI = cEdges[i];
00258             const edge& e = mesh_.edges()[edgeI];
00259 
00260             // Get the faces on the cell using the edge
00261             label face0, face1;
00262             meshTools::getEdgeFaces(mesh_, cellI, edgeI, face0, face1);
00263 
00264             // Get the cells on both sides of the face by indexing into cFaces.
00265             // (since newly created cells are stored in cFaces order)
00266             const labelList& newCells = cellToCells[cellI];
00267 
00268             label cell0 = newCells[findIndex(cFaces, face0)];
00269             label cell1 = newCells[findIndex(cFaces, face1)];
00270 
00271             if (cell0 < cell1)
00272             {
00273                 // Construct face to midpoint that is pointing away from
00274                 // (pyramid split off from) cellI
00275 
00276                 const face& f0 = mesh_.faces()[face0];
00277 
00278                 label index = findIndex(f0, e[0]);
00279 
00280                 bool edgeInFaceOrder = (f0[f0.fcIndex(index)] == e[1]);
00281 
00282                 // Check if cellI is the face owner
00283 
00284                 face newF(3);
00285                 if (edgeInFaceOrder == (mesh_.faceOwner()[face0] == cellI))
00286                 {
00287                     // edge used in face order.
00288                     newF[0] = e[1];
00289                     newF[1] = e[0];
00290                     newF[2] = midPointI;
00291                 }
00292                 else
00293                 {
00294                     newF[0] = e[0];
00295                     newF[1] = e[1];
00296                     newF[2] = midPointI;
00297                 }
00298 
00299                 // Now newF points away from cell0
00300                 meshMod.setAction
00301                 (
00302                     polyAddFace
00303                     (
00304                         newF,                       // face
00305                         cell0,                      // owner
00306                         cell1,                      // neighbour
00307                         -1,                         // master point
00308                         -1,                         // master edge
00309                         face0,                      // master face for addition
00310                         false,                      // flux flip
00311                         -1,                         // patch for face
00312                         -1,                         // zone for face
00313                         false                       // face zone flip
00314                     )
00315                 );
00316             }
00317             else
00318             {
00319                 // Construct face to midpoint that is pointing away from
00320                 // (pyramid split off from) cellI
00321 
00322                 const face& f1 = mesh_.faces()[face1];
00323 
00324                 label index = findIndex(f1, e[0]);
00325 
00326                 bool edgeInFaceOrder = (f1[f1.fcIndex(index)] == e[1]);
00327 
00328                 // Check if cellI is the face owner
00329 
00330                 face newF(3);
00331                 if (edgeInFaceOrder == (mesh_.faceOwner()[face1] == cellI))
00332                 {
00333                     // edge used in face order.
00334                     newF[0] = e[1];
00335                     newF[1] = e[0];
00336                     newF[2] = midPointI;
00337                 }
00338                 else
00339                 {
00340                     newF[0] = e[0];
00341                     newF[1] = e[1];
00342                     newF[2] = midPointI;
00343                 }
00344 
00345                 // Now newF points away from cell1
00346                 meshMod.setAction
00347                 (
00348                     polyAddFace
00349                     (
00350                         newF,                       // face
00351                         cell1,                      // owner
00352                         cell0,                      // neighbour
00353                         -1,                         // master point
00354                         -1,                         // master edge
00355                         face0,                      // master face for addition
00356                         false,                      // flux flip
00357                         -1,                         // patch for face
00358                         -1,                         // zone for face
00359                         false                       // face zone flip
00360                     )
00361                 );
00362             }
00363         }
00364     }
00365 
00366 
00367     //
00368     // Update all existing faces for split owner or neighbour.
00369     //
00370 
00371 
00372     // Mark off affected face.
00373     boolList faceUpToDate(mesh_.nFaces(), true);
00374 
00375     forAllConstIter(Map<point>, cellToMidPoint, iter)
00376     {
00377         label cellI = iter.key();
00378 
00379         const cell& cFaces = mesh_.cells()[cellI];
00380 
00381         forAll(cFaces, i)
00382         {
00383             label faceI = cFaces[i];
00384 
00385             faceUpToDate[faceI] = false;
00386         }
00387     }
00388 
00389     forAll(faceUpToDate, faceI)
00390     {
00391         if (!faceUpToDate[faceI])
00392         {
00393             const face& f = mesh_.faces()[faceI];
00394 
00395             if (mesh_.isInternalFace(faceI))
00396             {
00397                 label newOwn = newOwner(faceI, cellToCells);
00398                 label newNbr = newNeighbour(faceI, cellToCells);
00399 
00400                 if (newOwn < newNbr)
00401                 {
00402                     meshMod.setAction
00403                     (
00404                         polyModifyFace
00405                         (
00406                             f,
00407                             faceI,
00408                             newOwn,         // owner
00409                             newNbr,         // neighbour
00410                             false,          // flux flip
00411                             -1,             // patch for face
00412                             false,          // remove from zone
00413                             -1,             // zone for face
00414                             false           // face zone flip
00415                         )
00416                     );
00417                 }
00418                 else
00419                 {
00420                     meshMod.setAction
00421                     (
00422                         polyModifyFace
00423                         (
00424                             f.reverseFace(),
00425                             faceI,
00426                             newNbr,         // owner
00427                             newOwn,         // neighbour
00428                             false,          // flux flip
00429                             -1,             // patch for face
00430                             false,          // remove from zone
00431                             -1,             // zone for face
00432                             false           // face zone flip
00433                         )
00434                     );
00435                 }
00436 
00437             }
00438             else
00439             {
00440                 label newOwn = newOwner(faceI, cellToCells);
00441 
00442                 label patchID, zoneID, zoneFlip;
00443                 getFaceInfo(faceI, patchID, zoneID, zoneFlip);
00444 
00445                 meshMod.setAction
00446                 (
00447                     polyModifyFace
00448                     (
00449                         mesh_.faces()[faceI],
00450                         faceI,
00451                         newOwn,         // owner
00452                         -1,             // neighbour
00453                         false,          // flux flip
00454                         patchID,        // patch for face
00455                         false,          // remove from zone
00456                         zoneID,         // zone for face
00457                         zoneFlip        // face zone flip
00458                     )
00459                 );
00460             }
00461 
00462             faceUpToDate[faceI] = true;
00463         }
00464     }
00465 }
00466 
00467 
00468 void Foam::cellSplitter::updateMesh(const mapPolyMesh& morphMap)
00469 {
00470     // Create copy since we're deleting entries. Only if both cell and added
00471     // point get mapped do they get inserted.
00472     Map<label> newAddedPoints(addedPoints_.size());
00473 
00474     forAllConstIter(Map<label>, addedPoints_, iter)
00475     {
00476         label oldCellI = iter.key();
00477 
00478         label newCellI = morphMap.reverseCellMap()[oldCellI];
00479 
00480         label oldPointI = iter();
00481 
00482         label newPointI = morphMap.reversePointMap()[oldPointI];
00483 
00484         if (newCellI >= 0 && newPointI >= 0)
00485         {
00486             newAddedPoints.insert(newCellI, newPointI);
00487         }
00488     }
00489 
00490     // Copy
00491     addedPoints_.transfer(newAddedPoints);
00492 }
00493 
00494 
00495 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd