FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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