# [petsc-users] KSP solver iteration stop criteria

Danyang Su danyang.su at gmail.com
Sun Feb 16 02:04:22 CST 2014

```Thanks for the quick reply.
I just made a second check on it. I use the preconditioned norm type for
both runs. The ex2f was taken as the example to implement
MyKSPConverged. For KSPDefaultConverged, I comment out the codes between
"!***", for MyKSPConverged, just uncomment this section. See below.

subroutine mykspconverged(ksp,n,rnorm,flag,dummy,ierr)
implicit none
KSP                :: ksp
PetscErrorCode     :: ierr
PetscInt           :: n,dummy
KSPConvergedReason :: flag
PetscReal          :: rnorm

!****This section is comment out for KSPDefaultConverged option****
if(n > 0) then
call KSPBuildSolution(ksp,PETSC_NULL_OBJECT,x,ierrcode)
call KSPGetRhs(ksp, b, ierrcode)
call KSPGetOperators(ksp,a,PETSC_NULL_OBJECT
,PETSC_NULL_OBJECT, ierrcode)
call MatMult(a, x, u, ierrcode)
call VecAXPY(u, neg_one, b, ierrcode)
call VecNormBegin(u, Norm_2, rnorm, ierrcode)
call VecNormEnd(u, Norm_2, rnorm, ierrcode)
end if
!***************************************************************

if (n > 0 .and. rnorm< rtol) then
flag = 1
else
flag = 0
endif
ierr = 0
end subroutine mykspconverged

On 15/02/2014 10:05 PM, Barry Smith wrote:
>    What is different in the two runs below? Are you using -ksp_norm_type unpreconditioned in one case and not the other?
>
>    What KSPType are you using?
>
>     If the options are identical the residual norms (both preconditioned and true) should be identical for each run, changing the convergence test should not affect the residuals being printed at all (it would only possibly change when it decides it has converged).
>
>
>      Barry
>
> On Feb 15, 2014, at 11:40 PM, Danyang Su <danyang.su at gmail.com> wrote:
>>> -ksp_norm_type unpreconditioned  -ksp_set_pc_side right
>>>
>> The "-ksp_norm_type unpreconditioned" does not work well for my cases. When I set the following function in MyKSPConverged function, the result is different from KSPDefaultConverged though the same number of iteration is used.
>>      call KSPBuildSolution(ksp,PETSC_NULL_OBJECT,x,ierrcode)
>>      call KSPGetRhs(ksp, b, ierrcode)
>>      call KSPGetOperators(ksp,a,PETSC_NULL_OBJECT ,PETSC_NULL_OBJECT, ierrcode)
>>      call MatMult(a, x, u, ierrcode)
>>      call VecAXPY(u, neg_one, b, ierrcode)
>>      call VecNormBegin(u, Norm_2, rnorm_true, ierrcode)
>>      call VecNormEnd(u, Norm_2, rnorm_true, ierrcode)
>>
>> The residual in the first few time steps are as follows:
>>
>> Residual by KSPDefaultConverged
>> 0 KSP preconditioned resid norm 6.641270532099e-002 true resid norm 3.885510397175e+011 ||r(i)||/||b|| 1.000000000000e+000
>> 1 KSP preconditioned resid norm 2.813454813221e-007 true resid norm 4.527102473818e+008 ||r(i)||/||b|| 1.165124272247e-003
>> 0 KSP preconditioned resid norm 1.797117552956e-002 true resid norm 1.165522868790e+011 ||r(i)||/||b|| 1.000000000000e+000
>> 1 KSP preconditioned resid norm 7.459908279054e-008 true resid norm 1.612654804265e+008 ||r(i)||/||b|| 1.383632056862e-003
>> 0 KSP preconditioned resid norm 5.245498186783e-003 true resid norm 3.496944019232e+010 ||r(i)||/||b|| 1.000000000000e+000
>> 1 KSP preconditioned resid norm 4.876784688167e-008 true resid norm 9.615426089812e+007 ||r(i)||/||b|| 2.749665432712e-003
>>
>> Residual by MYKSPConverged
>> 0 KSP preconditioned resid norm 6.641270532099e-002 true resid norm 3.885510397175e+011 ||r(i)||/||b|| 1.000000000000e+000
>> 1 KSP preconditioned resid norm 2.813454813221e-007 true resid norm 4.527102473818e+008 ||r(i)||/||b|| 1.165124272247e-003
>> 0 KSP preconditioned resid norm 1.797117552956e-002 true resid norm 1.165522868790e+011 ||r(i)||/||b|| 1.000000000000e+000
>> 1 KSP preconditioned resid norm 7.459908279054e-008 true resid norm 1.165341354855e+011 ||r(i)||/||b|| 9.998442639434e-001
>> 0 KSP preconditioned resid norm 7.321104403842e-003 true resid norm 4.661428829096e+010 ||r(i)||/||b|| 1.000000000000e+000
>> 1 KSP preconditioned resid norm 2.815380746230e-008 true resid norm 4.661536958529e+010 ||r(i)||/||b|| 1.000023196628e+000
>>
>> Is there anything wrong in MyKSPConverged function because the result get even worse later and at last failed in newton iteration.
>>
>> If I use KSPDefaultConverged, it can work but after some steps, the true residual norm become very small but the solver still need a lot of iteration as the relative preconditioned residual norm does not match the convergence criteria. What I need is to stop the iteration when either preconditioned residual norm (rtol) or true residual norm matches the stop criteria.
>>
>>    0 KSP preconditioned resid norm 4.208619583781e-002 true resid norm 2.227983625699e-005 ||r(i)||/||b|| 1.000000000000e+000
>>    1 KSP preconditioned resid norm 6.403275610900e-003 true resid norm 2.795175726580e-007 ||r(i)||/||b|| 1.254576422528e-002
>>    2 KSP preconditioned resid norm 7.416300263102e-004 true resid norm 3.382118379502e-010 ||r(i)||/||b|| 1.518017610404e-005   I want to stop iteration here as the true residual meet the stop criteria.
>>    3 KSP preconditioned resid norm 6.144409100696e-005 true resid norm 1.260466100377e-009 ||r(i)||/||b|| 5.657429820568e-005
>>    4 KSP preconditioned resid norm 4.990158094666e-005 true resid norm 3.632496726225e-008 ||r(i)||/||b|| 1.630396509349e-003
>>    5 KSP preconditioned resid norm 2.743769015994e-005 true resid norm 1.101666758939e-009 ||r(i)||/||b|| 4.944680680017e-005
>>    6 KSP preconditioned resid norm 4.269805798759e-006 true resid norm 7.915494521121e-011 ||r(i)||/||b|| 3.552761532813e-006
>>    7 KSP preconditioned resid norm 1.632413041808e-006 true resid norm 1.163794439302e-009 ||r(i)||/||b|| 5.223532282187e-005
>>    8 KSP preconditioned resid norm 8.010590587547e-007 true resid norm 1.246839309256e-011 ||r(i)||/||b|| 5.596267831030e-007
>>    9 KSP preconditioned resid norm 4.794695759395e-007 true resid norm 3.155943984006e-012 ||r(i)||/||b|| 1.416502324166e-007
>>   10 KSP preconditioned resid norm 4.500033799628e-007 true resid norm 4.554787575528e-011 ||r(i)||/||b|| 2.044354151884e-006
>>
>> Thanks,
>>
>> Danyang
>>> Don’t include the -ksp_set_pc_side right if you are using CG.
>>>
>>>     Barry
>>>
>>>
>>>
>>>> Thanks and regards,
>>>>
>>>> Danyang
>>>>

```