diff --git a/src/OpenFOAM/matrices/scalarMatrices/SVD/SVD.C b/src/OpenFOAM/matrices/scalarMatrices/SVD/SVD.C index 4c833cd40..9f9c7a865 100644 --- a/src/OpenFOAM/matrices/scalarMatrices/SVD/SVD.C +++ b/src/OpenFOAM/matrices/scalarMatrices/SVD/SVD.C @@ -48,11 +48,11 @@ Foam::SVD::SVD(const scalarRectangularMatrix& A, const scalar minCondition) scalar scale = 0; scalar s = 0; scalar anorm = 0; - label l=0; + label l = 0; for (label i=0; i= 0; i--) { if (i < Un-1) @@ -178,7 +180,7 @@ Foam::SVD::SVD(const scalarRectangularMatrix& A, const scalar minCondition) for (label j=l; j= 0; k--) { - for (label its = 0; its < 35; its++) + for (label its = 0; its < 30; its++) { bool flag = true; label mn; for (l = k; l >= 0; l--) { - mn = l-1; - if (mag(rv1[l]) + anorm == anorm) + mn = l - 1; + + if (l == 0 || mag(rv1[l]) <= anorm) { flag = false; break; } - if (mag(S_[mn]) + anorm == anorm) break; + + if (mag(S_[mn]) <= anorm) + { + break; + } } if (flag) { - scalar c = 0.0; - s = 1.0; + scalar c = 0; + s = 1; for (label i=l; i maxDiff) maxDiff = diff; - } - } - Info<< "Maximum discrepancy between A and svd(A) = " << maxDiff << endl; - - if (maxDiff > 4) - { - Info<< "singular values " << S_ << endl; - } - */ } diff --git a/src/OpenFOAM/matrices/scalarMatrices/SVD/SVDI.H b/src/OpenFOAM/matrices/scalarMatrices/SVD/SVDI.H index c3fdb9bb1..9ff7ade03 100644 --- a/src/OpenFOAM/matrices/scalarMatrices/SVD/SVDI.H +++ b/src/OpenFOAM/matrices/scalarMatrices/SVD/SVDI.H @@ -28,7 +28,8 @@ License template inline const T Foam::SVD::sign(const T& a, const T& b) { - return b >= 0 ? (a >= 0 ? a : -a) : (a >= 0 ? -a : a); + //return b >= 0 ? (a >= 0 ? a : -a) : (a >= 0 ? -a : a); + return b >= 0 ? a : -a; } diff --git a/src/thermophysicalModels/chemistryModel/chemistryModel/TDACChemistryModel/tabulation/ISAT/chemPointISAT/chemPointISAT.C b/src/thermophysicalModels/chemistryModel/chemistryModel/TDACChemistryModel/tabulation/ISAT/chemPointISAT/chemPointISAT.C index 92d41405e..7ff29e3ad 100644 --- a/src/thermophysicalModels/chemistryModel/chemistryModel/TDACChemistryModel/tabulation/ISAT/chemPointISAT/chemPointISAT.C +++ b/src/thermophysicalModels/chemistryModel/chemistryModel/TDACChemistryModel/tabulation/ISAT/chemPointISAT/chemPointISAT.C @@ -24,7 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "chemPointISAT.H" -#include +#include "SVD.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -49,14 +49,14 @@ void Foam::chemPointISAT::qrDecompose for (label k=0; k::qrDecompose { R(i, k) /= scale; } - sum = 0.0; + sum = 0; for (label i=k; i::qrDecompose d[k] = -scale*sigma; for (label j=k+1; j::qrDecompose } } } + d[nCols-1] = R(nCols-1, nCols-1); + // form R for (label i=0; i::qrUpdate const Foam::scalarField &v ) { - label k, i; + label k; + scalarField w(u); - for (k=n-1;k>=0;k--) + for (k=n-1; k>=0; k--) { - if (w[k] != 0.0) + if (w[k] != 0) { break; } } + if (k < 0) { - k=0; + k = 0; } - for (i=k-1;i>=0;i--) + + for (label i=k-1; i>=0; i--) { rotate(R, i, w[i],-w[i+1], n); - if (w[i] == 0.0) + if (w[i] == 0) { - w[i] = fabs(w[i+1]); + w[i] = mag(w[i+1]); } - else if (fabs(w[i]) > fabs(w[i+1])) + else if (mag(w[i]) > mag(w[i+1])) { - w[i] = fabs(w[i])*sqrt(1.0+sqr(w[i+1]/w[i])); + w[i] = mag(w[i])*sqrt(1 + sqr(w[i+1]/w[i])); } else { - w[i] = fabs(w[i+1])*sqrt(1.0+sqr(w[i]/w[i+1])); + w[i] = mag(w[i+1])*sqrt(1 + sqr(w[i]/w[i+1])); } } - for (i=0;i::rotate label n ) { - label j; scalar c, fact, s, w, y; - if (a == 0.0) + if (a == 0) { - c=0.0; - s=(b >= 0.0 ? 1.0 : -1.0); + c = 0; + s = (b >= 0 ? 1.0 : -1.0); } - else if (fabs(a) > fabs(b)) + else if (mag(a) > mag(b)) { fact = b/a; - c=sign(a)/sqrt(1.0+(fact*fact)); - s=fact*c; + c = sign(a)/sqrt(1 + sqr(fact)); + s = fact*c; } else { - fact=a/b; - s=sign(b)/sqrt(1.0+(fact*fact)); - c=fact*s; + fact = a/b; + s = sign(b)/sqrt(1 + sqr(fact)); + c = fact*s; } - for (j=i;j -void Foam::chemPointISAT::svd -( - scalarSquareMatrix& A, - label m, - label n, - scalarDiagonalMatrix& d, - scalarSquareMatrix& V -) -{ - // UPDATED VERSION NR3 - bool flag; - label i, its, j, jj, k, l, nm; - scalar anorm, c, f, g, h, s, scale, x, y, z; - scalarField rv1(n); - scalar eps = std::numeric_limits::epsilon(); - g = scale = anorm = 0.0; - - // Householder reduction to bidiagonal form - for ( i = 0; i=0; i--) - { - if (i < n-1) - { - if (g != 0.0) - { - for (j=l; j=0; i--) - { - l=i+1; - g=d[i]; - for (j=l; j=0; k--) - { - for (its=0; its<30; its++) - { - flag=true; - // Test for splitting (rv1[1] always zero) - for (l=k; l>=0; l--) - { - nm = l-1; - if (l == 0 || fabs(rv1[l]) <= eps*anorm) - { - flag = false; - break; - } - if (fabs(d[nm]) <= eps*anorm) - { - break; - } - } - // Cancellation of rv1[l], if l>1 - if (flag) - { - c = 0.0; - s = 1.0; - for (i=l; i -Foam::scalar -Foam::chemPointISAT::pythag(scalar a, scalar b) -{ - scalar absa, absb; - absa = fabs(a); - absb = fabs(b); - if (absa > absb) - { - return absa*sqrt(1.0+sqr(absb/absa)); - } - else - { - return (absb == 0.0 ? 0.0 : absb*sqrt(1.0+sqr(absa/absb))); + y = R(i, j); + w = R(i+1, j); + R(i, j) = c*y-s*w; + R(i+1, j) = s*y+c*w; } } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // - template Foam::chemPointISAT::chemPointISAT ( @@ -565,37 +248,38 @@ Foam::chemPointISAT::chemPointISAT label reduOrCompDim = completeSpaceSize; if (isMechRedActive) { - reduOrCompDim = nActiveSpecies_+2; + reduOrCompDim = nActiveSpecies_ + 2; } - // SVD decomposition A= U*D*V^T - scalarSquareMatrix Atmp(A);// A computed in ISAT.C - scalarSquareMatrix B(reduOrCompDim, Zero); - DiagonalMatrix diag(reduOrCompDim, Zero); - svd(Atmp, reduOrCompDim, reduOrCompDim, diag, B); + // SVD decomposition A = U*D*V^T + SVD svdA(A); - // replace the value of vector diag by max(diag, 1/2), first ISAT paper, - // Pope + scalarDiagonalMatrix D(reduOrCompDim); + const scalarDiagonalMatrix& S = svdA.S(); + + // Replace the value of vector D by max(D, 1/2), first ISAT paper for (label i=0; i::chemPointISAT tolerance_ = p.tolerance(); } + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template @@ -647,27 +332,30 @@ bool Foam::chemPointISAT::inEOA(const scalarField& phiq) scalarField dphi(phiq-phi()); bool isMechRedActive = chemistry_.mechRed()->active(); label dim = (isMechRedActive) ? nActiveSpecies_ : completeSpaceSize()-2; - scalar epsTemp=0.0; - List propEps(completeSpaceSize(),0.0); + scalar epsTemp=0; + List propEps(completeSpaceSize(),0); for (label i=0; i::inEOA(const scalarField& phiq) { temp = dphi[i]/(tolerance_*scaleFactor_[i]); } + epsTemp += sqr(temp); + if (printProportion_) { propEps[i] = temp; } } + // Temperature epsTemp += sqr @@ -688,6 +379,7 @@ bool Foam::chemPointISAT::inEOA(const scalarField& phiq) LT_(dim, dim)*dphi[completeSpaceSize()-2] +LT_(dim, dim+1)*dphi[completeSpaceSize()-1] ); + // Pressure epsTemp += sqr(LT_(dim+1, dim+1)*dphi[completeSpaceSize()-1]); @@ -703,12 +395,12 @@ bool Foam::chemPointISAT::inEOA(const scalarField& phiq) propEps[completeSpaceSize()-1] = sqr(LT_(dim+1, dim+1)*dphi[completeSpaceSize()-1]); } - if (sqrt(epsTemp) > 1.0+tolerance_) + if (sqrt(epsTemp) > 1 + tolerance_) { if (printProportion_) { - scalar max=-1.0; - label maxIndex=-1; + scalar max = -1; + label maxIndex = -1; for (label i=0; i::inEOA(const scalarField& phiq) Info<< "Direction maximum impact to error in ellipsoid: " << propName << endl; Info<< "Proportion to the total error on the retrieve: " - << max / (epsTemp+SMALL) << endl; + << max/(epsTemp+SMALL) << endl; } return false; } @@ -754,13 +446,13 @@ bool Foam::chemPointISAT::checkSolution const scalarField& Rphiq ) { - scalar eps2 = 0.0; + scalar eps2 = 0; scalarField dR(Rphiq - Rphi()); scalarField dphi(phiq - phi()); const scalarField& scaleFactorV(scaleFactor()); const scalarSquareMatrix& Avar(A()); bool isMechRedActive = chemistry_.mechRed()->active(); - scalar dRl = 0.0; + scalar dRl = 0; label dim = completeSpaceSize()-2; if (isMechRedActive) { @@ -771,12 +463,13 @@ bool Foam::chemPointISAT::checkSolution // included for (label i=0; i::grow(const scalarField& phiq) // corresponds to an inactive on the query side if ( - completeToSimplifiedIndex_[i]!=-1 + completeToSimplifiedIndex_[i] != -1 && chemistry_.completeToSimplifiedIndex()[i] == -1 ) { @@ -860,7 +553,7 @@ bool Foam::chemPointISAT::grow(const scalarField& phiq) ( completeToSimplifiedIndex_[i] == -1 && chemistry_.completeToSimplifiedIndex()[i] == -1 - && dphi[i] != 0.0 + && dphi[i] != 0 ) { activeAdded++; @@ -933,17 +626,17 @@ bool Foam::chemPointISAT::grow(const scalarField& phiq) { LT_(i, i)= 1.0 - / (tolerance_*scaleFactor_[simplifiedToCompleteIndex_[i]]); - A_(i, i)=1.0; + /(tolerance_*scaleFactor_[simplifiedToCompleteIndex_[i]]); + A_(i, i) = 1; } } - dim = nActiveSpecies_+2; + dim = nActiveSpecies_ + 2; } // beginning of grow algorithm - scalarField phiTilde(dim, 0.0); - scalar normPhiTilde = 0.0; + scalarField phiTilde(dim, 0); + scalar normPhiTilde = 0; // p' = L^T.(p-phi) for (label i=0; i::grow(const scalarField& phiq) label sj = j; if (isMechRedActive) { - sj=simplifiedToCompleteIndex_[j]; + sj = simplifiedToCompleteIndex_[j]; } phiTilde[i] += LT_(i, j)*dphi[sj]; } @@ -961,12 +654,15 @@ bool Foam::chemPointISAT::grow(const scalarField& phiq) phiTilde[i] += LT_(i, dim-1)*dphi[completeSpaceSize()-1]; normPhiTilde += sqr(phiTilde[i]); } + scalar invSqrNormPhiTilde = 1.0/normPhiTilde; normPhiTilde = sqrt(normPhiTilde); + // gamma = (1/|p'| - 1)/|p'|^2 scalar gamma = (1/normPhiTilde - 1)*invSqrNormPhiTilde; scalarField u(gamma*phiTilde); - scalarField v(dim,0.0); + scalarField v(dim, 0); + for ( label i=0; i