76 subroutine sx_gmres_init(this, n, max_iter, M, rel_tol, abs_tol, monitor)
77 class(
sx_gmres_t),
target,
intent(inout) :: this
78 integer,
intent(in) :: n
79 integer,
intent(in) :: max_iter
80 class(
pc_t),
optional,
intent(in),
target :: M
81 real(kind=
rp),
optional,
intent(in) :: rel_tol
82 real(kind=
rp),
optional,
intent(in) :: abs_tol
83 logical,
optional,
intent(in) :: monitor
97 allocate(this%c(this%lgmres))
98 allocate(this%s(this%lgmres))
99 allocate(this%gam(this%lgmres + 1))
101 allocate(this%z(n, this%lgmres))
102 allocate(this%v(n, this%lgmres))
104 allocate(this%h(this%lgmres, this%lgmres))
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)
185 class(
ax_t),
intent(in) :: ax
186 type(
field_t),
intent(inout) :: x
187 integer,
intent(in) :: n
188 real(kind=
rp),
dimension(n),
intent(in) :: f
189 type(
coef_t),
intent(inout) :: coef
191 type(
gs_t),
intent(inout) :: gs_h
193 integer,
optional,
intent(in) :: niter
194 integer :: iter, max_iter, glb_n
195 integer :: i, j, k, ierr
196 real(kind=
rp),
parameter :: one = 1.0
197 real(kind=
rp) :: rnorm
198 real(kind=
rp) :: alpha, temp, l
199 real(kind=
rp) :: ratio, div0, norm_fac
206 glb_n = n / x%msh%nelv * x%msh%glb_nelv
208 if (
present(niter))
then
211 max_iter = this%max_iter
214 call rone(this%ml, n)
215 call rone(this%mu, n)
216 norm_fac = one / sqrt(coef%volume)
218 call rzero(this%gam, this%lgmres + 1)
219 call rone(this%s, this%lgmres)
220 call rone(this%c, this%lgmres)
221 call rzero(this%h, this%lgmres * this%lgmres)
223 call this%monitor_start(
'GMRES')
224 do while (.not. conv .and. iter .lt. max_iter)
227 if (iter .eq. 0)
then
228 call col3(this%r, this%ml, f, n)
231 call copy (this%r,f,n)
232 call ax%compute(this%w, x%x, coef, x%msh, x%Xh)
233 call gs_h%op(this%w, n, gs_op_add)
234 call bc_projector%apply(this%w, n)
235 call add2s2(this%r, this%w, -one, n)
236 call col2(this%r, this%ml, n)
238 this%gam(1) = sqrt(
glsc3(this%r, this%r, coef%mult, n))
239 if (iter .eq. 0)
then
240 div0 = this%gam(1) * norm_fac
241 ksp_results%res_start = div0
244 if (
abscmp(this%gam(1), 0.0_rp))
exit
247 temp = one / this%gam(1)
248 call cmult2(this%v(1,1), this%r, temp, n)
249 do j = 1, this%lgmres
251 call col3(this%w, this%mu, this%v(1,j), n)
254 call this%M%solve(this%z(1,j), this%w, n)
256 call ax%compute(this%w, this%z(1,j), coef, x%msh, x%Xh)
257 call gs_h%op(this%w, n, gs_op_add)
258 call bc_projector%apply(this%w, n)
259 call col2(this%w, this%ml, n)
264 this%h(i,j) = this%h(i,j) + &
265 this%w(k) * this%v(k,i) * coef%mult(k,1,1,1)
270 call mpi_allreduce(this%h(1,j), this%wk1, j, &
272 call copy(this%h(1,j), this%wk1, j)
276 this%w(k) = this%w(k) - this%h(i,j) * this%v(k,i)
283 this%h(i ,j) = this%c(i)*temp + this%s(i)*this%h(i+1,j)
284 this%h(i+1,j) = -this%s(i)*temp + this%c(i)*this%h(i+1,j)
287 alpha = sqrt(
glsc3(this%w, this%w, coef%mult, n))
289 if (
abscmp(alpha, 0.0_rp))
then
293 l = sqrt(this%h(j,j) * this%h(j,j) + alpha**2)
295 this%c(j) = this%h(j,j) * temp
296 this%s(j) = alpha * temp
298 this%gam(j+1) = -this%s(j) * this%gam(j)
299 this%gam(j) = this%c(j) * this%gam(j)
301 rnorm = abs(this%gam(j+1)) * norm_fac
302 call this%monitor_iter(iter, rnorm)
304 if (rnorm .lt. this%abs_tol)
then
309 if (iter + 1 .gt. max_iter)
exit
311 if (j .lt. this%lgmres)
then
313 call cmult2(this%v(1,j+1), this%w, temp, n)
316 j = min(j, this%lgmres)
321 temp = temp - this%h(k,i) * this%c(i)
323 this%c(k) = temp / this%h(k,k)
328 x%x(k,1,1,1) = x%x(k,1,1,1) + this%c(i) * this%z(k,i)
332 call this%monitor_stop()
333 ksp_results%res_final = rnorm
334 ksp_results%iter = iter
335 ksp_results%converged = this%is_converged(iter, rnorm)
340 n, coef, bc_projector, gs_h, niter)
result(ksp_results)
342 class(
ax_t),
intent(in) :: ax
343 type(
field_t),
intent(inout) :: x
344 type(
field_t),
intent(inout) :: y
345 type(
field_t),
intent(inout) :: z
346 integer,
intent(in) :: n
347 real(kind=
rp),
dimension(n),
intent(in) :: fx
348 real(kind=
rp),
dimension(n),
intent(in) :: fy
349 real(kind=
rp),
dimension(n),
intent(in) :: fz
350 type(
coef_t),
intent(inout) :: coef
352 type(
gs_t),
intent(inout) :: gs_h
354 integer,
optional,
intent(in) :: niter
358 ksp_results(1) = this%solve(ax, x, fx, n, coef, bc_x, gs_h, niter)
359 ksp_results(2) = this%solve(ax, y, fy, n, coef, bc_y, gs_h, niter)
360 ksp_results(3) = this%solve(ax, z, fz, n, coef, bc_z, gs_h, niter)
type(ksp_monitor_t) function, dimension(3) sx_gmres_solve_coupled(this, ax, x, y, z, fx, fy, fz, n, coef, bc_projector, gs_h, niter)
Standard GMRES coupled solve.