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-2024 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 "foamVtkSurfaceWriter.H"
79 #include "attachPolyTopoChanger.H"
80 #include "polyTopoChange.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 "wordPair.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  " \n"
101  " The ordering of vertex and face labels within a block as shown "
102  "below.\n"
103  " For the local vertex numbering in the sequence 0 to 7:\n"
104  " Faces 0, 1 (x-direction) are left, right.\n"
105  " Faces 2, 3 (y-direction) are front, back.\n"
106  " Faces 4, 5 (z-direction) are bottom, top.\n"
107  " \n"
108  " 7 ---- 6\n"
109  " f5 |\\ :\\ f3\n"
110  " | | 4 ---- 5 \\\n"
111  " | 3.|....2 | \\\n"
112  " | \\| \\| f2\n"
113  " f4 0 ---- 1\n"
114  " Y Z\n"
115  " \\ | f0 ------ f1\n"
116  " \\|\n"
117  " o--- X\n"
118  );
119 
122 
124  (
125  "write-obj",
126  "Write block edges and centres as obj files and exit",
127  true // (old) mark as advanced option. -write-vtk is preferred
128  );
129  argList::addOptionCompat("write-obj", {"blockTopology", 1912});
130 
132  (
133  "write-vtk",
134  "Write topology as VTU file and exit"
135  );
137  (
138  "Force verbose output. (Can be used multiple times)"
139  );
140 
142  (
143  "merge-points",
144  "Geometric point merging instead of topological merging"
145  " [default for 1912 and earlier]."
146  // NOTE: " Slower, fails with high-aspect cells."
147  );
149  (
150  "no-clean",
151  "Do not remove polyMesh/ directory or files"
152  );
153  argList::addOptionCompat("no-clean", {"noClean", -2006});
154 
155  argList::addOption("dict", "file", "Alternative blockMeshDict");
157  (
158  "sets",
159  "Write cellZones as cellSets too (for processing purposes)"
160  );
162  (
163  "time",
164  "time",
165  "Specify a time to write mesh to (default: constant)"
166  );
167 
168  #include "addRegionOption.H"
169  #include "setRootCase.H"
170  #include "createTime.H"
171 
172  // Remove old files, unless disabled
173  const bool removeOldFiles = !args.found("no-clean");
174 
175  // Write cellSets
176  const bool writeCellSets = args.found("sets");
177 
178  // Default merge (topology), unless otherwise specified
179  const blockMesh::mergeStrategy strategy =
180  (
181  args.found("merge-points")
184  );
185 
186  // Specified region or default region
187  #include "getRegionOption.H"
188 
189  if (!polyMesh::regionName(regionName).empty())
190  {
191  Info<< nl << "Generating mesh for region " << regionName << nl;
192  }
193 
194  // Instance for resulting mesh
195  bool useTime = false;
196  word meshInstance(runTime.constant());
197 
198  if
199  (
200  args.readIfPresent("time", meshInstance)
201  && runTime.constant() != meshInstance
202  )
203  {
204  // Verify that the value is actually good
205  scalar timeValue;
206 
207  useTime = readScalar(meshInstance, timeValue);
208  if (!useTime)
209  {
211  << "Bad input value: " << meshInstance
212  << "Should be a scalar or 'constant'"
213  << nl << endl
214  << exit(FatalError);
215  }
216  }
217 
218 
219  // Locate appropriate blockMeshDict
220  #include "findBlockMeshDict.H"
221 
222  blockMesh blocks(meshDict, regionName, strategy, args.verbose());
223 
224  if (!blocks.good())
225  {
226  // Could/should be Fatal?
227 
229  << "Did not generate any blocks. Stopping." << nl << endl;
230 
231  return 1;
232  }
233 
234 
235  bool quickExit = false;
236 
237  if (args.found("write-obj"))
238  {
239  quickExit = true;
240  Info<< nl;
241  #include "blockMeshOBJ.H"
242  }
243 
244  if (args.found("write-vtk"))
245  {
246  quickExit = true;
247  Info<< nl;
248  #include "blockMeshVTK.H"
249  }
250 
251  if (quickExit)
252  {
253  Info<< "\nEnd\n" << endl;
254  return 0;
255  }
256 
257 
258  // Instance for resulting mesh
259  if (useTime)
260  {
261  Info<< "Writing polyMesh to " << meshInstance << nl << endl;
262 
263  // Make sure that the time is seen to be the current time.
264  // This is the logic inside regIOobject that resets the instance
265  // to the current time before writing
266  runTime.setTime(instant(meshInstance), 0);
267  }
268 
269  if (removeOldFiles)
270  {
271  #include "cleanMeshDirectory.H"
272  }
273 
274 
275  // Ensure we get information messages, even if turned off in dictionary
276  blocks.verbose(true);
277 
279  blocks.mesh
280  (
281  IOobject(regionName, meshInstance, runTime)
282  );
283 
284  polyMesh& mesh = *meshPtr;
285 
286  // Merge patch pairs (dictionary entry "mergePatchPairs")
287  #include "mergePatchPairs.H"
288 
289  // Handle cyclic patches
290  #include "handleCyclicPatches.H"
291 
292  // More precision (for points data)
294 
295  Info<< nl << "Writing polyMesh with "
296  << mesh.cellZones().size() << " cellZones";
297 
298  if (writeCellSets && !mesh.cellZones().empty())
299  {
300  Info<< " (written as cellSets too)";
301  }
302  Info<< endl;
303 
304  mesh.removeFiles();
305  if (!mesh.write())
306  {
308  << "Failed writing polyMesh."
309  << exit(FatalError);
310  }
311 
312  if (writeCellSets)
313  {
314  for (const cellZone& cz : mesh.cellZones())
315  {
316  cellSet(mesh, cz.name(), cz).write();
317  }
318  }
319 
320  #include "printMeshSummary.H"
321 
322  Info<< "\nEnd\n" << endl;
323 
324  return 0;
325 }
326 
327 
328 // ************************************************************************* //
const IOdictionary & meshDict
#define WarningIn(functionName)
Report a warning using Foam::Warning.
static void noFunctionObjects(bool addWithOption=false)
Remove &#39;-noFunctionObjects&#39; option and ignore any occurrences.
Definition: argList.C:547
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:462
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
const word & regionName() const
The mesh region name or word::null if polyMesh::defaultRegion.
Definition: polyMesh.C:847
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:374
static void noParallel()
Remove the parallel options.
Definition: argList.C:584
static void addOptionCompat(const word &optName, std::pair< const char *, int > compat)
Specify an alias for the option name.
Definition: argList.C:418
Required Classes.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Required Classes.
dynamicFvMesh & mesh
A multi-block mesh generator.
Definition: blockMesh.H:161
mergeStrategy
The block merging strategy.
Definition: blockMesh.H:180
void removeFiles(const fileName &instanceDir) const
Remove all files from mesh instance.
Definition: polyMesh.C:1329
Foam::autoPtr< Foam::dynamicFvMesh > meshPtr
A class for handling words, derived from Foam::string.
Definition: word.H:63
const word & executable() const noexcept
Name of executable without the path.
Definition: argListI.H:44
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:106
Default (TOPOLOGY), not selectable.
Definition: blockMesh.H:182
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:385
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:902
static unsigned int minPrecision(unsigned int prec) noexcept
Set the minimum default precision.
Definition: IOstream.H:440
virtual bool write(const bool writeOnProc=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1113
static void addVerboseOption(const string &usage="", bool advanced=false)
Enable a &#39;verbose&#39; bool option, with usage information.
Definition: argList.C:520
const word & constant() const noexcept
Return constant name.
Definition: TimePathsI.H:112
"points" merge by point geometry
Definition: blockMesh.H:184
A subset of mesh cells.
Definition: cellZone.H:58
bool empty() const noexcept
True if the list is empty (ie, size() is zero)
Definition: UPtrListI.H:99
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name...
Definition: instant.H:53
const word & name() const
Return reference to name.
Definition: fvMesh.H:387
Foam::word regionName(args.getOrDefault< word >("region", Foam::polyMesh::defaultRegion))
A collection of cell labels.
Definition: cellSet.H:47
const cellZoneMesh & cellZones() const noexcept
Return cell zone mesh.
Definition: polyMesh.H:679
messageStream Info
Information stream (stdout output on master, null elsewhere)
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:75
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:316
Foam::argList args(argc, argv)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:180
int verbose() const noexcept
Return the verbose flag.
Definition: argListI.H:121
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
Namespace for OpenFOAM.