initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / applications / utilities / mesh / conversion / mshToFoam / mshToFoam.C
blobefed09a588614921ade6fbccaacbaadd0c70c46e
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
26     Reads .msh format generated by the Adventure system.
28     Note: the .msh format does not contain any boundary information. It is
29     purely a description of the internal mesh.
31     Can read both linear-tet format (i.e. 4 verts per tet) and linear-hex
32     format (8 verts per hex) (if provided with the -hex option)
33     (Note: will bomb out if not supplied with the correct option for the
34      file format)
36     Not extensively tested.
38 \*---------------------------------------------------------------------------*/
40 #include "argList.H"
41 #include "Time.H"
42 #include "polyMesh.H"
43 #include "IFstream.H"
44 #include "polyPatch.H"
45 #include "ListOps.H"
46 #include "cellModeller.H"
48 #include <fstream>
50 using namespace Foam;
52 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 // Main program:
56 int main(int argc, char *argv[])
58     argList::noParallel();
59     argList::validArgs.append(".msh file");
60     argList::validOptions.insert("hex", "");
62 #   include "setRootCase.H"
63 #   include "createTime.H"
65     bool readHex(args.options().found("hex"));
67     fileName mshFile(args.additionalArgs()[0]);
69     IFstream mshStream(mshFile);
71     label nCells;
73     mshStream >> nCells;
75     if (readHex)
76     {
77         Info<< "Trying to read " << nCells << " hexes." << endl << endl;
78     }
79     else
80     {
81         Info<< "Trying to read " << nCells << " tets." << endl << endl;
82     }
84     cellShapeList cells(nCells);
86     const cellModel& tet = *(cellModeller::lookup("tet"));
87     const cellModel& hex = *(cellModeller::lookup("hex"));
89     labelList tetPoints(4);
90     labelList hexPoints(8);
92     if (readHex)
93     {
94         for (label cellI = 0; cellI < nCells; cellI++)
95         {
96             for (label cp = 0; cp < 8; cp++)
97             {
98                 mshStream >> hexPoints[cp];
99             }
100             cells[cellI] = cellShape(hex, hexPoints);
101         }
102     }
103     else
104     {
105         for (label cellI = 0; cellI < nCells; cellI++)
106         {
107             for (label cp = 0; cp < 4; cp++)
108             {
109                 mshStream >> tetPoints[cp];
110             }
111             cells[cellI] = cellShape(tet, tetPoints);
112         }
113     }
116     label nPoints;
118     mshStream >> nPoints;
120     Info<< "Trying to read " << nPoints << " points." << endl << endl;
122     pointField points(nPoints);
125     for (label pointI = 0; pointI < nPoints; pointI++)
126     {
127         scalar x, y, z;
129         mshStream >> x >> y >> z;
131         points[pointI] = point(x, y, z);
132     }
135     polyMesh mesh
136     (
137         IOobject
138         (
139             polyMesh::defaultRegion,
140             runTime.constant(),
141             runTime
142         ),
143         points,
144         cells,
145         faceListList(0),
146         wordList(0),
147         wordList(0),
148         "defaultFaces",
149         polyPatch::typeName,
150         wordList(0)
151     );
153     Info<< "Writing mesh ..." << endl;
155     mesh.write();
156     
158     Info<< "End\n" << endl;
160     return 0;
164 // ************************************************************************* //