Statistics
| Branch: | Revision:

root / synthbench / euroben-dm / mod2cr / .svn / text-base / cg.f.svn-base @ 0:839f52ef7657

History | View | Annotate | Download (3.1 kB)

1
      Subroutine cg( n1, n2, n3, m, mp, a, al, b, x, gamma, maxit,
2
     &               conv, tol, prec )
3
! ---------------------------------------------------------------------
4
! --- cg does an iterative solve of ax = b.
5
!     Real(l_) a(n1*n2*n3,0:3) : Main diagonal and the 3 upper
6
!                                diagonals of the matrix.
7
!     Real(l_) al(n1*n2*n3,1:3): The 3 lower diagonals. Identical to
8
!                                the upper diagonals but used for
9
!                                efficiency reasons.
10
!     Real(l_) b(n1*n2*n3)     : The right hand side.
11
!     Real(l_) x(n1*n2*n3)     : On input the initial estimate of the
12
!                                solution, on output the solution
13
!                                (hopefully).
14
!     Real(l_) gamma(m+1)      : Polynomial coefficients used in the
15
!                                preconditioning.
16
!     Integer  maxit           : On input the maximum number of
17
!                                iterations allowed.
18
!     Real(l_) conv(maxit)     : Residuals of the sequence of iter's.
19
!     Real(l_) tol             : Tolerance used as a stop criterion.
20
!     External prec            : Name of subroutine that implements the
21
!                                preconditioner.
22
! ---------------------------------------------------------------------
23
      Use         numerics
24
      Use         floptime
25
      Use         mpi_module
26
      Implicit    None
27

    
28
      Integer  :: n1, n2, n3, m, mp
29
      Real(l_) :: a(m,0:3), al(m,1:3), b(n1*n2*n3), x(n1*n2*n3)
30
      Real(l_) :: gamma(mp+1)
31
      Integer  :: maxit
32
      Real(l_) :: conv(maxit)
33
      Real(l_) :: tol
34
      External    prec
35

    
36
      Integer  :: i, it, its, j, ntot
37
      Real(l_) :: ap(n1*n2*n3), p(n1*n2*n3), r(n1*n2*n3)
38
      Real(l_) :: alpha, beta, nr0, nrm, nnrm, pap, tol2
39
      Real(l_) :: dotpr
40
      External    dotpr
41
! ---------------------------------------------------------------------
42
      ntot = n1*n2*n3
43
      tol2 = tol*tol
44
      Call sym7mxv( n1, n2, n3, m, a, al, x, r )
45
      r(lb:gub) = b(lb:gub) - r(lb:gub)
46
! ---------------------------------------------------------------------
47
! --- Precondition of initial residual.
48

    
49
      Call prec( n1, n2, n3, m, mp, a, al, nr0, r, p, gamma )
50
      nrm = nr0
51
! ---------------------------------------------------------------------
52
! --- Iterate at most maxit times.  
53

    
54
      its = maxit
55
      Do i = 1, its
56
         Call sym7mxv( n1, n2, n3, m, a, al, p, ap )
57
         pap = dotpr( ntot, p, ap )
58
         alpha = nrm/pap
59

    
60
         x(lb:gub) = x(lb:gub) + alpha*p(lb:gub)
61
         r(lb:gub) = r(lb:gub) - alpha*ap(lb:gub)
62

    
63
         Call prec( n1, n2, n3, m, mp, a, al, nnrm, r, ap, gamma )
64
         conv(i) = Sqrt( nnrm )
65
         If ( nnrm < nr0*tol2 ) Then
66
            maxit = i
67
            Go To 1000
68
         End If
69
         beta = nnrm/nrm
70
         nrm = nnrm
71
         p(lb:gub) = ap(lb:gub) + beta*p(lb:gub)
72
      End Do
73
!     Print *, 'CG: no convergence in ', maxit, ' iterations.'
74
 1000 flops = flops + maxit*( 6*m + 4 ) + m + 1
75
! ---------------------------------------------------------------------
76
      End Subroutine cg