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) :: alpha, lr, alpha2, norm_fac, tmp, acc
193 real(kind=
rp) :: temp, rnorm
200 if (
present(niter))
then
203 max_iter = this%max_iter
209 associate(w => this%w, c => this%c, r => this%r, z => this%z, h => this%h, &
210 v => this%v, s => this%s, gam => this%gam, hp => this%hp)
212 norm_fac = 1.0_rp / sqrt(coef%volume)
218 call this%monitor_start(
'GMRES')
219 do while (.not. conv .and. iter .lt. max_iter)
221 if (iter .eq. 0)
then
225 call ax%compute(w, x%x, coef, x%msh, x%Xh)
226 call gs_h%op(w, n, gs_op_add)
227 call bc_projector%apply(w, n)
231 gam(1) = sqrt(
glsc3(r, r, coef%mult, n))
232 if (iter .eq. 0)
then
233 ksp_results%res_start = gam(1) * norm_fac
236 if (
abscmp(gam(1), 0.0_xp))
exit
239 temp = 1.0_rp / gam(1)
240 call cmult2(v(1,1), r, temp, n)
241 do j = 1, this%lgmres
244 call this%M%solve(z(1,j), v(1,j), n)
246 call ax%compute(w, z(1,j), coef, x%msh, x%Xh)
247 call gs_h%op(w, n, gs_op_add)
248 call bc_projector%apply(w, n)
267 w(i+k) * v(i+k,l) * coef%mult(i+k,1,1,1)
274 hp(l,tid) = hp(l,tid) + &
275 w(i+k) * v(i+k,l) * coef%mult(i+k,1,1,1)
286 hp(l,1) = hp(l,1) + hp(l,k)
289 call mpi_allreduce(mpi_in_place, hp(1,1), j, &
311 w_plus(k) = w_plus(k) - h(l,j) * v(i+k,l)
315 w(i+k) = w(i+k) + w_plus(k)
316 alpha2 = alpha2 + w(i+k)**2 * coef%mult(i+k,1,1,1)
322 tmp = tmp - h(l,j) * v(i+k,l)
324 w(i+k) = w(i+k) + tmp
325 alpha2 = alpha2 + w(i+k)**2 * coef%mult(i+k,1,1,1)
330 call mpi_allreduce(mpi_in_place, alpha2, 1, &
335 h(i,j) = c(i)*temp + s(i) * h(i+1,j)
336 h(i+1,j) = -s(i)*temp + c(i) * h(i+1,j)
340 if (
abscmp(alpha, 0.0_xp))
then
345 lr = sqrt(h(j,j) * h(j,j) + alpha2)
350 gam(j+1) = -s(j) * gam(j)
351 gam(j) = c(j) * gam(j)
352 rnorm = abs(gam(j+1)) * norm_fac
353 call this%monitor_iter(iter, rnorm)
354 if (rnorm .lt. this%abs_tol)
then
359 if (iter + 1 .gt. max_iter)
exit
361 if (j .lt. this%lgmres)
then
362 temp = 1.0_rp / alpha
363 call cmult2(v(1,j+1), w, temp, n)
368 j = min(j, this%lgmres)
372 temp = temp - h(k,i) * c(i)
387 x_plus(k) = x_plus(k) + c(l) * z(i+k,l)
392 x%x(i+k,1,1,1) = x%x(i+k,1,1,1) + x_plus(k)
398 tmp = tmp + c(l) * z(i+k,l)
400 x%x(i+k,1,1,1) = x%x(i+k,1,1,1) + tmp
408 call this%monitor_stop()
409 ksp_results%res_final = rnorm
410 ksp_results%iter = iter
411 ksp_results%converged = this%is_converged(iter, rnorm)
417 n, coef, bc_projector, gs_h, niter)
result(ksp_results)
418 class(
gmres_t),
intent(inout) :: this
419 class(ax_t),
intent(in) :: ax
420 type(field_t),
intent(inout) :: x
421 type(field_t),
intent(inout) :: y
422 type(field_t),
intent(inout) :: z
423 integer,
intent(in) :: n
424 real(kind=rp),
dimension(n),
intent(in) :: fx
425 real(kind=rp),
dimension(n),
intent(in) :: fy
426 real(kind=rp),
dimension(n),
intent(in) :: fz
427 type(coef_t),
intent(inout) :: coef
428 class(vector_bc_projector_t),
intent(inout) :: bc_projector
429 type(gs_t),
intent(inout) :: gs_h
430 type(ksp_monitor_t),
dimension(3) :: ksp_results
431 integer,
optional,
intent(in) :: niter
432 type(scalar_bc_projector_t),
pointer :: bc_x, bc_y, bc_z
434 call vector_bc_projector_components(bc_projector, bc_x, bc_y, bc_z)
435 ksp_results(1) = this%solve(ax, x, fx, n, coef, bc_x, gs_h, niter)
436 ksp_results(2) = this%solve(ax, y, fy, n, coef, bc_y, gs_h, niter)
437 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.