75 subroutine gmres_init(this, n, max_iter, M, rel_tol, abs_tol, monitor)
76 class(
gmres_t),
target,
intent(inout) :: this
77 integer,
intent(in) :: n
78 integer,
intent(in) :: max_iter
79 class(
pc_t),
optional,
intent(in),
target :: M
80 real(kind=
rp),
optional,
intent(in) :: rel_tol
81 real(kind=
rp),
optional,
intent(in) :: abs_tol
82 logical,
optional,
intent(in) :: monitor
94 allocate(this%c(this%lgmres))
95 allocate(this%s(this%lgmres))
96 allocate(this%gam(this%lgmres + 1))
98 allocate(this%z(n, this%lgmres))
99 allocate(this%v(n, this%lgmres))
101 allocate(this%h(this%lgmres, this%lgmres))
105 allocate(this%hp(this%lgmres, nthrds))
107 if (
present(rel_tol) .and.
present(abs_tol) .and.
present(monitor))
then
108 call this%ksp_init(max_iter, rel_tol, abs_tol, monitor = monitor)
109 else if (
present(rel_tol) .and.
present(abs_tol))
then
110 call this%ksp_init(max_iter, rel_tol, abs_tol)
111 else if (
present(monitor) .and.
present(abs_tol))
then
112 call this%ksp_init(max_iter, abs_tol = abs_tol, monitor = monitor)
113 else if (
present(rel_tol) .and.
present(monitor))
then
114 call this%ksp_init(max_iter, rel_tol, monitor = monitor)
115 else if (
present(rel_tol))
then
116 call this%ksp_init(max_iter, rel_tol = rel_tol)
117 else if (
present(abs_tol))
then
118 call this%ksp_init(max_iter, abs_tol = abs_tol)
119 else if (
present(monitor))
then
120 call this%ksp_init(max_iter, monitor = monitor)
122 call this%ksp_init(max_iter)
175 function gmres_solve(this, Ax, x, f, n, coef, blst, gs_h, niter) &
177 class(
gmres_t),
intent(inout) :: this
178 class(
ax_t),
intent(in) :: ax
179 type(
field_t),
intent(inout) :: x
180 integer,
intent(in) :: n
181 real(kind=
rp),
dimension(n),
intent(in) :: f
182 type(
coef_t),
intent(inout) :: coef
184 type(
gs_t),
intent(inout) :: gs_h
186 integer,
optional,
intent(in) :: niter
187 integer :: iter, max_iter
188 integer :: i, j, k, l, ierr, tid, nthrds
190 real(kind=
xp) :: alpha, lr, alpha2, norm_fac, tmp, acc
191 real(kind=
rp) :: temp, rnorm
198 if (
present(niter))
then
201 max_iter = this%max_iter
207 associate(w => this%w, c => this%c, r => this%r, z => this%z, h => this%h, &
208 v => this%v, s => this%s, gam => this%gam, hp => this%hp)
210 norm_fac = 1.0_rp / sqrt(coef%volume)
216 call this%monitor_start(
'GMRES')
217 do while (.not. conv .and. iter .lt. max_iter)
219 if (iter .eq. 0)
then
223 call ax%compute(w, x%x, coef, x%msh, x%Xh)
224 call gs_h%op(w, n, gs_op_add)
225 call blst%apply(w, n)
229 gam(1) = sqrt(
glsc3(r, r, coef%mult, n))
230 if (iter .eq. 0)
then
231 ksp_results%res_start = gam(1) * norm_fac
234 if (
abscmp(gam(1), 0.0_xp))
exit
237 temp = 1.0_rp / gam(1)
238 call cmult2(v(1,1), r, temp, n)
239 do j = 1, this%lgmres
242 call this%M%solve(z(1,j), v(1,j), n)
244 call ax%compute(w, z(1,j), coef, x%msh, x%Xh)
245 call gs_h%op(w, n, gs_op_add)
246 call blst%apply(w, n)
265 w(i+k) * v(i+k,l) * coef%mult(i+k,1,1,1)
272 hp(l,tid) = hp(l,tid) + &
273 w(i+k) * v(i+k,l) * coef%mult(i+k,1,1,1)
284 hp(l,1) = hp(l,1) + hp(l,k)
287 call mpi_allreduce(mpi_in_place, hp(1,1), j, &
309 w_plus(k) = w_plus(k) - h(l,j) * v(i+k,l)
313 w(i+k) = w(i+k) + w_plus(k)
314 alpha2 = alpha2 + w(i+k)**2 * coef%mult(i+k,1,1,1)
320 tmp = tmp - h(l,j) * v(i+k,l)
322 w(i+k) = w(i+k) + tmp
323 alpha2 = alpha2 + w(i+k)**2 * coef%mult(i+k,1,1,1)
328 call mpi_allreduce(mpi_in_place, alpha2, 1, &
333 h(i,j) = c(i)*temp + s(i) * h(i+1,j)
334 h(i+1,j) = -s(i)*temp + c(i) * h(i+1,j)
338 if (
abscmp(alpha, 0.0_xp))
then
343 lr = sqrt(h(j,j) * h(j,j) + alpha2)
348 gam(j+1) = -s(j) * gam(j)
349 gam(j) = c(j) * gam(j)
350 rnorm = abs(gam(j+1)) * norm_fac
351 call this%monitor_iter(iter, rnorm)
352 if (rnorm .lt. this%abs_tol)
then
357 if (iter + 1 .gt. max_iter)
exit
359 if (j .lt. this%lgmres)
then
360 temp = 1.0_rp / alpha
361 call cmult2(v(1,j+1), w, temp, n)
366 j = min(j, this%lgmres)
370 temp = temp - h(k,i) * c(i)
385 x_plus(k) = x_plus(k) + c(l) * z(i+k,l)
390 x%x(i+k,1,1,1) = x%x(i+k,1,1,1) + x_plus(k)
396 tmp = tmp + c(l) * z(i+k,l)
398 x%x(i+k,1,1,1) = x%x(i+k,1,1,1) + tmp
406 call this%monitor_stop()
407 ksp_results%res_final = rnorm
408 ksp_results%iter = iter
409 ksp_results%converged = this%is_converged(iter, rnorm)
415 n, coef, blstx, blsty, blstz, gs_h, niter)
result(ksp_results)
416 class(
gmres_t),
intent(inout) :: this
417 class(ax_t),
intent(in) :: ax
418 type(field_t),
intent(inout) :: x
419 type(field_t),
intent(inout) :: y
420 type(field_t),
intent(inout) :: z
421 integer,
intent(in) :: n
422 real(kind=rp),
dimension(n),
intent(in) :: fx
423 real(kind=rp),
dimension(n),
intent(in) :: fy
424 real(kind=rp),
dimension(n),
intent(in) :: fz
425 type(coef_t),
intent(inout) :: coef
426 type(bc_list_t),
intent(inout) :: blstx
427 type(bc_list_t),
intent(inout) :: blsty
428 type(bc_list_t),
intent(inout) :: blstz
429 type(gs_t),
intent(inout) :: gs_h
430 type(ksp_monitor_t),
dimension(3) :: ksp_results
431 integer,
optional,
intent(in) :: niter
433 ksp_results(1) = this%solve(ax, x, fx, n, coef, blstx, gs_h, niter)
434 ksp_results(2) = this%solve(ax, y, fy, n, coef, blsty, gs_h, niter)
435 ksp_results(3) = this%solve(ax, z, fz, n, coef, blstz, gs_h, niter)
type(ksp_monitor_t) function, dimension(3) gmres_solve_coupled(this, ax, x, y, z, fx, fy, fz, n, coef, blstx, blsty, blstz, gs_h, niter)
Standard GMRES coupled solve.