Loading...
Searching...
No Matches
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-2024 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{
65 (
66 "Plot3d mesh (ascii/formatted format) converter"
67 );
69 argList::addArgument("PLOT3D geom file");
71 (
72 "scale",
73 "factor",
74 "Geometry scaling factor - default is 1"
75 );
77 (
78 "noBlank",
79 "Skip blank items"
80 );
82 (
83 "singleBlock",
84 "Input is a single block"
85 );
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 {
97 FatalError.exit();
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
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
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 (
254 runTime.constant(),
255 runTime
256 ),
257 std::move(newPoints),
259 boundary,
264 patchPhysicalTypes
265 );
266
267 // More precision (for points data)
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// ************************************************************************* //
Input from file stream as an ISstream, normally using std::ifstream for the actual input.
Definition IFstream.H:55
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
static unsigned int minPrecision(unsigned int prec) noexcept
Set the minimum default precision.
Definition IOstream.H:459
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition PtrList.H:67
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
Extract command arguments and options from the supplied argc and argv parameters.
Definition argList.H:119
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition argList.C:366
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition argList.C:389
static void noParallel()
Remove the parallel options.
Definition argList.C:599
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Add an option to validOptions with usage information.
Definition argList.C:400
static void addNote(const string &note)
Add extra notes for the usage information.
Definition argList.C:477
Maps a geometry to a set of cell primitives.
Definition cellModel.H:73
static const cellModel & ref(const modelType model)
Look up reference to cellModel by enumeration. Fatal on failure.
Definition cellModels.C:150
A class for handling file names.
Definition fileName.H:75
Hex block definition used in the cfx converter.
Definition hexBlock.H:49
Mesh consisting of general polyhedral cells.
Definition polyMesh.H:79
static word defaultRegion
Return the default region name.
Definition polyMesh.H:406
A class for handling words, derived from Foam::string.
Definition word.H:66
cellShapeList cellShapes
faceListList boundary
engineTime & runTime
const pointField & points
Geometric merging of points. See below.
Namespace for OpenFOAM.
List< word > wordList
List of word.
Definition fileName.H:60
List< labelList > labelListList
List of labelList.
Definition labelList.H:38
List< label > labelList
A List of labels.
Definition List.H:62
messageStream Info
Information stream (stdout output on master, null elsewhere).
IOstream & hex(IOstream &io)
Definition IOstream.H:579
label mergePoints(const PointList &points, labelList &pointToUnique, labelList &uniquePoints, const scalar mergeTol=SMALL, const bool verbose=false)
Calculate merge mapping, preserving the original point order. All points closer/equal mergeTol are to...
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
List< faceList > faceListList
List of faceList.
Definition faceListFwd.H:44
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
vectorField pointField
pointField is a vectorField.
List< cellShape > cellShapeList
List of cellShape.
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
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
word defaultFacesType
Foam::argList args(argc, argv)
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299