initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / matrices / scalarMatrices / scalarMatricesTemplates.C
blob5710df6d9dc7f3a1ade81d791a9ef6c98a50514f
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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 "scalarMatrices.H"
28 #include "Swap.H"
30 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
32 template<class Type>
33 void Foam::solve
35     scalarSquareMatrix& tmpMatrix,
36     Field<Type>& sourceSol
39     label n = tmpMatrix.n();
41     // Elimination
42     for (register label i=0; i<n; i++)
43     {
44         label iMax = 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++)
49         {
50             if (mag(tmpMatrix[j][i]) > largestCoeff)
51             {
52                 iMax = j;
53                 largestCoeff = mag(tmpMatrix[iMax][i]);
54             }
55         }
57         if (i != iMax)
58         {
59             //Info<< "Pivoted on " << i << " " << iMax << endl;
61             for (register label k=i; k<n; k++)
62             {
63                 Swap(tmpMatrix[i][k], tmpMatrix[iMax][k]);
64             }
65             Swap(sourceSol[i], sourceSol[iMax]);
66         }
68         // Check that the system of equations isn't singular
69         if (mag(tmpMatrix[i][i]) < 1e-20)
70         {
71             FatalErrorIn("solve(scalarSquareMatrix&, Field<Type>& sourceSol)")
72                 << "Singular Matrix"
73                 << exit(FatalError);
74         }
76         // Reduce to upper triangular form
77         for (register label j=i+1; j<n; j++)
78         {
79             sourceSol[j] -= sourceSol[i]*(tmpMatrix[j][i]/tmpMatrix[i][i]);
81             for (register label k=n-1; k>=i; k--)
82             {
83                 tmpMatrix[j][k] -=
84                     tmpMatrix[i][k]*tmpMatrix[j][i]/tmpMatrix[i][i];
85             }
86         }
87     }
89     // Back-substitution
90     for (register label j=n-1; j>=0; j--)
91     {
92         Type ntempvec = pTraits<Type>::zero;
94         for (register label k=j+1; k<n; k++)
95         {
96             ntempvec += tmpMatrix[j][k]*sourceSol[k];
97         }
99         sourceSol[j] = (sourceSol[j] - ntempvec)/tmpMatrix[j][j];
100     }
104 template<class Type>
105 void Foam::solve
107     Field<Type>& psi,
108     const scalarSquareMatrix& matrix,
109     const Field<Type>& source
112     scalarSquareMatrix tmpMatrix = matrix;
113     psi = source;
114     solve(tmpMatrix, psi);
118 template<class Type>
119 void Foam::LUBacksubstitute
121     const scalarSquareMatrix& luMatrix,
122     const labelList& pivotIndices,
123     Field<Type>& sourceSol
126     label n = luMatrix.n();
128     label ii = 0;
130     for (register label i=0; i<n; i++)
131     {
132         label ip = pivotIndices[i];
133         Type sum = sourceSol[ip];
134         sourceSol[ip] = sourceSol[i];
135         const scalar* __restrict__ luMatrixi = luMatrix[i];
137         if (ii != 0)
138         {
139             for (label j=ii-1; j<i; j++)
140             {
141                 sum -= luMatrixi[j]*sourceSol[j];
142             }
143         }
144         else if (sum != pTraits<Type>::zero)
145         {
146             ii = i+1;
147         }
149         sourceSol[i] = sum;
150     }
152     for (register label i=n-1; i>=0; i--)
153     {
154         Type sum = sourceSol[i];
155         const scalar* __restrict__ luMatrixi = luMatrix[i];
157         for (register label j=i+1; j<n; j++)
158         {
159             sum -= luMatrixi[j]*sourceSol[j];
160         }
162         sourceSol[i] = sum/luMatrixi[i];
163     }
167 template<class Type>
168 void Foam::LUsolve
170     scalarSquareMatrix& matrix,
171     Field<Type>& sourceSol
174     labelList pivotIndices(matrix.n());
175     LUDecompose(matrix, pivotIndices);
176     LUBacksubstitute(matrix, pivotIndices, sourceSol);
180 // ************************************************************************* //