1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM 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 2 of the License, or (at your
14 option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "scalarMatrix.H"
30 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 void Foam::scalarMatrix::solve
35 Matrix<scalar>& tmpMatrix,
39 label n = tmpMatrix.n();
42 for (register label i=0; i<n; i++)
45 scalar largestCoeff = mag(tmpMatrix[iMax][i]);
47 // Swap entries around to find a good pivot
48 for (register label j=i+1; j<n; j++)
50 if (mag(tmpMatrix[j][i]) > largestCoeff)
53 largestCoeff = mag(tmpMatrix[iMax][i]);
59 //Info<< "Pivoted on " << i << " " << iMax << endl;
61 for (register label k=i; k<n; k++)
63 Swap(tmpMatrix[i][k], tmpMatrix[iMax][k]);
65 Swap(sourceSol[i], sourceSol[iMax]);
68 // Check that the system of equations isn't singular
69 if (mag(tmpMatrix[i][i]) < 1e-20)
71 FatalErrorIn("scalarMatrix::solve()")
76 // Reduce to upper triangular form
77 for (register label j=i+1; j<n; j++)
79 sourceSol[j] -= sourceSol[i]*(tmpMatrix[j][i]/tmpMatrix[i][i]);
81 for (register label k=n-1; k>=i; k--)
84 tmpMatrix[i][k]*tmpMatrix[j][i]/tmpMatrix[i][i];
90 for (register label j=n-1; j>=0; j--)
92 T ntempvec = pTraits<T>::zero;
94 for (register label k=j+1; k<n; k++)
96 ntempvec += tmpMatrix[j][k]*sourceSol[k];
99 sourceSol[j] = (sourceSol[j] - ntempvec)/tmpMatrix[j][j];
105 void Foam::scalarMatrix::solve(Field<T>& psi, const Field<T>& source) const
107 Matrix<scalar> tmpMatrix = *this;
109 solve(tmpMatrix, psi);
114 void Foam::scalarMatrix::LUBacksubstitute
116 const Matrix<scalar>& luMatrix,
117 const labelList& pivotIndices,
121 label n = luMatrix.n();
125 for (register label i=0; i<n; i++)
127 label ip = pivotIndices[i];
128 T sum = sourceSol[ip];
129 sourceSol[ip] = sourceSol[i];
130 const scalar* __restrict__ luMatrixi = luMatrix[i];
134 for (label j=ii-1; j<i; j++)
136 sum -= luMatrixi[j]*sourceSol[j];
139 else if (sum != pTraits<T>::zero)
147 for (register label i=n-1; i>=0; i--)
149 T sum = sourceSol[i];
150 const scalar* __restrict__ luMatrixi = luMatrix[i];
152 for (register label j=i+1; j<n; j++)
154 sum -= luMatrixi[j]*sourceSol[j];
157 sourceSol[i] = sum/luMatrixi[i];
163 void Foam::scalarMatrix::LUsolve
165 Matrix<scalar>& matrix,
169 labelList pivotIndices(matrix.n());
170 LUDecompose(matrix, pivotIndices);
171 LUBacksubstitute(matrix, pivotIndices, sourceSol);
175 // ************************************************************************* //