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 | Copyright (C) 2011-2016 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  plot3dToFoam
26 
27 Description
28  Plot3d mesh (ascii/formatted format) converter.
29 
30  Work in progress! Handles ascii multiblock (and optionally singleBlock)
31  format.
32  By default expects blanking. Use -noBlank if none.
33  Use -2D \a thickness if 2D.
34 
35  Niklas Nordin has experienced a problem with lefthandedness of the blocks.
36  The code should detect this automatically - see hexBlock::readPoints but
37  if this goes wrong just set the blockHandedness_ variable to 'right'
38  always.
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #include "argList.H"
43 #include "Time.H"
44 #include "IFstream.H"
45 #include "hexBlock.H"
46 #include "polyMesh.H"
47 #include "wallPolyPatch.H"
48 #include "symmetryPolyPatch.H"
49 #include "cellShape.H"
50 #include "cellModeller.H"
51 #include "mergePoints.H"
52 
53 using namespace Foam;
54 
55 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 
57 int main(int argc, char *argv[])
58 {
60  argList::validArgs.append("PLOT3D geom file");
62  (
63  "scale",
64  "factor",
65  "geometry scaling factor - default is 1"
66  );
68  (
69  "noBlank",
70  "skip blank items"
71  );
73  (
74  "singleBlock",
75  "input is a single block"
76  );
78  (
79  "2D",
80  "thickness",
81  "use when converting a 2-D geometry"
82  );
83 
84  argList args(argc, argv);
85 
86  if (!args.check())
87  {
88  FatalError.exit();
89  }
90 
91  const scalar scaleFactor = args.optionLookupOrDefault("scale", 1.0);
92 
93  bool readBlank = !args.optionFound("noBlank");
94  bool singleBlock = args.optionFound("singleBlock");
95  scalar twoDThickness = -1;
96  if (args.optionReadIfPresent("2D", twoDThickness))
97  {
98  Info<< "Reading 2D case by extruding points by " << twoDThickness
99  << " in z direction." << nl << endl;
100  }
101 
102 
103  #include "createTime.H"
104 
105  IFstream plot3dFile(args[1]);
106 
107  // Read the plot3d information using a fixed format reader.
108  // Comments in the file are in C++ style, so the stream parser will remove
109  // them with no intervention
110  label nblock;
111 
112  if (singleBlock)
113  {
114  nblock = 1;
115  }
116  else
117  {
118  plot3dFile >> nblock;
119  }
120 
121  Info<< "Reading " << nblock << " blocks" << endl;
122 
123  PtrList<hexBlock> blocks(nblock);
124 
125  {
126  label nx, ny, nz;
127 
128  forAll(blocks, blockI)
129  {
130  if (twoDThickness > 0)
131  {
132  // Fake second set of points (done in readPoints below)
133  plot3dFile >> nx >> ny;
134  nz = 2;
135  }
136  else
137  {
138  plot3dFile >> nx >> ny >> nz;
139  }
140 
141  Info<< "block " << blockI << " nx:" << nx
142  << " ny:" << ny << " nz:" << nz << endl;
143 
144  blocks.set(blockI, new hexBlock(nx, ny, nz));
145  }
146  }
147 
148  Info<< "Reading block points" << endl;
149  label sumPoints(0);
150  label nMeshCells(0);
151 
152  forAll(blocks, blockI)
153  {
154  Info<< "block " << blockI << ":" << nl;
155  blocks[blockI].readPoints(readBlank, twoDThickness, plot3dFile);
156  sumPoints += blocks[blockI].nBlockPoints();
157  nMeshCells += blocks[blockI].nBlockCells();
158  Info<< nl;
159  }
160 
161  pointField points(sumPoints);
162  labelList blockOffsets(blocks.size());
163  sumPoints = 0;
164  forAll(blocks, blockI)
165  {
166  const pointField& blockPoints = blocks[blockI].points();
167  blockOffsets[blockI] = sumPoints;
168  forAll(blockPoints, i)
169  {
170  points[sumPoints++] = blockPoints[i];
171  }
172  }
173 
174  // From old to new master point
175  labelList oldToNew;
176  pointField newPoints;
177 
178  // Merge points
180  (
181  points,
182  SMALL,
183  false,
184  oldToNew,
185  newPoints
186  );
187 
188  Info<< "Merged points within " << SMALL << " distance. Merged from "
189  << oldToNew.size() << " down to " << newPoints.size()
190  << " points." << endl;
191 
192  // Scale the points
193  if (scaleFactor > 1.0 + SMALL || scaleFactor < 1.0 - SMALL)
194  {
195  newPoints *= scaleFactor;
196  }
197 
198  Info<< "Creating cells" << endl;
199 
200  cellShapeList cellShapes(nMeshCells);
201 
202  const cellModel& hex = *(cellModeller::lookup("hex"));
203 
204  label nCreatedCells = 0;
205 
206  forAll(blocks, blockI)
207  {
208  labelListList curBlockCells = blocks[blockI].blockCells();
209 
210  forAll(curBlockCells, blockCelli)
211  {
212  labelList cellPoints(curBlockCells[blockCelli].size());
213 
214  forAll(cellPoints, pointi)
215  {
216  cellPoints[pointi] =
217  oldToNew
218  [
219  curBlockCells[blockCelli][pointi]
220  + blockOffsets[blockI]
221  ];
222  }
223 
224  // Do automatic collapse from hex.
225  cellShapes[nCreatedCells] = cellShape(hex, cellPoints, true);
226 
227  nCreatedCells++;
228  }
229  }
230 
231  Info<< "Creating boundary patches" << endl;
232 
234  wordList patchNames(0);
235  wordList patchTypes(0);
236  word defaultFacesName = "defaultFaces";
237  word defaultFacesType = wallPolyPatch::typeName;
238  wordList patchPhysicalTypes(0);
239 
241  (
242  IOobject
243  (
245  runTime.constant(),
246  runTime
247  ),
248  xferMove(newPoints),
249  cellShapes,
250  boundary,
251  patchNames,
252  patchTypes,
255  patchPhysicalTypes
256  );
257 
258  // Set the precision of the points data to 10
260 
261  Info<< "Writing polyMesh" << endl;
262  pShapeMesh.write();
263 
264  Info<< "End\n" << endl;
265 
266  return 0;
267 }
268 
269 
270 // ************************************************************************* //
word defaultFacesType
Definition: readKivaGrid.H:461
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
static const cellModel * lookup(const word &)
Look up a model by name and return a pointer to the model or NULL.
Definition: cellModeller.C:88
error FatalError
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
An analytical geometric cellShape.
Definition: cellShape.H:69
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:461
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198
wordList patchTypes(nPatches)
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:306
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static void noParallel()
Remove the parallel options.
Definition: argList.C:146
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:154
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:168
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:237
const pointField & points
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
Xfer< T > xferMove(T &)
Construct by transferring the contents of the arg.
polyMesh pShapeMesh(IOobject(polyMesh::defaultRegion, runTime.constant(), runTime), xferMove(points), cellShapes, boundary, patchNames, patchDicts, defaultFacesName, defaultFacesType)
A class for handling words, derived from string.
Definition: word.H:59
const cellShapeList & cellShapes
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:102
bool check(bool checkArgs=true, bool checkOpts=true) const
Check argument list.
Definition: argList.C:1170
wordList patchNames(nPatches)
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:93
faceListList boundary(nPatches)
static const char nl
Definition: Ostream.H:262
Input from file stream.
Definition: IFstream.H:81
Merge points. See below.
word defaultFacesName
Definition: readKivaGrid.H:460
Hex block definition used in the cfx converter.
Definition: hexBlock.H:50
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:62
label mergePoints(const UList< Type > &points, const scalar mergeTol, const bool verbose, labelList &pointMap, const Type &origin=Type::zero)
Sorts and merges points. All points closer than/equal mergeTol get merged.
Maps a geometry to a set of cell primitives, which enables geometric cell data to be calculated witho...
Definition: cellModel.H:64
virtual bool write() const
Write using setting from DB.
messageStream Info
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:83
Foam::argList args(argc, argv)
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Namespace for OpenFOAM.