All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2021 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  );
171  setWriter = Foam::setWriter<scalar>::New
172  (
174  );
175  }
176 
177 
178  forAll(timeDirs, timeI)
179  {
180  runTime.setTime(timeDirs[timeI], timeI);
181 
183 
184  if
185  (
186  !timeI
187  || state == polyMesh::TOPO_CHANGE
188  || state == polyMesh::TOPO_PATCH_CHANGE
189  )
190  {
191  Info<< "Time = " << runTime.timeName() << nl << endl;
192 
193  // Reconstruct globalMeshData
194  mesh.globalData();
195 
196  printMeshStats(mesh, allTopology);
197 
198  label nFailedChecks = 0;
199 
200  if (!noTopology)
201  {
202  nFailedChecks += checkTopology
203  (
204  mesh,
205  allTopology,
206  allGeometry,
207  surfWriter,
208  setWriter
209  );
210  }
211 
212  nFailedChecks += checkGeometry
213  (
214  mesh,
215  allGeometry,
216  surfWriter,
217  setWriter
218  );
219 
220  if (meshQuality)
221  {
222  nFailedChecks += checkMeshQuality(mesh, qualDict(), surfWriter);
223  }
224 
225 
226  // Note: no reduction in nFailedChecks necessary since is
227  // counter of checks, not counter of failed cells,faces etc.
228 
229  if (nFailedChecks == 0)
230  {
231  Info<< "\nMesh OK.\n" << endl;
232  }
233  else
234  {
235  Info<< "\nFailed " << nFailedChecks << " mesh checks.\n"
236  << endl;
237  }
238  }
239  else if (state == polyMesh::POINTS_MOVED)
240  {
241  Info<< "Time = " << runTime.timeName() << nl << endl;
242 
243  label nFailedChecks = checkGeometry
244  (
245  mesh,
246  allGeometry,
247  surfWriter,
248  setWriter
249  );
250 
251  if (meshQuality)
252  {
253  nFailedChecks += checkMeshQuality(mesh, qualDict(), surfWriter);
254  }
255 
256 
257  if (nFailedChecks)
258  {
259  Info<< "\nFailed " << nFailedChecks << " mesh checks.\n"
260  << endl;
261  }
262  else
263  {
264  Info<< "\nMesh OK.\n" << endl;
265  }
266  }
267  }
268 
269  Info<< "End\n" << endl;
270 
271  return 0;
272 }
273 
274 
275 // ************************************************************************* //
#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:636
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:53
dynamicFvMesh & mesh
virtual readUpdateState readUpdate()
Update the mesh based on the mesh files saved in time.
Definition: fvMesh.C:489
label checkTopology(const polyMesh &, const bool, const bool, const autoPtr< surfaceWriter > &, const autoPtr< setWriter< scalar >> &)
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
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:899
const word & system() const
Return system name.
Definition: TimePaths.H:113
static const char nl
Definition: Ostream.H:260
IOstream::streamFormat writeFormat() const
Default write format.
Definition: Time.H:285
static instantList select0(Time &runTime, const argList &args)
Return the set of times selected based on the argList options.
Definition: timeSelector.C:252
static autoPtr< setWriter > New(const word &writeFormat)
Return a reference to the selected setWriter.
Definition: setWriter.C:35
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
label checkGeometry(const polyMesh &mesh, const bool allGeometry, const autoPtr< surfaceWriter > &, const autoPtr< setWriter< scalar >> &)
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.