Neko  0.8.1
A portable framework for high-order spectral element flow simulations
gmres_sx.f90
Go to the documentation of this file.
1 ! Copyright (c) 2021-2024, The Neko Authors
2 ! All rights reserved.
3 !
4 ! Redistribution and use in source and binary forms, with or without
5 ! modification, are permitted provided that the following conditions
6 ! are met:
7 !
8 ! * Redistributions of source code must retain the above copyright
9 ! notice, this list of conditions and the following disclaimer.
10 !
11 ! * Redistributions in binary form must reproduce the above
12 ! copyright notice, this list of conditions and the following
13 ! disclaimer in the documentation and/or other materials provided
14 ! with the distribution.
15 !
16 ! * Neither the name of the authors nor the names of its
17 ! contributors may be used to endorse or promote products derived
18 ! from this software without specific prior written permission.
19 !
20 ! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 ! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 ! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 ! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 ! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 ! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 ! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 ! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 ! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 ! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 ! POSSIBILITY OF SUCH DAMAGE.
32 !
34 module gmres_sx
35  use krylov, only : ksp_t, ksp_monitor_t
36  use precon, only : pc_t
37  use ax_product, only : ax_t
38  use num_types, only: rp
39  use field, only : field_t
40  use coefs, only : coef_t
41  use gather_scatter, only : gs_t, gs_op_add
42  use bc, only : bc_list_t, bc_list_apply
43  use math, only : glsc3, rzero, rone, copy, cmult2, col2, col3, add2s2, abscmp
44  use comm
45  implicit none
46  private
47 
49  type, public, extends(ksp_t) :: sx_gmres_t
50  integer :: lgmres
51  real(kind=rp), allocatable :: w(:)
52  real(kind=rp), allocatable :: c(:)
53  real(kind=rp), allocatable :: r(:)
54  real(kind=rp), allocatable :: z(:,:)
55  real(kind=rp), allocatable :: h(:,:)
56  real(kind=rp), allocatable :: ml(:)
57  real(kind=rp), allocatable :: v(:,:)
58  real(kind=rp), allocatable :: s(:)
59  real(kind=rp), allocatable :: mu(:)
60  real(kind=rp), allocatable :: gam(:)
61  real(kind=rp), allocatable :: wk1(:)
62  real(kind=rp) :: rnorm
63  contains
64  procedure, pass(this) :: init => sx_gmres_init
65  procedure, pass(this) :: free => sx_gmres_free
66  procedure, pass(this) :: solve => sx_gmres_solve
67  end type sx_gmres_t
68 
69 contains
70 
72  subroutine sx_gmres_init(this, n, max_iter, M, lgmres, rel_tol, abs_tol)
73  class(sx_gmres_t), intent(inout) :: this
74  integer, intent(in) :: n
75  integer, intent(in) :: max_iter
76  class(pc_t), optional, intent(inout), target :: M
77  integer, optional, intent(inout) :: lgmres
78  real(kind=rp), optional, intent(inout) :: rel_tol
79  real(kind=rp), optional, intent(inout) :: abs_tol
80 
81  if (present(lgmres)) then
82  this%lgmres = lgmres
83  else
84  this%lgmres = 30
85  end if
86 
87 
88  call this%free()
89 
90  if (present(m)) then
91  this%M => m
92  end if
93 
94  allocate(this%w(n))
95  allocate(this%r(n))
96  allocate(this%ml(n))
97  allocate(this%mu(n))
98  allocate(this%wk1(n))
99 
100  allocate(this%c(this%lgmres))
101  allocate(this%s(this%lgmres))
102  allocate(this%gam(this%lgmres + 1))
103 
104  allocate(this%z(n,this%lgmres))
105  allocate(this%v(n,this%lgmres))
106 
107  allocate(this%h(this%lgmres,this%lgmres))
108 
109 
110  if (present(rel_tol) .and. present(abs_tol)) then
111  call this%ksp_init(max_iter, rel_tol, abs_tol)
112  else if (present(rel_tol)) then
113  call this%ksp_init(max_iter, rel_tol=rel_tol)
114  else if (present(abs_tol)) then
115  call this%ksp_init(max_iter, abs_tol=abs_tol)
116  else
117  call this%ksp_init(max_iter)
118  end if
119 
120  end subroutine sx_gmres_init
121 
123  subroutine sx_gmres_free(this)
124  class(sx_gmres_t), intent(inout) :: this
125 
126  call this%ksp_free()
127 
128  if (allocated(this%w)) then
129  deallocate(this%w)
130  end if
131 
132  if (allocated(this%c)) then
133  deallocate(this%c)
134  end if
135 
136  if (allocated(this%r)) then
137  deallocate(this%r)
138  end if
139 
140  if (allocated(this%z)) then
141  deallocate(this%z)
142  end if
143 
144  if (allocated(this%h)) then
145  deallocate(this%h)
146  end if
147 
148  if (allocated(this%ml)) then
149  deallocate(this%ml)
150  end if
151 
152  if (allocated(this%v)) then
153  deallocate(this%v)
154  end if
155 
156  if (allocated(this%s)) then
157  deallocate(this%s)
158  end if
159 
160  if (allocated(this%mu)) then
161  deallocate(this%mu)
162  end if
163 
164  if (allocated(this%gam)) then
165  deallocate(this%gam)
166  end if
167 
168  if (allocated(this%wk1)) then
169  deallocate(this%wk1)
170  end if
171 
172  nullify(this%M)
173 
174  end subroutine sx_gmres_free
175 
177  function sx_gmres_solve(this, Ax, x, f, n, coef, blst, gs_h, niter) result(ksp_results)
178  class(sx_gmres_t), intent(inout) :: this
179  class(ax_t), intent(inout) :: ax
180  type(field_t), intent(inout) :: x
181  integer, intent(in) :: n
182  real(kind=rp), dimension(n), intent(inout) :: f
183  type(coef_t), intent(inout) :: coef
184  type(bc_list_t), intent(inout) :: blst
185  type(gs_t), intent(inout) :: gs_h
186  type(ksp_monitor_t) :: ksp_results
187  integer, optional, intent(in) :: niter
188  integer :: iter, max_iter, glb_n
189  integer :: i, j, k, ierr
190  real(kind=rp), parameter :: one = 1.0
191  real(kind=rp) :: rnorm
192  real(kind=rp) :: alpha, temp, l
193  real(kind=rp) :: ratio, div0, norm_fac
194  logical :: conv
195  integer outer
196 
197  conv = .false.
198  iter = 0
199  glb_n = n / x%msh%nelv * x%msh%glb_nelv
200 
201  if (present(niter)) then
202  max_iter = niter
203  else
204  max_iter = this%max_iter
205  end if
206 
207  call rone(this%ml, n)
208  call rone(this%mu, n)
209  norm_fac = one / sqrt(coef%volume)
210  call rzero(x%x, n)
211  call rzero(this%gam, this%lgmres + 1)
212  call rone(this%s, this%lgmres)
213  call rone(this%c, this%lgmres)
214  call rzero(this%h, this%lgmres * this%lgmres)
215  outer = 0
216  do while (.not. conv .and. iter .lt. max_iter)
217  outer = outer + 1
218 
219  if(iter.eq.0) then
220  call col3(this%r,this%ml,f,n)
221  else
222  !update residual
223  call copy (this%r,f,n)
224  call ax%compute(this%w, x%x, coef, x%msh, x%Xh)
225  call gs_h%op(this%w, n, gs_op_add)
226  call bc_list_apply(blst, this%w, n)
227  call add2s2(this%r,this%w,-one,n)
228  call col2(this%r,this%ml,n)
229  endif
230  this%gam(1) = sqrt(glsc3(this%r, this%r, coef%mult, n))
231  if(iter.eq.0) then
232  div0 = this%gam(1) * norm_fac
233  ksp_results%res_start = div0
234  endif
235 
236  if (abscmp(this%gam(1), 0.0_rp)) return
237 
238  rnorm = 0.0_rp
239  temp = one / this%gam(1)
240  call cmult2(this%v(1,1), this%r, temp, n)
241  do j = 1, this%lgmres
242  iter = iter+1
243  call col3(this%w, this%mu, this%v(1,j), n)
244 
245  !Apply precond
246  call this%M%solve(this%z(1,j), this%w, n)
247 
248  call ax%compute(this%w, this%z(1,j), coef, x%msh, x%Xh)
249  call gs_h%op(this%w, n, gs_op_add)
250  call bc_list_apply(blst, this%w, n)
251  call col2(this%w, this%ml, n)
252 
253  do i = 1, j
254  this%h(i,j) = 0.0_rp
255  do k = 1, n
256  this%h(i,j) = this%h(i,j) + &
257  this%w(k) * this%v(k,i) * coef%mult(k,1,1,1)
258  end do
259  end do
260 
261  !Could probably be done inplace...
262  call mpi_allreduce(this%h(1,j), this%wk1, j, &
263  mpi_real_precision, mpi_sum, neko_comm, ierr)
264  call copy(this%h(1,j), this%wk1, j)
265 
266  do i = 1, j
267  do k = 1, n
268  this%w(k) = this%w(k) - this%h(i,j) * this%v(k,i)
269  end do
270  end do
271 
272  !apply Givens rotations to new column
273  do i=1,j-1
274  temp = this%h(i,j)
275  this%h(i ,j) = this%c(i)*temp + this%s(i)*this%h(i+1,j)
276  this%h(i+1,j) = -this%s(i)*temp + this%c(i)*this%h(i+1,j)
277  end do
278 
279  alpha = sqrt(glsc3(this%w, this%w, coef%mult, n))
280  rnorm = 0.0_rp
281  if(abscmp(alpha, 0.0_rp)) then
282  conv = .true.
283  exit
284  end if
285  l = sqrt(this%h(j,j) * this%h(j,j) + alpha**2)
286  temp = one / l
287  this%c(j) = this%h(j,j) * temp
288  this%s(j) = alpha * temp
289  this%h(j,j) = l
290  this%gam(j+1) = -this%s(j) * this%gam(j)
291  this%gam(j) = this%c(j) * this%gam(j)
292 
293  rnorm = abs(this%gam(j+1)) * norm_fac
294  ratio = rnorm / div0
295  if (rnorm .lt. this%abs_tol) then
296  conv = .true.
297  exit
298  end if
299 
300  if (iter + 1 .gt. max_iter) exit
301 
302  if( j .lt. this%lgmres) then
303  temp = one / alpha
304  call cmult2(this%v(1,j+1), this%w, temp, n)
305  endif
306  end do
307  j = min(j, this%lgmres)
308  !back substitution
309  do k = j, 1, -1
310  temp = this%gam(k)
311  do i = j, k+1, -1
312  temp = temp - this%h(k,i) * this%c(i)
313  enddo
314  this%c(k) = temp / this%h(k,k)
315  enddo
316  !sum up Arnoldi vectors
317  do i = 1, j
318  do k = 1, n
319  x%x(k,1,1,1) = x%x(k,1,1,1) + this%c(i) * this%z(k,i)
320  end do
321  end do
322  end do
323 
324  ksp_results%res_final = rnorm
325  ksp_results%iter = iter
326  end function sx_gmres_solve
327 
328 end module gmres_sx
329 
330 
Defines a Matrix-vector product.
Definition: ax.f90:34
Defines a boundary condition.
Definition: bc.f90:34
Coefficients.
Definition: coef.f90:34
Definition: comm.F90:1
type(mpi_comm) neko_comm
MPI communicator.
Definition: comm.F90:16
type(mpi_datatype) mpi_real_precision
MPI type for working precision of REAL types.
Definition: comm.F90:22
Defines a field.
Definition: field.f90:34
Gather-scatter.
Defines various GMRES methods.
Definition: gmres_sx.f90:34
subroutine sx_gmres_free(this)
Deallocate a standard GMRES solver.
Definition: gmres_sx.f90:124
subroutine sx_gmres_init(this, n, max_iter, M, lgmres, rel_tol, abs_tol)
Initialise a standard GMRES solver.
Definition: gmres_sx.f90:73
type(ksp_monitor_t) function sx_gmres_solve(this, Ax, x, f, n, coef, blst, gs_h, niter)
Standard PCG solve.
Definition: gmres_sx.f90:178
Implements the base abstract type for Krylov solvers plus helper types.
Definition: krylov.f90:34
Definition: math.f90:60
subroutine, public cmult2(a, b, c, n)
Multiplication by constant c .
Definition: math.f90:617
real(kind=rp) function, public glsc3(a, b, c, n)
Weighted inner product .
Definition: math.f90:810
subroutine, public rone(a, n)
Set all elements to one.
Definition: math.f90:200
subroutine, public col2(a, b, n)
Vector multiplication .
Definition: math.f90:645
subroutine, public copy(a, b, n)
Copy a vector .
Definition: math.f90:211
subroutine, public col3(a, b, c, n)
Vector multiplication with 3 vectors .
Definition: math.f90:658
subroutine, public rzero(a, n)
Zero a real vector.
Definition: math.f90:167
subroutine, public add2s2(a, b, c1, n)
Vector addition with scalar multiplication (multiplication on second argument)
Definition: math.f90:589
integer, parameter, public rp
Global precision used in computations.
Definition: num_types.f90:12
Krylov preconditioner.
Definition: precon.f90:34
Base type for a matrix-vector product providing .
Definition: ax.f90:43
A list of boundary conditions.
Definition: bc.f90:102
Coefficients defined on a given (mesh, ) tuple. Arrays use indices (i,j,k,e): element e,...
Definition: coef.f90:54
Standard preconditioned generalized minimal residual method (SX version)
Definition: gmres_sx.f90:49
Type for storing initial and final residuals in a Krylov solver.
Definition: krylov.f90:55
Base abstract type for a canonical Krylov method, solving .
Definition: krylov.f90:65
Defines a canonical Krylov preconditioner.
Definition: precon.f90:40