All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
checkTools.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) 2015-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 \*---------------------------------------------------------------------------*/
25 
26 #include "checkTools.H"
27 #include "polyMesh.H"
28 #include "globalMeshData.H"
29 #include "hexMatcher.H"
30 #include "wedgeMatcher.H"
31 #include "prismMatcher.H"
32 #include "pyrMatcher.H"
33 #include "tetWedgeMatcher.H"
34 #include "tetMatcher.H"
35 #include "IOmanip.H"
36 #include "pointSet.H"
37 #include "faceSet.H"
38 #include "cellSet.H"
39 #include "Time.H"
40 #include "surfaceWriter.H"
41 #include "syncTools.H"
42 #include "globalIndex.H"
43 #include "PatchTools.H"
44 #include "writeFile.H"
45 #include "coordSet.H"
46 
47 
48 void Foam::printMeshStats(const polyMesh& mesh, const bool allTopology)
49 {
50  Info<< "Mesh stats" << nl
51  << " points: "
52  << returnReduce(mesh.points().size(), sumOp<label>()) << nl;
53 
54  label nInternalPoints = returnReduce
55  (
56  mesh.nInternalPoints(),
57  sumOp<label>()
58  );
59 
60  if (nInternalPoints != -Pstream::nProcs())
61  {
62  Info<< " internal points: " << nInternalPoints << nl;
63 
64  if (returnReduce(mesh.nInternalPoints(), minOp<label>()) == -1)
65  {
67  << "Some processors have their points sorted into internal"
68  << " and external and some do not." << endl
69  << "This can cause problems later on." << endl;
70  }
71  }
72 
73  if (allTopology && nInternalPoints != -Pstream::nProcs())
74  {
75  label nEdges = returnReduce(mesh.nEdges(), sumOp<label>());
76  label nInternalEdges = returnReduce
77  (
78  mesh.nInternalEdges(),
79  sumOp<label>()
80  );
81  label nInternal1Edges = returnReduce
82  (
83  mesh.nInternal1Edges(),
84  sumOp<label>()
85  );
86  label nInternal0Edges = returnReduce
87  (
88  mesh.nInternal0Edges(),
89  sumOp<label>()
90  );
91 
92  Info<< " edges: " << nEdges << nl
93  << " internal edges: " << nInternalEdges << nl
94  << " internal edges using one boundary point: "
95  << nInternal1Edges-nInternal0Edges << nl
96  << " internal edges using two boundary points: "
97  << nInternalEdges-nInternal1Edges << nl;
98  }
99 
100  label nFaces = returnReduce(mesh.faces().size(), sumOp<label>());
101  label nIntFaces = returnReduce(mesh.faceNeighbour().size(), sumOp<label>());
102  label nCells = returnReduce(mesh.cells().size(), sumOp<label>());
103 
104  Info<< " faces: " << nFaces << nl
105  << " internal faces: " << nIntFaces << nl
106  << " cells: " << nCells << nl
107  << " faces per cell: "
108  << scalar(nFaces + nIntFaces)/max(1, nCells) << nl
109  << " boundary patches: " << mesh.boundaryMesh().size() << nl
110  << " point zones: " << mesh.pointZones().size() << nl
111  << " face zones: " << mesh.faceZones().size() << nl
112  << " cell zones: " << mesh.cellZones().size() << nl
113  << endl;
114 
115  // Construct shape recognisers
116  hexMatcher hex;
117  prismMatcher prism;
118  wedgeMatcher wedge;
119  pyrMatcher pyr;
120  tetWedgeMatcher tetWedge;
121  tetMatcher tet;
122 
123  // Counters for different cell types
124  label nHex = 0;
125  label nWedge = 0;
126  label nPrism = 0;
127  label nPyr = 0;
128  label nTet = 0;
129  label nTetWedge = 0;
130  label nUnknown = 0;
131 
132  Map<label> polyhedralFaces;
133 
134  for (label celli = 0; celli < mesh.nCells(); celli++)
135  {
136  if (hex.isA(mesh, celli))
137  {
138  nHex++;
139  }
140  else if (tet.isA(mesh, celli))
141  {
142  nTet++;
143  }
144  else if (pyr.isA(mesh, celli))
145  {
146  nPyr++;
147  }
148  else if (prism.isA(mesh, celli))
149  {
150  nPrism++;
151  }
152  else if (wedge.isA(mesh, celli))
153  {
154  nWedge++;
155  }
156  else if (tetWedge.isA(mesh, celli))
157  {
158  nTetWedge++;
159  }
160  else
161  {
162  nUnknown++;
163  polyhedralFaces(mesh.cells()[celli].size())++;
164  }
165  }
166 
167  reduce(nHex,sumOp<label>());
168  reduce(nPrism,sumOp<label>());
169  reduce(nWedge,sumOp<label>());
170  reduce(nPyr,sumOp<label>());
171  reduce(nTetWedge,sumOp<label>());
172  reduce(nTet,sumOp<label>());
173  reduce(nUnknown,sumOp<label>());
174 
175  Info<< "Overall number of cells of each type:" << nl
176  << " hexahedra: " << nHex << nl
177  << " prisms: " << nPrism << nl
178  << " wedges: " << nWedge << nl
179  << " pyramids: " << nPyr << nl
180  << " tet wedges: " << nTetWedge << nl
181  << " tetrahedra: " << nTet << nl
182  << " polyhedra: " << nUnknown
183  << endl;
184 
185  if (nUnknown > 0)
186  {
187  Pstream::mapCombineGather(polyhedralFaces, plusEqOp<label>());
188 
189  Info<< " Breakdown of polyhedra by number of faces:" << nl
190  << " faces" << " number of cells" << endl;
191 
192  const labelList sortedKeys = polyhedralFaces.sortedToc();
193 
194  forAll(sortedKeys, keyI)
195  {
196  const label nFaces = sortedKeys[keyI];
197 
198  Info<< setf(std::ios::right) << setw(13)
199  << nFaces << " " << polyhedralFaces[nFaces] << nl;
200  }
201  }
202 
203  Info<< endl;
204 }
205 
206 
208 (
209  const polyMesh& mesh,
210  const surfaceWriter& writer,
211  const word& name,
212  const indirectPrimitivePatch setPatch,
213  const fileName& outputDir
214 )
215 {
216  if (Pstream::parRun())
217  {
218  labelList pointToGlobal;
219  labelList uniqueMeshPointLabels;
220  autoPtr<globalIndex> globalPoints;
221  autoPtr<globalIndex> globalFaces;
222  faceList mergedFaces;
223  pointField mergedPoints;
225  (
226  mesh,
227  setPatch.localFaces(),
228  setPatch.meshPoints(),
229  setPatch.meshPointMap(),
230 
231  pointToGlobal,
232  uniqueMeshPointLabels,
233  globalPoints,
234  globalFaces,
235 
236  mergedFaces,
237  mergedPoints
238  );
239 
240  // Write
241  if (Pstream::master())
242  {
243  writer.write(outputDir, name, mergedPoints, mergedFaces);
244  }
245  }
246  else
247  {
248  writer.write
249  (
250  outputDir,
251  name,
252  setPatch.localPoints(),
253  setPatch.localFaces()
254  );
255  }
256 }
257 
258 
260 (
261  const surfaceWriter& writer,
262  const faceSet& set
263 )
264 {
265  const polyMesh& mesh = refCast<const polyMesh>(set.db());
266 
267  const indirectPrimitivePatch setPatch
268  (
269  IndirectList<face>(mesh.faces(), set.sortedToc()),
270  mesh.points()
271  );
272 
273  fileName outputDir
274  (
275  set.time().globalPath()
276  /functionObjects::writeFile::outputPrefix
277  /mesh.pointsInstance()
278  /set.name()
279  );
280 
281  mergeAndWrite(mesh, writer, set.name(), setPatch, outputDir);
282 }
283 
284 
286 (
287  const surfaceWriter& writer,
288  const cellSet& set
289 )
290 {
291  const polyMesh& mesh = refCast<const polyMesh>(set.db());
292  const polyBoundaryMesh& pbm = mesh.boundaryMesh();
293 
294 
295  // Determine faces on outside of cellSet
296  PackedBoolList isInSet(mesh.nCells());
297  forAllConstIter(cellSet, set, iter)
298  {
299  isInSet[iter.key()] = true;
300  }
301 
302 
303  boolList bndInSet(mesh.nFaces()-mesh.nInternalFaces());
304  forAll(pbm, patchi)
305  {
306  const polyPatch& pp = pbm[patchi];
307  const labelList& fc = pp.faceCells();
308  forAll(fc, i)
309  {
310  bndInSet[pp.start()+i-mesh.nInternalFaces()] = isInSet[fc[i]];
311  }
312  }
313  syncTools::swapBoundaryFaceList(mesh, bndInSet);
314 
315 
316  DynamicList<label> outsideFaces(3*set.size());
317  for (label facei = 0; facei < mesh.nInternalFaces(); facei++)
318  {
319  bool ownVal = isInSet[mesh.faceOwner()[facei]];
320  bool neiVal = isInSet[mesh.faceNeighbour()[facei]];
321 
322  if (ownVal != neiVal)
323  {
324  outsideFaces.append(facei);
325  }
326  }
327 
328 
329  forAll(pbm, patchi)
330  {
331  const polyPatch& pp = pbm[patchi];
332  const labelList& fc = pp.faceCells();
333  if (pp.coupled())
334  {
335  forAll(fc, i)
336  {
337  label facei = pp.start()+i;
338 
339  bool neiVal = bndInSet[facei-mesh.nInternalFaces()];
340  if (isInSet[fc[i]] && !neiVal)
341  {
342  outsideFaces.append(facei);
343  }
344  }
345  }
346  else
347  {
348  forAll(fc, i)
349  {
350  if (isInSet[fc[i]])
351  {
352  outsideFaces.append(pp.start()+i);
353  }
354  }
355  }
356  }
357 
358 
359  const indirectPrimitivePatch setPatch
360  (
361  IndirectList<face>(mesh.faces(), outsideFaces),
362  mesh.points()
363  );
364 
365  fileName outputDir
366  (
367  set.time().path()
368  / (Pstream::parRun() ? ".." : "")
369  / "postProcessing"
370  / mesh.pointsInstance()
371  / set.name()
372  );
373  outputDir.clean();
374 
375  mergeAndWrite(mesh, writer, set.name(), setPatch, outputDir);
376 }
377 
378 
380 (
381  const setWriter& writer,
382  const pointSet& set
383 )
384 {
385  const polyMesh& mesh = refCast<const polyMesh>(set.db());
386 
387  pointField mergedPts;
388  labelList mergedIDs;
389 
390  if (Pstream::parRun())
391  {
392  // Note: we explicitly do not merge the points
393  // (mesh.globalData().mergePoints etc) since this might
394  // hide any synchronisation problem
395 
396  globalIndex globalNumbering(mesh.nPoints());
397 
398  mergedPts.setSize(returnReduce(set.size(), sumOp<label>()));
399  mergedIDs.setSize(mergedPts.size());
400 
401  labelList setPointIDs(set.sortedToc());
402 
403  // Get renumbered local data
404  pointField myPoints(mesh.points(), setPointIDs);
405  labelList myIDs(setPointIDs.size());
406  forAll(setPointIDs, i)
407  {
408  myIDs[i] = globalNumbering.toGlobal(setPointIDs[i]);
409  }
410 
411  if (Pstream::master())
412  {
413  // Insert master data first
414  label pOffset = 0;
415  SubList<point>(mergedPts, myPoints.size(), pOffset) = myPoints;
416  SubList<label>(mergedIDs, myIDs.size(), pOffset) = myIDs;
417  pOffset += myPoints.size();
418 
419  // Receive slave ones
420  for (int slave=1; slave<Pstream::nProcs(); slave++)
421  {
422  IPstream fromSlave(Pstream::commsTypes::scheduled, slave);
423 
424  pointField slavePts(fromSlave);
425  labelList slaveIDs(fromSlave);
426 
427  SubList<point>(mergedPts, slavePts.size(), pOffset) = slavePts;
428  SubList<label>(mergedIDs, slaveIDs.size(), pOffset) = slaveIDs;
429  pOffset += slaveIDs.size();
430  }
431  }
432  else
433  {
434  // Construct processor stream with estimate of size. Could
435  // be improved.
436  OPstream toMaster
437  (
438  Pstream::commsTypes::scheduled,
439  Pstream::masterNo(),
440  myPoints.byteSize() + myIDs.byteSize()
441  );
442  toMaster << myPoints << myIDs;
443  }
444  }
445  else
446  {
447  mergedIDs = set.sortedToc();
448  mergedPts = pointField(mesh.points(), mergedIDs);
449  }
450 
451  // Write with scalar pointID
452  if (Pstream::master())
453  {
454  writer.write
455  (
456  set.time().globalPath()
457  /functionObjects::writeFile::outputPrefix
458  /mesh.pointsInstance(),
459  set.name(),
460  coordSet(false, word::null, mergedPts),
461  "pointID",
462  scalarField(scalarList(mergedIDs))
463  );
464  }
465 }
466 
467 
468 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
FvWallInfoData< WallInfo, label > label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
IOstream & hex(IOstream &io)
Definition: IOstream.H:561
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
void mergeAndWrite(const polyMesh &mesh, const surfaceWriter &setWriter, const word &name, const indirectPrimitivePatch setPatch, const fileName &outputDir)
Generate merged surface on master and write. Needs input patch.
List< face > faceList
Definition: faceListFwd.H:43
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
PrimitivePatch< IndirectList< face >, const pointField & > indirectPrimitivePatch
Foam::indirectPrimitivePatch.
List< bool > boolList
Bool container classes.
Definition: boolList.H:50
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
List< scalar > scalarList
A List of scalars.
Definition: scalarList.H:50
static void gatherAndMerge(const scalar mergeDist, const PrimitivePatch< FaceList, PointField > &p, Field< typename PrimitivePatch< FaceList, PointField >::PointType > &mergedPoints, List< typename PrimitivePatch< FaceList, PointField >::FaceType > &mergedFaces, labelList &pointMergeMap)
Gather points and faces onto master and merge into single patch.
Smanip< ios_base::fmtflags > setf(const ios_base::fmtflags flags)
Definition: IOmanip.H:164
List< label > labelList
A List of labels.
Definition: labelList.H:56
volScalarField scalarField(fieldObject, mesh)
Istream and Ostream manipulators taking arguments.
static const char nl
Definition: Ostream.H:260
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void printMeshStats(const polyMesh &mesh, const bool allTopology)
void setSize(const label)
Reset size of List.
Definition: List.C:281
label patchi
#define WarningInFunction
Report a warning using Foam::Warning.
messageStream Info
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Omanip< int > setw(const int i)
Definition: IOmanip.H:199