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

hexBlock.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 
00026 \*---------------------------------------------------------------------------*/
00027 
00028 #include "hexBlock.H"
00029 
00030 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00031 
00032 namespace Foam
00033 {
00034 
00035 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00036 
00037 label hexBlock::vtxLabel(label a, label b, label c) const
00038 {
00039     return (a + b*(xDim_ + 1) + c*(xDim_ + 1)*(yDim_ + 1));
00040 }
00041 
00042 
00043 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00044 
00045 // Construct from components
00046 hexBlock::hexBlock(const label nx, const label ny, const label nz)
00047 :
00048     xDim_(nx),
00049     yDim_(ny),
00050     zDim_(nz),
00051     blockHandedness_(noPoints),
00052     points_((xDim_ + 1)*(yDim_ + 1)*(zDim_ + 1))
00053 {}
00054 
00055 
00056 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00057 
00058 void hexBlock::readPoints(Istream& is)
00059 {
00060     forAll (points_, i)
00061     {
00062         is >> points_[i].x() >> points_[i].y() >> points_[i].z();
00063     }
00064 
00065     // Calculate the handedness of the block
00066     vector i = points_[xDim_] - points_[0];
00067     vector j = points_[(xDim_ + 1)*yDim_] - points_[0];
00068     vector k = points_[(xDim_ + 1)*(yDim_ + 1)*zDim_] - points_[0];
00069 
00070     if (((i ^ j) & k) > 0)
00071     {
00072         Info << "right-handed block" << endl;
00073         blockHandedness_ = right;
00074     }
00075     else
00076     {
00077         Info << "left-handed block" << endl;
00078         blockHandedness_ = left;
00079     }
00080 }
00081 
00082 
00083 labelListList hexBlock::blockCells() const
00084 {
00085     labelListList result(xDim_*yDim_*zDim_);
00086 
00087     label cellNo = 0;
00088 
00089     if (blockHandedness_ == right)
00090     {
00091         for (label k = 0; k <= zDim_ - 1; k++)
00092         {
00093             for (label j = 0; j <= yDim_ - 1; j++)
00094             {
00095                 for (label i = 0; i <= xDim_ - 1; i++)
00096                 {
00097                     labelList& hexLabels = result[cellNo];
00098                     hexLabels.setSize(8);
00099 
00100                     hexLabels[0] = vtxLabel(i, j, k);
00101                     hexLabels[1] = vtxLabel(i+1, j, k);
00102                     hexLabels[2] = vtxLabel(i+1, j+1, k);
00103                     hexLabels[3] = vtxLabel(i, j+1, k);
00104                     hexLabels[4] = vtxLabel(i, j, k+1);
00105                     hexLabels[5] = vtxLabel(i+1, j, k+1);
00106                     hexLabels[6] = vtxLabel(i+1, j+1, k+1);
00107                     hexLabels[7] = vtxLabel(i, j+1, k+1);
00108 
00109                     cellNo++;
00110                 }
00111             }
00112         }
00113     }
00114     else if (blockHandedness_ == left)
00115     {
00116         for (label k = 0; k <= zDim_ - 1; k++)
00117         {
00118             for (label j = 0; j <= yDim_ - 1; j++)
00119             {
00120                 for (label i = 0; i <= xDim_ - 1; i++)
00121                 {
00122                     labelList& hexLabels = result[cellNo];
00123                     hexLabels.setSize(8);
00124 
00125                     hexLabels[0] = vtxLabel(i, j, k+1);
00126                     hexLabels[1] = vtxLabel(i+1, j, k+1);
00127                     hexLabels[2] = vtxLabel(i+1, j+1, k+1);
00128                     hexLabels[3] = vtxLabel(i, j+1, k+1);
00129                     hexLabels[4] = vtxLabel(i, j, k);
00130                     hexLabels[5] = vtxLabel(i+1, j, k);
00131                     hexLabels[6] = vtxLabel(i+1, j+1, k);
00132                     hexLabels[7] = vtxLabel(i, j+1, k);
00133 
00134                     cellNo++;
00135                 }
00136             }
00137         }
00138     }
00139     else
00140     {
00141         FatalErrorIn("hexBlock::cellShapes()")
00142             << "Unable to determine block handedness as points "
00143             << "have not been read in yet"
00144             << abort(FatalError);
00145     }
00146 
00147     return result;
00148 }
00149 
00150 
00151 // Return block patch faces given direction and range limits
00152 // From the cfx manual: direction
00153 // 0 = solid (3-D patch),
00154 // 1 = high i, 2 = high j, 3 = high k
00155 // 4 = low i, 5 = low j, 6 = low k
00156 faceList hexBlock::patchFaces(const label direc, const labelList& range) const
00157 {
00158     if (range.size() != 6)
00159     {
00160         FatalErrorIn
00161         (
00162             "patchFaces(const label direc, const labelList& range) const"
00163         )   << "Invalid size of the range array: " << range.size()
00164             << ". Should be 6 (xMin, xMax, yMin, yMax, zMin, zMax"
00165             << abort(FatalError);
00166     }
00167 
00168     label xMinRange = range[0];
00169     label xMaxRange = range[1];
00170     label yMinRange = range[2];
00171     label yMaxRange = range[3];
00172     label zMinRange = range[4];
00173     label zMaxRange = range[5];
00174 
00175     faceList result(0);
00176 
00177     switch (direc)
00178     {
00179         case 1:
00180         {
00181             // high i = xmax
00182 
00183             result.setSize
00184             (
00185                 (yMaxRange - yMinRange + 1)*(zMaxRange - zMinRange + 1)
00186             );
00187 
00188             label p = 0;
00189             for (label k = zMinRange - 1; k <= zMaxRange - 1; k++)
00190             {
00191                 for (label j = yMinRange - 1; j <= yMaxRange - 1; j++)
00192                 {
00193                     result[p].setSize(4);
00194 
00195                     // set the points
00196                     result[p][0] = vtxLabel(xDim_, j, k);
00197                     result[p][1] = vtxLabel(xDim_, j+1, k);
00198                     result[p][2] = vtxLabel(xDim_, j+1, k+1);
00199                     result[p][3] = vtxLabel(xDim_, j, k+1);
00200 
00201                     p++;
00202                 }
00203             }
00204 
00205             result.setSize(p);
00206             break;
00207         }
00208 
00209         case 2:
00210         {
00211             // high j = ymax
00212             result.setSize
00213             (
00214                 (xMaxRange - xMinRange + 1)*(zMaxRange - zMinRange + 1)
00215             );
00216 
00217             label p = 0;
00218             for (label i = xMinRange - 1; i <= xMaxRange - 1; i++)
00219             {
00220                 for (label k = zMinRange - 1; k <= zMaxRange - 1; k++)
00221                 {
00222                     result[p].setSize(4);
00223 
00224                     // set the points
00225                     result[p][0] = vtxLabel(i, yDim_, k);
00226                     result[p][1] = vtxLabel(i, yDim_, k + 1);
00227                     result[p][2] = vtxLabel(i + 1, yDim_, k + 1);
00228                     result[p][3] = vtxLabel(i + 1, yDim_, k);
00229 
00230                     p++;
00231                 }
00232             }
00233 
00234             result.setSize(p);
00235             break;
00236         }
00237 
00238         case 3:
00239         {
00240             // high k = zmax
00241             result.setSize
00242             (
00243                 (xMaxRange - xMinRange + 1)*(yMaxRange - yMinRange + 1)
00244             );
00245 
00246             label p = 0;
00247             for (label i = xMinRange - 1; i <= xMaxRange - 1; i++)
00248             {
00249                 for (label j = yMinRange - 1; j <= yMaxRange - 1; j++)
00250                 {
00251                     result[p].setSize(4);
00252 
00253                     // set the points
00254                     result[p][0] = vtxLabel(i, j, zDim_);
00255                     result[p][1] = vtxLabel(i + 1, j, zDim_);
00256                     result[p][2] = vtxLabel(i + 1, j + 1, zDim_);
00257                     result[p][3] = vtxLabel(i, j + 1, zDim_);
00258 
00259                     p++;
00260                 }
00261             }
00262 
00263             result.setSize(p);
00264             break;
00265         }
00266 
00267         case 4:
00268         {
00269             // low i = xmin
00270             result.setSize
00271             (
00272                 (yMaxRange - yMinRange + 1)*(zMaxRange - zMinRange + 1)
00273             );
00274 
00275             label p = 0;
00276             for (label k = zMinRange - 1; k <= zMaxRange - 1; k++)
00277             {
00278                 for (label j = yMinRange - 1; j <= yMaxRange - 1; j++)
00279                 {
00280                     result[p].setSize(4);
00281 
00282                     // set the points
00283                     result[p][0] = vtxLabel(0, j, k);
00284                     result[p][1] = vtxLabel(0, j, k + 1);
00285                     result[p][2] = vtxLabel(0, j + 1, k + 1);
00286                     result[p][3] = vtxLabel(0, j + 1, k);
00287 
00288                     p++;
00289                 }
00290             }
00291 
00292             result.setSize(p);
00293             break;
00294         }
00295 
00296         case 5:
00297         {
00298             // low j = ymin
00299             result.setSize
00300             (
00301                 (xMaxRange - xMinRange + 1)*(zMaxRange - zMinRange + 1)
00302             );
00303 
00304             label p = 0;
00305             for (label i = xMinRange - 1; i <= xMaxRange - 1; i++)
00306             {
00307                 for (label k = zMinRange - 1; k <= zMaxRange - 1; k++)
00308                 {
00309                     result[p].setSize(4);
00310 
00311                     // set the points
00312                     result[p][0] = vtxLabel(i, 0, k);
00313                     result[p][1] = vtxLabel(i + 1, 0, k);
00314                     result[p][2] = vtxLabel(i + 1, 0, k + 1);
00315                     result[p][3] = vtxLabel(i, 0, k + 1);
00316 
00317                     p++;
00318                 }
00319             }
00320 
00321             result.setSize(p);
00322             break;
00323         }
00324 
00325         case 6:
00326         {
00327             // low k = zmin
00328             result.setSize
00329             (
00330                 (xMaxRange - xMinRange + 1)*(yMaxRange - yMinRange + 1)
00331             );
00332 
00333             label p = 0;
00334             for (label i = xMinRange - 1; i <= xMaxRange - 1; i++)
00335             {
00336                 for (label j = yMinRange - 1; j <= yMaxRange - 1; j++)
00337                 {
00338                     result[p].setSize(4);
00339 
00340                     // set the points
00341                     result[p][0] = vtxLabel(i, j, 0);
00342                     result[p][1] = vtxLabel(i, j + 1, 0);
00343                     result[p][2] = vtxLabel(i + 1, j + 1, 0);
00344                     result[p][3] = vtxLabel(i + 1, j, 0);
00345 
00346                     p++;
00347                 }
00348             }
00349 
00350             result.setSize(p);
00351             break;
00352         }
00353 
00354         default:
00355         {
00356             FatalErrorIn
00357             (
00358                 "patchFaces(const label direc, const labelList& range) const"
00359             )   << "direction out of range (1 to 6): " << direc
00360                 << abort(FatalError);
00361         }
00362     }
00363 
00364     // Correct the face orientation based on the handedness of the block.
00365     // Do nothing for the right-handed block
00366     if (blockHandedness_ == noPoints)
00367     {
00368         FatalErrorIn
00369         (
00370             "patchFaces(const label direc, const labelList& range) const"
00371         )   << "Unable to determine block handedness as points "
00372             << "have not been read in yet"
00373             << abort(FatalError);
00374     }
00375     else if (blockHandedness_ == left)
00376     {
00377         // turn all faces inside out
00378         forAll (result, faceI)
00379         {
00380             result[faceI] = result[faceI].reverseFace();
00381         }
00382     }
00383 
00384     return result;
00385 }
00386 
00387 
00388 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00389 
00390 } // End namespace Foam
00391 
00392 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines