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-2020 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;
163  autoPtr<writer<scalar>> setWriter;
164  if (writeSets)
165  {
166  surfWriter = surfaceWriter::New
167  (
168  surfaceFormat,
169  mesh.time().writeFormat()
170  );
172  }
173 
174 
175  forAll(timeDirs, timeI)
176  {
177  runTime.setTime(timeDirs[timeI], timeI);
178 
180 
181  if
182  (
183  !timeI
184  || state == polyMesh::TOPO_CHANGE
185  || state == polyMesh::TOPO_PATCH_CHANGE
186  )
187  {
188  Info<< "Time = " << runTime.timeName() << nl << endl;
189 
190  // Reconstruct globalMeshData
191  mesh.globalData();
192 
193  printMeshStats(mesh, allTopology);
194 
195  label nFailedChecks = 0;
196 
197  if (!noTopology)
198  {
199  nFailedChecks += checkTopology
200  (
201  mesh,
202  allTopology,
203  allGeometry,
204  surfWriter,
205  setWriter
206  );
207  }
208 
209  nFailedChecks += checkGeometry
210  (
211  mesh,
212  allGeometry,
213  surfWriter,
214  setWriter
215  );
216 
217  if (meshQuality)
218  {
219  nFailedChecks += checkMeshQuality(mesh, qualDict(), surfWriter);
220  }
221 
222 
223  // Note: no reduction in nFailedChecks necessary since is
224  // counter of checks, not counter of failed cells,faces etc.
225 
226  if (nFailedChecks == 0)
227  {
228  Info<< "\nMesh OK.\n" << endl;
229  }
230  else
231  {
232  Info<< "\nFailed " << nFailedChecks << " mesh checks.\n"
233  << endl;
234  }
235  }
236  else if (state == polyMesh::POINTS_MOVED)
237  {
238  Info<< "Time = " << runTime.timeName() << nl << endl;
239 
240  label nFailedChecks = checkGeometry
241  (
242  mesh,
243  allGeometry,
244  surfWriter,
245  setWriter
246  );
247 
248  if (meshQuality)
249  {
250  nFailedChecks += checkMeshQuality(mesh, qualDict(), surfWriter);
251  }
252 
253 
254  if (nFailedChecks)
255  {
256  Info<< "\nFailed " << nFailedChecks << " mesh checks.\n"
257  << endl;
258  }
259  else
260  {
261  Info<< "\nMesh OK.\n" << endl;
262  }
263  }
264  }
265 
266  Info<< "End\n" << endl;
267 
268  return 0;
269 }
270 
271 
272 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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
void reset(T *=nullptr)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
static autoPtr< surfaceWriter > New(const word &writeType, const IOstream::streamFormat writeFormat)
Return a reference to the selected surfaceWriter.
Definition: surfaceWriter.C:45
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:622
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:239
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:204
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:52
dynamicFvMesh & mesh
label checkGeometry(const polyMesh &mesh, const bool allGeometry, const autoPtr< surfaceWriter > &, const autoPtr< writer< scalar >> &)
virtual readUpdateState readUpdate()
Update the mesh based on the mesh files saved in time.
Definition: fvMesh.C:489
A class for handling words, derived from string.
Definition: word.H:59
label checkMeshQuality(const polyMesh &, const dictionary &, const autoPtr< surfaceWriter > &)
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
label checkTopology(const polyMesh &, const bool, const bool, const autoPtr< surfaceWriter > &, const autoPtr< writer< scalar >> &)
const globalMeshData & globalData() const
Return parallel info.
Definition: polyMesh.C:1394
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:879
const word & system() const
Return system name.
Definition: TimePaths.H:114
static autoPtr< writer > New(const word &writeFormat)
Return a reference to the selected writer.
Definition: writer.C:35
static const char nl
Definition: Ostream.H:260
IOstream::streamFormat writeFormat() const
Default write format.
Definition: Time.H:287
static instantList select0(Time &runTime, const argList &args)
Return the set of times selected based on the argList options.
Definition: timeSelector.C:252
void printMeshStats(const polyMesh &mesh, const bool allTopology)
messageStream Info
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:118
readUpdateState
Enumeration defining the state of the mesh after a read update.
Definition: polyMesh.H:88
Foam::argList args(argc, argv)
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
Namespace for OpenFOAM.