[petsc-users] KSP solver iteration stop criteria

Danyang Su danyang.su at gmail.com
Sat Feb 15 23:40:58 CST 2014

```On 14/02/2014 11:12 AM, Barry Smith wrote:
> On Feb 14, 2014, at 1:07 PM, Danyang Su <danyang.su at gmail.com> wrote:
>
>> Hi All,
>>
>> Can I control KSP solver iteration by user defined criteria? I would like to check both preconditioned residual norm and true residual norm in every iteration, and force the solver to stop iteration if either criteria matches.
>>
>> If using KSPSetConvergenceTest, can I get the true residual norm directly from a function
>     Since it is not always available you need to compute it yourself.
>
>> or shall I compute it in the user-defined convergence function:
>>
>>       call KSPBuildSolution(ksp,PETSC_NULL_OBJECT,x,ierr)
>>       call KSPGetRhs(ksp, b, ierr)
>>       call KSPGetOperators(ksp,a,PETSC_NULL_OBJECT ,PETSC_NULL_OBJECT, ierr)
>>       call MatMult(a, x, u, ierr)
>>       call VecAXPY(u, -1.0d0, b, ierr)
>>       call VecNormBegin(u, Norm_2, rnorm_true, ierr)
>>       call VecNormEnd(u, Norm_2, rnorm_true, ierr)
>>
>     But note that this process is rather expensive (this is why we don’t do it in our convergence tests).
>
>     Better to just use right preconditioning and then the compute norm is the true residual norm and you just test on that.
>
> -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

-------------- next part --------------
An HTML attachment was scrubbed...