Changes in src/solvers/pcg.F90 [b378423:08dfb5f]
- File:
-
- 1 edited
-
src/solvers/pcg.F90 (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/solvers/pcg.F90
rb378423 r08dfb5f 41 41 #endif 42 42 43 integer,save :: coarseid=044 43 private :: & 45 44 preconditioner, & … … 50 49 51 50 contains 52 53 !--------------------------------------------54 ! Preconditioned conjugate gradient method55 !--------------------------------------------56 subroutine pcg (A, b, x, Msh, tol_, maxit_, &57 x0_, solinf, resvects_, CoarseMtx_)58 implicit none59 60 type(SpMtx),intent(in out) :: A ! System matrix (sparse)61 float(kind=rk),dimension(:),pointer :: b ! RHS62 float(kind=rk),dimension(:),pointer :: x ! Solution63 ! Mesh - aux data for Ax operation64 type(Mesh), intent(in) :: Msh65 66 ! Optional arguments67 ! Tolerance of the method68 real(kind=rk), intent(in), optional :: tol_69 ! Max number of iterations70 integer, intent(in), optional :: maxit_71 ! Initial guess72 float(kind=rk), dimension(:), intent(in), optional :: x0_73 ! Solution statistics74 type(ConvInf), intent(in out), optional :: solinf75 ! Fill in the 'resvect' or not76 logical, intent(in), optional :: resvects_77 type(SpMtx),optional :: CoarseMtx_ ! Coarse matrix78 79 real(kind=rk) :: tol ! Tolerance80 integer :: maxit ! Max number of iterations81 logical :: resvects=.false.82 83 !real(kind=rk), dimension(size(b)) :: r, p, q, m, z, b_k84 real(kind=rk),dimension(:),pointer :: r, p, q, m, z, b_k85 real(kind=rk) :: rho_curr, rho_prev, init_norm, res_norm86 real(kind=rk) :: alpha, beta, tmp, ratio_norm87 integer :: iter,nfreds88 integer :: ierr89 ! + test90 float(kind=rk), dimension(:), pointer :: arr_copy, gv_tmp91 92 !!$ real(kind=rk) :: r_max93 94 write(stream,'(/a)') 'Preconditioned conjugate gradient:'95 96 if (size(b) /= size(x)) &97 call DOUG_abort('[pcg] : SEVERE : size(b) /= size(x)',-1)98 99 !!$ if (present(x0_))100 !!$ ! use method with initial guess101 102 103 nfreds=size(b)104 allocate(r(nfreds))105 allocate(p(nfreds))106 allocate(q(nfreds))107 allocate(m(nfreds))108 allocate(z(nfreds))109 allocate(b_k(nfreds))110 111 112 tol = sctls%solve_tolerance113 if (present(tol_)) &114 tol = tol_115 116 maxit = sctls%solve_maxiters117 if (maxit==-1) maxit=100000118 if (present(maxit_)) &119 maxit = maxit_120 121 if (present(resvects_).and.(.not.present(solinf))) &122 call DOUG_abort('[pcg] : SEVERE : "resvects_" must be given along'//&123 ' with "solinf".',-1)124 125 ! Allocate convergence info structure126 if (present(solinf).and.(.not.present(resvects_))) then127 call ConvInf_Init(solinf)128 else if (present(solinf)) then129 if (present(resvects_).and.(resvects_.eqv.(.true.))) then130 call ConvInf_Init(solinf, maxit)131 resvects = .true.132 end if133 end if134 135 ! Initialise auxiliary data structures136 ! to assist with pmvm137 call pmvmCommStructs_init(A, Msh)138 139 ! + test140 if (ismaster()) &141 allocate(gv_tmp(Msh%ngf))142 allocate(arr_copy(Msh%nlf))143 144 ! Build preconditioner145 ! call preconditioner(A, m, CoarseMtx_)146 !!$ ! + test147 !!$ arr_copy = m148 !!$ call Vect_newToOldPerm(arr_copy)149 !!$ gv_tmp = 0.0_rk150 !!$ call Vect_Gather(arr_copy, gv_tmp, Msh)151 !!$ if (ismaster()) &152 !!$ call Vect_Print(gv_tmp, 'm ::')153 154 !!$ ! + test155 !!$ !b = 1.0_rk156 !!$ arr_copy = b157 !!$ call Vect_newToOldPerm(arr_copy)158 !!$ call Vect_Print(arr_copy, 'b local (original ordering) ::')159 !!$ gv_tmp = 0.0_rk160 !!$ call Vect_Gather(arr_copy, gv_tmp, Msh)161 !!$ if (ismaster()) &162 !!$ call Vect_Print(gv_tmp, 'b global (original ordering) ::')163 164 !!$ ! + test165 !!$ arr_copy = x166 !!$ call Vect_newToOldPerm(arr_copy)167 !!$ call Vect_Print(arr_copy, 'x local (original ordering) ::')168 !!$ gv_tmp = 0.0_rk169 !!$ call Vect_Gather(arr_copy, gv_tmp, Msh)170 !!$ if (ismaster()) &171 !!$ call Vect_Print(gv_tmp, 'x global (original ordering) ::')172 173 !r = b - SpMtx_pmvm(A, x, Msh)174 call SpMtx_pmvm(r,A,x,Msh)175 r = b - r176 !call Vect_Print(r,'initial residual')177 178 ! + test179 !!$ arr_copy = r180 !!$ call Vect_newToOldPerm(arr_copy)181 !!$ gv_tmp = 0.0_rk182 !!$ call Vect_Gather(arr_copy, gv_tmp, Msh)183 !!$ if (ismaster()) &184 !!$ call Vect_Print(gv_tmp, 'r ::')185 186 init_norm = Vect_dot_product(r,r)187 if (init_norm == 0.0) &188 init_norm = 1.0189 190 write(stream,'(a,i6,a,e8.2)') 'maxit = ',maxit,', tol = ',tol191 write(stream,'(a,e22.15)') 'init_norm = ', dsqrt(init_norm)192 193 iter = 1194 ratio_norm = 1.0_rk195 do while((ratio_norm > tol*tol).and.(iter <= maxit))196 call msolve(m,r,z)197 !if (present(CoarseMtx_)) then198 ! call preconditioner(z,A,r,CoarseMtx_)199 ! !z=r200 !else201 ! call preconditioner(z,A,r)202 !endif203 204 !call Vect_Print(z,'preconditioner')205 206 ! compute current rho207 rho_curr = Vect_dot_product(r,z)208 if (iter == 1) then209 p = z210 else211 beta = rho_curr / rho_prev212 p = z + beta * p213 end if214 !q = SpMtx_pmvm(A, p, Msh)215 call SpMtx_pmvm(q,A,p,Msh)216 ! compute alpha217 alpha = rho_curr / Vect_dot_product(p,q)218 x = x + alpha * p219 r = r - alpha * q220 rho_prev = rho_curr221 ! check222 res_norm = Vect_dot_product(r,r)223 ratio_norm = dsqrt(res_norm) / dsqrt(init_norm)224 !!$ ratio_norm = res_norm / init_norm225 if (ismaster()) &226 write(stream, '(i5,a,e22.15)') iter,': res_norm=',dsqrt(res_norm)227 !write(stream, '(i5,a,e22.15,e22.15)') iter,': dsqrt(ratio_norm)=',&228 !dsqrt(ratio_norm), dsqrt(res_norm)229 iter = iter + 1230 end do231 232 ! Deallocate auxiliary data structures233 ! helped to assist with pmvm234 call pmvmCommStructs_destroy()235 236 deallocate(b_k)237 deallocate(z)238 deallocate(m)239 deallocate(q)240 deallocate(p)241 deallocate(r)242 if (associated(gv_tmp)) deallocate(gv_tmp)243 if (associated(arr_copy)) deallocate(arr_copy)244 end subroutine pcg245 51 246 52 subroutine prec1Level(DD,sol,A,rhs,A_ghost,refactor)
Note: See TracChangeset
for help on using the changeset viewer.
