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  Copyright (C) 2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Application
28  mshToFoam
29 
30 Group
31  grpMeshConversionUtilities
32 
33 Description
34  Convert .msh file generated by the Adventure system.
35 
36  Note: the .msh format does not contain any boundary information. It is
37  purely a description of the internal mesh.
38 
39  Can read both linear-tet format (i.e. 4 verts per tet) and linear-hex
40  format (8 verts per hex) (if provided with the -hex (at your option)
41  (Note: will bomb out if not supplied with the correct option for the
42  file format)
43 
44  Not extensively tested.
45 
46 \*---------------------------------------------------------------------------*/
47 
48 #include "argList.H"
49 #include "Time.H"
50 #include "polyMesh.H"
51 #include "IFstream.H"
52 #include "polyPatch.H"
53 #include "ListOps.H"
54 #include "cellModel.H"
55 
56 #include <fstream>
57 
58 using namespace Foam;
59 
60 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
61 
62 
63 int main(int argc, char *argv[])
64 {
66  (
67  "Convert an Adventure .msh file to OpenFOAM"
68  );
70  argList::addArgument(".msh file");
72  (
73  "hex",
74  "Treat input as containing hex instead of tet cells"
75  );
76 
77  #include "setRootCase.H"
78  #include "createTime.H"
79 
80  const bool readHex = args.found("hex");
81  IFstream mshStream(args.get<fileName>(1));
82 
83  label nCells;
84  mshStream >> nCells;
85 
86  if (readHex)
87  {
88  Info<< "Trying to read " << nCells << " hexes." << nl << endl;
89  }
90  else
91  {
92  Info<< "Trying to read " << nCells << " tets." << nl << endl;
93  }
94 
95  cellShapeList cells(nCells);
96 
99 
100  labelList tetPoints(4);
101  labelList hexPoints(8);
102 
103  if (readHex)
104  {
105  for (label celli = 0; celli < nCells; celli++)
106  {
107  for (label cp = 0; cp < 8; cp++)
108  {
109  mshStream >> hexPoints[cp];
110  }
111  cells[celli].reset(hex, hexPoints);
112  }
113  }
114  else
115  {
116  for (label celli = 0; celli < nCells; celli++)
117  {
118  for (label cp = 0; cp < 4; cp++)
119  {
120  mshStream >> tetPoints[cp];
121  }
122  cells[celli].reset(tet, tetPoints);
123  }
124  }
125 
126 
127  label nPoints;
128 
129  mshStream >> nPoints;
130 
131  Info<< "Trying to read " << nPoints << " points." << endl << endl;
132 
134 
135 
136  for (label pointi = 0; pointi < nPoints; pointi++)
137  {
138  scalar x, y, z;
139 
140  mshStream >> x >> y >> z;
141 
142  points[pointi] = point(x, y, z);
143  }
144 
145 
146  polyMesh mesh
147  (
148  IOobject
149  (
151  runTime.constant(),
152  runTime
153  ),
154  std::move(points),
155  cells,
156  faceListList(),
157  wordList(),
158  wordList(),
159  "defaultFaces",
160  polyPatch::typeName,
161  wordList()
162  );
163 
164  // Set the precision of the points data to 10
166 
167  Info<< "Writing mesh ..." << endl;
168 
169  mesh.removeFiles();
170  mesh.write();
171 
172  Info<< "End\n" << endl;
173 
174  return 0;
175 }
176 
177 
178 // ************************************************************************* //
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:169
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::fvMesh::write
virtual bool write(const bool valid=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1041
Foam::polyMesh::defaultRegion
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:318
polyPatch.H
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:53
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:412
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
polyMesh.H
Foam::argList::get
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:278
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:62
Foam::argList::addArgument
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition: argList.C:301
Foam::Field< vector >
cellModel.H
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::polyMesh::removeFiles
void removeFiles(const fileName &instanceDir) const
Remove all files from mesh instance.
Definition: polyMesh.C:1325
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
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::hex
IOstream & hex(IOstream &io)
Definition: IOstream.H:446
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:324
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:342
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:404
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:510
Foam::TimePaths::constant
const word & constant() const
Return constant name.
Definition: TimePathsI.H:96
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:178
y
scalar y
Definition: LISASMDCalcMethod1.H:14