-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCGOp2.f90
44 lines (38 loc) · 1.26 KB
/
CGOp2.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Subroutine CGOp(a,b)
! Matrix Free Conjugate Gradient Method
! Input:
! a -> Input Matrix
! Output:
! b -> Solution
! List of Modifications:
! -> 10.03.2014: Routine Implementation (Rafael Cavalcanti)
! -> 10.03.2014: Fortran Sintax (Rafael Cavalcanti)
! Programmer: Michael Dumbser
Use MeshVars, Only: nElem
Implicit None
Double Precision:: a(nElem), b(nElem), r(nElem), Ab(nElem), pCG(nElem), v(nElem)
Double Precision:: alpha, tol, lambda, alphanew
Integer:: k, N
N = nElem
b = a ! Initial guess
Call MatOp(b,Ab)
r = b - Ab
pCG = r ! Steepest Descent Direction
alpha = Dot_Product(r,r) ! Square of the norm of r
tol = 1e-14 ! Tolerance
Do k = 1,N
If ( sqrt(alpha) < tol ) Then
! System has been solved
!Print*, 'The system has been solved with: ',k, 'CGOp iterations'
Return
EndIf
Call MatOp(pCG,v)
lambda = alpha/Dot_Product(pCG,v)
b = b + lambda*pCG
r = r - lambda*v
alphanew = Dot_Product(r,r)
pCG = r + alphanew/alpha*pCG
alpha = alphanew
EndDo
Return
End Subroutine CGOp