1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "scalarSquareMatrix.H"
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 Foam::scalarSquareMatrix::scalarSquareMatrix()
34 Foam::scalarSquareMatrix::scalarSquareMatrix(const label mSize)
36 SquareMatrix<scalar>(mSize, 0.0)
40 Foam::scalarSquareMatrix::scalarSquareMatrix
46 SquareMatrix<scalar>(mSize, v)
50 Foam::scalarSquareMatrix::scalarSquareMatrix(const scalarSquareMatrix& matrix)
52 SquareMatrix<scalar>(matrix)
56 Foam::scalarSquareMatrix::scalarSquareMatrix(Istream& is)
58 SquareMatrix<scalar>(is)
62 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
64 void Foam::scalarSquareMatrix::LUDecompose
66 scalarSquareMatrix& matrix,
67 labelList& pivotIndices
73 for (register label i=0; i<n; i++)
75 scalar largestCoeff = 0.0;
77 const scalar* __restrict__ matrixi = matrix[i];
79 for (register label j=0; j<n; j++)
81 if ((temp = mag(matrixi[j])) > largestCoeff)
87 if (largestCoeff == 0.0)
91 "scalarSquareMatrix::LUdecompose"
92 "(scalarSquareMatrix& matrix, labelList& rowIndices)"
93 ) << "Singular matrix" << exit(FatalError);
96 vv[i] = 1.0/largestCoeff;
99 for (register label j=0; j<n; j++)
101 scalar* __restrict__ matrixj = matrix[j];
103 for (register label i=0; i<j; i++)
105 scalar* __restrict__ matrixi = matrix[i];
107 scalar sum = matrixi[j];
108 for (register label k=0; k<i; k++)
110 sum -= matrixi[k]*matrix[k][j];
117 scalar largestCoeff = 0.0;
118 for (register label i=j; i<n; i++)
120 scalar* __restrict__ matrixi = matrix[i];
121 scalar sum = matrixi[j];
123 for (register label k=0; k<j; k++)
125 sum -= matrixi[k]*matrix[k][j];
131 if ((temp = vv[i]*mag(sum)) >= largestCoeff)
138 pivotIndices[j] = iMax;
142 scalar* __restrict__ matrixiMax = matrix[iMax];
144 for (register label k=0; k<n; k++)
146 Swap(matrixj[k], matrixiMax[k]);
152 if (matrixj[j] == 0.0)
159 scalar rDiag = 1.0/matrixj[j];
161 for (register label i=j+1; i<n; i++)
163 matrix[i][j] *= rDiag;
170 Foam::scalarSquareMatrix Foam::scalarSquareMatrix::LUinvert() const
172 scalarSquareMatrix luMatrix = *this;
174 scalarSquareMatrix luInvert(luMatrix.n());
175 scalarField column(luMatrix.n());
177 labelList pivotIndices(luMatrix.n());
179 LUDecompose(luMatrix, pivotIndices);
181 for (label j = 0; j < luMatrix.n(); j++)
183 for (label i = 0; i < luMatrix.n(); i++)
190 LUBacksubstitute(luMatrix, pivotIndices, column);
192 for (label i = 0; i < luMatrix.n(); i++)
194 luInvert[i][j] = column[i];
202 // ************************************************************************* //