mshToFoam.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  mshToFoam
26 
27 Description
28  Converts .msh file generated by the Adventure system.
29 
30  Note: the .msh format does not contain any boundary information. It is
31  purely a description of the internal mesh.
32 
33  Can read both linear-tet format (i.e. 4 verts per tet) and linear-hex
34  format (8 verts per hex) (if provided with the -hex (at your option)
35  (Note: will bomb out if not supplied with the correct option for the
36  file format)
37 
38  Not extensively tested.
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #include "argList.H"
43 #include "Time.H"
44 #include "polyMesh.H"
45 #include "IFstream.H"
46 #include "polyPatch.H"
47 #include "ListOps.H"
48 #include "cellModeller.H"
49 
50 #include <fstream>
51 
52 using namespace Foam;
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 
56 
57 int main(int argc, char *argv[])
58 {
60  argList::validArgs.append(".msh file");
62  (
63  "hex",
64  "treat input as containing hex instead of tet cells"
65  );
66 
67  #include "setRootCase.H"
68  #include "createTime.H"
69 
70  const bool readHex = args.optionFound("hex");
71  IFstream mshStream(args[1]);
72 
73  label nCells;
74  mshStream >> nCells;
75 
76  if (readHex)
77  {
78  Info<< "Trying to read " << nCells << " hexes." << nl << endl;
79  }
80  else
81  {
82  Info<< "Trying to read " << nCells << " tets." << nl << endl;
83  }
84 
85  cellShapeList cells(nCells);
86 
87  const cellModel& tet = *(cellModeller::lookup("tet"));
88  const cellModel& hex = *(cellModeller::lookup("hex"));
89 
91  labelList hexPoints(8);
92 
93  if (readHex)
94  {
95  for (label cellI = 0; cellI < nCells; cellI++)
96  {
97  for (label cp = 0; cp < 8; cp++)
98  {
99  mshStream >> hexPoints[cp];
100  }
101  cells[cellI] = cellShape(hex, hexPoints);
102  }
103  }
104  else
105  {
106  for (label cellI = 0; cellI < nCells; cellI++)
107  {
108  for (label cp = 0; cp < 4; cp++)
109  {
110  mshStream >> tetPoints[cp];
111  }
112  cells[cellI] = cellShape(tet, tetPoints);
113  }
114  }
115 
116 
117  label nPoints;
118 
119  mshStream >> nPoints;
120 
121  Info<< "Trying to read " << nPoints << " points." << endl << endl;
122 
124 
125 
126  for (label pointI = 0; pointI < nPoints; pointI++)
127  {
128  scalar x, y, z;
129 
130  mshStream >> x >> y >> z;
131 
132  points[pointI] = point(x, y, z);
133  }
134 
135 
136  polyMesh mesh
137  (
138  IOobject
139  (
141  runTime.constant(),
142  runTime
143  ),
144  xferMove(points),
145  cells,
146  faceListList(0),
147  wordList(0),
148  wordList(0),
149  "defaultFaces",
150  polyPatch::typeName,
151  wordList(0)
152  );
153 
154  Info<< "Writing mesh ..." << endl;
155 
156  mesh.write();
157 
158 
159  Info<< "End\n" << endl;
160 
161  return 0;
162 }
163 
164 
165 // ************************************************************************* //
Foam::argList::validArgs
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:143
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::polyMesh::defaultRegion
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:306
polyPatch.H
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::argList::addBoolOption
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:98
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::cp
bool cp(const fileName &src, const fileName &dst)
Copy, recursively if necessary, the source to the destination.
Definition: POSIX.C:755
polyMesh.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:54
Foam::fvMesh::write
virtual bool write() const
Write mesh using IO settings from time.
Definition: fvMesh.C:873
Foam::faceListList
List< faceList > faceListList
Definition: faceListFwd.H:45
cellModeller.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::cellModeller::lookup
static const cellModel * lookup(const word &)
Look up a model by name and return a pointer to the model or NULL.
Definition: cellModeller.C:91
argList.H
main
int main(int argc, char *argv[])
Definition: postCalc.C:54
IFstream.H
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam::cellShape
An analytical geometric cellShape.
Definition: cellShape.H:69
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::hex
IOstream & hex(IOstream &io)
Definition: IOstream.H:564
Foam::tetPoints
Tet storage. Null constructable (unfortunately tetrahedron<point, point> is not)
Definition: tetPoints.H:50
setRootCase.H
Foam::xferMove
Xfer< T > xferMove(T &)
Construct by transferring the contents of the arg.
Foam::List< cellShape >
points
const pointField & points
Definition: gmvOutputHeader.H:1
createTime.H
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::argList::optionFound
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
cells
const cellShapeList & cells
Definition: gmvOutputHeader.H:3
ListOps.H
Various functions to operate on Lists.
Foam::cellModel
Maps a geometry to a set of cell primitives, which enables geometric cell data to be calculated witho...
Definition: cellModel.H:64
Foam::point
vector point
Point is a vector.
Definition: point.H:41
Foam::argList::noParallel
static void noParallel()
Remove the parallel options.
Definition: argList.C:161
args
Foam::argList args(argc, argv)
y
scalar y
Definition: LISASMDCalcMethod1.H:14