blockMesh.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) 2016-2020 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  blockMesh
29 
30 Group
31  grpMeshGenerationUtilities
32 
33 Description
34  A multi-block mesh generator.
35 
36  Uses the block mesh description found in
37  - \c system/blockMeshDict
38  - \c system/<region>/blockMeshDict
39  - \c constant/polyMesh/blockMeshDict
40  - \c constant/<region>/polyMesh/blockMeshDict
41 
42 Usage
43  \b blockMesh [OPTION]
44 
45  Options:
46  - \par -write-obj
47  Write topology as a set of edges in OBJ format and exit.
48 
49  - \par -write-vtk
50  Write topology as VTK file (xml, ascii) and exit.
51 
52  - \par -merge-points
53  Merge points instead of default topological merge
54 
55  - \par -region <name>
56  Specify alternative mesh region.
57 
58  - \par -dict <filename>
59  Alternative dictionary for the block mesh description.
60 
61  - \par -sets
62  Write cellZones as cellSets too (for processing purposes)
63 
64  - \par -noClean
65  Do not remove any existing polyMesh/ directory or files
66 
67  - \par -time
68  Write resulting mesh to a time directory (instead of constant)
69 
70 \*---------------------------------------------------------------------------*/
71 
72 #include "Time.H"
73 #include "IOdictionary.H"
74 #include "IOPtrList.H"
75 
76 #include "blockMesh.H"
78 #include "attachPolyTopoChanger.H"
79 #include "polyTopoChange.H"
80 #include "emptyPolyPatch.H"
81 #include "cyclicPolyPatch.H"
82 #include "cellSet.H"
83 
84 #include "argList.H"
85 #include "OSspecific.H"
86 #include "OFstream.H"
87 
88 #include "Pair.H"
89 #include "slidingInterface.H"
90 
91 using namespace Foam;
92 
93 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
94 
95 int main(int argc, char *argv[])
96 {
98  (
99  "Block mesh generator.\n"
100  " The ordering of vertex and face labels within a block as shown "
101  "below.\n"
102  " For the local vertex numbering in the sequence 0 to 7:\n"
103  " Faces 0, 1 (x-direction) are left, right.\n"
104  " Faces 2, 3 (y-direction) are front, back.\n"
105  " Faces 4, 5 (z-direction) are bottom, top.\n"
106  "\n"
107  " 7 ---- 6\n"
108  " f5 |\\ |\\ f3\n"
109  " | | 4 ---- 5 \\\n"
110  " | 3 |--- 2 | \\\n"
111  " | \\| \\| f2\n"
112  " f4 0 ---- 1\n"
113  " Y Z\n"
114  " \\ | f0 ------ f1\n"
115  " \\|\n"
116  " O--- X\n"
117  );
118 
121 
123  (
124  "write-obj",
125  "Write block edges and centres as obj files and exit"
126  );
127  argList::addOptionCompat("write-obj", {"blockTopology", 1912});
128 
130  (
131  "write-vtk",
132  "Write topology as VTK file and exit"
133  );
134 
136  (
137  "merge-points",
138  "Geometric (point) merging instead of topological merging "
139  "(slower, fails with high-aspect cells. default for 1912 and earlier)",
140  true // mark as an advanced option
141  );
143  (
144  "noClean",
145  "Do not remove any existing polyMesh/ directory or files"
146  );
147  argList::addOption("dict", "file", "Alternative blockMeshDict");
149  (
150  "sets",
151  "Write cellZones as cellSets too (for processing purposes)"
152  );
154  (
155  "time",
156  "time",
157  "Specify a time to write mesh to (default: constant)"
158  );
159 
160  #include "addRegionOption.H"
161  #include "setRootCase.H"
162  #include "createTime.H"
163 
164  // Remove old files, unless disabled
165  const bool removeOldFiles = !args.found("noClean");
166 
167  // Default merge (topology), unless otherwise specified
169 
170  if (args.found("merge-points"))
171  {
172  strategy = blockMesh::MERGE_POINTS;
173  }
174 
176  word regionPath;
177 
178  // Check if the region is specified otherwise mesh the default region
179  if (args.readIfPresent("region", regionName))
180  {
181  Info<< nl << "Generating mesh for region " << regionName << endl;
182  regionPath = regionName;
183  }
184 
185 
186  // Instance for resulting mesh
187  bool useTime = false;
188  word meshInstance(runTime.constant());
189 
190  if
191  (
192  args.readIfPresent("time", meshInstance)
193  && runTime.constant() != meshInstance
194  )
195  {
196  // Verify that the value is actually good
197  scalar timeValue;
198 
199  useTime = readScalar(meshInstance, timeValue);
200  if (!useTime)
201  {
203  << "Bad input value: " << meshInstance
204  << "Should be a scalar or 'constant'"
205  << nl << endl
206  << exit(FatalError);
207  }
208  }
209 
210 
211  // Locate appropriate blockMeshDict
212  #include "findBlockMeshDict.H"
213 
214  blockMesh blocks(meshDict, regionName, strategy);
215 
216  if (!blocks.valid())
217  {
218  // Could/should be Fatal?
219 
221  << "Did not generate any blocks. Stopping." << nl << endl;
222 
223  return 1;
224  }
225 
226 
227  bool quickExit = false;
228 
229  if (args.found("write-obj"))
230  {
231  quickExit = true;
232 
233  Info<< nl;
234 
235  // Write mesh as edges
236  {
237  OFstream os(runTime.path()/"blockTopology.obj");
238 
239  Info<< "Writing block structure in obj format: "
240  << os.name().name() << endl;
241 
242  blocks.writeTopology(os);
243  }
244 
245  // Write centres of blocks
246  {
247  OFstream os(runTime.path()/"blockCentres.obj");
248 
249  Info<< "Writing block centres in obj format: "
250  << os.name().name() << endl;
251 
252  for (const point& cc : blocks.topology().cellCentres())
253  {
254  os << "v " << cc.x() << ' ' << cc.y() << ' ' << cc.z() << nl;
255  }
256  }
257  }
258 
259  if (args.found("write-vtk"))
260  {
261  quickExit = true;
262 
263  // non-legacy and ASCII (mesh is small, want readable output)
265 
266  Info<< nl;
267 
268  const polyMesh& topoMesh = blocks.topology();
269  const vtk::vtuCells topoCells(topoMesh, writeOpts);
270 
272  (
273  topoMesh,
274  topoCells,
275  writeOpts,
276  runTime.path()/"blockTopology"
277  );
278 
279  Info<< "Writing block topology in vtk format: "
280  << args.relativePath(writer.output()).c_str() << endl;
281 
282  writer.writeGeometry();
283  writer.beginCellData();
284  writer.writeCellIDs();
285  }
286 
287 
288  if (quickExit)
289  {
290  Info<< "\nEnd\n" << endl;
291 
292  return 0;
293  }
294 
295 
296  // Instance for resulting mesh
297  if (useTime)
298  {
299  Info<< "Writing polyMesh to " << meshInstance << nl << endl;
300 
301  // Make sure that the time is seen to be the current time.
302  // This is the logic inside regIOobject that resets the instance
303  // to the current time before writing
304  runTime.setTime(instant(meshInstance), 0);
305  }
306 
307  if (removeOldFiles)
308  {
309  const fileName polyMeshPath
310  (
311  runTime.path()/meshInstance/regionPath/polyMesh::meshSubDir
312  );
313 
314  if (exists(polyMeshPath))
315  {
316  if (exists(polyMeshPath/dictName))
317  {
318  Info<< "Not deleting polyMesh directory "
319  << runTime.relativePath(polyMeshPath) << nl
320  << " because it contains " << dictName << endl;
321  }
322  else
323  {
324  Info<< "Deleting polyMesh directory "
325  << runTime.relativePath(polyMeshPath) << endl;
326  rmDir(polyMeshPath);
327  }
328  }
329  }
330 
331 
332  Info<< nl << "Creating polyMesh from blockMesh" << endl;
333 
334  polyMesh mesh
335  (
336  IOobject
337  (
338  regionName,
339  meshInstance,
340  runTime
341  ),
342  pointField(blocks.points()), // Copy, could we re-use space?
343  blocks.cells(),
344  blocks.patches(),
345  blocks.patchNames(),
346  blocks.patchDicts(),
347  "defaultFaces", // Default patch name
348  emptyPolyPatch::typeName // Default patch type
349  );
350 
351 
352  // Handle merging of patch pairs. Dictionary entry "mergePatchPairs"
353  #include "mergePatchPairs.H"
354 
355  // Set any cellZones
356  #include "addCellZones.H"
357 
358 
359  // Detect any cyclic patches and force re-ordering of the faces
360  {
361  bool hasCyclic = false;
362  for (const polyPatch& pp : mesh.boundaryMesh())
363  {
364  if (isA<cyclicPolyPatch>(pp))
365  {
366  hasCyclic = true;
367  break;
368  }
369  }
370 
371  if (hasCyclic)
372  {
373  Info<< nl << "Detected cyclic patches; ordering boundary faces"
374  << endl;
375  const word oldInstance = mesh.instance();
376  polyTopoChange meshMod(mesh);
377  meshMod.changeMesh(mesh, false);
378  mesh.setInstance(oldInstance);
379  }
380  }
381 
382 
383  // Set the precision of the points data to 10
385 
386  Info<< nl << "Writing polyMesh with "
387  << mesh.cellZones().size() << " cellZones";
388 
389  if (args.found("sets") && !mesh.cellZones().empty())
390  {
391  Info<< " (written as cellSets too)";
392  }
393  Info<< endl;
394 
395  mesh.removeFiles();
396  if (!mesh.write())
397  {
399  << "Failed writing polyMesh."
400  << exit(FatalError);
401  }
402 
403  if (args.found("sets"))
404  {
405  for (const cellZone& cz : mesh.cellZones())
406  {
407  cellSet(mesh, cz.name(), cz).write();
408  }
409  }
410 
411  // Write summary
412  {
413  Info<< "----------------" << nl
414  << "Mesh Information" << nl
415  << "----------------" << nl
416  << " " << "boundingBox: " << boundBox(mesh.points()) << nl
417  << " " << "nPoints: " << mesh.nPoints() << nl
418  << " " << "nCells: " << mesh.nCells() << nl
419  << " " << "nFaces: " << mesh.nFaces() << nl
420  << " " << "nInternalFaces: " << mesh.nInternalFaces() << nl;
421 
422  Info<< "----------------" << nl
423  << "Patches" << nl
424  << "----------------" << nl;
425 
426  for (const polyPatch& p : mesh.boundaryMesh())
427  {
428  Info<< " " << "patch " << p.index()
429  << " (start: " << p.start()
430  << " size: " << p.size()
431  << ") name: " << p.name()
432  << nl;
433  }
434  }
435 
436  Info<< "\nEnd\n" << endl;
437 
438  return 0;
439 }
440 
441 
442 // ************************************************************************* //
Foam::vtk::outputOptions
Encapsulated combinations of output format options. This is primarily useful when defining the output...
Definition: foamVtkOutputOptions.H:59
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::polyMesh::points
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1038
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::exists
bool exists(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
Definition: MSwindows.C:625
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::blockMesh::DEFAULT_MERGE
Default (TOPOLOGY), not selectable.
Definition: blockMesh.H:158
Foam::fvMesh::write
virtual bool write(const bool valid=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:895
Foam::polyMesh::defaultRegion
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:312
cyclicPolyPatch.H
Foam::primitiveMesh::nInternalFaces
label nInternalFaces() const
Number of internal faces.
Definition: primitiveMeshI.H:78
Foam::vtk::formatType::INLINE_ASCII
XML inline ASCII, asciiFormatter.
Foam::primitiveMesh::nFaces
label nFaces() const
Number of mesh faces.
Definition: primitiveMeshI.H:90
Foam::IOobject::instance
const fileName & instance() const
Definition: IOobjectI.H:191
Foam::blockMesh::mergeStrategy
mergeStrategy
The block merging strategy.
Definition: blockMesh.H:156
Foam::polyMesh::meshSubDir
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:315
polyTopoChange.H
Foam::polyTopoChange
Direct mesh changes based on v1.3 polyTopoChange syntax.
Definition: polyTopoChange.H:99
Foam::argList::addNote
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:413
dictName
const word dictName("blockMeshDict")
Foam::polyMesh::cellZones
const cellZoneMesh & cellZones() const
Return cell zone mesh.
Definition: polyMesh.H:483
Foam::argList::addOptionCompat
static void addOptionCompat(const word &optName, std::pair< const char *, int > compat)
Specify an alias for the option name.
Definition: argList.C:369
findBlockMeshDict.H
Foam::polyMesh::boundaryMesh
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:435
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Pair.H
mergePatchPairs.H
blockMesh.H
Foam::argList::readIfPresent
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:302
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::cellZone
A subset of mesh cells.
Definition: cellZone.H:62
OFstream.H
regionName
Foam::word regionName
Definition: createNamedDynamicFvMesh.H:1
Foam::TimePaths::relativePath
fileName relativePath(const fileName &input, const bool caseTag=false) const
Definition: TimePathsI.H:79
Foam::argList::noFunctionObjects
static void noFunctionObjects(bool addWithOption=false)
Remove '-noFunctionObjects' option and ignore any occurrences.
Definition: argList.C:454
Foam::primitiveMesh::nCells
label nCells() const
Number of mesh cells.
Definition: primitiveMeshI.H:96
Foam::argList::executable
const word & executable() const
Name of executable without the path.
Definition: argListI.H:51
IOPtrList.H
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::polyPatch
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:67
Foam::polyMesh::removeFiles
void removeFiles(const fileName &instanceDir) const
Remove all files from mesh instance.
Definition: polyMesh.C:1264
argList.H
addRegionOption.H
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
Foam::FatalError
error FatalError
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::writer
Base class for graphics format writing. Entry points are.
Definition: writer.H:80
Foam::blockMesh::MERGE_POINTS
"points" merge by point geometry
Definition: blockMesh.H:160
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::cellSet
A collection of cell labels.
Definition: cellSet.H:51
emptyPolyPatch.H
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:87
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:325
IOdictionary.H
Time.H
setRootCase.H
attachPolyTopoChanger.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:372
slidingInterface.H
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::Time::path
fileName path() const
Return path.
Definition: Time.H:358
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:333
Foam::rmDir
bool rmDir(const fileName &directory, const bool silent=false)
Remove a directory and its contents (optionally silencing warnings)
Definition: MSwindows.C:1018
Foam::Vector< scalar >
Foam::Time::setTime
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:1006
Foam::vtk::vtuCells
A deep-copy description of an OpenFOAM volume mesh in data structures suitable for VTK UnstructuredGr...
Definition: foamVtuCells.H:73
Foam::primitiveMesh::nPoints
label nPoints() const
Number of mesh points.
Definition: primitiveMeshI.H:37
createTime.H
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:35
Foam::boundBox
A bounding box defined in terms of min/max extrema points.
Definition: boundBox.H:63
foamVtkInternalMeshWriter.H
addCellZones.H
Foam::instant
An instant of time. Contains the time value and name.
Definition: instant.H:52
WarningIn
#define WarningIn(functionName)
Report a warning using Foam::Warning.
Definition: messageStream.H:293
Foam::vtk::internalMeshWriter
Write an OpenFOAM volume (internal) geometry and internal fields as a vtu file or a legacy vtk file.
Definition: foamVtkInternalMeshWriter.H:69
cellSet.H
Foam::argList::noParallel
static void noParallel()
Remove the parallel options.
Definition: argList.C:491
Foam::TimePaths::constant
const word & constant() const
Return constant name.
Definition: TimePathsI.H:88
Foam::argList::addOption
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:336
args
Foam::argList args(argc, argv)
meshDict
const IOdictionary & meshDict
Definition: findBlockMeshDict.H:72
Foam::argList::relativePath
fileName relativePath(const fileName &input, const bool caseTag=false) const
Definition: argListI.H:94
Foam::polyMesh::setInstance
void setInstance(const fileName &instance, const IOobject::writeOption wOpt=IOobject::AUTO_WRITE)
Set the instance for mesh files.
Definition: polyMeshIO.C:36
Foam::argList::found
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:157
Foam::fvMesh::name
const word & name() const
Return reference to name.
Definition: fvMesh.H:268
Foam::blockMesh
A multi-block mesh generator.
Definition: blockMesh.H:147