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

addCellLayer.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 "layerAdditionRemoval.H"
00028 #include "polyMesh.H"
00029 #include "primitiveMesh.H"
00030 #include "polyTopoChange.H"
00031 #include "polyTopoChanger.H"
00032 #include "polyAddPoint.H"
00033 #include "polyAddCell.H"
00034 #include "polyAddFace.H"
00035 #include "polyModifyFace.H"
00036 
00037 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00038 
00039 //- Calculate the offset to the next layer
00040 Foam::tmp<Foam::vectorField> Foam::layerAdditionRemoval::extrusionDir() const
00041 {
00042     const polyMesh& mesh = topoChanger().mesh();
00043     const primitiveFacePatch& masterFaceLayer =
00044         mesh.faceZones()[faceZoneID_.index()]();
00045 
00046     const pointField& points = mesh.points();
00047     const labelList& mp = masterFaceLayer.meshPoints();
00048 
00049     tmp<vectorField> textrusionDir(new vectorField(mp.size()));
00050     vectorField& extrusionDir = textrusionDir();
00051 
00052     if (setLayerPairing())
00053     {
00054         if (debug)
00055         {
00056             Pout<< "void layerAdditionRemoval::extrusionDir() const "
00057                 << " for object " << name() << " : "
00058                 << "Using edges for point insertion" << endl;
00059         }
00060 
00061         // Detected a valid layer.  Grab the point and face collapse mapping
00062         const labelList& ptc = pointsPairing();
00063 
00064         forAll (extrusionDir, mpI)
00065         {
00066             extrusionDir[mpI] = points[ptc[mpI]] - points[mp[mpI]];
00067         }
00068     }
00069     else
00070     {
00071         if (debug)
00072         {
00073             Pout<< "void layerAdditionRemoval::extrusionDir() const "
00074                 << " for object " << name() << " : "
00075                 << "A valid layer could not be found in front of "
00076                 << "the addition face layer.  Using face-based "
00077                 << "point normals for point addition"
00078                 << endl;
00079         }
00080 
00081         extrusionDir = minLayerThickness_*masterFaceLayer.pointNormals();
00082     }
00083     return textrusionDir;
00084 }
00085 
00086 
00087 void Foam::layerAdditionRemoval::addCellLayer
00088 (
00089     polyTopoChange& ref
00090 ) const
00091 {
00092     // Insert the layer addition instructions into the topological change
00093 
00094     // Algorithm:
00095     // 1.  For every point in the master zone add a new point
00096     // 2.  For every face in the master zone add a cell
00097     // 3.  Go through all the edges of the master zone.  For all internal edges,
00098     //     add a face with the correct orientation and make it internal.
00099     //     For all boundary edges, find the patch it belongs to and add the face
00100     //     to this patch
00101     // 4.  Create a set of new faces on the patch image and assign them to be
00102     //     between the old master cells and the newly created cells
00103     // 5.  Modify all the faces in the patch such that they are located
00104     //     between the old slave cells and newly created cells
00105 
00106     if (debug)
00107     {
00108         Pout<< "void layerAdditionRemoval::addCellLayer("
00109             << "polyTopoChange& ref) const for object " << name() << " : "
00110             << "Adding cell layer" << endl;
00111     }
00112 
00113     // Create the points
00114 
00115     const polyMesh& mesh = topoChanger().mesh();
00116     const primitiveFacePatch& masterFaceLayer =
00117         mesh.faceZones()[faceZoneID_.index()]();
00118 
00119     const pointField& points = mesh.points();
00120     const labelList& mp = masterFaceLayer.meshPoints();
00121 
00122     // Get the extrusion direction for the added points
00123 
00124     tmp<vectorField> tpointOffsets = extrusionDir();
00125 
00126     // Add the new points
00127     labelList addedPoints(mp.size());
00128 
00129     forAll (mp, pointI)
00130     {
00131         // Add the point nominal thickness away from the master zone point
00132         // and grab the label
00133         addedPoints[pointI] =
00134             ref.setAction
00135             (
00136                 polyAddPoint
00137                 (
00138                     points[mp[pointI]]                  // point
00139                   + addDelta_*tpointOffsets()[pointI],
00140                     mp[pointI],                         // master point
00141                     -1,                                 // zone for point
00142                     true                                // supports a cell
00143                 )
00144             );
00145     }
00146 
00147 // Pout << "mp: " << mp << " addedPoints: " << addedPoints << endl;
00148     // Create the cells
00149 
00150     const labelList& mc =
00151         mesh.faceZones()[faceZoneID_.index()].masterCells();
00152     const labelList& sc =
00153         mesh.faceZones()[faceZoneID_.index()].slaveCells();
00154 // Pout << "mc: " << mc << " sc: " << sc << endl;
00155     const labelList& mf = mesh.faceZones()[faceZoneID_.index()];
00156     const boolList& mfFlip = mesh.faceZones()[faceZoneID_.index()].flipMap();
00157 
00158     const labelList& own = mesh.faceOwner();
00159     const labelList& nei = mesh.faceNeighbour();
00160 
00161     labelList addedCells(mf.size());
00162 
00163     forAll (mf, faceI)
00164     {
00165         addedCells[faceI] =
00166             ref.setAction
00167             (
00168                 polyAddCell
00169                 (
00170                     -1,           // master point
00171                     -1,           // master edge
00172                     mf[faceI],    // master face
00173                     -1,           // master cell
00174                     -1            // zone for cell
00175                 )
00176             );
00177     }
00178 
00179     // Create the new faces
00180 
00181     // Grab the local faces from the master zone
00182     const faceList& zoneFaces = masterFaceLayer.localFaces();
00183 
00184     // Create the new faces by copying the master zone.  All the added
00185     // faces need to point into the newly created cells, which means
00186     // all the faces from the master layer are flipped.  The flux flip
00187     // is determined from the original master layer face and the face
00188     // owner: if the master cell is equal to the face owner the flux
00189     // remains the same; otherwise it is flipped
00190 
00191     forAll (zoneFaces, faceI)
00192     {
00193         const face oldFace = zoneFaces[faceI].reverseFace();
00194 
00195         face newFace(oldFace.size());
00196 
00197         forAll (oldFace, pointI)
00198         {
00199             newFace[pointI] = addedPoints[oldFace[pointI]];
00200         }
00201 
00202         bool flipFaceFlux = false;
00203 
00204         // Flip the face as necessary
00205         if
00206         (
00207             mc[faceI] == nei[mf[faceI]]
00208          || !mesh.isInternalFace(mf[faceI])
00209         )
00210         {
00211             flipFaceFlux = true;
00212         }
00213 
00214         // Add the face
00215         ref.setAction
00216         (
00217             polyAddFace
00218             (
00219                 newFace,               // face
00220                 mc[faceI],             // owner
00221                 addedCells[faceI],     // neighbour
00222                 -1,                    // master point
00223                 -1,                    // master edge
00224                 mf[faceI],             // master face for addition
00225                 flipFaceFlux,          // flux flip
00226                 -1,                    // patch for face
00227                 -1,                    // zone for face
00228                 false                  // face zone flip
00229             )
00230         );
00231 
00232 // Pout << "adding face: " << newFace << " own: " << mc[faceI] << " nei: " << addedCells[faceI] << endl;
00233     }
00234 
00235     // Modify the faces from the master zone for the new neighbour
00236 
00237     const faceList& faces = mesh.faces();
00238 // Pout << "mfFlip: " << mfFlip << endl;
00239     forAll (mf, faceI)
00240     {
00241         const label curfaceID = mf[faceI];
00242 
00243         // If the face is internal, modify its owner to be the newly
00244         // created cell.  No flip is necessary
00245         if (!mesh.isInternalFace(curfaceID))
00246         {
00247             ref.setAction
00248             (
00249                 polyModifyFace
00250                 (
00251                     faces[curfaceID],            // modified face
00252                     curfaceID,                   // label of face being modified
00253                     addedCells[faceI],           // owner
00254                     -1,                          // neighbour
00255                     false,                       // face flip
00256                     mesh.boundaryMesh().whichPatch(curfaceID),// patch for face
00257                     false,                       // remove from zone
00258                     faceZoneID_.index(),         // zone for face
00259                     mfFlip[faceI]                // face flip in zone
00260                 )
00261             );
00262 // Pout << "Modifying a boundary face. Face: " << curfaceID << " flip: " << mfFlip[faceI] << endl;
00263         }
00264         // If slave cell is owner, the face remains the same (but with
00265         // a new neighbour - the newly created cell).  Otherwise, the
00266         // face is flipped.
00267         else if (sc[faceI] == own[curfaceID])
00268         {
00269             // Orientation is good, just change neighbour
00270             ref.setAction
00271             (
00272                 polyModifyFace
00273                 (
00274                     faces[curfaceID],            // modified face
00275                     curfaceID,                   // label of face being modified
00276                     own[curfaceID],              // owner
00277                     addedCells[faceI],           // neighbour
00278                     false,                       // face flip
00279                     mesh.boundaryMesh().whichPatch(curfaceID),// patch for face
00280                     false,                       // remove from zone
00281                     faceZoneID_.index(),         // zone for face
00282                     mfFlip[faceI]                // face flip in zone
00283                 )
00284             );
00285 
00286 // Pout << "modify face, no flip " << curfaceID << " own: " << own[curfaceID] << " nei: " << addedCells[faceI] << endl;
00287         }
00288         else
00289         {
00290             // Change in orientation; flip face
00291             ref.setAction
00292             (
00293                 polyModifyFace
00294                 (
00295                     faces[curfaceID].reverseFace(), // modified face
00296                     curfaceID,                   // label of face being modified
00297                     nei[curfaceID],                 // owner
00298                     addedCells[faceI],              // neighbour
00299                     true,                           // face flip
00300                     mesh.boundaryMesh().whichPatch(curfaceID), // patch for face
00301                     false,                          // remove from zone
00302                     faceZoneID_.index(),            // zone for face
00303                     !mfFlip[faceI]                  // face flip in zone
00304                 )
00305             );
00306 // Pout << "modify face, with flip " << curfaceID << " own: " << own[curfaceID] << " nei: " << addedCells[faceI] << endl;
00307         }
00308     }
00309 
00310     // Create new faces from edges
00311 
00312     const edgeList& zoneLocalEdges = masterFaceLayer.edges();
00313 
00314     const labelListList& edgeFaces = masterFaceLayer.edgeFaces();
00315 
00316     label nInternalEdges = masterFaceLayer.nInternalEdges();
00317 
00318     const labelList& meshEdges =
00319         mesh.faceZones()[faceZoneID_.index()].meshEdges();
00320 
00321     // Do all internal edges
00322     for (label curEdgeID = 0; curEdgeID < nInternalEdges; curEdgeID++)
00323     {
00324         face newFace(4);
00325 
00326         newFace[0] = mp[zoneLocalEdges[curEdgeID].start()];
00327         newFace[1] = mp[zoneLocalEdges[curEdgeID].end()];
00328         newFace[2] = addedPoints[zoneLocalEdges[curEdgeID].end()];
00329         newFace[3] = addedPoints[zoneLocalEdges[curEdgeID].start()];
00330 
00331         ref.setAction
00332         (
00333             polyAddFace
00334             (
00335                 newFace,                               // face
00336                 addedCells[edgeFaces[curEdgeID][0]],   // owner
00337                 addedCells[edgeFaces[curEdgeID][1]],   // neighbour
00338                 -1,                                    // master point
00339                 meshEdges[curEdgeID],                  // master edge
00340                 -1,                                    // master face
00341                 false,                                 // flip flux
00342                 -1,                                    // patch for face
00343                 -1,                                    // zone for face
00344                 false                                  // face zone flip
00345             )
00346         );
00347 
00348 // Pout << "Add internal face off edge: " << newFace << " own: " << addedCells[edgeFaces[curEdgeID][0]] << " nei: " << addedCells[edgeFaces[curEdgeID][1]] << endl;
00349     }
00350 
00351     // Prepare creation of faces from boundary edges.
00352     // Note:
00353     // A tricky part of the algorithm is finding the patch into which the
00354     // newly created face will be added.  For this purpose, take the edge
00355     // and grab all the faces coming from it.  From the set of faces
00356     // eliminate the internal faces and find the boundary face from the rest.
00357     //  If there is more than one boundary face (excluding the ones in
00358     // the master zone), the patch with the lower label is selected.
00359 
00360     const labelListList& meshEdgeFaces = mesh.edgeFaces();
00361 
00362     const faceZoneMesh& zoneMesh = mesh.faceZones();
00363 
00364     // Do all boundary edges
00365 
00366     for
00367     (
00368         label curEdgeID = nInternalEdges;
00369         curEdgeID < zoneLocalEdges.size();
00370         curEdgeID++
00371     )
00372     {
00373         face newFace(4);
00374         newFace[0] = mp[zoneLocalEdges[curEdgeID].start()];
00375         newFace[1] = mp[zoneLocalEdges[curEdgeID].end()];
00376         newFace[2] = addedPoints[zoneLocalEdges[curEdgeID].end()];
00377         newFace[3] = addedPoints[zoneLocalEdges[curEdgeID].start()];
00378 
00379         // Determine the patch for insertion
00380         const labelList& curFaces = meshEdgeFaces[meshEdges[curEdgeID]];
00381 
00382         label patchID = -1;
00383         label zoneID = -1;
00384 
00385         forAll (curFaces, faceI)
00386         {
00387             const label cf = curFaces[faceI];
00388 
00389             if (!mesh.isInternalFace(cf))
00390             {
00391                 // Face not internal. Check if it is in the zone
00392                 if (zoneMesh.whichZone(cf) != faceZoneID_.index())
00393                 {
00394                     // Found the face in a boundary patch which is not in zone
00395                     patchID = mesh.boundaryMesh().whichPatch(cf);
00396                     zoneID = mesh.faceZones().whichZone(cf);
00397 
00398                     break;
00399                 }
00400             }
00401         }
00402 
00403         if (patchID < 0)
00404         {
00405             FatalErrorIn
00406             (
00407                 "void Foam::layerAdditionRemoval::setRefinement("
00408                 "polyTopoChange& ref)"
00409             )   << "Cannot find patch for edge " << meshEdges[curEdgeID]
00410                 << ". Edge: " << mesh.edges()[meshEdges[curEdgeID]]
00411                 << abort(FatalError);
00412         }
00413 
00414         ref.setAction
00415         (
00416             polyAddFace
00417             (
00418                 newFace,                               // face
00419                 addedCells[edgeFaces[curEdgeID][0]],   // owner
00420                 -1,                                    // neighbour
00421                 -1,                                    // master point
00422                 meshEdges[curEdgeID],                  // master edge
00423                 -1,                                    // master face
00424                 false,                                 // flip flux
00425                 patchID,                               // patch for face
00426                 zoneID,                                // zone
00427                 false                                  // zone face flip
00428             )
00429         );
00430 // Pout << "add boundary face: " << newFace << " into patch " << patchID << " own: " << addedCells[edgeFaces[curEdgeID][0]] << endl;
00431     }
00432 
00433     // Modify the remaining faces of the master cells to reconnect to the new
00434     // layer of faces.
00435     // Algorithm: Go through all the cells of the master zone and make
00436     // a map of faces to avoid duplicates.  Do not insert the faces in
00437     // the master patch (as they have already been dealt with).  Make
00438     // a master layer point renumbering map, which for every point in
00439     // the master layer gives its new label. Loop through all faces in
00440     // the map and attempt to renumber them using the master layer
00441     // point renumbering map.  Once the face is renumbered, compare it
00442     // with the original face; if they are the same, the face has not
00443     // changed; if not, modify the face but keep all of its old
00444     // attributes (apart from the vertex numbers).
00445 
00446     // Create the map of faces in the master cell layer
00447     labelHashSet masterCellFaceMap(primitiveMesh::facesPerCell_*mc.size());
00448 
00449     const cellList& cells = mesh.cells();
00450 
00451     forAll (mc, cellI)
00452     {
00453         const labelList& curFaces = cells[mc[cellI]];
00454 
00455         forAll (curFaces, faceI)
00456         {
00457             // Check if the face belongs to the master zone; if not add it
00458             if (zoneMesh.whichZone(curFaces[faceI]) != faceZoneID_.index())
00459             {
00460                 masterCellFaceMap.insert(curFaces[faceI]);
00461             }
00462         }
00463     }
00464 
00465     // Create the master layer point map
00466     Map<label> masterLayerPointMap(2*mp.size());
00467 
00468     forAll (mp, pointI)
00469     {
00470         masterLayerPointMap.insert
00471         (
00472             mp[pointI],
00473             addedPoints[pointI]
00474         );
00475     }
00476 
00477     // Grab the list of faces of the master layer
00478     const labelList masterCellFaces = masterCellFaceMap.toc();
00479 
00480     forAll (masterCellFaces, faceI)
00481     {
00482         // Attempt to renumber the face using the masterLayerPointMap.
00483         // Missing point remain the same
00484 
00485         const label curFaceID = masterCellFaces[faceI];
00486 
00487         const face& oldFace = faces[curFaceID];
00488 
00489         face newFace(oldFace.size());
00490 
00491         bool changed = false;
00492 
00493         forAll (oldFace, pointI)
00494         {
00495             if (masterLayerPointMap.found(oldFace[pointI]))
00496             {
00497                 changed = true;
00498 
00499                 newFace[pointI] = masterLayerPointMap.find(oldFace[pointI])();
00500             }
00501             else
00502             {
00503                 newFace[pointI] = oldFace[pointI];
00504             }
00505         }
00506 
00507         // If the face has changed, create a modification entry
00508         if (changed)
00509         {
00510             // Get face zone and its flip
00511             label modifiedFaceZone = mesh.faceZones().whichZone(curFaceID);
00512             bool modifiedFaceZoneFlip = false;
00513 
00514             if (modifiedFaceZone >= 0)
00515             {
00516                 modifiedFaceZoneFlip =
00517                     mesh.faceZones()[modifiedFaceZone].flipMap()
00518                     [
00519                         mesh.faceZones()[modifiedFaceZone].whichFace(curFaceID)
00520                     ];
00521             }
00522 
00523             if (mesh.isInternalFace(curFaceID))
00524             {
00525                 ref.setAction
00526                 (
00527                     polyModifyFace
00528                     (
00529                         newFace,                // modified face
00530                         curFaceID,              // label of face being modified
00531                         own[curFaceID],         // owner
00532                         nei[curFaceID],         // neighbour
00533                         false,                  // face flip
00534                         -1,                     // patch for face
00535                         false,                  // remove from zone
00536                         modifiedFaceZone,       // zone for face
00537                         modifiedFaceZoneFlip    // face flip in zone
00538                     )
00539                 );
00540 // Pout << "modifying stick-out face. Internal Old face: " << oldFace << " new face: " << newFace << " own: " << own[curFaceID] << " nei: " << nei[curFaceID] << endl;
00541             }
00542             else
00543             {
00544                 ref.setAction
00545                 (
00546                     polyModifyFace
00547                     (
00548                         newFace,                // modified face
00549                         curFaceID,              // label of face being modified
00550                         own[curFaceID],         // owner
00551                         -1,                     // neighbour
00552                         false,                  // face flip
00553                         mesh.boundaryMesh().whichPatch(curFaceID), // patch for face
00554                         false,                  // remove from zone
00555                         modifiedFaceZone,       // zone for face
00556                         modifiedFaceZoneFlip    // face flip in zone
00557                     )
00558                 );
00559 // Pout << "modifying stick-out face. Boundary Old face: " << oldFace << " new face: " << newFace << " own: " << own[curFaceID] << " patch: " << mesh.boundaryMesh().whichPatch(curFaceID) << endl;
00560             }
00561         }
00562     }
00563 
00564     if (debug)
00565     {
00566         Pout<< "void layerAdditionRemoval::addCellLayer("
00567             << "polyTopoChange& ref) const "
00568             << " for object " << name() << " : "
00569             << "Finished adding cell layer" << endl;
00570     }
00571 }
00572 
00573 
00574 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd