scalarMatricesTemplates.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "scalarMatrices.H"
00028 #include "Swap.H"
00029
00030
00031
00032 template<class Type>
00033 void Foam::solve
00034 (
00035 scalarSquareMatrix& tmpMatrix,
00036 Field<Type>& sourceSol
00037 )
00038 {
00039 label n = tmpMatrix.n();
00040
00041
00042 for (register label i=0; i<n; i++)
00043 {
00044 label iMax = i;
00045 scalar largestCoeff = mag(tmpMatrix[iMax][i]);
00046
00047
00048 for (register label j=i+1; j<n; j++)
00049 {
00050 if (mag(tmpMatrix[j][i]) > largestCoeff)
00051 {
00052 iMax = j;
00053 largestCoeff = mag(tmpMatrix[iMax][i]);
00054 }
00055 }
00056
00057 if (i != iMax)
00058 {
00059
00060
00061 for (register label k=i; k<n; k++)
00062 {
00063 Swap(tmpMatrix[i][k], tmpMatrix[iMax][k]);
00064 }
00065 Swap(sourceSol[i], sourceSol[iMax]);
00066 }
00067
00068
00069 if (mag(tmpMatrix[i][i]) < 1e-20)
00070 {
00071 FatalErrorIn("solve(scalarSquareMatrix&, Field<Type>& sourceSol)")
00072 << "Singular Matrix"
00073 << exit(FatalError);
00074 }
00075
00076
00077 for (register label j=i+1; j<n; j++)
00078 {
00079 sourceSol[j] -= sourceSol[i]*(tmpMatrix[j][i]/tmpMatrix[i][i]);
00080
00081 for (register label k=n-1; k>=i; k--)
00082 {
00083 tmpMatrix[j][k] -=
00084 tmpMatrix[i][k]*tmpMatrix[j][i]/tmpMatrix[i][i];
00085 }
00086 }
00087 }
00088
00089
00090 for (register label j=n-1; j>=0; j--)
00091 {
00092 Type ntempvec = pTraits<Type>::zero;
00093
00094 for (register label k=j+1; k<n; k++)
00095 {
00096 ntempvec += tmpMatrix[j][k]*sourceSol[k];
00097 }
00098
00099 sourceSol[j] = (sourceSol[j] - ntempvec)/tmpMatrix[j][j];
00100 }
00101 }
00102
00103
00104 template<class Type>
00105 void Foam::solve
00106 (
00107 Field<Type>& psi,
00108 const scalarSquareMatrix& matrix,
00109 const Field<Type>& source
00110 )
00111 {
00112 scalarSquareMatrix tmpMatrix = matrix;
00113 psi = source;
00114 solve(tmpMatrix, psi);
00115 }
00116
00117
00118 template<class Type>
00119 void Foam::LUBacksubstitute
00120 (
00121 const scalarSquareMatrix& luMatrix,
00122 const labelList& pivotIndices,
00123 Field<Type>& sourceSol
00124 )
00125 {
00126 label n = luMatrix.n();
00127
00128 label ii = 0;
00129
00130 for (register label i=0; i<n; i++)
00131 {
00132 label ip = pivotIndices[i];
00133 Type sum = sourceSol[ip];
00134 sourceSol[ip] = sourceSol[i];
00135 const scalar* __restrict__ luMatrixi = luMatrix[i];
00136
00137 if (ii != 0)
00138 {
00139 for (label j=ii-1; j<i; j++)
00140 {
00141 sum -= luMatrixi[j]*sourceSol[j];
00142 }
00143 }
00144 else if (sum != pTraits<Type>::zero)
00145 {
00146 ii = i+1;
00147 }
00148
00149 sourceSol[i] = sum;
00150 }
00151
00152 for (register label i=n-1; i>=0; i--)
00153 {
00154 Type sum = sourceSol[i];
00155 const scalar* __restrict__ luMatrixi = luMatrix[i];
00156
00157 for (register label j=i+1; j<n; j++)
00158 {
00159 sum -= luMatrixi[j]*sourceSol[j];
00160 }
00161
00162 sourceSol[i] = sum/luMatrixi[i];
00163 }
00164 }
00165
00166
00167 template<class Type>
00168 void Foam::LUsolve
00169 (
00170 scalarSquareMatrix& matrix,
00171 Field<Type>& sourceSol
00172 )
00173 {
00174 labelList pivotIndices(matrix.n());
00175 LUDecompose(matrix, pivotIndices);
00176 LUBacksubstitute(matrix, pivotIndices, sourceSol);
00177 }
00178
00179
00180
Copyright © 2000-2009 OpenCFD Ltd