Newer
Older
Henry Weller
committed
#include "PatchTools.H"
#include "checkGeometry.H"
#include "polyMesh.H"
#include "cellSet.H"
#include "faceSet.H"
#include "pointSet.H"
#include "wedgePolyPatch.H"
#include "unitConversion.H"
#include "surfaceWriter.H"
#include "checkTools.H"
#include "functionObject.H"
Henry Weller
committed
#include "vtkSurfaceWriter.H"
Henry Weller
committed
#include "cyclicAMIPolyPatch.H"
#include "Time.H"
// Find wedge with opposite orientation. Note: does not actually check that
// it is opposite, only that it has opposite normal and same axis
Foam::label Foam::findOppositeWedge
(
const polyMesh& mesh,
const wedgePolyPatch& wpp
)
{
const polyBoundaryMesh& patches = mesh.boundaryMesh();
patchi != wpp.index()
&& patches[patchi].size()
&& isA<wedgePolyPatch>(patches[patchi])
refCast<const wedgePolyPatch>(patches[patchi]);
// Calculate (cos of) angle to wpp (not pp!) centre normal
if
(
pp.size() == wpp.size()
&& mag(pp.axis() & wpp.axis()) >= (1-1e-3)
&& mag(ppCosAngle - wppCosAngle) >= 1e-3
}
}
}
return -1;
}
bool Foam::checkWedges
(
const polyMesh& mesh,
const bool report,
const Vector<label>& directions,
labelHashSet* setPtr
)
{
// To mark edges without calculating edge addressing
EdgeMap<label> edgesInError;
const pointField& p = mesh.points();
const faceList& fcs = mesh.faces();
const polyBoundaryMesh& patches = mesh.boundaryMesh();
if (patches[patchi].size() && isA<wedgePolyPatch>(patches[patchi]))
refCast<const wedgePolyPatch>(patches[patchi]);
if (report)
{
Info<< " Wedge " << pp.name() << " with angle "
<< radToDeg(wedgeAngle) << " degrees"
label oppositePatchi = findOppositeWedge(mesh, pp);
if (oppositePatchi == -1)
{
if (report)
{
Info<< " ***Cannot find opposite wedge for wedge "
<< pp.name() << endl;
}
return true;
}
refCast<const wedgePolyPatch>(patches[oppositePatchi]);
if (mag(opp.axis() & pp.axis()) < (1-1e-3))
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
{
if (report)
{
Info<< " ***Wedges do not have the same axis."
<< " Encountered " << pp.axis()
<< " on patch " << pp.name()
<< " which differs from " << opp.axis()
<< " on opposite wedge patch" << opp.axis()
<< endl;
}
return true;
}
// Mark edges on wedgePatches
forAll(pp, i)
{
const face& f = pp[i];
forAll(f, fp)
{
label p0 = f[fp];
label p1 = f.nextLabel(fp);
edgesInError.insert(edge(p0, p1), -1); // non-error value
}
}
// Check that wedge patch is flat
const point& p0 = p[pp.meshPoints()[0]];
forAll(pp.meshPoints(), i)
{
const point& pt = p[pp.meshPoints()[i]];
{
if (report)
{
Info<< " ***Wedge patch " << pp.name() << " not planar."
<< " Point " << pt << " is not in patch plane by "
<< endl;
}
return true;
}
}
}
}
// Check all non-wedge faces
label nEdgesInError = 0;
const face& f = fcs[facei];
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
forAll(f, fp)
{
label p0 = f[fp];
label p1 = f.nextLabel(fp);
if (p0 < p1)
{
vector d(p[p1]-p[p0]);
scalar magD = mag(d);
if (magD > ROOTVSMALL)
{
d /= magD;
// Check how many empty directions are used by the edge.
label nEmptyDirs = 0;
label nNonEmptyDirs = 0;
for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
{
if (mag(d[cmpt]) > 1e-6)
{
if (directions[cmpt] == 0)
{
nEmptyDirs++;
}
else
{
nNonEmptyDirs++;
}
}
}
if (nEmptyDirs == 0)
{
// Purely in ok directions.
}
else if (nEmptyDirs == 1)
{
// Ok if purely in empty directions.
if (nNonEmptyDirs > 0)
{
if (edgesInError.insert(edge(p0, p1), facei))
{
nEdgesInError++;
}
}
}
else if (nEmptyDirs > 1)
{
// Always an error
if (edgesInError.insert(edge(p0, p1), facei))
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
{
nEdgesInError++;
}
}
}
}
}
}
label nErrorEdges = returnReduce(nEdgesInError, sumOp<label>());
if (nErrorEdges > 0)
{
if (report)
{
Info<< " ***Number of edges not aligned with or perpendicular to "
<< "non-empty directions: " << nErrorEdges << endl;
}
if (setPtr)
{
setPtr->resize(2*nEdgesInError);
forAllConstIter(EdgeMap<label>, edgesInError, iter)
{
if (iter() >= 0)
{
setPtr->insert(iter.key()[0]);
setPtr->insert(iter.key()[1]);
}
}
}
return true;
}
else
{
if (report)
{
Info<< " All edges aligned with or perpendicular to "
<< "non-empty directions." << endl;
}
return false;
}
}
//- Default transformation behaviour for position
class transformPositionList
//- Transform patch-based field
void operator()
(
const coupledPolyPatch& cpp,
List<pointField>& pts
) const
// Each element of pts is all the points in the face. Convert into
// lists of size cpp to transform.
List<pointField> newPts(pts.size());
newPts[facei].setSize(pts[facei].size());
label index = 0;
while (true)
{
label n = 0;
// Extract for every face the i'th position
const pointField& facePts = pts[facei];
if (facePts.size() > index)
{
ptsAtIndex[facei] = facePts[index];
if (n == 0)
{
break;
}
// Now ptsAtIndex will have for every face either zero or
// the position of the i'th vertex. Transform.
cpp.transformPosition(ptsAtIndex);
// Extract back from ptsAtIndex into newPts
pointField& facePts = newPts[facei];
facePts[index] = ptsAtIndex[facei];
bool Foam::checkCoupledPoints
(
const polyMesh& mesh,
const bool report,
labelHashSet* setPtr
)
{
const pointField& p = mesh.points();
const faceList& fcs = mesh.faces();
const polyBoundaryMesh& patches = mesh.boundaryMesh();
// Zero'th point on coupled faces
//pointField nbrZeroPoint(fcs.size()-mesh.nInternalFaces(), vector::max);
List<pointField> nbrPoints(fcs.size() - mesh.nInternalFaces());
// Exchange zero point
const coupledPolyPatch& cpp = refCast<const coupledPolyPatch>
(
label bFacei = cpp.start() + i - mesh.nInternalFaces();
nbrPoints[bFacei].setSize(f.size());
forAll(f, fp)
{
const point& p0 = p[f[fp]];
nbrPoints[bFacei][fp] = p0;
syncTools::syncBoundaryFaceList
(
mesh,
nbrPoints,
eqOp<pointField>(),
transformPositionList()
);
// Compare to local ones. Use same tolerance as for matching
label nErrorFaces = 0;
scalar avgMismatch = 0;
label nCoupledPoints = 0;
refCast<const coupledPolyPatch>(patches[patchi]);
cpp.calcFaceTol
(
//cpp.matchTolerance(),
cpp,
cpp.points(),
cpp.faceCentres()
)
);
forAll(cpp, i)
{
label bFacei = cpp.start() + i - mesh.nInternalFaces();
const face& f = cpp[i];
if (f.size() != nbrPoints[bFacei].size())
FatalErrorInFunction
<< "Local face size : " << f.size()
<< " does not equal neighbour face size : "
<< nbrPoints[bFacei].size()
label fp = 0;
forAll(f, j)
scalar d = mag(p0 - nbrPoints[bFacei][j]);
if (d > smallDist[i])
{
if (setPtr)
setPtr->insert(cpp.start()+i);
avgMismatch += d;
nCoupledPoints++;
fp = f.rcIndex(fp);
}
}
}
}
}
reduce(nErrorFaces, sumOp<label>());
reduce(avgMismatch, maxOp<scalar>());
reduce(nCoupledPoints, sumOp<label>());
if (nCoupledPoints > 0)
avgMismatch /= nCoupledPoints;
}
if (nErrorFaces > 0)
{
if (report)
{
Info<< " **Error in coupled point location: "
<< nErrorFaces
<< " faces have their 0th or consecutive vertex not opposite"
<< " their coupled equivalent. Average mismatch "
<< avgMismatch << "."
<< endl;
}
return true;
}
else
{
if (report)
{
Info<< " Coupled point location match (average "
<< avgMismatch << ") OK." << endl;
}
return false;
}
}
Foam::label Foam::checkGeometry
(
const polyMesh& mesh,
const bool allGeometry,
const autoPtr<surfaceWriter>& surfWriter,
const autoPtr<writer<scalar>>& setWriter
{
label noFailedChecks = 0;
Info<< "\nChecking geometry..." << endl;
// Get a small relative length from the bounding box
const boundBox& globalBb = mesh.bounds();
Info<< " Overall domain bounding box "
<< globalBb.min() << " " << globalBb.max() << endl;
// Min length
scalar minDistSqr = magSqr(1e-6 * globalBb.span());
// Geometric directions
const Vector<label> validDirs = (mesh.geometricD() + Vector<label>::one)/2;
Info<< " Mesh has " << mesh.nGeometricD()
<< " geometric (non-empty/wedge) directions " << validDirs << endl;
// Solution directions
const Vector<label> solDirs = (mesh.solutionD() + Vector<label>::one)/2;
Info<< " Mesh has " << mesh.nSolutionD()
<< " solution (non-empty) directions " << solDirs << endl;
{
pointSet nonAlignedPoints(mesh, "nonAlignedEdges", mesh.nPoints()/100);
if
(
(
validDirs != solDirs
&& checkWedges(mesh, true, validDirs, &nonAlignedPoints)
)
|| (
validDirs == solDirs
&& mesh.checkEdgeAlignment(true, validDirs, &nonAlignedPoints)
)
)
label nNonAligned = returnReduce
(
nonAlignedPoints.size(),
sumOp<label>()
);
<< " points on non-aligned edges to set "
<< nonAlignedPoints.name() << endl;
nonAlignedPoints.instance() = mesh.pointsInstance();
mergeAndWrite(setWriter(), nonAlignedPoints);
if (mesh.checkClosedBoundary(true)) noFailedChecks++;
{
cellSet cells(mesh, "nonClosedCells", mesh.nCells()/100+1);
cellSet aspectCells(mesh, "highAspectRatioCells", mesh.nCells()/100+1);
if
(
mesh.checkClosedCells
(
true,
&cells,
&aspectCells,
mesh.geometricD()
)
)
{
noFailedChecks++;
label nNonClosed = returnReduce(cells.size(), sumOp<label>());
if (nNonClosed > 0)
<< " non closed cells to set " << cells.name() << endl;
cells.instance() = mesh.pointsInstance();
mergeAndWrite(surfWriter(), cells);
label nHighAspect = returnReduce(aspectCells.size(), sumOp<label>());
if (nHighAspect > 0)
<< " cells with high aspect ratio to set "
<< aspectCells.name() << endl;
aspectCells.instance() = mesh.pointsInstance();
mergeAndWrite(surfWriter(), aspectCells);
faceSet faces(mesh, "zeroAreaFaces", mesh.nFaces()/100+1);
if (mesh.checkFaceAreas(true, &faces))
{
noFailedChecks++;
label nFaces = returnReduce(faces.size(), sumOp<label>());
if (nFaces > 0)
<< " zero area faces to set " << faces.name() << endl;
faces.instance() = mesh.pointsInstance();
mergeAndWrite(surfWriter(), faces);
cellSet cells(mesh, "zeroVolumeCells", mesh.nCells()/100+1);
if (mesh.checkCellVolumes(true, &cells))
{
noFailedChecks++;
label nCells = returnReduce(cells.size(), sumOp<label>());
if (nCells > 0)
<< " zero volume cells to set " << cells.name() << endl;
cells.instance() = mesh.pointsInstance();
mergeAndWrite(surfWriter(), cells);
faceSet faces(mesh, "nonOrthoFaces", mesh.nFaces()/100+1);
if (mesh.checkFaceOrthogonality(true, &faces))
{
noFailedChecks++;
}
label nFaces = returnReduce(faces.size(), sumOp<label>());
if (nFaces > 0)
<< " non-orthogonal faces to set " << faces.name() << endl;
faces.instance() = mesh.pointsInstance();
mergeAndWrite(surfWriter(), faces);
}
}
{
faceSet faces(mesh, "wrongOrientedFaces", mesh.nFaces()/100 + 1);
if (mesh.checkFacePyramids(true, -SMALL, &faces))
{
noFailedChecks++;
label nFaces = returnReduce(faces.size(), sumOp<label>());
if (nFaces > 0)
<< " faces with incorrect orientation to set "
<< faces.name() << endl;
faces.instance() = mesh.pointsInstance();
mergeAndWrite(surfWriter(), faces);
faceSet faces(mesh, "skewFaces", mesh.nFaces()/100+1);
if (mesh.checkFaceSkewness(true, &faces))
{
noFailedChecks++;
label nFaces = returnReduce(faces.size(), sumOp<label>());
if (nFaces > 0)
{
Info<< " <<Writing " << nFaces
<< " skew faces to set " << faces.name() << endl;
faces.instance() = mesh.pointsInstance();
faces.write();
mergeAndWrite(surfWriter(), faces);
{
faceSet faces(mesh, "coupledFaces", mesh.nFaces()/100 + 1);
if (checkCoupledPoints(mesh, true, &faces))
{
noFailedChecks++;
label nFaces = returnReduce(faces.size(), sumOp<label>());
if (nFaces > 0)
{
Info<< " <<Writing " << nFaces
<< " faces with incorrectly matched 0th (or consecutive)"
<< " vertex to set "
<< faces.name() << endl;
faces.instance() = mesh.pointsInstance();
faces.write();
mergeAndWrite(surfWriter(), faces);
}
}
}
faceSet faces(mesh, "lowQualityTetFaces", mesh.nFaces()/100+1);
if
(
polyMeshTetDecomposition::checkFaceTets
(
mesh,
polyMeshTetDecomposition::minTetQuality,
true,
&faces
)
)
{
noFailedChecks++;
label nFaces = returnReduce(faces.size(), sumOp<label>());
if (nFaces > 0)
<< " faces with low quality or negative volume "
<< "decomposition tets to set " << faces.name() << endl;
faces.instance() = mesh.pointsInstance();
mergeAndWrite(surfWriter(), faces);
}
}
}
if (allGeometry)
{
// Note use of nPoints since don't want edge construction.
pointSet points(mesh, "shortEdges", mesh.nPoints()/1000 + 1);
if (mesh.checkEdgeLength(true, minDistSqr, &points))
{
//noFailedChecks++;
label nPoints = returnReduce(points.size(), sumOp<label>());
if (nPoints > 0)
<< " points on short edges to set " << points.name()
<< endl;
points.instance() = mesh.pointsInstance();
mergeAndWrite(setWriter(), points);
label nEdgeClose = returnReduce(points.size(), sumOp<label>());
if (mesh.checkPointNearness(false, minDistSqr, &points))
{
//noFailedChecks++;
label nPoints = returnReduce(points.size(), sumOp<label>());
if (nPoints > nEdgeClose)
{
pointSet nearPoints(mesh, "nearPoints", points);
<< " near (closer than " << Foam::sqrt(minDistSqr)
<< " apart) points to set " << nearPoints.name() << endl;
nearPoints.instance() = mesh.pointsInstance();
mergeAndWrite(setWriter(), nearPoints);
}
}
}
if (allGeometry)
{
faceSet faces(mesh, "concaveFaces", mesh.nFaces()/100 + 1);
if (mesh.checkFaceAngles(true, 10, &faces))
{
//noFailedChecks++;
label nFaces = returnReduce(faces.size(), sumOp<label>());
if (nFaces > 0)
<< " faces with concave angles to set " << faces.name()
<< endl;
faces.instance() = mesh.pointsInstance();
mergeAndWrite(surfWriter(), faces);
}
}
}
if (allGeometry)
{
faceSet faces(mesh, "warpedFaces", mesh.nFaces()/100 + 1);
if (mesh.checkFaceFlatness(true, 0.8, &faces))
{
//noFailedChecks++;
label nFaces = returnReduce(faces.size(), sumOp<label>());
if (nFaces > 0)
<< " warped faces to set " << faces.name() << endl;
faces.instance() = mesh.pointsInstance();
mergeAndWrite(surfWriter(), faces);
}
}
}
if (allGeometry)
{
cellSet cells(mesh, "underdeterminedCells", mesh.nCells()/100);
if (mesh.checkCellDeterminant(true, &cells))
{
noFailedChecks++;
label nCells = returnReduce(cells.size(), sumOp<label>());
Info<< " <<Writing " << nCells
<< " under-determined cells to set " << cells.name() << endl;
cells.instance() = mesh.pointsInstance();
mergeAndWrite(surfWriter(), cells);
if (allGeometry)
{
cellSet cells(mesh, "concaveCells", mesh.nCells()/100);
if (mesh.checkConcaveCells(true, &cells))
{
noFailedChecks++;
label nCells = returnReduce(cells.size(), sumOp<label>());
Info<< " <<Writing " << nCells
<< " concave cells to set " << cells.name() << endl;
cells.instance() = mesh.pointsInstance();
cells.write();
mergeAndWrite(surfWriter(), cells);
if (allGeometry)
{
faceSet faces(mesh, "lowWeightFaces", mesh.nFaces()/100);
if (mesh.checkFaceWeight(true, 0.05, &faces))
{
noFailedChecks++;
label nFaces = returnReduce(faces.size(), sumOp<label>());
Info<< " <<Writing " << nFaces
<< " faces with low interpolation weights to set "
<< faces.name() << endl;
faces.instance() = mesh.pointsInstance();
faces.write();
mergeAndWrite(surfWriter(), faces);
}
}
if (allGeometry)
{
faceSet faces(mesh, "lowVolRatioFaces", mesh.nFaces()/100);
if (mesh.checkVolRatio(true, 0.01, &faces))
{
noFailedChecks++;
label nFaces = returnReduce(faces.size(), sumOp<label>());
Info<< " <<Writing " << nFaces
<< " faces with low volume ratio cells to set "
<< faces.name() << endl;
faces.instance() = mesh.pointsInstance();
faces.write();
mergeAndWrite(surfWriter(), faces);
Henry Weller
committed
if (allGeometry)
{
const polyBoundaryMesh& pbm = mesh.boundaryMesh();
const word tmName(mesh.time().timeName());
const word procAndTime(Foam::name(Pstream::myProcNo()) + "_" + tmName);
autoPtr<surfaceWriter> patchWriter;
Henry Weller
committed
{
patchWriter.reset(new vtkSurfaceWriter());
}
const surfaceWriter& wr =
(
surfWriter.valid()
? surfWriter()
: patchWriter()
);
Henry Weller
committed
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
forAll(pbm, patchi)
{
if (isA<cyclicAMIPolyPatch>(pbm[patchi]))
{
const cyclicAMIPolyPatch& cpp =
refCast<const cyclicAMIPolyPatch>(pbm[patchi]);
if (cpp.owner())
{
Info<< "Calculating AMI weights between owner patch: "
<< cpp.name() << " and neighbour patch: "
<< cpp.neighbPatch().name() << endl;
const AMIPatchToPatchInterpolation& ami =
cpp.AMI();
{
// Collect geometry
labelList pointToGlobal;
labelList uniqueMeshPointLabels;
autoPtr<globalIndex> globalPoints;
autoPtr<globalIndex> globalFaces;
faceList mergedFaces;
pointField mergedPoints;
Foam::PatchTools::gatherAndMerge
(
mesh,
cpp.localFaces(),
cpp.meshPoints(),
cpp.meshPointMap(),
pointToGlobal,
uniqueMeshPointLabels,
globalPoints,