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

detachInterface.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 "attachDetach.H"
00028 #include "polyMesh.H"
00029 #include "primitiveMesh.H"
00030 #include "polyTopoChange.H"
00031 #include "polyTopoChanger.H"
00032 #include "polyAddPoint.H"
00033 #include "polyModifyFace.H"
00034 #include "polyAddFace.H"
00035 
00036 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00037 
00038 void Foam::attachDetach::detachInterface
00039 (
00040     polyTopoChange& ref
00041 ) const
00042 {
00043     // Algorithm:
00044     // 1. Create new points for points of the master face zone
00045     // 2. Modify all faces of the master zone, by putting them into the master
00046     //    patch (look for orientation) and their renumbered mirror images
00047     //    into the slave patch
00048     // 3. Create a point renumbering list, giving a new point index for original
00049     //    points in the face patch
00050     // 4. Grab all faces in cells on the master side and renumber them 
00051     //    using the point renumbering list.  Exclude the ones that belong to
00052     //    the master face zone
00053     //
00054     // Note on point creation:
00055     // In order to take into account the issues related to partial
00056     // blocking in an attach/detach mesh modifier, special treatment
00057     // is required for the duplication of points on the edge of the
00058     // face zone.  Points which are shared only by internal edges need
00059     // not to be duplicated, as this would propagate the discontinuity
00060     // in the mesh beyond the face zone.  Therefore, before creating
00061     // the new points, check the external edge loop.  For each edge
00062     // check if the edge is internal (i.e. does not belong to a
00063     // patch); if so, exclude both of its points from duplication.
00064 
00065     if (debug)
00066     {
00067         Pout<< "void attachDetach::detachInterface("
00068             << "polyTopoChange& ref) const "
00069             << " for object " << name() << " : "
00070             << "Detaching interface" << endl;
00071     }
00072 
00073     const polyMesh& mesh = topoChanger().mesh();
00074     const faceZoneMesh& zoneMesh = mesh.faceZones();
00075 
00076     // Check that zone is in increasing order (needed since adding faces
00077     // in same order - otherwise polyTopoChange face ordering will mess up
00078     // correspondence)
00079     if (debug)
00080     {
00081         const labelList& faceLabels = zoneMesh[faceZoneID_.index()];
00082         if (faceLabels.size() > 0)
00083         {
00084             for (label i = 1; i < faceLabels.size(); i++)
00085             {
00086                 if (faceLabels[i] <= faceLabels[i-1])
00087                 {
00088                     FatalErrorIn
00089                     (
00090                         "attachDetach::detachInterface"
00091                         "(polyTopoChange&) const"
00092                     )   << "faceZone " << zoneMesh[faceZoneID_.index()].name()
00093                         << " does not have mesh face labels in"
00094                         << " increasing order." << endl
00095                         << "Face label " << faceLabels[i]
00096                         << " at position " << i
00097                         << " is smaller than the previous value "
00098                         << faceLabels[i-1]
00099                         << exit(FatalError);
00100                 }
00101             }
00102         }
00103     }
00104 
00105 
00106 
00107     const primitiveFacePatch& masterFaceLayer = zoneMesh[faceZoneID_.index()]();
00108     const pointField& points = mesh.points();
00109     const labelListList& meshEdgeFaces = mesh.edgeFaces();
00110 
00111     const labelList& mp = masterFaceLayer.meshPoints();
00112     const edgeList& zoneLocalEdges = masterFaceLayer.edges();
00113 
00114     const labelList& meshEdges = zoneMesh[faceZoneID_.index()].meshEdges();
00115 
00116     // Create the points
00117 
00118     labelList addedPoints(mp.size(), -1);
00119 
00120     // Go through boundary edges of the master patch.  If all the faces from
00121     // this patch are internal, mark the points in the addedPoints lookup
00122     // with their original labels to stop duplication
00123     label nIntEdges = masterFaceLayer.nInternalEdges();
00124 
00125     for (label curEdgeID = nIntEdges; curEdgeID < meshEdges.size(); curEdgeID++)
00126     {
00127         const labelList& curFaces = meshEdgeFaces[meshEdges[curEdgeID]];
00128 
00129         bool edgeIsInternal = true;
00130 
00131         forAll (curFaces, faceI)
00132         {
00133             if (!mesh.isInternalFace(curFaces[faceI]))
00134             {
00135                 // The edge belongs to a boundary face
00136                 edgeIsInternal = false;
00137                 break;
00138             }
00139         }
00140 
00141         if (edgeIsInternal)
00142         {
00143             const edge& e = zoneLocalEdges[curEdgeID];
00144 
00145             // Reset the point creation
00146             addedPoints[e.start()] = mp[e.start()];
00147             addedPoints[e.end()] = mp[e.end()];
00148         }
00149     }
00150 // Pout << "addedPoints before point creation: " << addedPoints << endl;
00151 
00152     // Create new points for face zone
00153     forAll (addedPoints, pointI)
00154     {
00155         if (addedPoints[pointI] < 0)
00156         {
00157             addedPoints[pointI] =
00158                 ref.setAction
00159                 (
00160                     polyAddPoint
00161                     (
00162                         points[mp[pointI]],        // point
00163                         mp[pointI],                // master point
00164                         -1,                        // zone ID
00165                         true                       // supports a cell
00166                     )
00167                 );
00168             //Pout<< "Adding point " << addedPoints[pointI]
00169             //    << " coord1:" << points[mp[pointI]]
00170             //    << " coord2:" << masterFaceLayer.localPoints()[pointI]
00171             //    << " for original point " << mp[pointI] << endl;
00172         }
00173     }
00174 
00175     // Modify faces in the master zone and duplicate for the slave zone
00176 
00177     const labelList& mf = zoneMesh[faceZoneID_.index()];
00178     const boolList& mfFlip = zoneMesh[faceZoneID_.index()].flipMap();
00179     const faceList& zoneFaces = masterFaceLayer.localFaces();
00180 
00181     const faceList& faces = mesh.faces();
00182     const labelList& own = mesh.faceOwner();
00183     const labelList& nei = mesh.faceNeighbour();
00184 
00185     forAll (mf, faceI)
00186     {
00187         const label curFaceID = mf[faceI];
00188 
00189         // Build the face for the slave patch by renumbering
00190         const face oldFace = zoneFaces[faceI].reverseFace();
00191 
00192         face newFace(oldFace.size());
00193 
00194         forAll (oldFace, pointI)
00195         {
00196             newFace[pointI] = addedPoints[oldFace[pointI]];
00197         }
00198 
00199         if (mfFlip[faceI])
00200         {
00201             // Face needs to be flipped for the master patch
00202             ref.setAction
00203             (
00204                 polyModifyFace
00205                 (
00206                     faces[curFaceID].reverseFace(), // modified face
00207                     curFaceID,                   // label of face being modified
00208                     nei[curFaceID],                 // owner
00209                     -1,                             // neighbour
00210                     true,                           // face flip
00211                     masterPatchID_.index(),         // patch for face
00212                     false,                          // remove from zone
00213                     faceZoneID_.index(),            // zone for face
00214                     !mfFlip[faceI]                  // face flip in zone
00215                 )
00216             );
00217 
00218             // Add renumbered face into the slave patch
00219             //label addedFaceI =
00220             ref.setAction
00221             (
00222                 polyAddFace
00223                 (
00224                     newFace,                        // face
00225                     own[curFaceID],                 // owner
00226                     -1,                             // neighbour
00227                     -1,                             // master point
00228                     -1,                             // master edge
00229                     curFaceID,                      // master face
00230                     false,                          // flip flux
00231                     slavePatchID_.index(),          // patch to add the face to
00232                     -1,                             // zone for face
00233                     false                           // zone flip
00234                 )
00235             );
00236             //{
00237             //    pointField newPts(ref.points());
00238             //Pout<< "Flip.  Modifying face: " << ref.faces()[curFaceID]
00239             //    << " fc:" <<  ref.faces()[curFaceID].centre(newPts)
00240             //    << " next to cell: " << nei[curFaceID]
00241             //    << " and adding face: " << newFace
00242             //    << " fc:" << ref.faces()[addedFaceI].centre(newPts)
00243             //    << " next to cell: " << own[curFaceID] << endl;
00244             //}
00245         }
00246         else
00247         {
00248             // No flip
00249             ref.setAction
00250             (
00251                 polyModifyFace
00252                 (
00253                     faces[curFaceID],         // modified face
00254                     curFaceID,                // label of face being modified
00255                     own[curFaceID],           // owner
00256                     -1,                       // neighbour
00257                     false,                    // face flip
00258                     masterPatchID_.index(),   // patch for face
00259                     false,                    // remove from zone
00260                     faceZoneID_.index(),      // zone for face
00261                     mfFlip[faceI]             // face flip in zone
00262                 )
00263             );
00264 
00265             // Add renumbered face into the slave patch
00266             //label addedFaceI =
00267             ref.setAction
00268             (
00269                 polyAddFace
00270                 (
00271                     newFace,                        // face
00272                     nei[curFaceID],                 // owner
00273                     -1,                             // neighbour
00274                     -1,                             // master point
00275                     -1,                             // master edge
00276                     curFaceID,                      // master face
00277                     true,                           // flip flux
00278                     slavePatchID_.index(),          // patch to add the face to
00279                     -1,                             // zone for face
00280                     false                           // face flip in zone
00281                 )
00282             );
00283             //{
00284             //    pointField newPts(ref.points());
00285             //Pout<< "No flip.  Modifying face: " << ref.faces()[curFaceID]
00286             //    << " fc:" <<  ref.faces()[curFaceID].centre(newPts)
00287             //    << " next to cell: " << own[curFaceID]
00288             //    << " and adding face: " << newFace
00289             //    << " fc:" << ref.faces()[addedFaceI].centre(newPts)
00290             //    << " next to cell: " << nei[curFaceID] << endl;
00291             //}
00292         }
00293     }
00294 
00295     // Modify the remaining faces of the master cells to reconnect to the new
00296     // layer of faces.
00297 
00298     // Algorithm: Go through all the cells of the master zone and make
00299     // a map of faces to avoid duplicates.  Do not insert the faces in
00300     // the master patch (as they have already been dealt with).  Make
00301     // a master layer point renumbering map, which for every point in
00302     // the master layer gives its new label. Loop through all faces in
00303     // the map and attempt to renumber them using the master layer
00304     // point renumbering map.  Once the face is renumbered, compare it
00305     // with the original face; if they are the same, the face has not
00306     // changed; if not, modify the face but keep all of its old
00307     // attributes (apart from the vertex numbers).
00308 
00309     // Create the map of faces in the master cell layer
00310     const labelList& mc =
00311         mesh.faceZones()[faceZoneID_.index()].masterCells();
00312 
00313     labelHashSet masterCellFaceMap(6*mc.size());
00314 
00315     const cellList& cells = mesh.cells();
00316 
00317     forAll (mc, cellI)
00318     {
00319         const labelList& curFaces = cells[mc[cellI]];
00320 
00321         forAll (curFaces, faceI)
00322         {
00323             // Check if the face belongs to the master patch; if not add it
00324             if (zoneMesh.whichZone(curFaces[faceI]) != faceZoneID_.index())
00325             {
00326                 masterCellFaceMap.insert(curFaces[faceI]);
00327             }
00328         }
00329     }
00330 
00331     // Extend the map to include first neighbours of the master cells to
00332     // deal with multiple corners.
00333     { // Protection and memory management
00334         // Make a map of master cells for quick reject
00335         labelHashSet mcMap(2*mc.size());
00336 
00337         forAll (mc, mcI)
00338         {
00339             mcMap.insert(mc[mcI]);
00340         }
00341 
00342         // Go through all the faces in the masterCellFaceMap.  If the
00343         // cells around them are not already used, add all of their
00344         // faces to the map
00345         const labelList mcf = masterCellFaceMap.toc();
00346 
00347         forAll (mcf, mcfI)
00348         {
00349             // Do the owner side
00350             const label ownCell = own[mcf[mcfI]];
00351 
00352             if (!mcMap.found(ownCell))
00353             {
00354                 // Cell not found. Add its faces to the map
00355                 const cell& curFaces = cells[ownCell];
00356 
00357                 forAll (curFaces, faceI)
00358                 {
00359                     masterCellFaceMap.insert(curFaces[faceI]);
00360                 }
00361             }
00362 
00363             // Do the neighbour side if face is internal
00364             if (mesh.isInternalFace(mcf[mcfI]))
00365             {
00366                 const label neiCell = nei[mcf[mcfI]];
00367 
00368                 if (!mcMap.found(neiCell))
00369                 {
00370                     // Cell not found. Add its faces to the map
00371                     const cell& curFaces = cells[neiCell];
00372 
00373                     forAll (curFaces, faceI)
00374                     {
00375                         masterCellFaceMap.insert(curFaces[faceI]);
00376                     }
00377                 }
00378             }
00379         }
00380     }
00381 
00382     // Create the master layer point map
00383     Map<label> masterLayerPointMap(2*mp.size());
00384 
00385     forAll (mp, pointI)
00386     {
00387         masterLayerPointMap.insert
00388         (
00389             mp[pointI],
00390             addedPoints[pointI]
00391         );
00392     }
00393 
00394     // Grab the list of faces of the master layer
00395     const labelList masterCellFaces = masterCellFaceMap.toc();
00396 
00397     forAll (masterCellFaces, faceI)
00398     {
00399         // Attempt to renumber the face using the masterLayerPointMap.
00400         // Missing point remain the same
00401 
00402         const label curFaceID = masterCellFaces[faceI];
00403 
00404         const face& oldFace = faces[curFaceID];
00405 
00406         face newFace(oldFace.size());
00407 
00408         bool changed = false;
00409 
00410         forAll (oldFace, pointI)
00411         {
00412             if (masterLayerPointMap.found(oldFace[pointI]))
00413             {
00414                 changed = true;
00415 
00416                 newFace[pointI] = masterLayerPointMap.find(oldFace[pointI])();
00417             }
00418             else
00419             {
00420                 newFace[pointI] = oldFace[pointI];
00421             }
00422         }
00423 
00424         // If the face has changed, create a modification entry
00425         if (changed)
00426         {
00427             if (mesh.isInternalFace(curFaceID))
00428             {
00429                 ref.setAction
00430                 (
00431                     polyModifyFace
00432                     (
00433                         newFace,                    // face
00434                         curFaceID,                  // master face
00435                         own[curFaceID],             // owner
00436                         nei[curFaceID],             // neighbour
00437                         false,                      // flip flux
00438                         -1,                         // patch for face
00439                         false,                      // remove from zone
00440                         -1,                         // zone for face
00441                         false                       // face zone flip
00442                     )
00443                 );
00444 // Pout << "modifying stick-out face. Internal Old face: " << oldFace << " new face: " << newFace << " own: " << own[curFaceID] << " nei: " << nei[curFaceID] << endl;
00445             }
00446             else
00447             {
00448                 ref.setAction
00449                 (
00450                     polyModifyFace
00451                     (
00452                         newFace,                     // face
00453                         curFaceID,                   // master face
00454                         own[curFaceID],              // owner
00455                         -1,                          // neighbour
00456                         false,                       // flip flux
00457                         mesh.boundaryMesh().whichPatch(curFaceID), // patch
00458                         false,                        // remove from zone
00459                         -1,                           // zone for face
00460                         false                         // face zone flip
00461                     )
00462                 );   
00463 // Pout << "modifying stick-out face. Boundary Old face: " << oldFace << " new face: " << newFace << " own: " << own[curFaceID] << " patch: " << mesh.boundaryMesh().whichPatch(curFaceID) << endl;
00464             }                                                  
00465         }
00466     }
00467 
00468     if (debug)
00469     {
00470         Pout<< "void attachDetach::detachInterface("
00471             << "polyTopoChange& ref) const "
00472             << " for object " << name() << " : "
00473             << "Finished detaching interface" << endl;
00474     }
00475 }
00476 
00477 
00478 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd