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

readCells.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 intermediate mesh from SAMM files
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "sammMesh.H"
00031 #include "IFstream.H"
00032 
00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00034 
00035 void sammMesh::addRegularCell
00036 (
00037     const labelList& labels,
00038     const label nCreatedCells
00039 )
00040 {
00041     // Momory management
00042     static labelList labelsHex(8);
00043     static labelList labelsWedge(7);
00044     static labelList labelsPrism(6);
00045     static labelList labelsPyramid(5);
00046     static labelList labelsTet(4);
00047     static labelList labelsTetWedge(5);
00048 
00049     if      // Tetrahedron
00050     (
00051         labels[2] == labels[3]
00052      && labels[4] == labels[5]
00053      && labels[5] == labels[6]
00054      && labels[6] == labels[7]
00055     )
00056     {
00057         labelsTet[0] = labels[0];
00058         labelsTet[1] = labels[1];
00059         labelsTet[2] = labels[2];
00060         labelsTet[3] = labels[4];
00061         cellShapes_[nCreatedCells] = cellShape(*tetPtr_, labelsTet);
00062     }
00063 
00064     else if // Square-based pyramid
00065     (
00066         labels[4] == labels[5]
00067      && labels[5] == labels[6]
00068      && labels[6] == labels[7]
00069     )
00070     {
00071         labelsPyramid[0] = labels[0];
00072         labelsPyramid[1] = labels[1];
00073         labelsPyramid[2] = labels[2];
00074         labelsPyramid[3] = labels[3];
00075         labelsPyramid[4] = labels[4];
00076         cellShapes_[nCreatedCells] = cellShape(*pyrPtr_, labelsPyramid);
00077     }
00078 
00079     else if // Tet Wedge
00080     (
00081         labels[2] == labels[3]
00082      && labels[4] == labels[5]
00083      && labels[6] == labels[7]
00084     )
00085     {
00086         labelsTetWedge[0] = labels[0];
00087         labelsTetWedge[1] = labels[1];
00088         labelsTetWedge[2] = labels[2];
00089         labelsTetWedge[3] = labels[4];
00090         labelsTetWedge[4] = labels[6];
00091         cellShapes_[nCreatedCells] = cellShape(*tetWedgePtr_, labelsTetWedge);
00092     }
00093 
00094     else if // Triangular prism
00095     (
00096         labels[2] == labels[3]
00097      && labels[6] == labels[7]
00098     )
00099     {
00100         labelsPrism[0] = labels[0];
00101         labelsPrism[1] = labels[1];
00102         labelsPrism[2] = labels[2];
00103         labelsPrism[3] = labels[4];
00104         labelsPrism[4] = labels[5];
00105         labelsPrism[5] = labels[6];
00106         cellShapes_[nCreatedCells] = cellShape(*prismPtr_, labelsPrism);
00107     }
00108 
00109     else if // Wedge
00110     (
00111         labels[4] == labels[7]
00112     )
00113     {
00114         labelsWedge[0] = labels[7];
00115         labelsWedge[1] = labels[6];
00116         labelsWedge[2] = labels[5];
00117         labelsWedge[3] = labels[3];
00118         labelsWedge[4] = labels[2];
00119         labelsWedge[5] = labels[1];
00120         labelsWedge[6] = labels[0];
00121         cellShapes_[nCreatedCells] = cellShape(*wedgePtr_, labelsWedge);
00122     }
00123 
00124     else    // Hex
00125     {
00126         labelsHex[0] = labels[0];
00127         labelsHex[1] = labels[1];
00128         labelsHex[2] = labels[2];
00129         labelsHex[3] = labels[3];
00130         labelsHex[4] = labels[4];
00131         labelsHex[5] = labels[5];
00132         labelsHex[6] = labels[6];
00133         labelsHex[7] = labels[7];
00134         cellShapes_[nCreatedCells] = cellShape(*hexPtr_, labelsHex);
00135     }
00136 }
00137 
00138 
00139 void sammMesh::addSAMMcell
00140 (
00141     const label typeFlag,
00142     const labelList& globalLabels,
00143     const label nCreatedCells
00144 )
00145 {
00146 
00147     // grab the shape from the table
00148     if (!sammShapeLookup[typeFlag] || !sammAddressingTable[typeFlag])
00149     {
00150         FatalErrorIn
00151         (
00152             "sammMesh::addRegularCell(const labelList& labels, "
00153             "const label nCreatedCells)"
00154         )   << "SAMM type " << typeFlag << " has no registered label. BUG!"
00155             << abort(FatalError);
00156     }
00157 
00158      const cellModel& curModel = *(sammShapeLookup[typeFlag]);
00159 
00160     // get reference to the addressing list
00161     const label* addressing = sammAddressingTable[typeFlag];
00162 
00163     // make a list of labels
00164     labelList sammCellLabels(curModel.nPoints(), -1);
00165 
00166     forAll (sammCellLabels, labelI)
00167     {
00168         sammCellLabels[labelI] = globalLabels[addressing[labelI]];
00169     }
00170 
00171     cellShapes_[nCreatedCells] = cellShape(curModel, sammCellLabels);
00172 }
00173 
00174 
00175 void sammMesh::readCells()
00176 {
00177     label nCells = 0;
00178     label maxLabel = -1;
00179 
00180     fileName cellsFileName(casePrefix_ + ".cel");
00181 
00182     {
00183         IFstream cellsFile(cellsFileName);
00184 
00185         if (cellsFile.good())
00186         {
00187             label lineLabel, cellLabel = -1, pointLabel, regionLabel, typeFlag;
00188 
00189             maxLabel = -1;
00190             while (!(cellsFile >> lineLabel).eof())
00191             {
00192                 maxLabel = max(maxLabel, lineLabel);
00193                 for (int i=0; i<8; i++)
00194                 {
00195                     cellsFile >> pointLabel;
00196                 }
00197 
00198                 cellsFile >> regionLabel;
00199                 cellsFile >> typeFlag;
00200 
00201                 if (lineLabel != cellLabel)
00202                 {
00203                     cellLabel = lineLabel;
00204                     nCells++;
00205                 }
00206             }
00207         }
00208         else
00209         {
00210             FatalErrorIn("sammMesh::readCells()")
00211                 << "Cannot read file "
00212                 << cellsFileName
00213                 << abort(FatalError);
00214         }
00215     }
00216 
00217     Info<< "Number of cells = " << nCells << endl << endl;
00218 
00219     cellShapes_.setSize(nCells);
00220 
00221     starCellLabelLookup_.setSize(maxLabel+1);
00222 
00223     // reset point labels to invalid value
00224     forAll (starCellLabelLookup_, i)
00225     {
00226         starCellLabelLookup_[i] = -1;
00227     }
00228 
00229 
00230     if (nCells > 0)
00231     {
00232         IFstream cellsFile(cellsFileName);
00233 
00234         labelList labels(24, -1);
00235         label lineLabel, sammLabel, regionLabel, typeFlag;
00236 
00237         for (label cellI = 0; cellI < nCells; cellI++)
00238         {
00239             label nLabels = 0;
00240 
00241             bool addOnToCell = false;
00242 
00243             do
00244             {
00245                 if (nLabels > 24)
00246                 {
00247                     FatalErrorIn("sammMesh::readCells()")
00248                         << "Unknown SAMM cell. "
00249                         << "More than 24 vertices"
00250                         << abort(FatalError);
00251                 }
00252 
00253                 if ((cellsFile >> lineLabel).eof())
00254                 {
00255                     FatalErrorIn("sammMesh::readCells()")
00256                         << "Reached end of cells file before "
00257                         << "all cells are read in."
00258                         << abort(FatalError);
00259                 }
00260 
00261                 // prepare for possible continuation
00262                 nLabels += 8;
00263 
00264                 for (int i=nLabels-8; i<nLabels; i++)
00265                 {
00266                     cellsFile >> sammLabel;
00267 
00268                     if (sammLabel != 0)
00269                     {
00270                         // Convert Samm vertex number to point label
00271                         labels[i] = starPointLabelLookup_[sammLabel];
00272 
00273                         if (labels[i] < 0)
00274                         {
00275                             Info<< "Cell file not consistent with vertex file. "
00276                                 << "Samm vertex number " << sammLabel
00277                                 << " does not exist\n";
00278                         }
00279                     }
00280                     else
00281                     {
00282                         labels[i] = -1;
00283                     }
00284                 }
00285 
00286                 cellsFile >> regionLabel;
00287                 cellsFile >> typeFlag;
00288 
00289                 // check for continuation line
00290                 if (!addOnToCell && typeFlag == 255)
00291                 {
00292                     addOnToCell = true;
00293                 }
00294                 else
00295                 {
00296                     addOnToCell = false;
00297                 }
00298 
00299             } while (typeFlag == -1 || addOnToCell);
00300 
00301             starCellLabelLookup_[lineLabel] = cellI;
00302 
00303             if (nLabels == 8)
00304             {
00305                 addRegularCell(labels, cellI);
00306             }
00307             else
00308             {
00309                 addSAMMcell(typeFlag, labels, cellI);
00310             }
00311         }
00312     }
00313     else
00314     {
00315         FatalErrorIn("sammMesh::readCells()")
00316             << "No cells in file "
00317             << cellsFileName
00318             << abort(FatalError);
00319     }
00320 }
00321 
00322 
00323 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd