OpenFOAM Matrix Solution
OpenFOAM Matrix Solution
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 1/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Matrix solver
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 2/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Matrix solver
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 3/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Matrix solver
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 4/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Solving an equation
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 5/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 6/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Correspondence
Mesh ⇔ Au = b
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 7/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Mesh example
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 8/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 9/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Matrix structure
Matrix according to the mesh example
10 volumes/cells
36 nodes/corners
47 faces, 13 internal faces
a0,0 a0,1 0 0 0 a0,5 0 0 0 0
a1,0 a1,1 a1,2 0 0 0 a1,6 0 0 0
0 a2,1 a2,2 a2,3 0 0 0 a2,7 0 0
0 0 a 3,2 a 3,3 a3,4 0 0 0 a3,8 0
0 0 0 a 4,3 a4,4 0 0 0 0 a4,9
a5,0 0 0 0 0 a5,5 a5,6 0 0 0
0 a6,1 0 0 0 a6,5 a6,6 a6,7 0 0
0 0 a7,2 0 0 0 a7,6 a7,7 a7,8 0
0 0 0 a8,3 0 0 0 a8,7 a8,8 a8,9
0 0 0 0 a9,4 0 0 0 a9,8 a9,9
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 10/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
lduMatrix
Matrix according to the mesh example
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 11/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Run order
Matrix according to the mesh example
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 12/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Addressing
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 13/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Addressing
Matrix according to the mesh example
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 14/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 15/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Matrix manipulation
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 16/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Test case
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 17/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Test case
Expectations
What do we expect?
myLaplacianFoam
Clone solver
> run
> mkdir programming
> cd programming
> cp -r $FOAM_SOLVERS/basic/laplacianFoam myLaplacianFoam
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 19/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Adapt code
myLaplacianFoam.c
Change name
> mv laplacianFoam.C myLaplacianFoam.C
In myLaplacianFoam.C change
while (simple.loop())
{
Info<< "Time = " << runTime.timeName()
while (runTime.loop())
<< nl << endl;
{
fvScalarMatrix
while (simple.correctNonOrthogonal())
TEqn(fvm::laplacian(k, T));
{
TEqn.solve();
solve
(
forAll(T, cellI)
fvm::ddt(T)
{
);
- fvm::laplacian(DT, T) ⇒ Info<< "X = " <<
mesh.C()[cellI].component(vector::X)
}
<< ", T = " << T[cellI] << endl;
#include "write.H"
}
Info<< "ExecutionTime = "
<< runTime.elapsedCpuTime()
runTime.writeAndEnd();
<< " s"
}
<< " ClockTime = "
<< runTime.elapsedClockTime()
<< " s"
<< nl << endl;
}
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 20/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Adapt code
createFields.C
In create.C change
Info<< "Reading diffusivity DT\n" << endl; Info<< "Reading diffusivity k\n" << endl;
dimensionedScalar DT
(
⇒ dimensionedScalar k
(
transportProperties.lookup("DT") transportProperties.lookup("k")
); );
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 21/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Result
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 22/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Extension
Change in myLaplacianFoam.C
#include "fvCFD.H"
#include "simpleMatrix.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
while (runTime.loop())
{
fvScalarMatrix TEqn(fvm::laplacian(k, T));
TEqn.solve();
forAll(T, cellI)
{
Info<< "X = " <<
mesh.C()[cellI].component(vector::X)
<< ", T = " << T[cellI] << endl;
}
#include "writeMatCoeffs.H"
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 23/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Adapt code
Create writeMatCoeffs.C
Investigating the coefficient matrix
// Assigning contribution from BC
label NC = mesh.nCells(); //Number of cells
forAll(T.boundaryField(), patchI)
simpleMatrix<scalar> A(NC); //Coeff.matrix
{
// Initialization of matrix
const fvPatch &pp =
for(label i=0; i<NC; i++)
T.boundaryField()[patchI].patch();
{
forAll(pp, faceI)
A.source()[i] = 0.0;
{
for(label j=0; j<NC; j++)
label cellI = pp.faceCells()[faceI];
{
A[cellI][cellI]
A[i][j] = 0.0;
+= TEqn.internalCoeffs()[patchI][faceI];
}
A.source()[cellI]
}
+= TEqn.boundaryCoeffs()[patchI][faceI];
// Assigning diagonal coefficients
}
for(label i=0; i<NC; i++)
}
{
Info << "\n==Coefficients of Matrix A=="
A[i][i] = TEqn.diag()[i];
<< endl;
}
for(label i=0; i<NC; i++)
// Assigning off-diagonal coefficients
{
for(label faceI=0;
for(label j=0; j<NC; j++)
faceI<TEqn.lduAddr().lowerAddr().size(); faceI++)
{
{
Info<< A[i][j] << " ";
label l = TEqn.lduAddr().lowerAddr()[faceI];
}
label u = TEqn.lduAddr().upperAddr()[faceI];
Info<< A.source()[i] << endl;
A[l][u] = TEqn.upper()[faceI];
}
A[u][l] = TEqn.upper()[faceI];
Info<< "\n==> Solution: " << A.solve()
}
<< endl;
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 24/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
==Coefficient Matrix==
-300 100 0 0 0 -20000
100 -200 100 0 0 0
0 100 -200 100 0 0
0 0 100 -200 100 0
0 0 0 100 -300 -100000
Solution: 5(140 220 300 380 460)
End
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 25/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Linear systems
Iterative solvers
The system of equation in OpenFOAM is solved by iterative
matrix algorithms
Based on the starting vector x0 the iteration rule
xk+1 = Axk
is applied
The iteration stops, when the residual is smaller than a prescribed
tolerance tol:
Preconditioning
Goal
Enhance matrix properties in order to optimize condition number
(cond(A) close to 1)
⇒ faster convergence of the solver
One is looking for a matrix M with
M−1 Ax = M−1 b
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 27/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Preconditioner in OpenFOAM
diagonalPreconditioner
for symmetric & nonsymmetric matrices (not very effective)
DICPreconditioner
Diagonal Incomplete Cholesky preconditioner
for symmetric matrices
DILUPreconditioner
Diagonal Incomplete LU preconditioner
for nonsymmetric matrices
FDICPreconditioner
Fast Diagonal Incomplete Cholesky preconditioner
GAMGPreconditioner
Geometric Agglomerated algebraic MultiGrid preconditioner
noPreconditioner
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 28/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Preconditioners
Diagonal preconditioner M = D
Uses the diagonal as preconditioner. Not very efficient.
Incomplete LU preconditioner A = LU + R, M = LU
Apply Gaussian elimination algorithm, but only on allowed pat-
tern ⇒ incomplete LU factorization called ILU.
Reduce in the for-loops the indices to the indices with
– allowed pattern, e.g. ILU(0) for pattern of A
– values that are not too small, ILUT for ILU with treshold
Leads to approximate LU factorization
A = LU + R, preconditioner M = LU
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 29/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
BICCG
Diagonal incomplete LU preconditioned BiCG solver
diagonalSolver
with
r0 = b − Ax0 , (b − Axm ) ⊥ Lm , Km , Lm ∈ Rm ⊂ Rn
1
Aleksey Nikolaevich Krylov (1863 - 1945), russian naval engineer and mathe-
matician
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 31/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Krylov method
Motivation
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 32/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Krylov method
The iteration approach
Ax = b
xj ∈ x0 + Kj , Kj (A, r0 ), r0 = b − Ax0 .
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 33/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Krylov methods
Remarks
Kj (A, b) = span[v0 , v1 , . . . , vj ]
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 34/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Krylov methods
Standard methods
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 35/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Ax = b
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 36/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Conjugated Gradients
Why????
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 37/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Conjugated Gradients
∇f (x) = Ax − b
Hf (x) = A
with
the gradient ∇f = (∂1 f , . . . , ∂n f )
the Hessian Hf = (∂ij2 f )ij .
Since all higher order derivatives vanish, we can rewrite f as its
Taylor polynomial.
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 38/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Taylor polynomial
1
f (x) = f (y) − (x − y)T ∇f (y) + (x − y)T Hf (y)(x − y)
2
1
= f (y) − (x − y) (Ay − b) + (x − y)T A(x − y)
T
2
T
≥ f (y) − (x − y) (Ay − b)
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 39/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 40/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
xj+1 = xj + αj pj
Question?
How to chose α.
How to choose pj
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 41/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Assume for a moment that we already have chosen the search direc-
tion pj .
With given pj the best possible step-length can be determined
by finding the minimum of f along xj + αpj .
Setting ψ(α) = f (xj + αpj ) the necessary condition for a mini-
mum is
0 = ψ 0 (α) = pT
j ∇f (xj + αpj )
= pT
j [A(xj + αpj ) − b]
= pT T T
j Axj + αpj Apj − pj b.
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 42/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Step-length αj
Theorem
Assume that the search direction pj 6= 0 has already been determined.
Then we can compute the optimal step-length α for the new iteration
xj+1 = xj + αj pj
by
pT
j (b − Axj ) pT
j rj
αj = = (1)
pT
j Apj pT
j Apj
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 43/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
p0 = e1 , p1 = e2 , . . . , pn−1 = en , pn = e1 , pn+1 = e2 , . . .
Than we have
n
X
eT
i Aei = aii and eT
i (Ax − b)ei = aij xj − bi
j=1
for k = 0, 1, . . . , n − 1.
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 44/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Problematic:
Only the k-th component of the vector xk is updated.
If we consider one cycle, we get
n
1 X X
xk = bk − akj xjk − akj xj0 ,
akk
j<k
xik = xik−1 , k 6= i
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 45/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
pj = −∇f (xj )
= −(Axj − b)
= rj
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 46/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Steepest Descent
Algorithm
Choose x1 and set p1 =
b − Ax1
for j = 1, 2, . . . do
pT rj
αj = pTjAp
j j
xj+1 = xj + αj pj
pj+1 = b−Axj+1
end
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 47/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
0 = pT T
j (Axj+1 − b) = −pj pj+1
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 48/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 49/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Example
Convergence history:
j−1 9
xj = (0.8)
(−1)j−1
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 50/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Idea:
Determine search direction pk such, that pk is
w.r.t. the previous directions p0 , p1 , . . . pk+1
Definition
Two vectors x, y ∈ Rn are called
A-conjugated
if
xT Ay = 0
holds.
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 51/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 52/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Theorem
Assume p0 , p1 , . . . , pn−1 6= 0 are pairwise A-conjugated vectors.
Then, the scheme
xk+1 = xk + αk pk
with
(rk )T rk
αk = −
(pk )T Apk
converges in at most n steps against the exact solution.
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 53/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Conjugated Gradients
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 54/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Conjugated Gradients
Remarks
It can be proved, that the vectors pk are pair-wise A-conjugated.
Theoretically, after at most n steps the solution can be computed.
Due to rounding errors in practice you will not get the solution
after n steps.
In practice we have n 1. Therefore, the CG method is used
as an iterative method.
In each iteration step there are
1 matrix-vector product,
2 scalar products
3 scalar multiplications
necessary.
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 55/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Theorem
The k-th iteration xk of the CG method minimizes the functional
f (·) w.r.t. the subspace
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 56/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 57/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
CG method: Example
Consider Ax = b with
λ1 0 λ1
A= , b= .
0 λ2 λ2
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 58/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
BiCG solver
Bi-Conjugated Gradient method
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 59/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 60/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 61/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 62/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 63/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
BiCG method
tion
The method has no minimization properties for the iteration
Tutorial PBiCGstab
Motivation
We have just seen that the method has some disadvantages.
We consider the BiCGstab method from van der Vorst [8] which
avoids these disadvantages.
In general, BiCGstab has smoother convergence properties
Multiplication with AT is not necessary.
Furthermore, we want to know how to expand a linear system
solver in OpenFOAM.
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 65/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Tutorial PBiCGstab
Goal
Goal
Extension of the BiCG solver (PBiCG.C) to the stabilized version
BiCGStab
Proceeding
Change to the directory $WM_PROJECT_USER_DIR$
Clone the folder PBICG
cp -r $FOAM_SRC/OpenFOAM/matrices/lduMatrice/solver/PBICG .
mv PBiCG PBiCGSTAB
cd PBiCGSTAB
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 66/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Tutorial BiCGstab
Pseudo-code BiCGstab (from: Meister [9])
Choose x0 ∈ Rn und > 0,
r0 = p0 := b − Ax0 , rP0 = pP0 := PL r0 , ρP0 := (rP0 , rP0 )2 , j := 0
While krj k2 >
vj := ApPj , vPj := PL vj
ρPj
αjP := , sj := rj − αjP vj , sPj := PL sj
(vPj , rP0 )2
tj := AsPj , tPj := PL tj
(tPj , sPj )2
ωjP :=
(tPj , tPj )2
xPj+1 := xPj + αjP pPj + ωjP sPj
rj+1 := sj − ωjP tj , rPj+1 := sPj − ωjP tPj
αjP ρPj+1
ρPj+1 := (rPj+1 , rP0 )2 , βjP := P P
ω j ρj
pPj+1 := rPj+1 + βjP (pPj − ωjP vPj ), j := j + 1
xj = PR xPj
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 67/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Tutorial BiCGstab
Procedure
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 68/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Tutorial BiCGstab
Make
LIB = \$(FOAM_USER_LIBBIN)/PBiCGSTAB
options
EXE_INC = -I$(OBJECTS_DIR)
LIB_LIBS = \
$(FOAM_LIBBIN)/libOSspecific.o \
-L$(FOAM_LIBBIN)/dummy -lPstream \
-lz
Or add the entries to the Make file for our common user-defined
library libmylib
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 69/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Tutorial BiCGstab
PBiCGSTAB.C -- ::solve I
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::lduMatrix::solverPerformance Foam::PBiCGSTAB::solve
(
scalarField& psi,
const scalarField& source,
const direction cmpt
) const
{
// --- Setup class containing solver performance data
lduMatrix::solverPerformance solverPerf
(
lduMatrix::preconditioner::getName(controlDict_) + typeName,
fieldName_
);
Tutorial BiCGstab
PBiCGSTAB.C -- ::solve II
// --- Calculate initial residual
scalarField rA(source - wA);
scalar* __restrict__ rAPtr = rA.begin();
scalarField rP(nCells);
scalar* __restrict__ rPPtr = rP.begin();
scalarField r0(nCells);
scalar* __restrict__ r0Ptr = r0.begin();
scalarField sP(nCells);
scalar* __restrict__ sPPtr = sP.begin();
scalarField tP(nCells);
scalar* __restrict__ tPPtr = tP.begin();
if (lduMatrix::debug >= 2)
{
Info<< " Normalisation factor = " << normFactor << endl;
}
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 71/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Tutorial BiCGstab
PBiCGSTAB.C -- ::solve III
// --- Check convergence, solve if not converged
if (!solverPerf.checkConvergence(tolerance_, relTol_))
{
// --- Select and construct the preconditioner
autoPtr<lduMatrix::preconditioner> preconPtr =
lduMatrix::preconditioner::New
(
*this,
controlDict_
);
// --- precondition r0
preconPtr->precondition(rP, rA, cmpt);
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 72/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Tutorial BiCGstab
PBiCGSTAB.C -- ::solve IV
// --- Calculate alpha=rPr0/vPr0
scalar alpha2 = gSumProd(vP, r0);
scalar alpha = rPr0/alpha2;
// --- Precondition s
preconPtr->precondition(sP, sA, cmpt);
Tutorial BiCGstab
PBiCGSTAB.C -- ::solve V
solverPerf.finalResidual() = gSumMag(rA)/normFactor;
} while
(
solverPerf.nIterations()++ < maxIter_
&& !(solverPerf.checkConvergence(tolerance_, relTol_))
);
}
return solverPerf;
}
// ************************************************************************* //
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 74/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Tutorial BiCGstab
Test case pitzDaily
Solver simpleFoam
Settings
"U|k|epsilon|R"
{
solver PBiCGSTAB;
preconditioner DILU;
tolerance 1e-05;
relTol 0.1;
}
BiCG analogue
Convergence after
BiCG 952 Iterations
BiCGSTAB 754 Iterations
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 75/77
Mesh & matrix Sparse adressing Preconditioner Krylov-solvers CG BiCG Tutorial:PBiCGSTAB
Tutorial BiCGstab
Comparison BiCG/BiCGSTAB – Residuals
Literature I
[1] Hirt, C. W., Nichols, B. D.Volume of Fluid (VOF) Method for the Dynamic sof Free Boundaries, Journal of
Computational Physics, 39: 201-225-65, 1981.
[2] Ubbink, O, Numerical prediction of two fluid systems with sharp interfaces, Ph.D Thesis, Imperial College of
Science, Technology and Medicine, London, 1997.
[3] Rusche, H. Computational Fluid Dynamics of Dispersed Two-Phase Flows at High Phase Fractions, Ph.D Thesis,
Imperial College of Science, Technology and Medicine, London, 2002.
[4] Damián, S. M. Description and utilization of interFoam multiphase solver, -Final Work-Computational Fluid
Dynamics. http://infofich.unl.edu.ar/upload/5e6dfd7ff282e2deabe7447979a16d49b0b1b675.pdf
[5] Eslamdoost, A. Forced Roll Motion of a 2D Box and Interaction with Free-Surface, PhD course in CFD with
OpenSource software, Chalmers University, 2009 http://www.tfd.chalmers.se/~hani/kurser/OS_CFD_2009/
ArashEslamdoost/RollMotionofaBoxandInteractionwithFreeSurface.pdf
[6] Issa, R. I. Solution of the implicitly discretised fluid flow equations by operator splitting, Journal of Computational
Physics, 62: 40-65, 1985.
[7] Van Leer, B. Towards the ultimate conservative difference scheme III. Upstream-centered finite-difference
schemes for ideal compressible flow, J. Comp. Phys. 23 (3): 263–275, 1977.
[8] van der Vorst, H. A. BI-CGSTAB: A fast and smoothly converging variant of BI-CG for the solution of nonsym-
metric linear systems, SIAM J. Sci. Stat. Comput., 13: 631-644, 1992.
move http://www.move-csc.de | Dr. Thorsten Grahs — Linear solvers & preconditioners | 77/77