3 #include "sll_assert.h"
30 real(
wp) :: tol = 1.0e-14_wp
31 logical :: verbose = .true.
44 logical :: allocate_once
61 real(wp),
optional,
intent(in) :: tol
62 logical,
optional,
intent(in) :: verbose
65 if (
present(tol)) self%tol = tol
66 if (
present(verbose)) self%verbose = verbose
69 if (
present(template_vector))
then
71 call template_vector%source(self%p)
72 call template_vector%source(self%r)
73 call template_vector%source(self%v)
75 self%allocate_once = .true.
79 self%allocate_once = .false.
93 real(wp) :: am, am1, l, tol_sqr
99 sll_assert(n(1) == n(2))
102 if (.not. self%allocate_once)
then
104 call x%source(self%p)
105 call x%source(self%r)
106 call x%source(self%v)
111 associate(p => self%p, r => self%r, v => self%v)
120 tol_sqr = self%tol**2
125 if (am < tol_sqr)
exit
130 call x%incr_mult(l, p)
131 call r%incr_mult(-l, v)
142 if (.not. self%allocate_once)
then
144 select type (p => self%p)
153 select type (r => self%r)
162 select type (v => self%v)
175 self%success = .true.
176 self%residual = sqrt(am)
179 if (self%verbose)
write (*,
'(/a,i0,a,es8.2)')
" CG method converged after ", &
180 self%iterations,
" iterations with residual ", self%residual
188 if (self%allocate_once)
then
190 select type (p => self%p)
191 type is (sll_t_vector_space_real_array_1d)
193 type is (sll_t_vector_space_real_array_2d)
195 type is (sll_t_vector_space_real_array_3d)
199 select type (r => self%r)
200 type is (sll_t_vector_space_real_array_1d)
202 type is (sll_t_vector_space_real_array_2d)
204 type is (sll_t_vector_space_real_array_3d)
208 select type (v => self%v)
209 type is (sll_t_vector_space_real_array_1d)
211 type is (sll_t_vector_space_real_array_2d)
213 type is (sll_t_vector_space_real_array_3d)
subroutine s_conjugate_gradient__free(self)
subroutine s_conjugate_gradient__init(self, tol, verbose, template_vector)
subroutine s_conjugate_gradient__solve(self, A, b, x)
Abstract type implementing a generic vector space.
Vector space for wrapping 1D Fortran real arrays.
Vector space for wrapping 2D Fortran real arrays.
Vector space for wrapping 3D Fortran real arrays.
Module to select the kind parameter.
integer, parameter, public f64
f64 is the kind type for 64-bit reals (double precision)
Abstract base class for all vector spaces.
Vector space for wrapping 1D Fortran real arrays.
Vector space for wrapping 2D Fortran real arrays.
Vector space for wrapping 3D Fortran real arrays.