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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Application
27  mshToFoam
28 
29 Group
30  grpMeshConversionUtilities
31 
32 Description
33  Convert .msh file generated by the Adventure system.
34 
35  Note: the .msh format does not contain any boundary information. It is
36  purely a description of the internal mesh.
37 
38  Can read both linear-tet format (i.e. 4 verts per tet) and linear-hex
39  format (8 verts per hex) (if provided with the -hex (at your option)
40  (Note: will bomb out if not supplied with the correct option for the
41  file format)
42 
43  Not extensively tested.
44 
45 \*---------------------------------------------------------------------------*/
46 
47 #include "argList.H"
48 #include "Time.H"
49 #include "polyMesh.H"
50 #include "IFstream.H"
51 #include "polyPatch.H"
52 #include "ListOps.H"
53 #include "cellModel.H"
54 
55 #include <fstream>
56 
57 using namespace Foam;
58 
59 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60 
61 
62 int main(int argc, char *argv[])
63 {
65  (
66  "Convert an Adventure .msh file to OpenFOAM"
67  );
69  argList::addArgument(".msh file");
71  (
72  "hex",
73  "Treat input as containing hex instead of tet cells"
74  );
75 
76  #include "setRootCase.H"
77  #include "createTime.H"
78 
79  const bool readHex = args.found("hex");
80  IFstream mshStream(args[1]);
81 
82  label nCells;
83  mshStream >> nCells;
84 
85  if (readHex)
86  {
87  Info<< "Trying to read " << nCells << " hexes." << nl << endl;
88  }
89  else
90  {
91  Info<< "Trying to read " << nCells << " tets." << nl << endl;
92  }
93 
94  cellShapeList cells(nCells);
95 
98 
100  labelList hexPoints(8);
101 
102  if (readHex)
103  {
104  for (label celli = 0; celli < nCells; celli++)
105  {
106  for (label cp = 0; cp < 8; cp++)
107  {
108  mshStream >> hexPoints[cp];
109  }
110  cells[celli] = cellShape(hex, hexPoints);
111  }
112  }
113  else
114  {
115  for (label celli = 0; celli < nCells; celli++)
116  {
117  for (label cp = 0; cp < 4; cp++)
118  {
119  mshStream >> tetPoints[cp];
120  }
121  cells[celli] = cellShape(tet, tetPoints);
122  }
123  }
124 
125 
126  label nPoints;
127 
128  mshStream >> nPoints;
129 
130  Info<< "Trying to read " << nPoints << " points." << endl << endl;
131 
133 
134 
135  for (label pointi = 0; pointi < nPoints; pointi++)
136  {
137  scalar x, y, z;
138 
139  mshStream >> x >> y >> z;
140 
141  points[pointi] = point(x, y, z);
142  }
143 
144 
145  polyMesh mesh
146  (
147  IOobject
148  (
150  runTime.constant(),
151  runTime
152  ),
153  std::move(points),
154  cells,
155  faceListList(),
156  wordList(),
157  wordList(),
158  "defaultFaces",
159  polyPatch::typeName,
160  wordList()
161  );
162 
163  // Set the precision of the points data to 10
165 
166  Info<< "Writing mesh ..." << endl;
167 
168  mesh.removeFiles();
169  mesh.write();
170 
171  Info<< "End\n" << endl;
172 
173  return 0;
174 }
175 
176 
177 // ************************************************************************* //
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::fvMesh::write
virtual bool write(const bool valid=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:895
Foam::polyMesh::defaultRegion
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:312
polyPatch.H
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:85
Foam::cellModel::HEX
hex
Definition: cellModel.H:81
Foam::argList::addNote
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:413
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
polyMesh.H
Foam::cellModel::TET
tet
Definition: cellModel.H:85
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:59
Foam::argList::addArgument
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition: argList.C:302
Foam::Field< vector >
cellModel.H
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::polyMesh::removeFiles
void removeFiles(const fileName &instanceDir) const
Remove all files from mesh instance.
Definition: polyMesh.C:1264
argList.H
Foam::cellModel::ref
static const cellModel & ref(const modelType model)
Look up reference to cellModel by enumeration. Fatal on failure.
Definition: cellModels.C:157
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
IFstream.H
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::cellShape
An analytical geometric cellShape.
Definition: cellShape.H:71
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::hex
IOstream & hex(IOstream &io)
Definition: IOstream.H:437
Foam::tetPoints
Tet storage. Null constructable (unfortunately tetrahedron<point, point> is not)
Definition: tetPoints.H:53
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:325
Time.H
setRootCase.H
Foam::faceListList
List< faceList > faceListList
A List of faceList.
Definition: faceListFwd.H:49
Foam::cp
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy the source to the destination (recursively if necessary).
Definition: MSwindows.C:802
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:333
Foam::List< cellShape >
points
const pointField & points
Definition: gmvOutputHeader.H:1
createTime.H
x
x
Definition: LISASMDCalcMethod2.H:52
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.
Definition: cellModel.H:72
Foam::point
vector point
Point is a vector.
Definition: point.H:43
Foam::argList::noParallel
static void noParallel()
Remove the parallel options.
Definition: argList.C:491
Foam::TimePaths::constant
const word & constant() const
Return constant name.
Definition: TimePathsI.H:88
args
Foam::argList args(argc, argv)
Foam::argList::found
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:157
y
scalar y
Definition: LISASMDCalcMethod1.H:14