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 Prostar files
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "starMesh.H"
00030 #include <OpenFOAM/IFstream.H>
00031 
00032 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00033 
00034 void starMesh::addRegularCell
00035 (
00036     const labelList& labels,
00037     const label nCreatedCells
00038 )
00039 {
00040     // Momory management
00041     static labelList labelsHex(8);
00042     static labelList labelsPrism(6);
00043     static labelList labelsPyramid(5);
00044     static labelList labelsTet(4);
00045     static labelList labelsTetWedge(5);
00046 
00047     label regularTypeFlag = -1;
00048 
00049     // grab the shape from the table
00050     const cellModel* curModelPtr = reinterpret_cast<cellModel*>(0);
00051 
00052     if      // Tetrahedron
00053     (
00054         labels[2] == labels[3]
00055      && labels[4] == labels[5]
00056      && labels[5] == labels[6]
00057      && labels[6] == labels[7]
00058     )
00059     {
00060         regularTypeFlag = 0;
00061         curModelPtr = tetPtr_;
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         regularTypeFlag = 1;
00071         curModelPtr = pyrPtr_;
00072     }
00073     else if // Tet Wedge
00074     (
00075         labels[2] == labels[3]
00076      && labels[4] == labels[5]
00077      && labels[6] == labels[7]
00078     )
00079     {
00080         regularTypeFlag = 2;
00081         curModelPtr = tetWedgePtr_;
00082     }
00083     else if // Triangular prism
00084     (
00085         labels[2] == labels[3]
00086      && labels[6] == labels[7]
00087     )
00088     {
00089         regularTypeFlag = 3;
00090         curModelPtr = prismPtr_;
00091     }
00092     else if // Wedge
00093     (
00094         labels[4] == labels[7]
00095     )
00096     {
00097         regularTypeFlag = 4;
00098         curModelPtr = wedgePtr_;
00099     }
00100     else    // Hex
00101     {
00102         regularTypeFlag = 5;
00103         curModelPtr = hexPtr_;
00104     }
00105 
00106     labelList regularCellLabels(curModelPtr->nPoints(), -1);
00107     // get reference to the addressing list
00108     const label* addressing = regularAddressingTable[regularTypeFlag];
00109 
00110     forAll (regularCellLabels, labelI)
00111     {
00112         regularCellLabels[labelI] = labels[addressing[labelI]];
00113     }
00114 
00115     cellShapes_[nCreatedCells] = cellShape(*curModelPtr, regularCellLabels);
00116 }
00117 
00118 
00119 void starMesh::addSAMMcell
00120 (
00121     const labelList& labels,
00122     const label nCreatedCells
00123 )
00124 {
00125     // get type, reg and permutation flag
00126     label typeFlag = labels[21];
00127 //     label regularityFlag = labels[22];  // Not used. 
00128     label permutationFlag = labels[23];
00129 
00130     // grab the shape from the table
00131     label sammTypeFlag = -1;
00132     const cellModel* curModelPtr = reinterpret_cast<cellModel*>(0);
00133 
00134     switch (typeFlag)
00135     {
00136         case 1:
00137         {
00138             sammTypeFlag = 1;
00139             curModelPtr = sammTrim1Ptr_;
00140             break;
00141         }
00142 
00143         case 2:
00144         {
00145             sammTypeFlag = 2;
00146             curModelPtr = sammTrim2Ptr_;
00147             break;
00148         }
00149 
00150         case 7:
00151         {
00152             if (labels[0] != -1)
00153             {
00154                 sammTypeFlag = 3;
00155                 curModelPtr = sammTrim3Ptr_;
00156             }
00157             else
00158             {
00159                 sammTypeFlag = 5;
00160                 curModelPtr = sammTrim5Ptr_;
00161             }
00162 
00163             break;
00164         }
00165 
00166         case 8:
00167         {
00168             sammTypeFlag = 4;
00169             curModelPtr = sammTrim4Ptr_;
00170             break;
00171         }
00172 
00173         case 85:
00174         {
00175             sammTypeFlag = 8;
00176             curModelPtr = sammTrim8Ptr_;
00177             break;
00178         }
00179 
00180         default:
00181         {
00182             FatalErrorIn
00183             (
00184                 "starMesh::addSAMMcell"
00185                 "(const labelList& labels, const label nCreatedCells)"
00186             )   << "SAMM type " << sammTypeFlag << " is invalid"
00187                 << abort(FatalError);
00188         }
00189     }
00190 
00191     // make a list of labels
00192     labelList sammCellLabels(curModelPtr->nPoints(), -1);
00193     // get reference to the addressing list
00194     const label* addressing = sammAddressingTable[sammTypeFlag];
00195 
00196     forAll (sammCellLabels, labelI)
00197     {
00198         sammCellLabels[labelI] = labels[addressing[labelI]];
00199     }
00200 
00201     cellShapes_[nCreatedCells] = cellShape(*curModelPtr, sammCellLabels);
00202 
00203     // set permutation flag for cell
00204     starCellPermutation_[nCreatedCells] = permutationFlag;
00205 }
00206 
00207 
00208 void starMesh::readCells()
00209 {
00210     label nCells = 0;
00211     label maxLabel = -1;
00212 
00213     fileName cellsFileName(casePrefix_ + ".cel");
00214 
00215     {
00216         IFstream cellsFile(cellsFileName);
00217 
00218         if (cellsFile.good())
00219         {
00220             label lineLabel, pointLabel, regionLabel, typeFlag;
00221 
00222             maxLabel = -1;
00223             while (!(cellsFile >> lineLabel).eof())
00224             {
00225                 maxLabel = max(maxLabel, lineLabel);
00226                 for (int i=0; i<8; i++)
00227                 {
00228                     cellsFile >> pointLabel;
00229                 }
00230 
00231                 cellsFile >> regionLabel;
00232                 cellsFile >> typeFlag;
00233 
00234                 // lines with typeFlag of zero are continuation lines.
00235                 if (typeFlag != 0)
00236                 {
00237                     nCells++;
00238                 }
00239 
00240                 // backward compatibility: number of trailing rubbish in
00241                 // STAR is unknown. 
00242                 // Fixed to cope with missing \n on last line.
00243                 readToNl(cellsFile);
00244             }
00245         }
00246         else
00247         {
00248             FatalErrorIn("starMesh::readCells()")
00249                 << "Cannot read file " << cellsFileName
00250                 << abort(FatalError);
00251         }
00252     }
00253 
00254     Info<< "Number of cells = " << nCells << endl << endl;
00255 
00256     cellShapes_.setSize(nCells);
00257     starCellID_.setSize(nCells);
00258     starCellPermutation_.setSize(nCells);
00259 
00260     // reset permutation to invalid value
00261     forAll (starCellPermutation_, i)
00262     {
00263         starCellPermutation_[i] = -1;
00264     }
00265 
00266     starCellLabelLookup_.setSize(maxLabel+1);
00267     
00268     // reset point labels to invalid value
00269     forAll (starCellLabelLookup_, i)
00270     {
00271         starCellLabelLookup_[i] = -1;
00272     }
00273 
00274     if (nCells > 0)
00275     {
00276         IFstream cellsFile(cellsFileName);
00277 
00278         labelList labels(24, -1);
00279         label lineLabel, starLabel, regionLabel, typeFlag;
00280 
00281         for (label cellI = 0; cellI < nCells; cellI++)
00282         {
00283             label nLabels = 0;
00284 
00285             label addOnToCell = 0;
00286 
00287             // reset the labels to -1. Debugging. 
00288             forAll (labels, i)
00289             {
00290                 labels[i] = -1;
00291             }
00292 
00293             do
00294             {
00295                 if ((cellsFile >> lineLabel).eof())
00296                 {
00297                     FatalErrorIn("starMesh::readCells()")
00298                         << "Reached end of cells file before "
00299                         << "all cells are read in."
00300                         << abort(FatalError);
00301                 }
00302 
00303                 nLabels += 8;
00304 
00305                 for (int i=nLabels-8; i<nLabels; i++)
00306                 {
00307                     cellsFile >> starLabel;
00308 
00309                     if (i < 21)
00310                     {
00311                         if (starLabel != 0)
00312                         {
00313                             // Convert Star vertex number to point label
00314                             labels[i] = starPointLabelLookup_[starLabel];
00315 
00316                             if (labels[i] < 0)
00317                             {
00318                                 Info<< "Cells not consistent with vertex file. "
00319                                     << "Star vertex number " << starLabel
00320                                     << " does not exist\n";
00321                             }
00322                         }
00323                         else
00324                         {
00325                             labels[i] = -1;
00326                         }
00327                     }
00328                     else
00329                     {
00330                         labels[i] = starLabel;
00331                     }
00332                 }
00333 
00334                 cellsFile >> regionLabel;
00335                 cellsFile >> typeFlag;
00336 
00337                 // check for continuation line
00338                 if (typeFlag == -1)
00339                 {
00340                     addOnToCell = 2;
00341                 }
00342 
00343                 // backward compatibility: number of trailing rubbish in
00344                 // STAR is unknown. 
00345                 readToNl(cellsFile);
00346 
00347                 addOnToCell--;
00348 
00349             } while (addOnToCell >= 0);
00350 
00351             // Record STAR cell number (used for debugging)
00352             starCellID_[cellI] = lineLabel;
00353 
00354             // insert STAR lookup addressing
00355             starCellLabelLookup_[lineLabel] = cellI;
00356 
00357             if (nLabels == 8)
00358             {
00359                 addRegularCell(labels, cellI);
00360             }
00361             else
00362             {
00363                 addSAMMcell(labels, cellI);
00364             }
00365 
00366             // check cell labels
00367             const labelList& curShapeLabels = cellShapes_[cellI];
00368 
00369             forAll (curShapeLabels, i)
00370             {
00371                 if (curShapeLabels[i] < 0)
00372                 {
00373                     FatalErrorIn("starMesh::readCells()")
00374                         << "Invalid vertex found in cell " << cellI
00375                         << ". STAR cell no: " << lineLabel 
00376                         << " labels: " << curShapeLabels
00377                         << abort(FatalError);
00378                 }
00379             }
00380         }
00381     }
00382     else
00383     {
00384         FatalErrorIn("starMesh::readCells()")
00385             << "No cells in file " << cellsFileName
00386             << abort(FatalError);
00387     }
00388 }
00389 
00390 
00391 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines