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

polyMeshFromShapeMesh.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     Create polyMesh from cell and patch shapes
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "polyMesh.H"
00031 #include "Time.H"
00032 #include "primitiveMesh.H"
00033 #include "DynamicList.H"
00034 
00035 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00036 
00037 Foam::labelListList Foam::polyMesh::cellShapePointCells
00038 (
00039     const cellShapeList& c
00040 ) const
00041 {
00042     List<DynamicList<label, primitiveMesh::cellsPerPoint_> >
00043         pc(points().size());
00044 
00045     // For each cell
00046     forAll(c, i)
00047     {
00048         // For each vertex
00049         const labelList& labels = c[i];
00050 
00051         forAll(labels, j)
00052         {
00053             // Set working point label
00054             label curPoint = labels[j];
00055             DynamicList<label, primitiveMesh::cellsPerPoint_>& curPointCells =
00056                 pc[curPoint];
00057 
00058             // Enter the cell label in the point's cell list
00059             curPointCells.append(i);
00060         }
00061     }
00062 
00063     labelListList pointCellAddr(pc.size());
00064 
00065     forAll (pc, pointI)
00066     {
00067         pointCellAddr[pointI].transfer(pc[pointI]);
00068     }
00069 
00070     return pointCellAddr;
00071 }
00072 
00073 
00074 Foam::labelList Foam::polyMesh::facePatchFaceCells
00075 (
00076     const faceList& patchFaces,
00077     const labelListList& pointCells,
00078     const faceListList& cellsFaceShapes,
00079     const label patchID
00080 ) const
00081 {
00082     register bool found;
00083 
00084     labelList FaceCells(patchFaces.size());
00085 
00086     forAll(patchFaces, fI)
00087     {
00088         found = false;
00089 
00090         const face& curFace = patchFaces[fI];
00091         const labelList& facePoints = patchFaces[fI];
00092 
00093         forAll(facePoints, pointI)
00094         {
00095             const labelList& facePointCells = pointCells[facePoints[pointI]];
00096 
00097             forAll(facePointCells, cellI)
00098             {
00099                 faceList cellFaces = cellsFaceShapes[facePointCells[cellI]];
00100 
00101                 forAll(cellFaces, cellFace)
00102                 {
00103                     if (cellFaces[cellFace] == curFace)
00104                     {
00105                         // Found the cell corresponding to this face
00106                         FaceCells[fI] = facePointCells[cellI];
00107 
00108                         found = true;
00109                     }
00110                     if (found) break;
00111                 }
00112                 if (found) break;
00113             }
00114             if (found) break;
00115         }
00116 
00117         if (!found)
00118         {
00119             FatalErrorIn
00120             (
00121                 "polyMesh::facePatchFaceCells(const faceList& patchFaces,"
00122                 "const labelListList& pointCells,"
00123                 "const faceListList& cellsFaceShapes,"
00124                 "const label patchID)"
00125             )   << "face " << fI << " in patch " << patchID
00126                 << " does not have neighbour cell"
00127                 << " face: " << patchFaces[fI]
00128                 << abort(FatalError);
00129         }
00130     }
00131 
00132     return FaceCells;
00133 }
00134 
00135 
00136 Foam::polyMesh::polyMesh
00137 (
00138     const IOobject& io,
00139     const Xfer<pointField>& points,
00140     const cellShapeList& cellsAsShapes,
00141     const faceListList& boundaryFaces,
00142     const wordList& boundaryPatchNames,
00143     const wordList& boundaryPatchTypes,
00144     const word& defaultBoundaryPatchName,
00145     const word& defaultBoundaryPatchType,
00146     const wordList& boundaryPatchPhysicalTypes,
00147     const bool syncPar
00148 )
00149 :
00150     objectRegistry(io),
00151     primitiveMesh(),
00152     points_
00153     (
00154         IOobject
00155         (
00156             "points",
00157             instance(),
00158             meshSubDir,
00159             *this,
00160             IOobject::NO_READ,
00161             IOobject::AUTO_WRITE
00162         ),
00163         points
00164     ),
00165     faces_
00166     (
00167         IOobject
00168         (
00169             "faces",
00170             instance(),
00171             meshSubDir,
00172             *this,
00173             IOobject::NO_READ,
00174             IOobject::AUTO_WRITE
00175         ),
00176         0
00177     ),
00178     owner_
00179     (
00180         IOobject
00181         (
00182             "owner",
00183             instance(),
00184             meshSubDir,
00185             *this,
00186             IOobject::NO_READ,
00187             IOobject::AUTO_WRITE
00188         ),
00189         0
00190     ),
00191     neighbour_
00192     (
00193         IOobject
00194         (
00195             "neighbour",
00196             instance(),
00197             meshSubDir,
00198             *this,
00199             IOobject::NO_READ,
00200             IOobject::AUTO_WRITE
00201         ),
00202         0
00203     ),
00204     clearedPrimitives_(false),
00205     boundary_
00206     (
00207         IOobject
00208         (
00209             "boundary",
00210             instance(),
00211             meshSubDir,
00212             *this,
00213             IOobject::NO_READ,
00214             IOobject::AUTO_WRITE
00215         ),
00216         *this,
00217         boundaryFaces.size() + 1    // add room for a default patch
00218     ),
00219     bounds_(points_, syncPar),
00220     geometricD_(Vector<label>::zero),
00221     solutionD_(Vector<label>::zero),
00222     pointZones_
00223     (
00224         IOobject
00225         (
00226             "pointZones",
00227             instance(),
00228             meshSubDir,
00229             *this,
00230             IOobject::NO_READ,
00231             IOobject::NO_WRITE
00232         ),
00233         *this,
00234         0
00235     ),
00236     faceZones_
00237     (
00238         IOobject
00239         (
00240             "faceZones",
00241             instance(),
00242             meshSubDir,
00243             *this,
00244             IOobject::NO_READ,
00245             IOobject::NO_WRITE
00246         ),
00247         *this,
00248         0
00249     ),
00250     cellZones_
00251     (
00252         IOobject
00253         (
00254             "cellZones",
00255             instance(),
00256             meshSubDir,
00257             *this,
00258             IOobject::NO_READ,
00259             IOobject::NO_WRITE
00260         ),
00261         *this,
00262         0
00263     ),
00264     globalMeshDataPtr_(NULL),
00265     moving_(false),
00266     curMotionTimeIndex_(time().timeIndex()),
00267     oldPointsPtr_(NULL)
00268 {
00269     if (debug)
00270     {
00271         Info<<"Constructing polyMesh from cell and boundary shapes." << endl;
00272     }
00273 
00274     // Remove all of the old mesh files if they exist
00275     removeFiles(instance());
00276 
00277     // Calculate the faces of all cells
00278     // Initialise maximum possible numer of mesh faces to 0
00279     label maxFaces = 0;
00280 
00281     // Set up a list of face shapes for each cell
00282     faceListList cellsFaceShapes(cellsAsShapes.size());
00283     cellList cells(cellsAsShapes.size());
00284 
00285     forAll(cellsFaceShapes, cellI)
00286     {
00287         cellsFaceShapes[cellI] = cellsAsShapes[cellI].faces();
00288 
00289         cells[cellI].setSize(cellsFaceShapes[cellI].size());
00290 
00291         // Initialise cells to -1 to flag undefined faces
00292         static_cast<labelList&>(cells[cellI]) = -1;
00293 
00294         // Count maximum possible numer of mesh faces
00295         maxFaces += cellsFaceShapes[cellI].size();
00296     }
00297 
00298     // Set size of faces array to maximum possible number of mesh faces
00299     faces_.setSize(maxFaces);
00300 
00301     // Initialise number of faces to 0
00302     label nFaces = 0;
00303 
00304     // set reference to point-cell addressing
00305     labelListList PointCells = cellShapePointCells(cellsAsShapes);
00306 
00307     bool found = false;
00308 
00309     forAll(cells, cellI)
00310     {
00311         // Note:
00312         // Insertion cannot be done in one go as the faces need to be
00313         // added into the list in the increasing order of neighbour
00314         // cells.  Therefore, all neighbours will be detected first
00315         // and then added in the correct order.
00316 
00317         const faceList& curFaces = cellsFaceShapes[cellI];
00318 
00319         // Record the neighbour cell
00320         labelList neiCells(curFaces.size(), -1);
00321 
00322         // Record the face of neighbour cell
00323         labelList faceOfNeiCell(curFaces.size(), -1);
00324 
00325         label nNeighbours = 0;
00326 
00327         // For all faces ...
00328         forAll(curFaces, faceI)
00329         {
00330             // Skip faces that have already been matched
00331             if (cells[cellI][faceI] >= 0) continue;
00332 
00333             found = false;
00334 
00335             const face& curFace = curFaces[faceI];
00336 
00337             // Get the list of labels
00338             const labelList& curPoints = curFace;
00339 
00340             // For all points
00341             forAll(curPoints, pointI)
00342             {
00343                 // dGget the list of cells sharing this point
00344                 const labelList& curNeighbours =
00345                     PointCells[curPoints[pointI]];
00346 
00347                 // For all neighbours
00348                 forAll(curNeighbours, neiI)
00349                 {
00350                     label curNei = curNeighbours[neiI];
00351 
00352                     // Reject neighbours with the lower label
00353                     if (curNei > cellI)
00354                     {
00355                         // Get the list of search faces
00356                         const faceList& searchFaces = cellsFaceShapes[curNei];
00357 
00358                         forAll(searchFaces, neiFaceI)
00359                         {
00360                             if (searchFaces[neiFaceI] == curFace)
00361                             {
00362                                 // Match!!
00363                                 found = true;
00364 
00365                                 // Record the neighbour cell and face
00366                                 neiCells[faceI] = curNei;
00367                                 faceOfNeiCell[faceI] = neiFaceI;
00368                                 nNeighbours++;
00369 
00370                                 break;
00371                             }
00372                         }
00373                         if (found) break;
00374                     }
00375                     if (found) break;
00376                 }
00377                 if (found) break;
00378             } // End of current points
00379         }  // End of current faces
00380 
00381         // Add the faces in the increasing order of neighbours
00382         for (label neiSearch = 0; neiSearch < nNeighbours; neiSearch++)
00383         {
00384             // Find the lowest neighbour which is still valid
00385             label nextNei = -1;
00386             label minNei = cells.size();
00387 
00388             forAll (neiCells, ncI)
00389             {
00390                 if (neiCells[ncI] > -1 && neiCells[ncI] < minNei)
00391                 {
00392                     nextNei = ncI;
00393                     minNei = neiCells[ncI];
00394                 }
00395             }
00396 
00397             if (nextNei > -1)
00398             {
00399                 // Add the face to the list of faces
00400                 faces_[nFaces] = curFaces[nextNei];
00401 
00402                 // Set cell-face and cell-neighbour-face to current face label
00403                 cells[cellI][nextNei] = nFaces;
00404                 cells[neiCells[nextNei]][faceOfNeiCell[nextNei]] = nFaces;
00405 
00406                 // Stop the neighbour from being used again
00407                 neiCells[nextNei] = -1;
00408 
00409                 // Increment number of faces counter
00410                 nFaces++;
00411             }
00412             else
00413             {
00414                 FatalErrorIn
00415                 (
00416                     "polyMesh::polyMesh\n"
00417                     "(\n"
00418                     "    const IOobject&,\n"
00419                     "    const Xfer<pointField>&,\n"
00420                     "    const cellShapeList& cellsAsShapes,\n"
00421                     "    const faceListList& boundaryFaces,\n"
00422                     "    const wordList& boundaryPatchTypes,\n"
00423                     "    const wordList& boundaryPatchNames,\n"
00424                     "    const word& defaultBoundaryPatchType\n"
00425                     ")"
00426                 )   << "Error in internal face insertion"
00427                     << abort(FatalError);
00428             }
00429         }
00430     }
00431 
00432     // Do boundary faces
00433 
00434     labelList patchSizes(boundaryFaces.size(), -1);
00435     labelList patchStarts(boundaryFaces.size(), -1);
00436 
00437     forAll (boundaryFaces, patchI)
00438     {
00439         const faceList& patchFaces = boundaryFaces[patchI];
00440 
00441         labelList curPatchFaceCells =
00442             facePatchFaceCells
00443             (
00444                 patchFaces,
00445                 PointCells,
00446                 cellsFaceShapes,
00447                 patchI
00448             );
00449 
00450         // Grab the start label
00451         label curPatchStart = nFaces;
00452 
00453         forAll (patchFaces, faceI)
00454         {
00455             const face& curFace = patchFaces[faceI];
00456 
00457             const label cellInside = curPatchFaceCells[faceI];
00458 
00459             faces_[nFaces] = curFace;
00460 
00461             // get faces of the cell inside
00462             const faceList& facesOfCellInside = cellsFaceShapes[cellInside];
00463 
00464             bool found = false;
00465 
00466             forAll (facesOfCellInside, cellFaceI)
00467             {
00468                 if (facesOfCellInside[cellFaceI] == curFace)
00469                 {
00470                     if (cells[cellInside][cellFaceI] >= 0)
00471                     {
00472                         FatalErrorIn
00473                         (
00474                             "polyMesh::polyMesh\n"
00475                             "(\n"
00476                             "    const IOobject&,\n"
00477                             "    const Xfer<pointField>&,\n"
00478                             "    const cellShapeList& cellsAsShapes,\n"
00479                             "    const faceListList& boundaryFaces,\n"
00480                             "    const wordList& boundaryPatchTypes,\n"
00481                             "    const wordList& boundaryPatchNames,\n"
00482                             "    const word& defaultBoundaryPatchType\n"
00483                             ")"
00484                         )   << "Trying to specify a boundary face " << curFace
00485                             << " on the face on cell " << cellInside
00486                             << " which is either an internal face or already "
00487                             << "belongs to some other patch.  This is face "
00488                             << faceI << " of patch "
00489                             << patchI << " named "
00490                             << boundaryPatchNames[patchI] << "."
00491                             << abort(FatalError);
00492                     }
00493 
00494                     found = true;
00495 
00496                     cells[cellInside][cellFaceI] = nFaces;
00497 
00498                     break;
00499                 }
00500             }
00501 
00502             if (!found)
00503             {
00504                 FatalErrorIn("polyMesh::polyMesh(... construct from shapes...)")
00505                     << "face " << faceI << " of patch " << patchI
00506                     << " does not seem to belong to cell " << cellInside
00507                     << " which, according to the addressing, "
00508                     << "should be next to it."
00509                     << abort(FatalError);
00510             }
00511 
00512             // increment the counter of faces
00513             nFaces++;
00514         }
00515 
00516         patchSizes[patchI] = nFaces - curPatchStart;
00517         patchStarts[patchI] = curPatchStart;
00518     }
00519 
00520     // Grab "non-existing" faces and put them into a default patch
00521 
00522     label defaultPatchStart = nFaces;
00523 
00524     forAll(cells, cellI)
00525     {
00526         labelList& curCellFaces = cells[cellI];
00527 
00528         forAll(curCellFaces, faceI)
00529         {
00530             if (curCellFaces[faceI] == -1) // "non-existent" face
00531             {
00532                 curCellFaces[faceI] = nFaces;
00533                 faces_[nFaces] = cellsFaceShapes[cellI][faceI];
00534 
00535                 nFaces++;
00536             }
00537         }
00538     }
00539 
00540     // Reset the size of the face list
00541     faces_.setSize(nFaces);
00542 
00543     // Warning: Patches can only be added once the face list is
00544     // completed, as they hold a subList of the face list
00545     forAll (boundaryFaces, patchI)
00546     {
00547         // add the patch to the list
00548         boundary_.set
00549         (
00550             patchI,
00551             polyPatch::New
00552             (
00553                 boundaryPatchTypes[patchI],
00554                 boundaryPatchNames[patchI],
00555                 patchSizes[patchI],
00556                 patchStarts[patchI],
00557                 patchI,
00558                 boundary_
00559             )
00560         );
00561 
00562         if
00563         (
00564             boundaryPatchPhysicalTypes.size()
00565          && boundaryPatchPhysicalTypes[patchI].size()
00566         )
00567         {
00568             boundary_[patchI].physicalType() =
00569                 boundaryPatchPhysicalTypes[patchI];
00570         }
00571     }
00572 
00573     label nAllPatches = boundaryFaces.size();
00574 
00575     if (nFaces > defaultPatchStart)
00576     {
00577         WarningIn("polyMesh::polyMesh(... construct from shapes...)")
00578             << "Found " << nFaces - defaultPatchStart
00579             << " undefined faces in mesh; adding to default patch." << endl;
00580 
00581         boundary_.set
00582         (
00583             nAllPatches,
00584             polyPatch::New
00585             (
00586                 defaultBoundaryPatchType,
00587                 defaultBoundaryPatchName,
00588                 nFaces - defaultPatchStart,
00589                 defaultPatchStart,
00590                 boundary_.size() - 1,
00591                 boundary_
00592             )
00593         );
00594 
00595         nAllPatches++;
00596     }
00597 
00598     // Reset the size of the boundary
00599     boundary_.setSize(nAllPatches);
00600 
00601     // Set the primitive mesh
00602     initMesh(cells);
00603 
00604     if (syncPar)
00605     {
00606         // Calculate topology for the patches (processor-processor comms etc.)
00607         boundary_.updateMesh();
00608 
00609         // Calculate the geometry for the patches (transformation tensors etc.)
00610         boundary_.calcGeometry();
00611     }
00612 
00613     if (debug)
00614     {
00615         if (checkMesh())
00616         {
00617             Info << "Mesh OK" << endl;
00618         }
00619     }
00620 }
00621 
00622 
00623 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd