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-2021 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  - \par -allRegions
54  Check all regions in regionProperties.
55 
56  \param -writeSets <surfaceFormat> \n
57  Reconstruct all cellSets and faceSets geometry and write to postProcessing
58  directory according to surfaceFormat (e.g. vtk or ensight). Additionally
59  reconstructs all pointSets and writes as vtk format.
60 
61  \param -writeAllFields \n
62  Writes all mesh quality measures as fields.
63 
64  \param -writeAllSurfaceFields \n
65  Adds writing of surface fields when used in combination with writeAllFields.
66 
67  \param -writeFields '(<fieldName>)' \n
68  Writes selected mesh quality measures as fields.
69 
70 \*---------------------------------------------------------------------------*/
71 
72 #include "argList.H"
73 #include "timeSelector.H"
74 #include "Time.H"
75 #include "fvMesh.H"
76 #include "globalMeshData.H"
77 #include "vtkSetWriter.H"
78 #include "vtkSurfaceWriter.H"
79 #include "IOdictionary.H"
80 #include "regionProperties.H"
81 #include "polyMeshTools.H"
82 
83 #include "checkTools.H"
84 #include "checkTopology.H"
85 #include "checkGeometry.H"
86 #include "checkMeshQuality.H"
87 #include "writeFields.H"
88 
89 using namespace Foam;
90 
91 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
92 
93 int main(int argc, char *argv[])
94 {
96  (
97  "Checks validity of a mesh"
98  );
99 
101  #include "addAllRegionOptions.H"
102 
104  (
105  "noTopology",
106  "Skip checking the mesh topology"
107  );
109  (
110  "allGeometry",
111  "Include bounding box checks"
112  );
114  (
115  "allTopology",
116  "Include extra topology checks"
117  );
119  (
120  "writeAllFields",
121  "Write volFields with mesh quality parameters"
122  );
124  (
125  "writeAllSurfaceFields",
126  "Write surfaceFields with mesh quality parameters"
127  );
129  (
130  "writeFields",
131  "wordList",
132  "Write volFields with selected mesh quality parameters"
133  );
135  (
136  "meshQuality",
137  "Read user-defined mesh quality criteria from system/meshQualityDict"
138  );
140  (
141  "writeSets",
142  "surfaceFormat",
143  "Reconstruct and write all faceSets and cellSets in selected format"
144  );
145 
146  #include "setRootCase.H"
147  #include "createTime.H"
148  #include "getAllRegionOptions.H"
150  #include "createNamedMeshes.H"
151 
152  const bool noTopology = args.found("noTopology");
153  const bool allGeometry = args.found("allGeometry");
154  const bool allTopology = args.found("allTopology");
155  const bool meshQuality = args.found("meshQuality");
156 
157  const word surfaceFormat = args.getOrDefault<word>("writeSets", "");
158  const bool writeSets = surfaceFormat.size();
159 
160 
161  // All potential writeable fields
162  const wordHashSet allFields
163  ({
164  "nonOrthoAngle",
165  "faceWeight",
166  "skewness",
167  "cellDeterminant",
168  "aspectRatio",
169  "cellShapes",
170  "cellVolume",
171  "cellVolumeRatio",
172  "cellAspectRatio",
173  "minTetVolume",
174  "minPyrVolume",
175  "cellRegion",
176  "wallDistance",
177  "cellZone",
178  "faceZone"
179  });
180 
181  const bool writeFaceFields = args.found("writeAllSurfaceFields");
182  wordHashSet selectedFields;
183  if (args.found("writeFields"))
184  {
185  selectedFields = args.getList<word>("writeFields");
186  const wordHashSet badFields(selectedFields - allFields);
187 
188  if (!badFields.empty())
189  {
191  << "Illegal field(s) " << flatOutput(badFields.sortedToc())
192  << nl
193  << "Valid fields are " << flatOutput(allFields.sortedToc())
194  << nl << exit(FatalError);
195  }
196  }
197  else if (args.found("writeAllFields"))
198  {
199  selectedFields = allFields;
200  }
201  else if (writeFaceFields)
202  {
204  << "Option 'writeAllSurfaceFields' only valid in combination"
205  << " with 'writeFields' or 'writeAllFields'"
206  << nl << exit(FatalError);
207  }
208 
209 
210  if (noTopology)
211  {
212  Info<< "Disabling all topology checks." << nl << endl;
213  }
214  if (allTopology)
215  {
216  Info<< "Enabling all (cell, face, edge, point) topology checks."
217  << nl << endl;
218  }
219  if (allGeometry)
220  {
221  Info<< "Enabling all geometry checks." << nl << endl;
222  }
223  if (meshQuality)
224  {
225  Info<< "Enabling user-defined geometry checks." << nl << endl;
226  }
227  if (writeSets)
228  {
229  Info<< "Reconstructing and writing " << surfaceFormat
230  << " representation"
231  << " of all faceSets and cellSets." << nl << endl;
232  }
233  if (selectedFields.size())
234  {
235  Info<< "Writing mesh quality as fields " << selectedFields << nl
236  << endl;
237  }
238 
239 
240  PtrList<IOdictionary> qualDict(meshes.size());
241  if (meshQuality)
242  {
243  forAll(meshes, meshi)
244  {
245  qualDict.set
246  (
247  meshi,
248  new IOdictionary
249  (
250  IOobject
251  (
252  "meshQualityDict",
253  meshes[meshi].time().system(),
254  meshes[meshi],
257  )
258  )
259  );
260  }
261  }
262 
263 
264  autoPtr<surfaceWriter> surfWriter;
265  autoPtr<writer<scalar>> setWriter;
266  if (writeSets)
267  {
268  surfWriter = surfaceWriter::New(surfaceFormat);
270  }
271 
272 
273  forAll(timeDirs, timeI)
274  {
275  runTime.setTime(timeDirs[timeI], timeI);
276 
277  // Get most changed of all meshes
279  for (auto& mesh : meshes)
280  {
281  state = polyMeshTools::combine(state, mesh.readUpdate());
282  }
283 
284 
285  if
286  (
287  !timeI
288  || state == polyMesh::TOPO_CHANGE
289  || state == polyMesh::TOPO_PATCH_CHANGE
290  )
291  {
292  Info<< "Time = " << runTime.timeName() << nl << endl;
293 
294  forAll(meshes, meshi)
295  {
296  const auto& mesh = meshes[meshi];
297 
298  // Reconstruct globalMeshData
299  mesh.globalData();
300 
301  printMeshStats(mesh, allTopology);
302 
303  label nFailedChecks = 0;
304 
305  if (!noTopology)
306  {
307  nFailedChecks += checkTopology
308  (
309  mesh,
310  allTopology,
311  allGeometry,
312  surfWriter,
313  setWriter
314  );
315  }
316 
317  nFailedChecks += checkGeometry
318  (
319  mesh,
320  allGeometry,
321  surfWriter,
322  setWriter
323  );
324 
325  if (meshQuality)
326  {
327  nFailedChecks +=
328  checkMeshQuality(mesh, qualDict[meshi], surfWriter);
329  }
330 
331 
332  // Note: no reduction in nFailedChecks necessary since is
333  // counter of checks, not counter of failed cells,faces
334  // etc.
335 
336  if (nFailedChecks == 0)
337  {
338  Info<< "\nMesh OK.\n" << endl;
339  }
340  else
341  {
342  Info<< "\nFailed " << nFailedChecks << " mesh checks.\n"
343  << endl;
344  }
345 
346 
347  // Write selected fields
348  Foam::writeFields(mesh, selectedFields, writeFaceFields);
349  }
350  }
351  else if (state == polyMesh::POINTS_MOVED)
352  {
353  Info<< "Time = " << runTime.timeName() << nl << endl;
354 
355  forAll(meshes, meshi)
356  {
357  const auto& mesh = meshes[meshi];
358 
359  label nFailedChecks = checkGeometry
360  (
361  mesh,
362  allGeometry,
363  surfWriter,
364  setWriter
365  );
366 
367  if (meshQuality)
368  {
369  nFailedChecks +=
370  checkMeshQuality(mesh, qualDict[meshi], surfWriter);
371  }
372 
373 
374  if (nFailedChecks)
375  {
376  Info<< "\nFailed " << nFailedChecks << " mesh checks.\n"
377  << endl;
378  }
379  else
380  {
381  Info<< "\nMesh OK.\n" << endl;
382  }
383 
384 
385  // Write selected fields
386  Foam::writeFields(mesh, selectedFields, writeFaceFields);
387  }
388  }
389  }
390 
391  Info<< "End\n" << endl;
392 
393  return 0;
394 }
395 
396 
397 // ************************************************************************* //
Foam::IOobject::NO_WRITE
@ NO_WRITE
Definition: IOobject.H:191
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:50
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:165
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
regionProperties.H
Foam::polyMesh::POINTS_MOVED
@ POINTS_MOVED
Definition: polyMesh.H:89
Foam::argList::getOrDefault
T getOrDefault(const word &optName, const T &deflt) const
Definition: argListI.H:300
checkGeometry.H
globalMeshData.H
Foam::system
int system(const std::string &command, const bool bg=false)
Definition: POSIX.C:1548
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:773
Foam::argList::addNote
static void addNote(const string &note)
Definition: argList.C:405
Foam::vtkSetWriter
Definition: vtkSetWriter.H:50
checkTopology.H
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
Foam::checkTopology
label checkTopology(const polyMesh &mesh, const bool allTopology, const bool allGeometry, autoPtr< surfaceWriter > &surfWriter, const autoPtr< writer< scalar >> &setWriter)
Foam::HashSet
A HashTable with keys but without contents that is similar to std::unordered_set.
Definition: HashSet.H:73
forAll
#define forAll(list, i)
Definition: stdFoam.H:349
Foam::fvMesh::readUpdate
virtual readUpdateState readUpdate()
Definition: fvMesh.C:641
Foam::polyMesh::TOPO_PATCH_CHANGE
@ TOPO_PATCH_CHANGE
Definition: polyMesh.H:91
Foam::Info
messageStream Info
argList.H
meshes
Foam::PtrList< Foam::fvMesh > meshes(regionNames.size())
Foam::polyMesh::UNCHANGED
@ UNCHANGED
Definition: polyMesh.H:88
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:55
Foam::polyMesh::TOPO_CHANGE
@ TOPO_CHANGE
Definition: polyMesh.H:90
Foam::checkGeometry
label checkGeometry(const polyMesh &mesh, const bool allGeometry, autoPtr< surfaceWriter > &surfWriter, const autoPtr< writer< scalar >> &setWriter)
writeFields.H
Foam::FatalError
error FatalError
Foam::checkMeshQuality
label checkMeshQuality(const polyMesh &mesh, const dictionary &dict, autoPtr< surfaceWriter > &writer)
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
fvMesh.H
Foam
Definition: atmBoundaryLayer.C:26
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Definition: FlatOutput.H:217
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Foam::polyMesh::readUpdateState
readUpdateState
Definition: polyMesh.H:86
getAllRegionOptions.H
Priority.
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Definition: argList.C:317
IOdictionary.H
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:49
setRootCase.H
polyMeshTools.H
vtkSetWriter.H
FatalErrorInFunction
#define FatalErrorInFunction
Definition: error.H:465
addAllRegionOptions.H
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Foam::timeSelector::addOptions
static void addOptions(const bool constant=true, const bool withZero=false)
Definition: timeSelector.C:95
Foam::surfaceWriter::New
static autoPtr< surfaceWriter > New(const word &writeType)
Definition: surfaceWriter.C:57
vtkSurfaceWriter.H
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:58
Foam::Time::setTime
virtual void setTime(const Time &t)
Definition: Time.C:996
Foam::printMeshStats
void printMeshStats(const polyMesh &mesh, const bool allTopology)
timeSelector.H
createTime.H
Foam::polyMeshTools::combine
static polyMesh::readUpdateState combine(const polyMesh::readUpdateState &state0, const polyMesh::readUpdateState &state1)
Definition: polyMeshTools.C:278
Foam::argList::getList
List< T > getList(const label index) const
checkMeshQuality.H
Foam::polyMesh::globalData
const globalMeshData & globalData() const
Definition: polyMesh.C:1288
Foam::timeSelector::select0
static instantList select0(Time &runTime, const argList &args)
Definition: timeSelector.C:228
Foam::argList::addOption
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Definition: argList.C:328
args
Foam::argList args(argc, argv)
createNamedMeshes.H
Required Variables.
checkTools.H
Foam::writeFields
void writeFields(const fvMesh &mesh, const wordHashSet &selectedFields, const bool writeFaceFields)
Foam::argList::found
bool found(const word &optName) const
Definition: argListI.H:171
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:181
Foam::writer::New
static autoPtr< writer > New(const word &writeFormat)
Definition: writer.C:31