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

plot3dToFoam.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     Plot3d mesh (ascii/formatted format) converter.
00027 
00028     Work in progress! Handles ascii multiblock (and optionally singleBlock)
00029     format.
00030     By default expects blanking. Use -noBlank if none.
00031     Use -2D @a thickness if 2D.
00032 
00033     Niklas Nordin has experienced a problem with lefthandedness of the blocks.
00034     The code should detect this automatically - see hexBlock::readPoints but
00035     if this goes wrong just set the blockHandedness_ variable to 'right'
00036     always.
00037 
00038 \*---------------------------------------------------------------------------*/
00039 
00040 #include "argList.H"
00041 #include "Time.H"
00042 #include "IFstream.H"
00043 #include "hexBlock.H"
00044 #include "polyMesh.H"
00045 #include "wallPolyPatch.H"
00046 #include "symmetryPolyPatch.H"
00047 #include "preservePatchTypes.H"
00048 #include "cellShape.H"
00049 #include "cellModeller.H"
00050 #include "mergePoints.H"
00051 
00052 using namespace Foam;
00053 
00054 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00055 // Main program:
00056 
00057 int main(int argc, char *argv[])
00058 {
00059     argList::noParallel();
00060     argList::validArgs.append("PLOT3D geom file");
00061     argList::validOptions.insert("scale", "scale factor");
00062     argList::validOptions.insert("noBlank", "");
00063     argList::validOptions.insert("singleBlock", "");
00064     argList::validOptions.insert("2D", "thickness");
00065 
00066     argList args(argc, argv);
00067 
00068     if (!args.check())
00069     {
00070          FatalError.exit();
00071     }
00072 
00073     scalar scaleFactor = 1.0;
00074     args.optionReadIfPresent("scale", scaleFactor);
00075 
00076     bool readBlank = !args.optionFound("noBlank");
00077     bool singleBlock = args.optionFound("singleBlock");
00078     scalar twoDThickness = -1;
00079     if (args.optionReadIfPresent("2D", twoDThickness))
00080     {
00081         Info<< "Reading 2D case by extruding points by " << twoDThickness
00082             << " in z direction." << nl << endl;
00083     }
00084 
00085 
00086 #   include "createTime.H"
00087 
00088     IFstream plot3dFile(args.additionalArgs()[0]);
00089 
00090     // Read the plot3d information using a fixed format reader.
00091     // Comments in the file are in C++ style, so the stream parser will remove
00092     // them with no intervention
00093     label nblock;
00094 
00095     if (singleBlock)
00096     {
00097         nblock = 1;
00098     }
00099     else
00100     {
00101         plot3dFile >> nblock;
00102     }
00103 
00104     Info<< "Reading " << nblock << " blocks" << endl;
00105 
00106     PtrList<hexBlock> blocks(nblock);
00107 
00108     {
00109         label nx, ny, nz;
00110 
00111         forAll (blocks, blockI)
00112         {
00113             if (twoDThickness > 0)
00114             {
00115                 // Fake second set of points (done in readPoints below)
00116                 plot3dFile >> nx >> ny;
00117                 nz = 2;
00118             }
00119             else
00120             {
00121                 plot3dFile >> nx >> ny >> nz;
00122             }
00123 
00124             Info<< "block " << blockI << " nx:" << nx
00125                 << " ny:" << ny << " nz:" << nz << endl;
00126 
00127             blocks.set(blockI, new hexBlock(nx, ny, nz));
00128         }
00129     }
00130 
00131     Info<< "Reading block points" << endl;
00132     label sumPoints(0);
00133     label nMeshCells(0);
00134 
00135     forAll (blocks, blockI)
00136     {
00137         Info<< "block " << blockI << ":" << nl;
00138         blocks[blockI].readPoints(readBlank, twoDThickness, plot3dFile);
00139         sumPoints += blocks[blockI].nBlockPoints();
00140         nMeshCells += blocks[blockI].nBlockCells();
00141         Info<< nl;
00142     }
00143 
00144     pointField points(sumPoints);
00145     labelList blockOffsets(blocks.size());
00146     sumPoints = 0;
00147     forAll (blocks, blockI)
00148     {
00149         const pointField& blockPoints = blocks[blockI].points();
00150         blockOffsets[blockI] = sumPoints;
00151         forAll (blockPoints, i)
00152         {
00153             points[sumPoints++] = blockPoints[i];
00154         }
00155     }
00156 
00157     // From old to new master point
00158     labelList oldToNew;
00159     pointField newPoints;
00160 
00161     // Merge points
00162     mergePoints
00163     (
00164         points,
00165         SMALL,
00166         false,
00167         oldToNew,
00168         newPoints
00169     );
00170 
00171     Info<< "Merged points within " << SMALL << " distance. Merged from "
00172         << oldToNew.size() << " down to " << newPoints.size()
00173         << " points." << endl;
00174 
00175     // Scale the points
00176     if (scaleFactor > 1.0 + SMALL || scaleFactor < 1.0 - SMALL)
00177     {
00178         newPoints *= scaleFactor;
00179     }
00180 
00181     Info<< "Creating cells" << endl;
00182 
00183     cellShapeList cellShapes(nMeshCells);
00184 
00185     const cellModel& hex = *(cellModeller::lookup("hex"));
00186 
00187     label nCreatedCells = 0;
00188 
00189     forAll (blocks, blockI)
00190     {
00191         labelListList curBlockCells = blocks[blockI].blockCells();
00192 
00193         forAll (curBlockCells, blockCellI)
00194         {
00195             labelList cellPoints(curBlockCells[blockCellI].size());
00196 
00197             forAll (cellPoints, pointI)
00198             {
00199                 cellPoints[pointI] =
00200                     oldToNew
00201                     [
00202                         curBlockCells[blockCellI][pointI]
00203                       + blockOffsets[blockI]
00204                     ];
00205             }
00206 
00207             // Do automatic collapse from hex.
00208             cellShapes[nCreatedCells] = cellShape(hex, cellPoints, true);
00209 
00210             nCreatedCells++;
00211         }
00212     }
00213 
00214     Info<< "Creating boundary patches" << endl;
00215 
00216     faceListList boundary(0);
00217     wordList patchNames(0);
00218     wordList patchTypes(0);
00219     word defaultFacesName = "defaultFaces";
00220     word defaultFacesType = wallPolyPatch::typeName;
00221     wordList patchPhysicalTypes(0);
00222 
00223     polyMesh pShapeMesh
00224     (
00225         IOobject
00226         (
00227             polyMesh::defaultRegion,
00228             runTime.constant(),
00229             runTime
00230         ),
00231         xferMove(newPoints),
00232         cellShapes,
00233         boundary,
00234         patchNames,
00235         patchTypes,
00236         defaultFacesName,
00237         defaultFacesType,
00238         patchPhysicalTypes
00239     );
00240 
00241     // Set the precision of the points data to 10
00242     IOstream::defaultPrecision(10);
00243 
00244     Info<< "Writing polyMesh" << endl;
00245     pShapeMesh.write();
00246 
00247     Info<< "End\n" << endl;
00248 
00249     return 0;
00250 }
00251 
00252 
00253 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd