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

mshToFoam.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     Converts .msh file generated by the Adventure system.
00027 
00028     Note: the .msh format does not contain any boundary information. It is
00029     purely a description of the internal mesh.
00030 
00031     Can read both linear-tet format (i.e. 4 verts per tet) and linear-hex
00032     format (8 verts per hex) (if provided with the -hex option)
00033     (Note: will bomb out if not supplied with the correct option for the
00034      file format)
00035 
00036     Not extensively tested.
00037 
00038 \*---------------------------------------------------------------------------*/
00039 
00040 #include "argList.H"
00041 #include "Time.H"
00042 #include "polyMesh.H"
00043 #include "IFstream.H"
00044 #include "polyPatch.H"
00045 #include "ListOps.H"
00046 #include "cellModeller.H"
00047 
00048 #include <fstream>
00049 
00050 using namespace Foam;
00051 
00052 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00053 
00054 // Main program:
00055 
00056 int main(int argc, char *argv[])
00057 {
00058     argList::noParallel();
00059     argList::validArgs.append(".msh file");
00060     argList::validOptions.insert("hex", "");
00061 
00062 #   include "setRootCase.H"
00063 #   include "createTime.H"
00064 
00065     bool readHex = args.optionFound("hex");
00066 
00067     fileName mshFile(args.additionalArgs()[0]);
00068     IFstream mshStream(mshFile);
00069 
00070     label nCells;
00071     mshStream >> nCells;
00072 
00073     if (readHex)
00074     {
00075         Info<< "Trying to read " << nCells << " hexes." << endl << endl;
00076     }
00077     else
00078     {
00079         Info<< "Trying to read " << nCells << " tets." << endl << endl;
00080     }
00081 
00082     cellShapeList cells(nCells);
00083 
00084     const cellModel& tet = *(cellModeller::lookup("tet"));
00085     const cellModel& hex = *(cellModeller::lookup("hex"));
00086 
00087     labelList tetPoints(4);
00088     labelList hexPoints(8);
00089 
00090     if (readHex)
00091     {
00092         for (label cellI = 0; cellI < nCells; cellI++)
00093         {
00094             for (label cp = 0; cp < 8; cp++)
00095             {
00096                 mshStream >> hexPoints[cp];
00097             }
00098             cells[cellI] = cellShape(hex, hexPoints);
00099         }
00100     }
00101     else
00102     {
00103         for (label cellI = 0; cellI < nCells; cellI++)
00104         {
00105             for (label cp = 0; cp < 4; cp++)
00106             {
00107                 mshStream >> tetPoints[cp];
00108             }
00109             cells[cellI] = cellShape(tet, tetPoints);
00110         }
00111     }
00112 
00113 
00114     label nPoints;
00115 
00116     mshStream >> nPoints;
00117 
00118     Info<< "Trying to read " << nPoints << " points." << endl << endl;
00119 
00120     pointField points(nPoints);
00121 
00122 
00123     for (label pointI = 0; pointI < nPoints; pointI++)
00124     {
00125         scalar x, y, z;
00126 
00127         mshStream >> x >> y >> z;
00128 
00129         points[pointI] = point(x, y, z);
00130     }
00131 
00132 
00133     polyMesh mesh
00134     (
00135         IOobject
00136         (
00137             polyMesh::defaultRegion,
00138             runTime.constant(),
00139             runTime
00140         ),
00141         xferMove(points),
00142         cells,
00143         faceListList(0),
00144         wordList(0),
00145         wordList(0),
00146         "defaultFaces",
00147         polyPatch::typeName,
00148         wordList(0)
00149     );
00150 
00151     Info<< "Writing mesh ..." << endl;
00152 
00153     mesh.write();
00154 
00155 
00156     Info<< "End\n" << endl;
00157 
00158     return 0;
00159 }
00160 
00161 
00162 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd