71 subroutine gmres_init(this, n, max_iter, M, rel_tol, abs_tol, monitor)
72 class(
gmres_t),
target,
intent(inout) :: this
73 integer,
intent(in) :: n
74 integer,
intent(in) :: max_iter
75 class(
pc_t),
optional,
intent(in),
target :: M
76 real(kind=
rp),
optional,
intent(in) :: rel_tol
77 real(kind=
rp),
optional,
intent(in) :: abs_tol
78 logical,
optional,
intent(in) :: monitor
89 allocate(this%c(this%lgmres))
90 allocate(this%s(this%lgmres))
91 allocate(this%gam(this%lgmres + 1))
93 allocate(this%z(n, this%lgmres))
94 allocate(this%v(n, this%lgmres))
96 allocate(this%h(this%lgmres, this%lgmres))
98 if (
present(rel_tol) .and.
present(abs_tol) .and.
present(monitor))
then
99 call this%ksp_init(max_iter, rel_tol, abs_tol, monitor = monitor)
100 else if (
present(rel_tol) .and.
present(abs_tol))
then
101 call this%ksp_init(max_iter, rel_tol, abs_tol)
102 else if (
present(monitor) .and.
present(abs_tol))
then
103 call this%ksp_init(max_iter, abs_tol = abs_tol, monitor = monitor)
104 else if (
present(rel_tol) .and.
present(monitor))
then
105 call this%ksp_init(max_iter, rel_tol, monitor = monitor)
106 else if (
present(rel_tol))
then
107 call this%ksp_init(max_iter, rel_tol = rel_tol)
108 else if (
present(abs_tol))
then
109 call this%ksp_init(max_iter, abs_tol = abs_tol)
110 else if (
present(monitor))
then
111 call this%ksp_init(max_iter, monitor = monitor)
113 call this%ksp_init(max_iter)
162 function gmres_solve(this, Ax, x, f, n, coef, blst, gs_h, niter) &
164 class(
gmres_t),
intent(inout) :: this
165 class(
ax_t),
intent(in) :: ax
166 type(
field_t),
intent(inout) :: x
167 integer,
intent(in) :: n
168 real(kind=
rp),
dimension(n),
intent(in) :: f
169 type(
coef_t),
intent(inout) :: coef
171 type(
gs_t),
intent(inout) :: gs_h
173 integer,
optional,
intent(in) :: niter
174 integer :: iter, max_iter
175 integer :: i, j, k, l, ierr
177 real(kind=
xp) :: alpha, lr, alpha2, norm_fac
178 real(kind=
rp) :: temp, rnorm
184 if (
present(niter))
then
187 max_iter = this%max_iter
190 associate(w => this%w, c => this%c, r => this%r, z => this%z, h => this%h, &
191 v => this%v, s => this%s, gam => this%gam)
193 norm_fac = 1.0_rp / sqrt(coef%volume)
199 call this%monitor_start(
'GMRES')
200 do while (.not. conv .and. iter .lt. max_iter)
202 if (iter .eq. 0)
then
206 call ax%compute(w, x%x, coef, x%msh, x%Xh)
207 call gs_h%op(w, n, gs_op_add)
208 call blst%apply(w, n)
212 gam(1) = sqrt(
glsc3(r, r, coef%mult, n))
213 if (iter .eq. 0)
then
214 ksp_results%res_start = gam(1) * norm_fac
217 if (
abscmp(gam(1), 0.0_xp))
return
220 temp = 1.0_rp / gam(1)
221 call cmult2(v(1,1), r, temp, n)
222 do j = 1, this%lgmres
225 call this%M%solve(z(1,j), v(1,j), n)
227 call ax%compute(w, z(1,j), coef, x%msh, x%Xh)
228 call gs_h%op(w, n, gs_op_add)
229 call blst%apply(w, n)
240 w(i+k) * v(i+k,l) * coef%mult(i+k,1,1,1)
247 w(i+k) * v(i+k,l) * coef%mult(i+k,1,1,1)
253 call mpi_allreduce(mpi_in_place, h(1,j), j, &
264 w_plus(k) = w_plus(k) - h(l,j) * v(i+k,l)
268 w(i+k) = w(i+k) + w_plus(k)
269 alpha2 = alpha2 + w(i+k)**2 * coef%mult(i+k,1,1,1)
275 w_plus(1) = w_plus(1) - h(l,j) * v(i+k,l)
277 w(i+k) = w(i+k) + w_plus(1)
278 alpha2 = alpha2 + (w(i+k)**2) * coef%mult(i+k,1,1,1)
283 call mpi_allreduce(mpi_in_place,alpha2, 1, &
288 h(i,j) = c(i)*temp + s(i) * h(i+1,j)
289 h(i+1,j) = -s(i)*temp + c(i) * h(i+1,j)
293 if (
abscmp(alpha, 0.0_xp))
then
298 lr = sqrt(h(j,j) * h(j,j) + alpha2)
303 gam(j+1) = -s(j) * gam(j)
304 gam(j) = c(j) * gam(j)
305 rnorm = abs(gam(j+1)) * norm_fac
306 call this%monitor_iter(iter, rnorm)
307 if (rnorm .lt. this%abs_tol)
then
312 if (iter + 1 .gt. max_iter)
exit
314 if (j .lt. this%lgmres)
then
315 temp = 1.0_rp / alpha
316 call cmult2(v(1,j+1), w, temp, n)
321 j = min(j, this%lgmres)
325 temp = temp - h(k,i) * c(i)
337 x_plus(k) = x_plus(k) + c(l) * z(i+k,l)
341 x%x(i+k,1,1,1) = x%x(i+k,1,1,1) + x_plus(k)
347 x_plus(1) = x_plus(1) + c(l) * z(i+k,l)
349 x%x(i+k,1,1,1) = x%x(i+k,1,1,1) + x_plus(1)
356 call this%monitor_stop()
357 ksp_results%res_final = rnorm
358 ksp_results%iter = iter
359 ksp_results%converged = this%is_converged(iter, rnorm)
365 n, coef, blstx, blsty, blstz, gs_h, niter)
result(ksp_results)
366 class(
gmres_t),
intent(inout) :: this
367 class(ax_t),
intent(in) :: ax
368 type(field_t),
intent(inout) :: x
369 type(field_t),
intent(inout) :: y
370 type(field_t),
intent(inout) :: z
371 integer,
intent(in) :: n
372 real(kind=rp),
dimension(n),
intent(in) :: fx
373 real(kind=rp),
dimension(n),
intent(in) :: fy
374 real(kind=rp),
dimension(n),
intent(in) :: fz
375 type(coef_t),
intent(inout) :: coef
376 type(bc_list_t),
intent(inout) :: blstx
377 type(bc_list_t),
intent(inout) :: blsty
378 type(bc_list_t),
intent(inout) :: blstz
379 type(gs_t),
intent(inout) :: gs_h
380 type(ksp_monitor_t),
dimension(3) :: ksp_results
381 integer,
optional,
intent(in) :: niter
383 ksp_results(1) = this%solve(ax, x, fx, n, coef, blstx, gs_h, niter)
384 ksp_results(2) = this%solve(ax, y, fy, n, coef, blsty, gs_h, niter)
385 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.