77 subroutine gmres_init(this, n, max_iter, M, rel_tol, abs_tol, monitor)
78 class(
gmres_t),
target,
intent(inout) :: this
79 integer,
intent(in) :: n
80 integer,
intent(in) :: max_iter
81 class(
pc_t),
optional,
intent(in),
target :: M
82 real(kind=
rp),
optional,
intent(in) :: rel_tol
83 real(kind=
rp),
optional,
intent(in) :: abs_tol
84 logical,
optional,
intent(in) :: monitor
96 allocate(this%c(this%lgmres))
97 allocate(this%s(this%lgmres))
98 allocate(this%gam(this%lgmres + 1))
100 allocate(this%z(n, this%lgmres))
101 allocate(this%v(n, this%lgmres))
103 allocate(this%h(this%lgmres, this%lgmres))
107 allocate(this%hp(this%lgmres, nthrds))
109 if (
present(rel_tol) .and.
present(abs_tol) .and.
present(monitor))
then
110 call this%ksp_init(max_iter, rel_tol, abs_tol, monitor = monitor)
111 else if (
present(rel_tol) .and.
present(abs_tol))
then
112 call this%ksp_init(max_iter, rel_tol, abs_tol)
113 else if (
present(monitor) .and.
present(abs_tol))
then
114 call this%ksp_init(max_iter, abs_tol = abs_tol, monitor = monitor)
115 else if (
present(rel_tol) .and.
present(monitor))
then
116 call this%ksp_init(max_iter, rel_tol, monitor = monitor)
117 else if (
present(rel_tol))
then
118 call this%ksp_init(max_iter, rel_tol = rel_tol)
119 else if (
present(abs_tol))
then
120 call this%ksp_init(max_iter, abs_tol = abs_tol)
121 else if (
present(monitor))
then
122 call this%ksp_init(max_iter, monitor = monitor)
124 call this%ksp_init(max_iter)
177 function gmres_solve(this, Ax, x, f, n, coef, bc_projector, gs_h, niter) &
179 class(
gmres_t),
intent(inout) :: this
180 class(
ax_t),
intent(in) :: ax
181 type(
field_t),
intent(inout) :: x
182 integer,
intent(in) :: n
183 real(kind=
rp),
dimension(n),
intent(in) :: f
184 type(
coef_t),
intent(inout) :: coef
186 type(
gs_t),
intent(inout) :: gs_h
188 integer,
optional,
intent(in) :: niter
189 integer :: iter, max_iter
190 integer :: i, j, k, l, ierr, tid, nthrds
192 real(kind=
xp) :: hl(this%lgmres)
193 real(kind=
xp) :: alpha, lr, alpha2, norm_fac, tmp, acc
194 real(kind=
rp) :: temp, rnorm
201 if (
present(niter))
then
204 max_iter = this%max_iter
210 associate(w => this%w, c => this%c, r => this%r, z => this%z, h => this%h, &
211 v => this%v, s => this%s, gam => this%gam, hp => this%hp)
213 norm_fac = 1.0_rp / sqrt(coef%volume)
219 call this%monitor_start(
'GMRES')
220 do while (.not. conv .and. iter .lt. max_iter)
222 if (iter .eq. 0)
then
226 call ax%compute(w, x%x, coef, x%msh, x%Xh)
227 call gs_h%op(w, n, gs_op_add)
228 call bc_projector%apply(w, n)
232 gam(1) = sqrt(
glsc3(r, r, coef%mult, n))
233 if (iter .eq. 0)
then
234 ksp_results%res_start = gam(1) * norm_fac
237 if (
abscmp(gam(1), 0.0_xp))
exit
240 temp = 1.0_rp / gam(1)
241 call cmult2(v(1,1), r, temp, n)
242 do j = 1, this%lgmres
245 call this%M%solve(z(1,j), v(1,j), n)
247 call ax%compute(w, z(1,j), coef, x%msh, x%Xh)
248 call gs_h%op(w, n, gs_op_add)
249 call bc_projector%apply(w, n)
269 w(i+k) * v(i+k,l) * coef%mult(i+k,1,1,1)
277 w(i+k) * v(i+k,l) * coef%mult(i+k,1,1,1)
291 hp(l,1) = hp(l,1) + hp(l,k)
294 call mpi_allreduce(mpi_in_place, hp(1,1), j, &
316 w_plus(k) = w_plus(k) - h(l,j) * v(i+k,l)
321 w(i+k) = w(i+k) + w_plus(k)
322 alpha2 = alpha2 + w(i+k)**2 * coef%mult(i+k,1,1,1)
328 tmp = tmp - h(l,j) * v(i+k,l)
330 w(i+k) = w(i+k) + tmp
331 alpha2 = alpha2 + w(i+k)**2 * coef%mult(i+k,1,1,1)
336 call mpi_allreduce(mpi_in_place, alpha2, 1, &
341 h(i,j) = c(i)*temp + s(i) * h(i+1,j)
342 h(i+1,j) = -s(i)*temp + c(i) * h(i+1,j)
346 if (
abscmp(alpha, 0.0_xp))
then
351 lr = sqrt(h(j,j) * h(j,j) + alpha2)
356 gam(j+1) = -s(j) * gam(j)
357 gam(j) = c(j) * gam(j)
358 rnorm = abs(gam(j+1)) * norm_fac
359 call this%monitor_iter(iter, rnorm)
360 if (rnorm .lt. this%abs_tol)
then
365 if (iter + 1 .gt. max_iter)
exit
367 if (j .lt. this%lgmres)
then
368 temp = 1.0_rp / alpha
369 call cmult2(v(1,j+1), w, temp, n)
374 j = min(j, this%lgmres)
378 temp = temp - h(k,i) * c(i)
393 x_plus(k) = x_plus(k) + c(l) * z(i+k,l)
398 x%x(i+k,1,1,1) = x%x(i+k,1,1,1) + x_plus(k)
404 tmp = tmp + c(l) * z(i+k,l)
406 x%x(i+k,1,1,1) = x%x(i+k,1,1,1) + tmp
414 call this%monitor_stop()
415 ksp_results%res_final = rnorm
416 ksp_results%iter = iter
417 ksp_results%converged = this%is_converged(iter, rnorm)
423 n, coef, bc_projector, gs_h, niter)
result(ksp_results)
424 class(
gmres_t),
intent(inout) :: this
425 class(ax_t),
intent(in) :: ax
426 type(field_t),
intent(inout) :: x
427 type(field_t),
intent(inout) :: y
428 type(field_t),
intent(inout) :: z
429 integer,
intent(in) :: n
430 real(kind=rp),
dimension(n),
intent(in) :: fx
431 real(kind=rp),
dimension(n),
intent(in) :: fy
432 real(kind=rp),
dimension(n),
intent(in) :: fz
433 type(coef_t),
intent(inout) :: coef
434 class(vector_bc_projector_t),
intent(inout) :: bc_projector
435 type(gs_t),
intent(inout) :: gs_h
436 type(ksp_monitor_t),
dimension(3) :: ksp_results
437 integer,
optional,
intent(in) :: niter
438 type(scalar_bc_projector_t),
pointer :: bc_x, bc_y, bc_z
440 call vector_bc_projector_components(bc_projector, bc_x, bc_y, bc_z)
441 ksp_results(1) = this%solve(ax, x, fx, n, coef, bc_x, gs_h, niter)
442 ksp_results(2) = this%solve(ax, y, fy, n, coef, bc_y, gs_h, niter)
443 ksp_results(3) = this%solve(ax, z, fz, n, coef, bc_z, gs_h, niter)
type(ksp_monitor_t) function, dimension(3) gmres_solve_coupled(this, ax, x, y, z, fx, fy, fz, n, coef, bc_projector, gs_h, niter)
Standard GMRES coupled solve.