checkMesh.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2023 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  checkMesh
26 
27 Description
28  Checks validity of a mesh.
29 
30 Usage
31  \b checkMesh [OPTION]
32 
33  Options:
34  - \par -allGeometry
35  Checks all (including non finite-volume specific) geometry
36 
37  - \par -allTopology
38  Checks all (including non finite-volume specific) addressing
39 
40  - \par -meshQuality
41  Checks against user defined (in \a system/meshQualityDict) quality
42  settings
43 
44  - \par -region <name>
45  Specify an alternative mesh region.
46 
47  - \par -writeSets <surfaceFormat>
48  Reconstruct all cellSets and faceSets geometry and write to
49  postProcessing directory according to surfaceFormat
50  (e.g. vtk or ensight). Additionally reconstructs all pointSets and
51  writes as vtk format.
52 
53 \*---------------------------------------------------------------------------*/
54 
55 #include "argList.H"
56 #include "timeSelector.H"
57 #include "Time.H"
58 #include "polyMesh.H"
59 #include "globalMeshData.H"
60 #include "surfaceWriter.H"
61 #include "vtkSetWriter.H"
62 #include "IOdictionary.H"
63 
64 #include "checkTools.H"
65 #include "checkTopology.H"
66 #include "checkGeometry.H"
67 #include "checkMeshQuality.H"
68 
69 using namespace Foam;
70 
71 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72 
73 int main(int argc, char *argv[])
74 {
76  #include "addRegionOption.H"
78  (
79  "noTopology",
80  "skip checking the mesh topology"
81  );
83  (
84  "allGeometry",
85  "include bounding box checks"
86  );
88  (
89  "allTopology",
90  "include extra topology checks"
91  );
93  (
94  "meshQuality",
95  "read user-defined mesh quality criterions from system/meshQualityDict"
96  );
98  (
99  "writeSets",
100  "surfaceFormat",
101  "reconstruct and write all faceSets and cellSets in selected format"
102  );
103 
104  #include "setRootCase.H"
105  #include "createTime.H"
107  #include "createNamedPolyMesh.H"
108 
109  const bool noTopology = args.optionFound("noTopology");
110  const bool allGeometry = args.optionFound("allGeometry");
111  const bool allTopology = args.optionFound("allTopology");
112  const bool meshQuality = args.optionFound("meshQuality");
113 
114  word surfaceFormat;
115  const bool writeSets = args.optionReadIfPresent("writeSets", surfaceFormat);
116 
117  if (noTopology)
118  {
119  Info<< "Disabling all topology checks." << nl << endl;
120  }
121  if (allTopology)
122  {
123  Info<< "Enabling all (cell, face, edge, point) topology checks."
124  << nl << endl;
125  }
126  if (allGeometry)
127  {
128  Info<< "Enabling all geometry checks." << nl << endl;
129  }
130  if (meshQuality)
131  {
132  Info<< "Enabling user-defined geometry checks." << nl << endl;
133  }
134  if (writeSets)
135  {
136  Info<< "Reconstructing and writing " << surfaceFormat
137  << " representation"
138  << " of all faceSets and cellSets." << nl << endl;
139  }
140 
141 
142  autoPtr<IOdictionary> qualDict;
143  if (meshQuality)
144  {
145  qualDict.reset
146  (
147  new IOdictionary
148  (
149  IOobject
150  (
151  "meshQualityDict",
152  mesh.time().system(),
153  mesh,
156  )
157  )
158  );
159  }
160 
161 
162  autoPtr<surfaceWriter> surfWriter;
164  if (writeSets)
165  {
166  surfWriter = surfaceWriter::New
167  (
168  surfaceFormat,
169  mesh.time().writeFormat(),
170  mesh.time().writeCompression()
171  );
173  (
174  vtkSetWriter::typeName,
175  mesh.time().writeFormat(),
176  mesh.time().writeCompression()
177  );
178  }
179 
180 
181  forAll(timeDirs, timeI)
182  {
183  runTime.setTime(timeDirs[timeI], timeI);
184 
185  polyMesh::readUpdateState state = mesh.readUpdate();
186 
187  if
188  (
189  !timeI
190  || state == polyMesh::TOPO_CHANGE
191  || state == polyMesh::TOPO_PATCH_CHANGE
192  )
193  {
194  Info<< "Time = " << runTime.userTimeName() << nl << endl;
195 
196  // Reconstruct globalMeshData
197  mesh.globalData();
198 
199  printMeshStats(mesh, allTopology);
200 
201  label nFailedChecks = 0;
202 
203  if (!noTopology)
204  {
205  nFailedChecks += checkTopology
206  (
207  mesh,
208  allTopology,
209  allGeometry,
210  surfWriter,
211  setWriter
212  );
213  }
214 
215  nFailedChecks += checkGeometry
216  (
217  mesh,
218  allGeometry,
219  surfWriter,
220  setWriter
221  );
222 
223  if (meshQuality)
224  {
225  nFailedChecks += checkMeshQuality(mesh, qualDict(), surfWriter);
226  }
227 
228 
229  // Note: no reduction in nFailedChecks necessary since is
230  // counter of checks, not counter of failed cells,faces etc.
231 
232  if (nFailedChecks == 0)
233  {
234  Info<< "\nMesh OK.\n" << endl;
235  }
236  else
237  {
238  Info<< "\nFailed " << nFailedChecks << " mesh checks.\n"
239  << endl;
240  }
241  }
242  else if (state == polyMesh::POINTS_MOVED)
243  {
244  Info<< "Time = " << runTime.userTimeName() << nl << endl;
245 
246  label nFailedChecks = checkGeometry
247  (
248  mesh,
249  allGeometry,
250  surfWriter,
251  setWriter
252  );
253 
254  if (meshQuality)
255  {
256  nFailedChecks += checkMeshQuality(mesh, qualDict(), surfWriter);
257  }
258 
259 
260  if (nFailedChecks)
261  {
262  Info<< "\nFailed " << nFailedChecks << " mesh checks.\n"
263  << endl;
264  }
265  else
266  {
267  Info<< "\nMesh OK.\n" << endl;
268  }
269  }
270  }
271 
272  Info<< "End\n" << endl;
273 
274  return 0;
275 }
276 
277 
278 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Routines for checking mesh geometry.
Routines for checking mesh quality.
Tools for checking the mesh.
Tools for checking the mesh.
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:57
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:128
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:118
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:204
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
void reset(T *=nullptr)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
readUpdateState
Enumeration defining the state of the mesh after a read update.
Definition: polyMesh.H:91
@ TOPO_PATCH_CHANGE
Definition: polyMesh.H:95
Base class for writing coordinate sets with data.
Definition: setWriter.H:64
static autoPtr< setWriter > New(const word &writeType, const IOstream::streamFormat writeFormat=IOstream::ASCII, const IOstream::compressionType writeCompression=IOstream::UNCOMPRESSED)
Select given write options.
Definition: setWriter.C:286
static autoPtr< surfaceWriter > New(const word &writeType, const IOstream::streamFormat writeFormat, const IOstream::compressionType writeCompression)
Select given write options.
Definition: surfaceWriter.C:75
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
static instantList select0(Time &runTime, const argList &args)
Return the set of times selected based on the argList options.
Definition: timeSelector.C:252
A class for handling words, derived from string.
Definition: word.H:62
int main(int argc, char *argv[])
Definition: financialFoam.C:44
static instantList timeDirs
Definition: globalFoam.H:44
Namespace for OpenFOAM.
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
messageStream Info
label checkGeometry(const polyMesh &mesh, const bool allGeometry, const autoPtr< surfaceWriter > &, const autoPtr< setWriter > &)
Check the geometry.
void printMeshStats(const polyMesh &mesh, const bool allTopology)
Print mesh statistics.
label checkTopology(const polyMesh &mesh, const bool allTopology, const bool allGeometry, const autoPtr< surfaceWriter > &surfWriter, const autoPtr< Foam::setWriter > &setWriter)
label checkMeshQuality(const polyMesh &, const dictionary &, const autoPtr< surfaceWriter > &)
static const char nl
Definition: Ostream.H:260
Foam::argList args(argc, argv)