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

scalarMatrices.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 "scalarMatrices.H"
00028 #include "SVD.H"
00029 
00030 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00031 
00032 void Foam::LUDecompose
00033 (
00034     scalarSquareMatrix& matrix,
00035     labelList& pivotIndices
00036 )
00037 {
00038     label n = matrix.n();
00039     scalar vv[n];
00040 
00041     for (register label i=0; i<n; i++)
00042     {
00043         scalar largestCoeff = 0.0;
00044         scalar temp;
00045         const scalar* __restrict__ matrixi = matrix[i];
00046 
00047         for (register label j=0; j<n; j++)
00048         {
00049             if ((temp = mag(matrixi[j])) > largestCoeff)
00050             {
00051                 largestCoeff = temp;
00052             }
00053         }
00054 
00055         if (largestCoeff == 0.0)
00056         {
00057             FatalErrorIn
00058             (
00059                 "LUdecompose"
00060                 "(scalarSquareMatrix& matrix, labelList& rowIndices)"
00061             )   << "Singular matrix" << exit(FatalError);
00062         }
00063 
00064         vv[i] = 1.0/largestCoeff;
00065     }
00066 
00067     for (register label j=0; j<n; j++)
00068     {
00069         scalar* __restrict__ matrixj = matrix[j];
00070 
00071         for (register label i=0; i<j; i++)
00072         {
00073             scalar* __restrict__ matrixi = matrix[i];
00074 
00075             scalar sum = matrixi[j];
00076             for (register label k=0; k<i; k++)
00077             {
00078                 sum -= matrixi[k]*matrix[k][j];
00079             }
00080             matrixi[j] = sum;
00081         }
00082 
00083         label iMax = 0;
00084 
00085         scalar largestCoeff = 0.0;
00086         for (register label i=j; i<n; i++)
00087         {
00088             scalar* __restrict__ matrixi = matrix[i];
00089             scalar sum = matrixi[j];
00090 
00091             for (register label k=0; k<j; k++)
00092             {
00093                 sum -= matrixi[k]*matrix[k][j];
00094             }
00095 
00096             matrixi[j] = sum;
00097 
00098             scalar temp;
00099             if ((temp = vv[i]*mag(sum)) >= largestCoeff)
00100             {
00101                 largestCoeff = temp;
00102                 iMax = i;
00103             }
00104         }
00105 
00106         pivotIndices[j] = iMax;
00107 
00108         if (j != iMax)
00109         {
00110             scalar* __restrict__ matrixiMax = matrix[iMax];
00111 
00112             for (register label k=0; k<n; k++)
00113             {
00114                 Swap(matrixj[k], matrixiMax[k]);
00115             }
00116 
00117             vv[iMax] = vv[j];
00118         }
00119 
00120         if (matrixj[j] == 0.0)
00121         {
00122             matrixj[j] = SMALL;
00123         }
00124 
00125         if (j != n-1)
00126         {
00127             scalar rDiag = 1.0/matrixj[j];
00128 
00129             for (register label i=j+1; i<n; i++)
00130             {
00131                 matrix[i][j] *= rDiag;
00132             }
00133         }
00134     }
00135 }
00136 
00137 
00138 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
00139 
00140 void Foam::multiply
00141 (
00142     scalarRectangularMatrix& ans,         // value changed in return
00143     const scalarRectangularMatrix& A,
00144     const scalarRectangularMatrix& B
00145 )
00146 {
00147     if (A.m() != B.n())
00148     {
00149         FatalErrorIn
00150         (
00151             "multiply("
00152             "scalarRectangularMatrix& answer "
00153             "const scalarRectangularMatrix& A, "
00154             "const scalarRectangularMatrix& B)"
00155         )   << "A and B must have identical inner dimensions but A.m = "
00156             << A.m() << " and B.n = " << B.n()
00157             << abort(FatalError);
00158     }
00159 
00160     ans = scalarRectangularMatrix(A.n(), B.m(), scalar(0));
00161 
00162     for(register label i = 0; i < A.n(); i++)
00163     {
00164         for(register label j = 0; j < B.m(); j++)
00165         {
00166             for(register label l = 0; l < B.n(); l++)
00167             {
00168                 ans[i][j] += A[i][l]*B[l][j];
00169             }
00170         }
00171     }
00172 }
00173 
00174 
00175 void Foam::multiply
00176 (
00177     scalarRectangularMatrix& ans,         // value changed in return
00178     const scalarRectangularMatrix& A,
00179     const scalarRectangularMatrix& B,
00180     const scalarRectangularMatrix& C
00181 )
00182 {
00183     if (A.m() != B.n())
00184     {
00185         FatalErrorIn
00186         (
00187             "multiply("
00188             "const scalarRectangularMatrix& A, "
00189             "const scalarRectangularMatrix& B, "
00190             "const scalarRectangularMatrix& C, "
00191             "scalarRectangularMatrix& answer)"
00192         )   << "A and B must have identical inner dimensions but A.m = "
00193             << A.m() << " and B.n = " << B.n()
00194             << abort(FatalError);
00195     }
00196 
00197     if (B.m() != C.n())
00198     {
00199         FatalErrorIn
00200         (
00201             "multiply("
00202             "const scalarRectangularMatrix& A, "
00203             "const scalarRectangularMatrix& B, "
00204             "const scalarRectangularMatrix& C, "
00205             "scalarRectangularMatrix& answer)"
00206         )   << "B and C must have identical inner dimensions but B.m = "
00207             << B.m() << " and C.n = " << C.n()
00208             << abort(FatalError);
00209     }
00210 
00211     ans = scalarRectangularMatrix(A.n(), C.m(), scalar(0));
00212 
00213     for(register label i = 0; i < A.n(); i++)
00214     {
00215         for(register label g = 0; g < C.m(); g++)
00216         {
00217             for(register label l = 0; l < C.n(); l++)
00218             {
00219                 scalar ab = 0;
00220                 for(register label j = 0; j < A.m(); j++)
00221                 {
00222                     ab += A[i][j]*B[j][l];
00223                 }
00224                 ans[i][g] += C[l][g] * ab;
00225             }
00226         }
00227     }
00228 }
00229 
00230 
00231 void Foam::multiply
00232 (
00233     scalarRectangularMatrix& ans,         // value changed in return
00234     const scalarRectangularMatrix& A,
00235     const DiagonalMatrix<scalar>& B,
00236     const scalarRectangularMatrix& C
00237 )
00238 {
00239     if (A.m() != B.size())
00240     {
00241         FatalErrorIn
00242         (
00243             "multiply("
00244             "const scalarRectangularMatrix& A, "
00245             "const DiagonalMatrix<scalar>& B, "
00246             "const scalarRectangularMatrix& C, "
00247             "scalarRectangularMatrix& answer)"
00248         )   << "A and B must have identical inner dimensions but A.m = "
00249             << A.m() << " and B.n = " << B.size()
00250             << abort(FatalError);
00251     }
00252 
00253     if (B.size() != C.n())
00254     {
00255         FatalErrorIn
00256         (
00257             "multiply("
00258             "const scalarRectangularMatrix& A, "
00259             "const DiagonalMatrix<scalar>& B, "
00260             "const scalarRectangularMatrix& C, "
00261             "scalarRectangularMatrix& answer)"
00262         )   << "B and C must have identical inner dimensions but B.m = "
00263             << B.size() << " and C.n = " << C.n()
00264             << abort(FatalError);
00265     }
00266 
00267     ans = scalarRectangularMatrix(A.n(), C.m(), scalar(0));
00268 
00269     for(register label i = 0; i < A.n(); i++)
00270     {
00271         for(register label g = 0; g < C.m(); g++)
00272         {
00273             for(register label l = 0; l < C.n(); l++)
00274             {
00275                 ans[i][g] += C[l][g] * A[i][l]*B[l];
00276             }
00277         }
00278     }
00279 }
00280 
00281 
00282 Foam::RectangularMatrix<Foam::scalar> Foam::SVDinv
00283 (
00284     const scalarRectangularMatrix& A,
00285     scalar minCondition
00286 )
00287 {
00288     SVD svd(A, minCondition);
00289     return svd.VSinvUt();
00290 }
00291 
00292 
00293 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd