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-2016 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 
90  labelList tetPoints(4);
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 
123  pointField points(nPoints);
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 // ************************************************************************* //
List< faceList > faceListList
Definition: faceListFwd.H:45
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
static const cellModel * lookup(const word &)
Look up a model by name and return a pointer to the model or nullptr.
Definition: cellModeller.C:88
virtual bool write(const bool valid=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:877
An analytical geometric cellShape.
Definition: cellShape.H:69
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:309
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy, recursively if necessary, the source to the destination.
Definition: POSIX.C:739
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
static void noParallel()
Remove the parallel options.
Definition: argList.C:148
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:154
Various functions to operate on Lists.
scalar y
dynamicFvMesh & mesh
const cellShapeList & cells
const pointField & points
Xfer< T > xferMove(T &)
Construct by transferring the contents of the arg.
label nPoints
static const char nl
Definition: Ostream.H:262
Input from file stream.
Definition: IFstream.H:81
List< word > wordList
A List of words.
Definition: fileName.H:54
vector point
Point is a vector.
Definition: point.H:41
Maps a geometry to a set of cell primitives, which enables geometric cell data to be calculated witho...
Definition: cellModel.H:64
messageStream Info
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:85
Foam::argList args(argc, argv)
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
Namespace for OpenFOAM.