OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

primitiveMeshGeometry.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "primitiveMeshGeometry.H"
00028 #include "pyramidPointFaceRef.H"
00029 
00030 namespace Foam
00031 {
00032 
00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00034 
00035 defineTypeNameAndDebug(primitiveMeshGeometry, 0);
00036 
00037 }
00038 
00039 
00040 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00041 
00042 void Foam::primitiveMeshGeometry::updateFaceCentresAndAreas
00043 (
00044     const pointField& p,
00045     const labelList& changedFaces
00046 )
00047 {
00048     const faceList& fs = mesh_.faces();
00049 
00050     forAll(changedFaces, i)
00051     {
00052         label facei = changedFaces[i];
00053 
00054         const labelList& f = fs[facei];
00055         label nPoints = f.size();
00056 
00057         // If the face is a triangle, do a direct calculation for efficiency
00058         // and to avoid round-off error-related problems
00059         if (nPoints == 3)
00060         {
00061             faceCentres_[facei] = (1.0/3.0)*(p[f[0]] + p[f[1]] + p[f[2]]);
00062             faceAreas_[facei] = 0.5*((p[f[1]] - p[f[0]])^(p[f[2]] - p[f[0]]));
00063         }
00064         else
00065         {
00066             vector sumN = vector::zero;
00067             scalar sumA = 0.0;
00068             vector sumAc = vector::zero;
00069 
00070             point fCentre = p[f[0]];
00071             for (label pi = 1; pi < nPoints; pi++)
00072             {
00073                 fCentre += p[f[pi]];
00074             }
00075 
00076             fCentre /= nPoints;
00077 
00078             for (label pi = 0; pi < nPoints; pi++)
00079             {
00080                 const point& nextPoint = p[f[(pi + 1) % nPoints]];
00081 
00082                 vector c = p[f[pi]] + nextPoint + fCentre;
00083                 vector n = (nextPoint - p[f[pi]])^(fCentre - p[f[pi]]);
00084                 scalar a = mag(n);
00085 
00086                 sumN += n;
00087                 sumA += a;
00088                 sumAc += a*c;
00089             }
00090 
00091             faceCentres_[facei] = (1.0/3.0)*sumAc/(sumA + VSMALL);
00092             faceAreas_[facei] = 0.5*sumN;
00093         }
00094     }
00095 }
00096 
00097 
00098 void Foam::primitiveMeshGeometry::updateCellCentresAndVols
00099 (
00100     const labelList& changedCells,
00101     const labelList& changedFaces
00102 )
00103 {
00104     // Clear the fields for accumulation
00105     UIndirectList<vector>(cellCentres_, changedCells) = vector::zero;
00106     UIndirectList<scalar>(cellVolumes_, changedCells) = 0.0;
00107 
00108     const labelList& own = mesh_.faceOwner();
00109     const labelList& nei = mesh_.faceNeighbour();
00110 
00111     // first estimate the approximate cell centre as the average of face centres
00112 
00113     vectorField cEst(mesh_.nCells());
00114     UIndirectList<vector>(cEst, changedCells) = vector::zero;
00115     scalarField nCellFaces(mesh_.nCells());
00116     UIndirectList<scalar>(nCellFaces, changedCells) = 0.0;
00117 
00118     forAll(changedFaces, i)
00119     {
00120         label faceI = changedFaces[i];
00121         cEst[own[faceI]] += faceCentres_[faceI];
00122         nCellFaces[own[faceI]] += 1;
00123 
00124         if (mesh_.isInternalFace(faceI))
00125         {
00126             cEst[nei[faceI]] += faceCentres_[faceI];
00127             nCellFaces[nei[faceI]] += 1;
00128         }
00129     }
00130 
00131     forAll(changedCells, i)
00132     {
00133         label cellI = changedCells[i];
00134         cEst[cellI] /= nCellFaces[cellI];
00135     }
00136 
00137     forAll(changedFaces, i)
00138     {
00139         label faceI = changedFaces[i];
00140 
00141         // Calculate 3*face-pyramid volume
00142         scalar pyr3Vol = max
00143         (
00144             faceAreas_[faceI] & (faceCentres_[faceI] - cEst[own[faceI]]),
00145             VSMALL
00146         );
00147 
00148         // Calculate face-pyramid centre
00149         vector pc = (3.0/4.0)*faceCentres_[faceI] + (1.0/4.0)*cEst[own[faceI]];
00150 
00151         // Accumulate volume-weighted face-pyramid centre
00152         cellCentres_[own[faceI]] += pyr3Vol*pc;
00153 
00154         // Accumulate face-pyramid volume
00155         cellVolumes_[own[faceI]] += pyr3Vol;
00156 
00157         if (mesh_.isInternalFace(faceI))
00158         {
00159             // Calculate 3*face-pyramid volume
00160             scalar pyr3Vol = max
00161             (
00162                 faceAreas_[faceI] & (cEst[nei[faceI]] - faceCentres_[faceI]),
00163                 VSMALL
00164             );
00165 
00166             // Calculate face-pyramid centre
00167             vector pc =
00168                 (3.0/4.0)*faceCentres_[faceI]
00169               + (1.0/4.0)*cEst[nei[faceI]];
00170 
00171             // Accumulate volume-weighted face-pyramid centre
00172             cellCentres_[nei[faceI]] += pyr3Vol*pc;
00173 
00174             // Accumulate face-pyramid volume
00175             cellVolumes_[nei[faceI]] += pyr3Vol;
00176         }
00177     }
00178 
00179     forAll(changedCells, i)
00180     {
00181         label cellI = changedCells[i];
00182 
00183         cellCentres_[cellI] /= cellVolumes_[cellI];
00184         cellVolumes_[cellI] *= (1.0/3.0);
00185     }
00186 }
00187 
00188 
00189 Foam::labelList Foam::primitiveMeshGeometry::affectedCells
00190 (
00191     const labelList& changedFaces
00192 ) const
00193 {
00194     const labelList& own = mesh_.faceOwner();
00195     const labelList& nei = mesh_.faceNeighbour();
00196 
00197     labelHashSet affectedCells(2*changedFaces.size());
00198 
00199     forAll(changedFaces, i)
00200     {
00201         label faceI = changedFaces[i];
00202 
00203         affectedCells.insert(own[faceI]);
00204 
00205         if (mesh_.isInternalFace(faceI))
00206         {
00207             affectedCells.insert(nei[faceI]);
00208         }
00209     }
00210     return affectedCells.toc();
00211 }
00212 
00213 
00214 
00215 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00216 
00217 // Construct from components
00218 Foam::primitiveMeshGeometry::primitiveMeshGeometry
00219 (
00220     const primitiveMesh& mesh
00221 )
00222 :
00223     mesh_(mesh)
00224 {
00225     correct();
00226 }
00227 
00228 
00229 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00230 
00231 
00232 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00233 
00234 //- Take over properties from mesh
00235 void Foam::primitiveMeshGeometry::correct()
00236 {
00237     faceAreas_ = mesh_.faceAreas();
00238     faceCentres_ = mesh_.faceCentres();
00239     cellCentres_ = mesh_.cellCentres();
00240     cellVolumes_ = mesh_.cellVolumes();
00241 }
00242 
00243 
00244 //- Recalculate on selected faces
00245 void Foam::primitiveMeshGeometry::correct
00246 (
00247     const pointField& p,
00248     const labelList& changedFaces
00249 )
00250 {
00251     // Update face quantities
00252     updateFaceCentresAndAreas(p, changedFaces);
00253     // Update cell quantities from face quantities
00254     updateCellCentresAndVols(affectedCells(changedFaces), changedFaces);
00255 }
00256 
00257 
00258 bool Foam::primitiveMeshGeometry::checkFaceDotProduct
00259 (
00260     const bool report,
00261     const scalar orthWarn,
00262     const primitiveMesh& mesh,
00263     const vectorField& cellCentres,
00264     const vectorField& faceAreas,
00265     const labelList& checkFaces,
00266     labelHashSet* setPtr
00267 )
00268 {
00269     // for all internal faces check theat the d dot S product is positive
00270 
00271     const labelList& own = mesh.faceOwner();
00272     const labelList& nei = mesh.faceNeighbour();
00273 
00274     // Severe nonorthogonality threshold
00275     const scalar severeNonorthogonalityThreshold =
00276         ::cos(orthWarn/180.0*mathematicalConstant::pi);
00277 
00278     scalar minDDotS = GREAT;
00279 
00280     scalar sumDDotS = 0;
00281 
00282     label severeNonOrth = 0;
00283 
00284     label errorNonOrth = 0;
00285 
00286     forAll(checkFaces, i)
00287     {
00288         label faceI = checkFaces[i];
00289 
00290         if (mesh.isInternalFace(faceI))
00291         {
00292             vector d = cellCentres[nei[faceI]] - cellCentres[own[faceI]];
00293             const vector& s = faceAreas[faceI];
00294 
00295             scalar dDotS = (d & s)/(mag(d)*mag(s) + VSMALL);
00296 
00297             if (dDotS < severeNonorthogonalityThreshold)
00298             {
00299                 if (dDotS > SMALL)
00300                 {
00301                     if (report)
00302                     {
00303                         // Severe non-orthogonality but mesh still OK
00304                         Pout<< "Severe non-orthogonality for face " << faceI
00305                             << " between cells " << own[faceI]
00306                             << " and " << nei[faceI]
00307                             << ": Angle = "
00308                             << ::acos(dDotS)/mathematicalConstant::pi*180.0
00309                             << " deg." << endl;
00310                     }
00311 
00312                     if (setPtr)
00313                     {
00314                         setPtr->insert(faceI);
00315                     }
00316 
00317                     severeNonOrth++;
00318                 }
00319                 else
00320                 {
00321                     // Non-orthogonality greater than 90 deg
00322                     if (report)
00323                     {
00324                         WarningIn
00325                         (
00326                             "primitiveMeshGeometry::checkFaceDotProduct"
00327                             "(const bool, const scalar, const labelList&"
00328                             ", labelHashSet*)"
00329                         )   << "Severe non-orthogonality detected for face "
00330                             << faceI
00331                             << " between cells " << own[faceI] << " and "
00332                             << nei[faceI]
00333                             << ": Angle = "
00334                             << ::acos(dDotS)/mathematicalConstant::pi*180.0
00335                             << " deg." << endl;
00336                     }
00337 
00338                     errorNonOrth++;
00339 
00340                     if (setPtr)
00341                     {
00342                         setPtr->insert(faceI);
00343                     }
00344                 }
00345             }
00346 
00347             if (dDotS < minDDotS)
00348             {
00349                 minDDotS = dDotS;
00350             }
00351 
00352             sumDDotS += dDotS;
00353         }
00354     }
00355 
00356     reduce(minDDotS, minOp<scalar>());
00357     reduce(sumDDotS, sumOp<scalar>());
00358     reduce(severeNonOrth, sumOp<label>());
00359     reduce(errorNonOrth, sumOp<label>());
00360 
00361     label neiSize = nei.size();
00362     reduce(neiSize, sumOp<label>());
00363 
00364     // Only report if there are some internal faces
00365     if (neiSize > 0)
00366     {
00367         if (report && minDDotS < severeNonorthogonalityThreshold)
00368         {
00369             Info<< "Number of non-orthogonality errors: " << errorNonOrth
00370                 << ". Number of severely non-orthogonal faces: "
00371                 << severeNonOrth  << "." << endl;
00372         }
00373     }
00374 
00375     if (report)
00376     {
00377         if (neiSize > 0)
00378         {
00379             Info<< "Mesh non-orthogonality Max: "
00380                 << ::acos(minDDotS)/mathematicalConstant::pi*180.0
00381                 << " average: " <<
00382                    ::acos(sumDDotS/neiSize)/mathematicalConstant::pi*180.0
00383                 << endl;
00384         }
00385     }
00386 
00387     if (errorNonOrth > 0)
00388     {
00389         if (report)
00390         {
00391             SeriousErrorIn
00392             (
00393                 "primitiveMeshGeometry::checkFaceDotProduct"
00394                 "(const bool, const scalar, const labelList&, labelHashSet*)"
00395             )   << "Error in non-orthogonality detected" << endl;
00396         }
00397 
00398         return true;
00399     }
00400     else
00401     {
00402         if (report)
00403         {
00404             Info<< "Non-orthogonality check OK.\n" << endl;
00405         }
00406 
00407         return false;
00408     }
00409 }
00410 
00411 
00412 bool Foam::primitiveMeshGeometry::checkFacePyramids
00413 (
00414     const bool report,
00415     const scalar minPyrVol,
00416     const primitiveMesh& mesh,
00417     const vectorField& cellCentres,
00418     const pointField& p,
00419     const labelList& checkFaces,
00420     labelHashSet* setPtr
00421 )
00422 {
00423     // check whether face area vector points to the cell with higher label
00424     const labelList& own = mesh.faceOwner();
00425     const labelList& nei = mesh.faceNeighbour();
00426 
00427     const faceList& f = mesh.faces();
00428 
00429     label nErrorPyrs = 0;
00430 
00431     forAll(checkFaces, i)
00432     {
00433         label faceI = checkFaces[i];
00434 
00435         // Create the owner pyramid - it will have negative volume
00436         scalar pyrVol = pyramidPointFaceRef
00437         (
00438             f[faceI],
00439             cellCentres[own[faceI]]
00440         ).mag(p);
00441 
00442         if (pyrVol > -minPyrVol)
00443         {
00444             if (report)
00445             {
00446                 Pout<< "bool primitiveMeshGeometry::checkFacePyramids("
00447                     << "const bool, const scalar, const pointField&"
00448                     << ", const labelList&, labelHashSet*): "
00449                     << "face " << faceI << " points the wrong way. " << endl
00450                     << "Pyramid volume: " << -pyrVol
00451                     << " Face " << f[faceI] << " area: " << f[faceI].mag(p)
00452                     << " Owner cell: " << own[faceI] << endl
00453                     << "Owner cell vertex labels: "
00454                     << mesh.cells()[own[faceI]].labels(f)
00455                     << endl;
00456             }
00457 
00458 
00459             if (setPtr)
00460             {
00461                 setPtr->insert(faceI);
00462             }
00463 
00464             nErrorPyrs++;
00465         }
00466 
00467         if (mesh.isInternalFace(faceI))
00468         {
00469             // Create the neighbour pyramid - it will have positive volume
00470             scalar pyrVol =
00471                 pyramidPointFaceRef(f[faceI], cellCentres[nei[faceI]]).mag(p);
00472 
00473             if (pyrVol < minPyrVol)
00474             {
00475                 if (report)
00476                 {
00477                     Pout<< "bool primitiveMeshGeometry::checkFacePyramids("
00478                         << "const bool, const scalar, const pointField&"
00479                         << ", const labelList&, labelHashSet*): "
00480                         << "face " << faceI << " points the wrong way. " << endl
00481                         << "Pyramid volume: " << -pyrVol
00482                         << " Face " << f[faceI] << " area: " << f[faceI].mag(p)
00483                         << " Neighbour cell: " << nei[faceI] << endl
00484                         << "Neighbour cell vertex labels: "
00485                         << mesh.cells()[nei[faceI]].labels(f)
00486                         << endl;
00487                 }
00488 
00489                 if (setPtr)
00490                 {
00491                     setPtr->insert(faceI);
00492                 }
00493 
00494                 nErrorPyrs++;
00495             }
00496         }
00497     }
00498 
00499     reduce(nErrorPyrs, sumOp<label>());
00500 
00501     if (nErrorPyrs > 0)
00502     {
00503         if (report)
00504         {
00505             SeriousErrorIn
00506             (
00507                 "primitiveMeshGeometry::checkFacePyramids("
00508                 "const bool, const scalar, const pointField&"
00509                 ", const labelList&, labelHashSet*)"
00510             )   << "Error in face pyramids: faces pointing the wrong way!"
00511                 << endl;
00512         }
00513 
00514         return true;
00515     }
00516     else
00517     {
00518         if (report)
00519         {
00520             Info<< "Face pyramids OK.\n" << endl;
00521         }
00522 
00523         return false;
00524     }
00525 }
00526 
00527 
00528 bool Foam::primitiveMeshGeometry::checkFaceSkewness
00529 (
00530     const bool report,
00531     const scalar internalSkew,
00532     const scalar boundarySkew,
00533     const primitiveMesh& mesh,
00534     const vectorField& cellCentres,
00535     const vectorField& faceCentres,
00536     const vectorField& faceAreas,
00537     const labelList& checkFaces,
00538     labelHashSet* setPtr
00539 )
00540 {
00541     // Warn if the skew correction vector is more than skew times
00542     // larger than the face area vector
00543 
00544     const labelList& own = mesh.faceOwner();
00545     const labelList& nei = mesh.faceNeighbour();
00546 
00547     scalar maxSkew = 0;
00548 
00549     label nWarnSkew = 0;
00550 
00551     forAll(checkFaces, i)
00552     {
00553         label faceI = checkFaces[i];
00554 
00555         if (mesh.isInternalFace(faceI))
00556         {
00557             scalar dOwn = mag(faceCentres[faceI] - cellCentres[own[faceI]]);
00558             scalar dNei = mag(faceCentres[faceI] - cellCentres[nei[faceI]]);
00559 
00560             point faceIntersection =
00561                 cellCentres[own[faceI]]*dNei/(dOwn+dNei)
00562               + cellCentres[nei[faceI]]*dOwn/(dOwn+dNei);
00563 
00564             scalar skewness = 
00565                 mag(faceCentres[faceI] - faceIntersection)
00566               / (
00567                     mag(cellCentres[nei[faceI]]-cellCentres[own[faceI]])
00568                   + VSMALL
00569                 );
00570 
00571             // Check if the skewness vector is greater than the PN vector.
00572             // This does not cause trouble but is a good indication of a poor
00573             // mesh.
00574             if (skewness > internalSkew)
00575             {
00576                 if (report)
00577                 {
00578                     Pout<< "Severe skewness for face " << faceI
00579                         << " skewness = " << skewness << endl;
00580                 }
00581 
00582                 if (setPtr)
00583                 {
00584                     setPtr->insert(faceI);
00585                 }
00586 
00587                 nWarnSkew++;
00588             }
00589 
00590             if (skewness > maxSkew)
00591             {
00592                 maxSkew = skewness;
00593             }
00594         }
00595         else
00596         {
00597             // Boundary faces: consider them to have only skewness error.
00598             // (i.e. treat as if mirror cell on other side)
00599 
00600             vector faceNormal = faceAreas[faceI];
00601             faceNormal /= mag(faceNormal) + VSMALL;
00602 
00603             vector dOwn = faceCentres[faceI] - cellCentres[own[faceI]];
00604 
00605             vector dWall = faceNormal*(faceNormal & dOwn);
00606 
00607             point faceIntersection = cellCentres[own[faceI]] + dWall;
00608 
00609             scalar skewness =
00610                 mag(faceCentres[faceI] - faceIntersection)
00611                 /(2*mag(dWall) + VSMALL);
00612 
00613             // Check if the skewness vector is greater than the PN vector.
00614             // This does not cause trouble but is a good indication of a poor
00615             // mesh.
00616             if (skewness > boundarySkew)
00617             {
00618                 if (report)
00619                 {
00620                     Pout<< "Severe skewness for boundary face " << faceI
00621                         << " skewness = " << skewness << endl;
00622                 }
00623 
00624                 if (setPtr)
00625                 {
00626                     setPtr->insert(faceI);
00627                 }
00628 
00629                 nWarnSkew++;
00630             }
00631 
00632             if (skewness > maxSkew)
00633             {
00634                 maxSkew = skewness;
00635             }
00636         }
00637     }
00638 
00639     reduce(maxSkew, maxOp<scalar>());
00640     reduce(nWarnSkew, sumOp<label>());
00641 
00642     if (nWarnSkew > 0)
00643     {
00644         if (report)
00645         {
00646             WarningIn
00647             (
00648                 "primitiveMeshGeometry::checkFaceSkewness"
00649                 "(const bool, const scalar, const labelList&, labelHashSet*)"
00650             )   << "Large face skewness detected.  Max skewness = "
00651                 << 100*maxSkew
00652                 << " percent.\nThis may impair the quality of the result." << nl
00653                 << nWarnSkew << " highly skew faces detected."
00654                 << endl;
00655         }
00656 
00657         return true;
00658     }
00659     else
00660     {
00661         if (report)
00662         {
00663             Info<< "Max skewness = " << 100*maxSkew
00664                 << " percent.  Face skewness OK.\n" << endl;
00665         }
00666 
00667         return false;
00668     }
00669 }
00670 
00671 
00672 bool Foam::primitiveMeshGeometry::checkFaceWeights
00673 (
00674     const bool report,
00675     const scalar warnWeight,
00676     const primitiveMesh& mesh,
00677     const vectorField& cellCentres,
00678     const vectorField& faceCentres,
00679     const vectorField& faceAreas,
00680     const labelList& checkFaces,
00681     labelHashSet* setPtr
00682 )
00683 {
00684     // Warn if the delta factor (0..1) is too large.
00685 
00686     const labelList& own = mesh.faceOwner();
00687     const labelList& nei = mesh.faceNeighbour();
00688 
00689     scalar minWeight = GREAT;
00690 
00691     label nWarnWeight = 0;
00692 
00693     forAll(checkFaces, i)
00694     {
00695         label faceI = checkFaces[i];
00696 
00697         if (mesh.isInternalFace(faceI))
00698         {
00699             const point& fc = faceCentres[faceI];
00700 
00701             scalar dOwn = mag(faceAreas[faceI] & (fc-cellCentres[own[faceI]]));
00702             scalar dNei = mag(faceAreas[faceI] & (cellCentres[nei[faceI]]-fc));
00703 
00704             scalar weight = min(dNei,dOwn)/(dNei+dOwn);
00705 
00706             if (weight < warnWeight)
00707             {
00708                 if (report)
00709                 {
00710                     Pout<< "Small weighting factor for face " << faceI
00711                         << " weight = " << weight << endl;
00712                 }
00713 
00714                 if (setPtr)
00715                 {
00716                     setPtr->insert(faceI);
00717                 }
00718 
00719                 nWarnWeight++;
00720             }
00721 
00722             minWeight = min(minWeight, weight);
00723         }
00724     }
00725 
00726     reduce(minWeight, minOp<scalar>());
00727     reduce(nWarnWeight, sumOp<label>());
00728 
00729     if (minWeight < warnWeight)
00730     {
00731         if (report)
00732         {
00733             WarningIn
00734             (
00735                 "primitiveMeshGeometry::checkFaceWeights"
00736                 "(const bool, const scalar, const labelList&, labelHashSet*)"
00737             )   << "Small interpolation weight detected.  Min weight = "
00738                 << minWeight << '.' << nl
00739                 << nWarnWeight << " faces with small weights detected."
00740                 << endl;
00741         }
00742 
00743         return true;
00744     }
00745     else
00746     {
00747         if (report)
00748         {
00749             Info<< "Min weight = " << minWeight
00750                 << " percent.  Weights OK.\n" << endl;
00751         }
00752 
00753         return false;
00754     }
00755 }
00756 
00757 
00758 // Check convexity of angles in a face. Allow a slight non-convexity.
00759 // E.g. maxDeg = 10 allows for angles < 190 (or 10 degrees concavity)
00760 // (if truly concave and points not visible from face centre the face-pyramid
00761 //  check in checkMesh will fail)
00762 bool Foam::primitiveMeshGeometry::checkFaceAngles
00763 (
00764     const bool report,
00765     const scalar maxDeg,
00766     const primitiveMesh& mesh,
00767     const vectorField& faceAreas,
00768     const pointField& p,
00769     const labelList& checkFaces,
00770     labelHashSet* setPtr
00771 )
00772 {
00773     if (maxDeg < -SMALL || maxDeg > 180+SMALL)
00774     {
00775         FatalErrorIn
00776         (
00777             "primitiveMeshGeometry::checkFaceAngles"
00778             "(const bool, const scalar, const pointField&, const labelList&"
00779             ", labelHashSet*)"
00780         )   << "maxDeg should be [0..180] but is now " << maxDeg
00781             << abort(FatalError);
00782     }
00783 
00784     const scalar maxSin = Foam::sin(maxDeg/180.0*mathematicalConstant::pi);
00785 
00786     const faceList& fcs = mesh.faces();
00787 
00788     scalar maxEdgeSin = 0.0;
00789 
00790     label nConcave = 0;
00791 
00792     label errorFaceI = -1;
00793 
00794     forAll(checkFaces, i)
00795     {
00796         label faceI = checkFaces[i];
00797 
00798         const face& f = fcs[faceI];
00799 
00800         vector faceNormal = faceAreas[faceI];
00801         faceNormal /= mag(faceNormal) + VSMALL;
00802 
00803         // Get edge from f[0] to f[size-1];
00804         vector ePrev(p[f[0]] - p[f[f.size()-1]]);
00805         scalar magEPrev = mag(ePrev);
00806         ePrev /= magEPrev + VSMALL;
00807 
00808         forAll(f, fp0)
00809         {
00810             // Get vertex after fp
00811             label fp1 = f.fcIndex(fp0);
00812 
00813             // Normalized vector between two consecutive points
00814             vector e10(p[f[fp1]] - p[f[fp0]]);
00815             scalar magE10 = mag(e10);
00816             e10 /= magE10 + VSMALL;
00817 
00818             if (magEPrev > SMALL && magE10 > SMALL)
00819             {
00820                 vector edgeNormal = ePrev ^ e10;
00821                 scalar magEdgeNormal = mag(edgeNormal);
00822 
00823                 if (magEdgeNormal < maxSin)
00824                 {
00825                     // Edges (almost) aligned -> face is ok.
00826                 }
00827                 else
00828                 {
00829                     // Check normal
00830                     edgeNormal /= magEdgeNormal;
00831 
00832                     if ((edgeNormal & faceNormal) < SMALL)
00833                     {
00834                         if (faceI != errorFaceI)
00835                         {
00836                             // Count only one error per face.
00837                             errorFaceI = faceI;
00838                             nConcave++;
00839                         }
00840 
00841                         if (setPtr)
00842                         {
00843                             setPtr->insert(faceI);
00844                         }
00845 
00846                         maxEdgeSin = max(maxEdgeSin, magEdgeNormal);
00847                     }
00848                 }
00849             }
00850 
00851             ePrev = e10;
00852             magEPrev = magE10;
00853         }
00854     }
00855 
00856     reduce(nConcave, sumOp<label>());
00857     reduce(maxEdgeSin, maxOp<scalar>());
00858 
00859     if (report)
00860     {
00861         if (maxEdgeSin > SMALL)
00862         {
00863             scalar maxConcaveDegr =
00864                 Foam::asin(Foam::min(1.0, maxEdgeSin))
00865              * 180.0/mathematicalConstant::pi;
00866 
00867             Info<< "There are " << nConcave
00868                 << " faces with concave angles between consecutive"
00869                 << " edges. Max concave angle = " << maxConcaveDegr
00870                 << " degrees.\n" << endl;
00871         }
00872         else
00873         {
00874             Info<< "All angles in faces are convex or less than "  << maxDeg
00875                 << " degrees concave.\n" << endl;
00876         }
00877     }
00878 
00879     if (nConcave > 0)
00880     {
00881         if (report)
00882         {
00883             WarningIn
00884             (
00885                 "primitiveMeshGeometry::checkFaceAngles"
00886                 "(const bool, const scalar,  const pointField&"
00887                 ", const labelList&, labelHashSet*)"
00888             )   << nConcave  << " face points with severe concave angle (> "
00889                 << maxDeg << " deg) found.\n"
00890                 << endl;
00891         }
00892 
00893         return true;
00894     }
00895     else
00896     {
00897         return false;
00898     }
00899 }
00900 
00901 
00905 //bool Foam::primitiveMeshGeometry::checkFaceFlatness
00906 //(
00907 //    const bool report,
00908 //    const scalar warnFlatness,
00909 //    const primitiveMesh& mesh,
00910 //    const vectorField& faceAreas,
00911 //    const vectorField& faceCentres,
00912 //    const pointField& p,
00913 //    const labelList& checkFaces,
00914 //    labelHashSet* setPtr
00915 //)
00916 //{
00917 //    if (warnFlatness < 0 || warnFlatness > 1)
00918 //    {
00919 //        FatalErrorIn
00920 //        (
00921 //            "primitiveMeshGeometry::checkFaceFlatness"
00922 //            "(const bool, const scalar, const pointField&"
00923 //            ", const labelList&, labelHashSet*)"
00924 //        )   << "warnFlatness should be [0..1] but is now " << warnFlatness
00925 //            << abort(FatalError);
00926 //    }
00927 //
00928 //
00929 //    const faceList& fcs = mesh.faces();
00930 //
00931 //    // Areas are calculated as the sum of areas. (see
00932 //    // primitiveMeshFaceCentresAndAreas.C)
00933 //
00934 //    label nWarped = 0;
00935 //
00936 //    scalar minFlatness = GREAT;
00937 //    scalar sumFlatness = 0;
00938 //    label nSummed = 0;
00939 //
00940 //    forAll(checkFaces, i)
00941 //    {
00942 //        label faceI = checkFaces[i];
00943 //
00944 //        const face& f = fcs[faceI];
00945 //
00946 //        scalar magArea = mag(faceAreas[faceI]);
00947 //
00948 //        if (f.size() > 3 && magArea > VSMALL)
00949 //        {
00950 //            const point& fc = faceCentres[faceI];
00951 //
00952 //            // Calculate the sum of magnitude of areas and compare to magnitude
00953 //            // of sum of areas.
00954 //
00955 //            scalar sumA = 0.0;
00956 //
00957 //            forAll(f, fp)
00958 //            {
00959 //                const point& thisPoint = p[f[fp]];
00960 //                const point& nextPoint = p[f.nextLabel(fp)];
00961 //
00962 //                // Triangle around fc.
00963 //                vector n = 0.5*((nextPoint - thisPoint)^(fc - thisPoint));
00964 //                sumA += mag(n);
00965 //            }
00966 //
00967 //            scalar flatness = magArea / (sumA+VSMALL);
00968 //
00969 //            sumFlatness += flatness;
00970 //            nSummed++;
00971 //
00972 //            minFlatness = min(minFlatness, flatness);
00973 //
00974 //            if (flatness < warnFlatness)
00975 //            {
00976 //                nWarped++;
00977 //
00978 //                if (setPtr)
00979 //                {
00980 //                    setPtr->insert(faceI);
00981 //                }
00982 //            }
00983 //        }
00984 //    }
00985 //
00986 //
00987 //    reduce(nWarped, sumOp<label>());
00988 //    reduce(minFlatness, minOp<scalar>());
00989 //
00990 //    reduce(nSummed, sumOp<label>());
00991 //    reduce(sumFlatness, sumOp<scalar>());
00992 //
00993 //    if (report)
00994 //    {
00995 //        if (nSummed > 0)
00996 //        {
00997 //            Info<< "Face flatness (1 = flat, 0 = butterfly) : average = "
00998 //                << sumFlatness / nSummed << "  min = " << minFlatness << endl;
00999 //        }
01000 //
01001 //        if (nWarped> 0)
01002 //        {
01003 //            Info<< "There are " << nWarped
01004 //                << " faces with ratio between projected and actual area < "
01005 //                << warnFlatness
01006 //                << ".\nMinimum ratio (minimum flatness, maximum warpage) = "
01007 //                << minFlatness << nl << endl;
01008 //        }
01009 //        else
01010 //        {
01011 //            Info<< "All faces are flat in that the ratio between projected"
01012 //                << " and actual area is > " << warnFlatness << nl << endl;
01013 //        }
01014 //    }
01015 //
01016 //    if (nWarped > 0)
01017 //    {
01018 //        if (report)
01019 //        {
01020 //            WarningIn
01021 //            (
01022 //                "primitiveMeshGeometry::checkFaceFlatness"
01023 //                "(const bool, const scalar, const pointField&"
01024 //                ", const labelList&, labelHashSet*)"
01025 //            )   << nWarped  << " faces with severe warpage (flatness < "
01026 //                << warnFlatness << ") found.\n"
01027 //                << endl;
01028 //        }
01029 //
01030 //        return true;
01031 //    }
01032 //    else
01033 //    {
01034 //        return false;
01035 //    }
01036 //}
01037 
01038 
01039 // Check twist of faces. Is calculated as the difference between areas of
01040 // individual triangles and the overall area of the face (which ifself is
01041 // is the average of the areas of the individual triangles).
01042 bool Foam::primitiveMeshGeometry::checkFaceTwist
01043 (
01044     const bool report,
01045     const scalar minTwist,
01046     const primitiveMesh& mesh,
01047     const vectorField& faceAreas,
01048     const vectorField& faceCentres,
01049     const pointField& p,
01050     const labelList& checkFaces,
01051     labelHashSet* setPtr
01052 )
01053 {
01054     if (minTwist < -1-SMALL || minTwist > 1+SMALL)
01055     {
01056         FatalErrorIn
01057         (
01058             "primitiveMeshGeometry::checkFaceTwist"
01059             "(const bool, const scalar, const primitiveMesh&, const pointField&"
01060             ", const labelList&, labelHashSet*)"
01061         )   << "minTwist should be [-1..1] but is now " << minTwist
01062             << abort(FatalError);
01063     }
01064 
01065 
01066     const faceList& fcs = mesh.faces();
01067 
01068     // Areas are calculated as the sum of areas. (see
01069     // primitiveMeshFaceCentresAndAreas.C)
01070 
01071     label nWarped = 0;
01072 
01073     forAll(checkFaces, i)
01074     {
01075         label faceI = checkFaces[i];
01076 
01077         const face& f = fcs[faceI];
01078 
01079         scalar magArea = mag(faceAreas[faceI]);
01080 
01081         if (f.size() > 3 && magArea > VSMALL)
01082         {
01083             const vector nf = faceAreas[faceI] / magArea;
01084 
01085             const point& fc = faceCentres[faceI];
01086 
01087             forAll(f, fpI)
01088             {
01089                 vector triArea
01090                 (
01091                     triPointRef
01092                     (
01093                         p[f[fpI]],
01094                         p[f.nextLabel(fpI)],
01095                         fc
01096                     ).normal()
01097                 );
01098 
01099                 scalar magTri = mag(triArea);
01100 
01101                 if (magTri > VSMALL && ((nf & triArea/magTri) < minTwist))
01102                 {
01103                     nWarped++;
01104 
01105                     if (setPtr)
01106                     {
01107                         setPtr->insert(faceI);
01108                     }
01109                 }
01110             }
01111         }
01112     }
01113 
01114 
01115     reduce(nWarped, sumOp<label>());
01116 
01117     if (report)
01118     {
01119         if (nWarped> 0)
01120         {
01121             Info<< "There are " << nWarped
01122                 << " faces with cosine of the angle"
01123                 << " between triangle normal and face normal less than "
01124                 << minTwist << nl << endl;
01125         }
01126         else
01127         {
01128             Info<< "All faces are flat in that the cosine of the angle"
01129                 << " between triangle normal and face normal less than "
01130                 << minTwist << nl << endl;
01131         }
01132     }
01133 
01134     if (nWarped > 0)
01135     {
01136         if (report)
01137         {
01138             WarningIn
01139             (
01140                 "primitiveMeshGeometry::checkFaceTwist"
01141                 "(const bool, const scalar, const primitiveMesh&"
01142                 ", const pointField&, const labelList&, labelHashSet*)"
01143             )   << nWarped  << " faces with severe warpage "
01144                 << "(cosine of the angle between triangle normal and face normal"
01145                 << " < " << minTwist << ") found.\n"
01146                 << endl;
01147         }
01148 
01149         return true;
01150     }
01151     else
01152     {
01153         return false;
01154     }
01155 }
01156 
01157 
01158 bool Foam::primitiveMeshGeometry::checkFaceArea
01159 (
01160     const bool report,
01161     const scalar minArea,
01162     const primitiveMesh& mesh,
01163     const vectorField& faceAreas,
01164     const labelList& checkFaces,
01165     labelHashSet* setPtr
01166 )
01167 {
01168     label nZeroArea = 0;
01169 
01170     forAll(checkFaces, i)
01171     {
01172         label faceI = checkFaces[i];
01173 
01174         if (mag(faceAreas[faceI]) < minArea)
01175         {
01176             if (setPtr)
01177             {
01178                 setPtr->insert(faceI);
01179             }
01180             nZeroArea++;
01181         }
01182     }
01183 
01184 
01185     reduce(nZeroArea, sumOp<label>());
01186 
01187     if (report)
01188     {
01189         if (nZeroArea > 0)
01190         {
01191             Info<< "There are " << nZeroArea
01192                 << " faces with area < " << minArea << '.' << nl << endl;
01193         }
01194         else
01195         {
01196             Info<< "All faces have area > " << minArea << '.' << nl << endl;
01197         }
01198     }
01199 
01200     if (nZeroArea > 0)
01201     {
01202         if (report)
01203         {
01204             WarningIn
01205             (
01206                 "primitiveMeshGeometry::checkFaceArea"
01207                 "(const bool, const scalar, const primitiveMesh&"
01208                 ", const pointField&, const labelList&, labelHashSet*)"
01209             )   << nZeroArea  << " faces with area < " << minArea
01210                 << " found.\n"
01211                 << endl;
01212         }
01213 
01214         return true;
01215     }
01216     else
01217     {
01218         return false;
01219     }
01220 }
01221 
01222 
01223 bool Foam::primitiveMeshGeometry::checkCellDeterminant
01224 (
01225     const bool report,
01226     const scalar warnDet,
01227     const primitiveMesh& mesh,
01228     const vectorField& faceAreas,
01229     const labelList& checkFaces,
01230     const labelList& affectedCells,
01231     labelHashSet* setPtr
01232 )
01233 {
01234     const cellList& cells = mesh.cells();
01235 
01236     scalar minDet = GREAT;
01237     scalar sumDet = 0.0;
01238     label nSumDet = 0;
01239     label nWarnDet = 0;
01240 
01241     forAll(affectedCells, i)
01242     {
01243         const cell& cFaces = cells[affectedCells[i]];
01244 
01245         tensor areaSum(tensor::zero);
01246         scalar magAreaSum = 0;
01247 
01248         forAll(cFaces, cFaceI)
01249         {
01250             label faceI = cFaces[cFaceI];
01251     
01252             scalar magArea = mag(faceAreas[faceI]);
01253 
01254             magAreaSum += magArea;
01255             areaSum += faceAreas[faceI]*(faceAreas[faceI]/magArea);
01256         }
01257 
01258         scalar scaledDet = det(areaSum/magAreaSum)/0.037037037037037;
01259 
01260         minDet = min(minDet, scaledDet);
01261         sumDet += scaledDet;
01262         nSumDet++;
01263 
01264         if (scaledDet < warnDet)
01265         {
01266             if (setPtr)
01267             {
01268                 // Insert all faces of the cell.
01269                 forAll(cFaces, cFaceI)
01270                 {
01271                     label faceI = cFaces[cFaceI];
01272                     setPtr->insert(faceI);
01273                 }
01274             }
01275             nWarnDet++;
01276         }
01277     }
01278     
01279     reduce(minDet, minOp<scalar>());
01280     reduce(sumDet, sumOp<scalar>());
01281     reduce(nSumDet, sumOp<label>());
01282     reduce(nWarnDet, sumOp<label>());
01283 
01284     if (report)
01285     {
01286         if (nSumDet > 0)
01287         {
01288             Info<< "Cell determinant (1 = uniform cube) : average = "
01289                 << sumDet / nSumDet << "  min = " << minDet << endl;
01290         }
01291 
01292         if (nWarnDet > 0)
01293         {
01294             Info<< "There are " << nWarnDet
01295                 << " cells with determinant < " << warnDet << '.' << nl
01296                 << endl;
01297         }
01298         else
01299         {
01300             Info<< "All faces have determinant > " << warnDet << '.' << nl
01301                 << endl;
01302         }
01303     }
01304 
01305     if (nWarnDet > 0)
01306     {
01307         if (report)
01308         {
01309             WarningIn
01310             (
01311                 "primitiveMeshGeometry::checkCellDeterminant"
01312                 "(const bool, const scalar, const primitiveMesh&"
01313                 ", const pointField&, const labelList&, const labelList&"
01314                 ", labelHashSet*)"
01315             )   << nWarnDet << " cells with determinant < " << warnDet
01316                 << " found.\n"
01317                 << endl;
01318         }
01319 
01320         return true;
01321     }
01322     else
01323     {
01324         return false;
01325     }
01326 }
01327 
01328 
01329 bool Foam::primitiveMeshGeometry::checkFaceDotProduct
01330 (
01331     const bool report,
01332     const scalar orthWarn,
01333     const labelList& checkFaces,
01334     labelHashSet* setPtr
01335 ) const
01336 {
01337     return checkFaceDotProduct
01338     (
01339         report,
01340         orthWarn,
01341         mesh_,
01342         cellCentres_,
01343         faceAreas_,
01344         checkFaces,
01345         setPtr
01346     );
01347 }
01348 
01349 
01350 bool Foam::primitiveMeshGeometry::checkFacePyramids
01351 (
01352     const bool report,
01353     const scalar minPyrVol,
01354     const pointField& p,
01355     const labelList& checkFaces,
01356     labelHashSet* setPtr
01357 ) const
01358 {
01359     return checkFacePyramids
01360     (
01361         report,
01362         minPyrVol,
01363         mesh_,
01364         cellCentres_,
01365         p,
01366         checkFaces,
01367         setPtr
01368     );
01369 }
01370 
01371 
01372 bool Foam::primitiveMeshGeometry::checkFaceSkewness
01373 (
01374     const bool report,
01375     const scalar internalSkew,
01376     const scalar boundarySkew,
01377     const labelList& checkFaces,
01378     labelHashSet* setPtr
01379 ) const
01380 {
01381     return checkFaceSkewness
01382     (
01383         report,
01384         internalSkew,
01385         boundarySkew,
01386         mesh_,
01387         cellCentres_,
01388         faceCentres_,
01389         faceAreas_,
01390         checkFaces,
01391         setPtr
01392     );
01393 }
01394 
01395 
01396 bool Foam::primitiveMeshGeometry::checkFaceWeights
01397 (
01398     const bool report,
01399     const scalar warnWeight,
01400     const labelList& checkFaces,
01401     labelHashSet* setPtr
01402 ) const
01403 {
01404     return checkFaceWeights
01405     (
01406         report,
01407         warnWeight,
01408         mesh_,
01409         cellCentres_,
01410         faceCentres_,
01411         faceAreas_,
01412         checkFaces,
01413         setPtr
01414     );
01415 }
01416 
01417 
01418 bool Foam::primitiveMeshGeometry::checkFaceAngles
01419 (
01420     const bool report,
01421     const scalar maxDeg,
01422     const pointField& p,
01423     const labelList& checkFaces,
01424     labelHashSet* setPtr
01425 ) const
01426 {
01427     return checkFaceAngles
01428     (
01429         report,
01430         maxDeg,
01431         mesh_,
01432         faceAreas_,
01433         p,
01434         checkFaces,
01435         setPtr
01436     );
01437 }
01438 
01439 
01440 //bool Foam::primitiveMeshGeometry::checkFaceFlatness
01441 //(
01442 //    const bool report,
01443 //    const scalar warnFlatness,
01444 //    const pointField& p,
01445 //    const labelList& checkFaces,
01446 //    labelHashSet* setPtr
01447 //) const
01448 //{
01449 //    return checkFaceFlatness
01450 //    (
01451 //        report,
01452 //        warnFlatness,
01453 //        mesh_,
01454 //        faceAreas_,
01455 //        faceCentres_,
01456 //        p,
01457 //        checkFaces,
01458 //        setPtr
01459 //    );
01460 //}
01461 
01462 
01463 bool Foam::primitiveMeshGeometry::checkFaceTwist
01464 (
01465     const bool report,
01466     const scalar minTwist,
01467     const pointField& p,
01468     const labelList& checkFaces,
01469     labelHashSet* setPtr
01470 ) const
01471 {
01472     return checkFaceTwist
01473     (
01474         report,
01475         minTwist,
01476         mesh_,
01477         faceAreas_,
01478         faceCentres_,
01479         p,
01480         checkFaces,
01481         setPtr
01482     );
01483 }
01484 
01485 
01486 bool Foam::primitiveMeshGeometry::checkFaceArea
01487 (
01488     const bool report,
01489     const scalar minArea,
01490     const labelList& checkFaces,
01491     labelHashSet* setPtr
01492 ) const
01493 {
01494     return checkFaceArea
01495     (
01496         report,
01497         minArea,
01498         mesh_,
01499         faceAreas_,
01500         checkFaces,
01501         setPtr
01502     );
01503 }
01504 
01505 
01506 bool Foam::primitiveMeshGeometry::checkCellDeterminant
01507 (
01508     const bool report,
01509     const scalar warnDet,
01510     const labelList& checkFaces,
01511     const labelList& affectedCells,
01512     labelHashSet* setPtr
01513 ) const
01514 {
01515     return checkCellDeterminant
01516     (
01517         report,
01518         warnDet,
01519         mesh_,
01520         faceAreas_,
01521         checkFaces,
01522         affectedCells,
01523         setPtr
01524     );
01525 }
01526 
01527 
01528 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd