checkMesh.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) 2015-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  checkMesh
29 
30 Group
31  grpMeshManipulationUtilities
32 
33 Description
34  Checks validity of a mesh.
35 
36 Usage
37  \b checkMesh [OPTION]
38 
39  Options:
40  - \par -allGeometry
41  Checks all (including non finite-volume specific) geometry
42 
43  - \par -allTopology
44  Checks all (including non finite-volume specific) addressing
45 
46  - \par -meshQuality
47  Checks against user defined (in \a system/meshQualityDict) quality
48  settings
49 
50  - \par -region <name>
51  Specify an alternative mesh region.
52 
53  \param -writeSets <surfaceFormat> \n
54  Reconstruct all cellSets and faceSets geometry and write to postProcessing
55  directory according to surfaceFormat (e.g. vtk or ensight). Additionally
56  reconstructs all pointSets and writes as vtk format.
57 
58  \param -writeAllFields \n
59  Writes all mesh quality measures as fields.
60 
61  \param -writeFields '(<fieldName>)' \n
62  Writes selected mesh quality measures as fields.
63 
64 \*---------------------------------------------------------------------------*/
65 
66 #include "argList.H"
67 #include "timeSelector.H"
68 #include "Time.H"
69 #include "fvMesh.H"
70 #include "globalMeshData.H"
71 #include "vtkSetWriter.H"
72 #include "vtkSurfaceWriter.H"
73 #include "IOdictionary.H"
74 
75 #include "checkTools.H"
76 #include "checkTopology.H"
77 #include "checkGeometry.H"
78 #include "checkMeshQuality.H"
79 #include "writeFields.H"
80 
81 using namespace Foam;
82 
83 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
84 
85 int main(int argc, char *argv[])
86 {
88  (
89  "Checks validity of a mesh"
90  );
91 
93  #include "addRegionOption.H"
95  (
96  "noTopology",
97  "Skip checking the mesh topology"
98  );
100  (
101  "allGeometry",
102  "Include bounding box checks"
103  );
105  (
106  "allTopology",
107  "Include extra topology checks"
108  );
110  (
111  "writeAllFields",
112  "Write volFields with mesh quality parameters"
113  );
115  (
116  "writeFields",
117  "wordList",
118  "Write volFields with selected mesh quality parameters"
119  );
121  (
122  "meshQuality",
123  "Read user-defined mesh quality criteria from system/meshQualityDict"
124  );
126  (
127  "writeSets",
128  "surfaceFormat",
129  "Reconstruct and write all faceSets and cellSets in selected format"
130  );
131 
132  #include "setRootCase.H"
133  #include "createTime.H"
135  #include "createNamedMesh.H"
136 
137  const bool noTopology = args.found("noTopology");
138  const bool allGeometry = args.found("allGeometry");
139  const bool allTopology = args.found("allTopology");
140  const bool meshQuality = args.found("meshQuality");
141 
142  const word surfaceFormat = args.getOrDefault<word>("writeSets", "");
143  const bool writeSets = surfaceFormat.size();
144 
145  wordHashSet selectedFields;
147  (
148  "writeFields",
149  selectedFields
150  );
151  if (!writeFields && args.found("writeAllFields"))
152  {
153  selectedFields.insert
154  ({
155  "nonOrthoAngle",
156  "faceWeight",
157  "skewness",
158  "cellDeterminant",
159  "aspectRatio",
160  "cellShapes",
161  "cellVolume",
162  "cellVolumeRatio",
163  "minTetVolume",
164  "minPyrVolume",
165  "cellRegion",
166  "wallDistance",
167  "cellZone",
168  "faceZone"
169  });
170  }
171 
172 
173  if (noTopology)
174  {
175  Info<< "Disabling all topology checks." << nl << endl;
176  }
177  if (allTopology)
178  {
179  Info<< "Enabling all (cell, face, edge, point) topology checks."
180  << nl << endl;
181  }
182  if (allGeometry)
183  {
184  Info<< "Enabling all geometry checks." << nl << endl;
185  }
186  if (meshQuality)
187  {
188  Info<< "Enabling user-defined geometry checks." << nl << endl;
189  }
190  if (writeSets)
191  {
192  Info<< "Reconstructing and writing " << surfaceFormat
193  << " representation"
194  << " of all faceSets and cellSets." << nl << endl;
195  }
196  if (selectedFields.size())
197  {
198  Info<< "Writing mesh quality as fields " << selectedFields << nl
199  << endl;
200  }
201 
202 
203  autoPtr<IOdictionary> qualDict;
204  if (meshQuality)
205  {
206  qualDict.reset
207  (
208  new IOdictionary
209  (
210  IOobject
211  (
212  "meshQualityDict",
213  mesh.time().system(),
214  mesh,
217  )
218  )
219  );
220  }
221 
222 
223  autoPtr<surfaceWriter> surfWriter;
224  autoPtr<writer<scalar>> setWriter;
225  if (writeSets)
226  {
227  surfWriter = surfaceWriter::New(surfaceFormat);
229  }
230 
231 
232  forAll(timeDirs, timeI)
233  {
234  runTime.setTime(timeDirs[timeI], timeI);
235 
237 
238  if
239  (
240  !timeI
241  || state == polyMesh::TOPO_CHANGE
242  || state == polyMesh::TOPO_PATCH_CHANGE
243  )
244  {
245  Info<< "Time = " << runTime.timeName() << nl << endl;
246 
247  // Reconstruct globalMeshData
248  mesh.globalData();
249 
250  printMeshStats(mesh, allTopology);
251 
252  label nFailedChecks = 0;
253 
254  if (!noTopology)
255  {
256  nFailedChecks += checkTopology
257  (
258  mesh,
259  allTopology,
260  allGeometry,
261  surfWriter,
262  setWriter
263  );
264  }
265 
266  nFailedChecks += checkGeometry
267  (
268  mesh,
269  allGeometry,
270  surfWriter,
271  setWriter
272  );
273 
274  if (meshQuality)
275  {
276  nFailedChecks += checkMeshQuality(mesh, qualDict(), surfWriter);
277  }
278 
279 
280  // Note: no reduction in nFailedChecks necessary since is
281  // counter of checks, not counter of failed cells,faces etc.
282 
283  if (nFailedChecks == 0)
284  {
285  Info<< "\nMesh OK.\n" << endl;
286  }
287  else
288  {
289  Info<< "\nFailed " << nFailedChecks << " mesh checks.\n"
290  << endl;
291  }
292 
293 
294  // Write selected fields
295  Foam::writeFields(mesh, selectedFields);
296  }
297  else if (state == polyMesh::POINTS_MOVED)
298  {
299  Info<< "Time = " << runTime.timeName() << nl << endl;
300 
301  label nFailedChecks = checkGeometry
302  (
303  mesh,
304  allGeometry,
305  surfWriter,
306  setWriter
307  );
308 
309  if (meshQuality)
310  {
311  nFailedChecks += checkMeshQuality(mesh, qualDict(), surfWriter);
312  }
313 
314 
315  if (nFailedChecks)
316  {
317  Info<< "\nFailed " << nFailedChecks << " mesh checks.\n"
318  << endl;
319  }
320  else
321  {
322  Info<< "\nMesh OK.\n" << endl;
323  }
324 
325 
326  // Write selected fields
327  Foam::writeFields(mesh, selectedFields);
328  }
329  }
330 
331  Info<< "End\n" << endl;
332 
333  return 0;
334 }
335 
336 
337 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:54
Foam::HashTable::size
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::autoPtr::reset
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:109
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::polyMesh::POINTS_MOVED
Definition: polyMesh.H:94
Foam::argList::getOrDefault
T getOrDefault(const word &optName, const T &deflt) const
Get a value from the named option if present, or return default.
Definition: argListI.H:286
checkGeometry.H
globalMeshData.H
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:785
Foam::argList::addNote
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:413
Foam::vtkSetWriter
Definition: vtkSetWriter.H:53
checkTopology.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::checkTopology
label checkTopology(const polyMesh &mesh, const bool allTopology, const bool allGeometry, autoPtr< surfaceWriter > &surfWriter, const autoPtr< writer< scalar >> &setWriter)
Foam::HashSet< word >
Foam::argList::readIfPresent
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:302
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::fvMesh::readUpdate
virtual readUpdateState readUpdate()
Update the mesh based on the mesh files saved in time.
Definition: fvMesh.C:519
Foam::polyMesh::TOPO_PATCH_CHANGE
Definition: polyMesh.H:96
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
argList.H
Foam::writeFields
void writeFields(const fvMesh &mesh, const wordHashSet &selectedFields)
addRegionOption.H
Foam::polyMesh::TOPO_CHANGE
Definition: polyMesh.H:95
Foam::checkGeometry
label checkGeometry(const polyMesh &mesh, const bool allGeometry, autoPtr< surfaceWriter > &surfWriter, const autoPtr< writer< scalar >> &setWriter)
writeFields.H
Foam::checkMeshQuality
label checkMeshQuality(const polyMesh &mesh, const dictionary &dict, autoPtr< surfaceWriter > &writer)
createNamedMesh.H
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::polyMesh::readUpdateState
readUpdateState
Enumeration defining the state of the mesh after a read update.
Definition: polyMesh.H:91
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:325
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
vtkSetWriter.H
Foam::TimePaths::system
const word & system() const
Return system name.
Definition: TimePathsI.H:94
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::timeSelector::addOptions
static void addOptions(const bool constant=true, const bool withZero=false)
Add timeSelector options to argList::validOptions.
Definition: timeSelector.C:108
Foam::surfaceWriter::New
static autoPtr< surfaceWriter > New(const word &writeType)
Return a reference to the selected surfaceWriter.
Definition: surfaceWriter.C:64
vtkSurfaceWriter.H
Foam::List< instant >
Foam::Time::setTime
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:1006
Foam::printMeshStats
void printMeshStats(const polyMesh &mesh, const bool allTopology)
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:181
timeSelector.H
createTime.H
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:248
checkMeshQuality.H
Foam::polyMesh::globalData
const globalMeshData & globalData() const
Return parallel info.
Definition: polyMesh.C:1234
Foam::timeSelector::select0
static instantList select0(Time &runTime, const argList &args)
Definition: timeSelector.C:241
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:336
args
Foam::argList args(argc, argv)
checkTools.H
Foam::argList::found
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:157
Foam::IOobject::MUST_READ
Definition: IOobject.H:120
Foam::writer::New
static autoPtr< writer > New(const word &writeFormat)
Return a reference to the selected writer.
Definition: writer.C:38