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-------------------------------------------------------------------------------
11License
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
27Application
28 mshToFoam
29
30Group
31 grpMeshConversionUtilities
32
33Description
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
58using namespace Foam;
59
60// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
61
62
63int main(int argc, char *argv[])
64{
65 argList::addNote
66 (
67 "Convert an Adventure .msh file to OpenFOAM"
68 );
69 argList::noParallel();
70 argList::addArgument(".msh file");
71 argList::addBoolOption
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
97 const cellModel& tet = cellModel::ref(cellModel::TET);
98 const cellModel& hex = cellModel::ref(cellModel::HEX);
99
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
147 (
149 (
150 polyMesh::defaultRegion,
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
165 IOstream::defaultPrecision(max(10u, IOstream::defaultPrecision()));
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// ************************************************************************* //
scalar y
Various functions to operate on Lists.
Y[inertIndex] max(0.0)
Input from file stream, using an ISstream.
Definition: IFstream.H:57
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:278
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:178
Maps a geometry to a set of cell primitives.
Definition: cellModel.H:73
A class for handling file names.
Definition: fileName.H:76
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
Tet storage. Null constructable (unfortunately tetrahedron<point, point> is not)
Definition: tetPoints.H:56
const cellModel & hex
dynamicFvMesh & mesh
engineTime & runTime
const pointField & points
label nPoints
const cellShapeList & cells
Namespace for OpenFOAM.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
const volScalarField & cp
Foam::argList args(argc, argv)