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 -no-clean
65  Do not remove 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 "cyclicPolyPatch.H"
81 #include "cellSet.H"
82 
83 #include "argList.H"
84 #include "OSspecific.H"
85 #include "OFstream.H"
86 
87 #include "wordPair.H"
88 #include "slidingInterface.H"
89 
90 using namespace Foam;
91 
92 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
93 
94 int main(int argc, char *argv[])
95 {
97  (
98  "Block mesh generator.\n"
99  " The ordering of vertex and face labels within a block as shown "
100  "below.\n"
101  " For the local vertex numbering in the sequence 0 to 7:\n"
102  " Faces 0, 1 (x-direction) are left, right.\n"
103  " Faces 2, 3 (y-direction) are front, back.\n"
104  " Faces 4, 5 (z-direction) are bottom, top.\n"
105  "\n"
106  " 7 ---- 6\n"
107  " f5 |\\ |\\ f3\n"
108  " | | 4 ---- 5 \\\n"
109  " | 3 |--- 2 | \\\n"
110  " | \\| \\| f2\n"
111  " f4 0 ---- 1\n"
112  " Y Z\n"
113  " \\ | f0 ------ f1\n"
114  " \\|\n"
115  " O--- X\n"
116  );
117 
120 
122  (
123  "write-obj",
124  "Write block edges and centres as obj files and exit",
125  true // (old) mark as advanced option. -write-vtk is preferred
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  " [default for 1912 and earlier]."
140  // NOTE: " Slower, fails with high-aspect cells."
141  );
143  (
144  "no-clean",
145  "Do not remove polyMesh/ directory or files"
146  );
147  argList::addOptionCompat("no-clean", {"noClean", -2006});
148 
149  argList::addOption("dict", "file", "Alternative blockMeshDict");
151  (
152  "sets",
153  "Write cellZones as cellSets too (for processing purposes)"
154  );
156  (
157  "time",
158  "time",
159  "Specify a time to write mesh to (default: constant)"
160  );
161 
162  #include "addRegionOption.H"
163  #include "setRootCase.H"
164  #include "createTime.H"
165 
166  // Remove old files, unless disabled
167  const bool removeOldFiles = !args.found("no-clean");
168 
169  // Write cellSets
170  const bool writeCellSets = args.found("sets");
171 
172  // Default merge (topology), unless otherwise specified
174 
175  if (args.found("merge-points"))
176  {
177  strategy = blockMesh::MERGE_POINTS;
178  }
179 
181  word regionPath;
182 
183  // Check if the region is specified otherwise mesh the default region
184  if (args.readIfPresent("region", regionName))
185  {
186  Info<< nl << "Generating mesh for region " << regionName << endl;
187  regionPath = regionName;
188  }
189 
190 
191  // Instance for resulting mesh
192  bool useTime = false;
193  word meshInstance(runTime.constant());
194 
195  if
196  (
197  args.readIfPresent("time", meshInstance)
198  && runTime.constant() != meshInstance
199  )
200  {
201  // Verify that the value is actually good
202  scalar timeValue;
203 
204  useTime = readScalar(meshInstance, timeValue);
205  if (!useTime)
206  {
208  << "Bad input value: " << meshInstance
209  << "Should be a scalar or 'constant'"
210  << nl << endl
211  << exit(FatalError);
212  }
213  }
214 
215 
216  // Locate appropriate blockMeshDict
217  #include "findBlockMeshDict.H"
218 
219  blockMesh blocks(meshDict, regionName, strategy);
220 
221  if (!blocks.valid())
222  {
223  // Could/should be Fatal?
224 
226  << "Did not generate any blocks. Stopping." << nl << endl;
227 
228  return 1;
229  }
230 
231 
232  bool quickExit = false;
233 
234  if (args.found("write-obj"))
235  {
236  quickExit = true;
237  Info<< nl;
238  #include "blockMeshOBJ.H"
239  }
240 
241  if (args.found("write-vtk"))
242  {
243  quickExit = true;
244  Info<< nl;
245  #include "blockMeshVTK.H"
246  }
247 
248  if (quickExit)
249  {
250  Info<< "\nEnd\n" << endl;
251  return 0;
252  }
253 
254 
255  // Instance for resulting mesh
256  if (useTime)
257  {
258  Info<< "Writing polyMesh to " << meshInstance << nl << endl;
259 
260  // Make sure that the time is seen to be the current time.
261  // This is the logic inside regIOobject that resets the instance
262  // to the current time before writing
263  runTime.setTime(instant(meshInstance), 0);
264  }
265 
266  if (removeOldFiles)
267  {
268  #include "cleanMeshDirectory.H"
269  }
270 
271 
272  // Ensure we get information messages, even if turned off in dictionary
273  blocks.verbose(true);
274 
276  blocks.mesh
277  (
278  IOobject(regionName, meshInstance, runTime)
279  );
280 
281  polyMesh& mesh = *meshPtr;
282 
283  // Merge patch pairs (dictionary entry "mergePatchPairs")
284  #include "mergePatchPairs.H"
285 
286  // Handle cyclic patches
287  #include "handleCyclicPatches.H"
288 
289  // Set the precision of the points data to 10
291 
292  Info<< nl << "Writing polyMesh with "
293  << mesh.cellZones().size() << " cellZones";
294 
295  if (writeCellSets && !mesh.cellZones().empty())
296  {
297  Info<< " (written as cellSets too)";
298  }
299  Info<< endl;
300 
301  mesh.removeFiles();
302  if (!mesh.write())
303  {
305  << "Failed writing polyMesh."
306  << exit(FatalError);
307  }
308 
309  if (writeCellSets)
310  {
311  for (const cellZone& cz : mesh.cellZones())
312  {
313  cellSet(mesh, cz.name(), cz).write();
314  }
315  }
316 
317  #include "printMeshSummary.H"
318 
319  Info<< "\nEnd\n" << endl;
320 
321  return 0;
322 }
323 
324 
325 // ************************************************************************* //
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
meshPtr
Foam::autoPtr< Foam::fvMesh > meshPtr(nullptr)
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::blockMesh::DEFAULT_MERGE
Default (TOPOLOGY), not selectable.
Definition: blockMesh.H:165
Foam::fvMesh::write
virtual bool write(const bool valid=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1027
Foam::polyMesh::defaultRegion
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:318
cyclicPolyPatch.H
blockMeshVTK.H
Foam::blockMesh::mergeStrategy
mergeStrategy
The block merging strategy.
Definition: blockMesh.H:163
polyTopoChange.H
blockMeshOBJ.H
Foam::argList::addNote
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:426
Foam::polyMesh::cellZones
const cellZoneMesh & cellZones() const
Return cell zone mesh.
Definition: polyMesh.H:492
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:382
findBlockMeshDict.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
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:296
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
wordPair.H
Foam::cellZone
A subset of mesh cells.
Definition: cellZone.H:62
OFstream.H
regionName
Foam::word regionName
Definition: createNamedDynamicFvMesh.H:1
Foam::argList::noFunctionObjects
static void noFunctionObjects(bool addWithOption=false)
Remove '-noFunctionObjects' option and ignore any occurrences.
Definition: argList.C:467
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::polyMesh::removeFiles
void removeFiles(const fileName &instanceDir) const
Remove all files from mesh instance.
Definition: polyMesh.C:1325
argList.H
handleCyclicPatches.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::blockMesh::MERGE_POINTS
"points" merge by point geometry
Definition: blockMesh.H:167
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::cellSet
A collection of cell labels.
Definition: cellSet.H:51
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
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:338
IOdictionary.H
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
setRootCase.H
attachPolyTopoChanger.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
slidingInterface.H
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:333
Foam::Time::setTime
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:1003
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
foamVtkInternalMeshWriter.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:298
cellSet.H
Foam::argList::noParallel
static void noParallel()
Remove the parallel options.
Definition: argList.C:504
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:349
args
Foam::argList args(argc, argv)
meshDict
const IOdictionary & meshDict
Definition: findBlockMeshDict.H:72
Foam::argList::found
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:151
Foam::fvMesh::name
const word & name() const
Return reference to name.
Definition: fvMesh.H:295
Foam::blockMesh
A multi-block mesh generator.
Definition: blockMesh.H:148