138 function cacg_solve(this, Ax, x, f, n, coef, blst, gs_h, niter)
result(ksp_results)
139 class(
cacg_t),
intent(inout) :: this
140 class(
ax_t),
intent(in) :: ax
141 type(
field_t),
intent(inout) :: x
142 integer,
intent(in) :: n
143 real(kind=
rp),
dimension(n),
intent(in) :: f
144 type(
coef_t),
intent(inout) :: coef
146 type(
gs_t),
intent(inout) :: gs_h
148 integer,
optional,
intent(in) :: niter
149 integer :: i, j, k, l, iter, max_iter, s, ierr, it
150 real(kind=
rp) :: rnorm, rtr, rtz1, tmp
151 real(kind=
rp) :: beta(this%s+1), alpha(this%s+1), alpha1, alpha2, norm_fac
152 real(kind=
rp),
dimension(4*this%s+1,4*this%s+1) :: tt, g, gtt, temp, temp2
153 real(kind=
rp) :: p_c(4*this%s+1,this%s+1)
154 real(kind=
rp) :: r_c(4*this%s+1,this%s+1)
155 real(kind=
rp) :: z_c(4*this%s+1,this%s+1)
156 real(kind=
rp) :: x_c(4*this%s+1,this%s+1)
158 associate(pr => this%PR, r => this%r, p => this%p)
160 if (
present(niter))
then
163 max_iter = this%max_iter
165 norm_fac = 1.0_rp / sqrt(coef%volume)
170 call this%M%solve(p, r, n)
172 rtr =
glsc3(r, coef%mult, r, n)
173 rnorm = sqrt(rtr)*norm_fac
174 ksp_results%res_start = rnorm
175 ksp_results%res_final = rnorm
178 if(
abscmp(rnorm, 0.0_rp))
return
179 call this%monitor_start(
'CACG')
180 do while (iter < max_iter)
183 call copy(pr(1,2*s+2), r, n)
187 if (mod(i,2) .eq. 0)
then
188 call ax%compute(pr(1,i), pr(1,i-1), coef, x%msh, x%Xh)
189 call gs_h%gs_op_vector(pr(1,i), n, gs_op_add)
190 call blst%apply_scalar(pr(1,i), n)
192 call this%M%solve(pr(1,i), pr(1,i-1), n)
197 if (mod(i,2) == 0)
then
198 call this%M%solve(pr(1,i+1), pr(1,i), n)
200 call ax%compute(pr(1,i+1), pr(1,i), coef, x%msh, x%Xh)
201 call gs_h%gs_op_vector(pr(1,i+1), n, gs_op_add)
202 call blst%apply_scalar(pr(1,1+i), n)
207 call rzero(p_c, (4*s+1) * (s+1))
209 call rzero(r_c, (4*s+1) * (s+1))
210 r_c(2*s+2,1) = 1.0_rp
211 call mxm(tt, 4*s+1, r_c, 4*s+1, z_c,s+1)
212 call rzero(x_c, (4*s+1) * (s+1))
213 call rzero(temp, (4*s+1)**2)
215 do i = 0, n, neko_blk_size
217 if (i + neko_blk_size .le. n)
then
221 do k = 1, neko_blk_size
222 temp(it,1) = temp(it,1) &
223 + pr(i+k,j) * pr(i+k,l) * coef%mult(i+k,1,1,1)
232 temp(it,1) = temp(it,1) &
233 + pr(i+k,j) * pr(i+k,l) * coef%mult(i+k,1,1,1)
240 call mpi_allreduce(temp, temp2, it, &
251 call mxm(g,4*s+1, tt, 4*s+1,gtt,4*s+1)
256 call mxm(g, 4*s+1, r_c(1,j), 4*s+1,temp, 1)
257 call mxm(gtt, 4*s+1, p_c(1,j), 4*s+1,temp2, 1)
261 alpha1 = alpha1 + temp(i,1) * z_c(i,j)
262 alpha2 = alpha2 + temp2(i,1) * p_c(i,j)
264 alpha(j) = alpha1/alpha2
267 x_c(i,j+1) = x_c(i,j) + alpha(j) * p_c(i,j)
270 tmp = tmp + tt(i,k) * p_c(k,j)
272 r_c(i,j+1) = r_c(i,j) - alpha(j)*tmp
275 tmp = tmp + tt(i,k)*r_c(k,j+1)
280 call mxm(g,4*s+1,r_c(1,j+1),4*s+1,temp2,1)
283 alpha2 = alpha2 + temp2(i,1)*z_c(i,j+1)
285 beta(j) = alpha2 / alpha1
287 p_c(i,j+1) = z_c(i,j+1) + beta(j)*p_c(i,j)
294 do i = 0, n, neko_blk_size
295 if (i + neko_blk_size .le. n)
then
297 do k = 1, neko_blk_size
298 x%x(i+k,1,1,1) = x%x(i+k,1,1,1) + pr(i+k,j) * x_c(j,s+1)
299 p(i+k) = p(i+k) + pr(i+k,j) * p_c(j,s+1)
300 tmp = pr(i+k,j) * r_c(j,s+1)
301 r(i+k) = r(i+k) + tmp
304 do k = 1, neko_blk_size
305 rtr = rtr + r(i+k)**2 * coef%mult(i+k,1,1,1)
310 x%x(i+k,1,1,1) = x%x(i+k,1,1,1) + pr(i+k,j) * x_c(j,s+1)
311 p(i+k) = p(i+k) + pr(i+k,j) * p_c(j,s+1)
312 tmp = pr(i+k,j) * r_c(j,s+1)
313 r(i+k) = r(i+k) + tmp
317 rtr = rtr + r(i+k)**2 * coef%mult(i+k,1,1,1)
322 call mpi_allreduce(rtr, tmp, 1, &
324 rnorm = norm_fac*sqrt(tmp)
325 call this%monitor_iter(iter, rnorm)
326 if( rnorm <= this%abs_tol)
exit
328 call this%monitor_stop()
329 ksp_results%res_final = rnorm
330 ksp_results%iter = iter
331 ksp_results%converged = this%is_converged(iter, rnorm)