63 subroutine sx_cg_init(this, n, max_iter, M, rel_tol, abs_tol, monitor)
64 class(
sx_cg_t),
target,
intent(inout) :: this
65 class(
pc_t),
optional,
intent(in),
target :: M
66 integer,
intent(in) :: n
67 integer,
intent(in) :: max_iter
68 real(kind=
rp),
optional,
intent(in) :: rel_tol
69 real(kind=
rp),
optional,
intent(in) :: abs_tol
70 logical,
optional,
intent(in) :: monitor
83 if (
present(rel_tol) .and.
present(abs_tol) .and.
present(monitor))
then
84 call this%ksp_init(max_iter, rel_tol, abs_tol, monitor = monitor)
85 else if (
present(rel_tol) .and.
present(abs_tol))
then
86 call this%ksp_init(max_iter, rel_tol, abs_tol)
87 else if (
present(monitor) .and.
present(abs_tol))
then
88 call this%ksp_init(max_iter, abs_tol = abs_tol, monitor = monitor)
89 else if (
present(rel_tol) .and.
present(monitor))
then
90 call this%ksp_init(max_iter, rel_tol, monitor = monitor)
91 else if (
present(rel_tol))
then
92 call this%ksp_init(max_iter, rel_tol = rel_tol)
93 else if (
present(abs_tol))
then
94 call this%ksp_init(max_iter, abs_tol = abs_tol)
95 else if (
present(monitor))
then
96 call this%ksp_init(max_iter, monitor = monitor)
98 call this%ksp_init(max_iter)
130 function sx_cg_solve(this, Ax, x, f, n, coef, blst, gs_h, niter) &
132 class(
sx_cg_t),
intent(inout) :: this
133 class(
ax_t),
intent(in) :: ax
134 type(
field_t),
intent(inout) :: x
135 integer,
intent(in) :: n
136 real(kind=
rp),
dimension(n),
intent(in) :: f
137 type(
coef_t),
intent(inout) :: coef
139 type(
gs_t),
intent(inout) :: gs_h
141 integer,
optional,
intent(in) :: niter
142 real(kind=
rp),
parameter :: one = 1.0
143 real(kind=
rp),
parameter :: zero = 0.0
144 integer :: i, iter, max_iter
145 real(kind=
rp) :: rnorm, rtr, rtr0, rtz2, rtz1
146 real(kind=
rp) :: beta, pap, alpha, alphm, norm_fac
148 if (
present(niter))
then
151 max_iter = this%max_iter
153 norm_fac = one / sqrt(coef%volume)
157 x%x(i,1,1,1) = 0.0_rp
162 rtr =
glsc3(this%r, coef%mult, this%r, n)
163 rnorm = sqrt(rtr)*norm_fac
164 ksp_results%res_start = rnorm
165 ksp_results%res_final = rnorm
167 if(
abscmp(rnorm, zero))
then
168 ksp_results%converged = .true.
172 call this%monitor_start(
'CG')
173 do iter = 1, max_iter
174 call this%M%solve(this%z, this%r, n)
176 rtz1 =
glsc3(this%r, coef%mult, this%z, n)
179 if (iter .eq. 1) beta = zero
180 call add2s1(this%p, this%z, beta, n)
182 call ax%compute(this%w, this%p, coef, x%msh, x%Xh)
183 call gs_h%op(this%w, n, gs_op_add)
184 call blst%apply_scalar(this%w, n)
186 pap =
glsc3(this%w, coef%mult, this%p, n)
191 x%x(i,1,1,1) = x%x(i,1,1,1) + alpha * this%p(i)
192 this%r(i) = this%r(i) + alphm * this%w(i)
195 rtr =
glsc3(this%r, coef%mult, this%r, n)
196 if (iter .eq. 1) rtr0 = rtr
197 rnorm = sqrt(rtr) * norm_fac
198 call this%monitor_iter(iter, rnorm)
199 if (rnorm .lt. this%abs_tol)
then
203 call this%monitor_stop()
204 ksp_results%res_final = rnorm
205 ksp_results%iter = iter
206 ksp_results%converged = this%is_converged(iter, rnorm)
211 n, coef, blstx, blsty, blstz, gs_h, niter)
result(ksp_results)
212 class(
sx_cg_t),
intent(inout) :: this
213 class(
ax_t),
intent(in) :: ax
214 type(
field_t),
intent(inout) :: x
215 type(
field_t),
intent(inout) :: y
216 type(
field_t),
intent(inout) :: z
217 integer,
intent(in) :: n
218 real(kind=
rp),
dimension(n),
intent(in) :: fx
219 real(kind=
rp),
dimension(n),
intent(in) :: fy
220 real(kind=
rp),
dimension(n),
intent(in) :: fz
221 type(
coef_t),
intent(inout) :: coef
225 type(
gs_t),
intent(inout) :: gs_h
227 integer,
optional,
intent(in) :: niter
229 ksp_results(1) = this%solve(ax, x, fx, n, coef, blstx, gs_h, niter)
230 ksp_results(2) = this%solve(ax, y, fy, n, coef, blsty, gs_h, niter)
231 ksp_results(3) = this%solve(ax, z, fz, n, coef, blstz, gs_h, niter)
type(ksp_monitor_t) function, dimension(3) sx_cg_solve_coupled(this, ax, x, y, z, fx, fy, fz, n, coef, blstx, blsty, blstz, gs_h, niter)
Standard PCG coupled solve.
subroutine sx_cg_init(this, n, max_iter, m, rel_tol, abs_tol, monitor)
Initialise a standard PCG solver.
type(ksp_monitor_t) function sx_cg_solve(this, ax, x, f, n, coef, blst, gs_h, niter)
Standard PCG solve.
subroutine, public add2s1(a, b, c1, n)
Vector addition with scalar multiplication (multiplication on first argument)