plot3dToFoam.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-2017 OpenFOAM Foundation
9 Copyright (C) 2020-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 plot3dToFoam
29
30Group
31 grpMeshConversionUtilities
32
33Description
34 Plot3d mesh (ascii/formatted format) converter.
35
36 Work in progress! Handles ascii multiblock (and optionally singleBlock)
37 format.
38 By default expects blanking. Use -noBlank if none.
39 Use -2D \a thickness if 2D.
40
41 Niklas Nordin has experienced a problem with lefthandedness of the blocks.
42 The code should detect this automatically - see hexBlock::readPoints but
43 if this goes wrong just set the blockHandedness_ variable to 'right'
44 always.
45
46\*---------------------------------------------------------------------------*/
47
48#include "argList.H"
49#include "Time.H"
50#include "IFstream.H"
51#include "hexBlock.H"
52#include "polyMesh.H"
53#include "wallPolyPatch.H"
54#include "symmetryPolyPatch.H"
55#include "cellShape.H"
56#include "mergePoints.H"
57
58using namespace Foam;
59
60// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
61
62int main(int argc, char *argv[])
63{
64 argList::addNote
65 (
66 "Plot3d mesh (ascii/formatted format) converter"
67 );
68 argList::noParallel();
69 argList::addArgument("PLOT3D geom file");
70 argList::addOption
71 (
72 "scale",
73 "factor",
74 "Geometry scaling factor - default is 1"
75 );
76 argList::addBoolOption
77 (
78 "noBlank",
79 "Skip blank items"
80 );
81 argList::addBoolOption
82 (
83 "singleBlock",
84 "Input is a single block"
85 );
86 argList::addOption
87 (
88 "2D",
89 "thickness",
90 "Use when converting a 2-D mesh (applied before scale)"
91 );
92
93 argList args(argc, argv);
94
95 if (!args.check())
96 {
98 }
99
100 const scalar scaleFactor = args.getOrDefault<scalar>("scale", 1);
101
102 const bool readBlank = !args.found("noBlank");
103 const bool singleBlock = args.found("singleBlock");
104 scalar twoDThickness = -1;
105 if (args.readIfPresent("2D", twoDThickness))
106 {
107 Info<< "Reading 2D case by extruding points by " << twoDThickness
108 << " in z direction." << nl << endl;
109 }
110
111
112 #include "createTime.H"
113
114 IFstream plot3dFile(args.get<fileName>(1));
115
116 // Read the plot3d information using a fixed format reader.
117 // Comments in the file are in C++ style, so the stream parser will remove
118 // them with no intervention
119 label nblock;
120
121 if (singleBlock)
122 {
123 nblock = 1;
124 }
125 else
126 {
127 plot3dFile >> nblock;
128 }
129
130 Info<< "Reading " << nblock << " blocks" << endl;
131
132 PtrList<hexBlock> blocks(nblock);
133
134 {
135 label nx, ny, nz;
136
137 forAll(blocks, blockI)
138 {
139 if (twoDThickness > 0)
140 {
141 // Fake second set of points (done in readPoints below)
142 plot3dFile >> nx >> ny;
143 nz = 2;
144 }
145 else
146 {
147 plot3dFile >> nx >> ny >> nz;
148 }
149
150 Info<< "block " << blockI << " nx:" << nx
151 << " ny:" << ny << " nz:" << nz << endl;
152
153 blocks.set(blockI, new hexBlock(nx, ny, nz));
154 }
155 }
156
157 Info<< "Reading block points" << endl;
158 label sumPoints(0);
159 label nMeshCells(0);
160
161 forAll(blocks, blockI)
162 {
163 Info<< "block " << blockI << ":" << nl;
164 blocks[blockI].readPoints(readBlank, twoDThickness, plot3dFile);
165 sumPoints += blocks[blockI].nBlockPoints();
166 nMeshCells += blocks[blockI].nBlockCells();
167 Info<< nl;
168 }
169
170 pointField points(sumPoints);
171 labelList blockOffsets(blocks.size());
172 sumPoints = 0;
173 forAll(blocks, blockI)
174 {
175 const pointField& blockPoints = blocks[blockI].points();
176 blockOffsets[blockI] = sumPoints;
177 forAll(blockPoints, i)
178 {
179 points[sumPoints++] = blockPoints[i];
180 }
181 }
182
183 // From old to new master point
184 labelList oldToNew;
185 pointField newPoints;
186
187 // Merge points
189 (
190 points,
191 SMALL,
192 false,
193 oldToNew,
194 newPoints
195 );
196
197 Info<< "Merged points within " << SMALL << " distance. Merged from "
198 << oldToNew.size() << " down to " << newPoints.size()
199 << " points." << endl;
200
201 // Scale the points
202 if (scaleFactor > 1.0 + SMALL || scaleFactor < 1.0 - SMALL)
203 {
204 newPoints *= scaleFactor;
205 }
206
207 Info<< "Creating cells" << endl;
208
209 cellShapeList cellShapes(nMeshCells);
210
211 const cellModel& hex = cellModel::ref(cellModel::HEX);
212
213 label nCreatedCells = 0;
214
215 forAll(blocks, blockI)
216 {
217 labelListList curBlockCells = blocks[blockI].blockCells();
218
219 forAll(curBlockCells, blockCelli)
220 {
221 labelList cellPoints(curBlockCells[blockCelli].size());
222
223 forAll(cellPoints, pointi)
224 {
225 cellPoints[pointi] =
226 oldToNew
227 [
228 curBlockCells[blockCelli][pointi]
229 + blockOffsets[blockI]
230 ];
231 }
232
233 // Do automatic collapse from hex.
234 cellShapes[nCreatedCells].reset(hex, cellPoints, true);
235
236 nCreatedCells++;
237 }
238 }
239
240 Info<< "Creating boundary patches" << endl;
241
245 word defaultFacesName = "defaultFaces";
246 word defaultFacesType = wallPolyPatch::typeName;
247 wordList patchPhysicalTypes(0);
248
250 (
252 (
253 polyMesh::defaultRegion,
254 runTime.constant(),
255 runTime
256 ),
257 std::move(newPoints),
259 boundary,
264 patchPhysicalTypes
265 );
266
267 // Set the precision of the points data to 10
268 IOstream::defaultPrecision(max(10u, IOstream::defaultPrecision()));
269
270 Info<< "Writing polyMesh" << endl;
271 pShapeMesh.removeFiles();
272 pShapeMesh.write();
273
274 Info<< "End\n" << endl;
275
276 return 0;
277}
278
279
280// ************************************************************************* //
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
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: PtrList.H:73
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:124
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:278
bool check(bool checkArgs=argList::argsMandatory(), bool checkOpts=true) const
Definition: argList.C:1913
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:178
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:323
T getOrDefault(const word &optName, const T &deflt) const
Get a value from the named option if present, or return default.
Definition: argListI.H:307
Maps a geometry to a set of cell primitives.
Definition: cellModel.H:73
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:331
A class for handling file names.
Definition: fileName.H:76
Hex block definition used in the cfx converter.
Definition: hexBlock.H:53
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
A class for handling words, derived from Foam::string.
Definition: word.H:68
const cellModel & hex
cellShapeList cellShapes
faceListList boundary
engineTime & runTime
const pointField & points
Geometric merging of points. See below.
label mergePoints(const PointList &points, const IndexerOp &indexer, const label nSubPoints, labelList &pointToUnique, labelList &uniquePoints, const scalar mergeTol, const bool verbose)
Implementation detail for Foam::mergePoints.
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
error FatalError
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
wordList patchTypes(nPatches)
wordList patchNames(nPatches)
polyMesh pShapeMesh(IOobject(polyMesh::defaultRegion, runTime.constant(), runTime), std::move(points), cellShapes, boundary, patchNames, patchDicts, defaultFacesName, defaultFacesType)
word defaultFacesName
Definition: readKivaGrid.H:455
word defaultFacesType
Definition: readKivaGrid.H:456
Foam::argList args(argc, argv)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333