<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 14/02/2014 11:12 AM, Barry Smith
      wrote:<br>
    </div>
    <blockquote
      cite="mid:59CA4974-9471-4C66-B30B-E7DF7F535022@mcs.anl.gov"
      type="cite">
      <pre wrap="">
On Feb 14, 2014, at 1:07 PM, Danyang Su <a class="moz-txt-link-rfc2396E" href="mailto:danyang.su@gmail.com"><danyang.su@gmail.com></a> wrote:

</pre>
      <blockquote type="cite">
        <pre wrap="">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
</pre>
      </blockquote>
      <pre wrap="">
   Since it is not always available you need to compute it yourself.

</pre>
      <blockquote type="cite">
        <pre wrap="">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)

</pre>
      </blockquote>
      <pre wrap="">   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 </pre>
    </blockquote>
    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. <br>
        call KSPBuildSolution(ksp,PETSC_NULL_OBJECT,x,ierrcode)<br>
        call KSPGetRhs(ksp, b, ierrcode)<br>
        call KSPGetOperators(ksp,a,PETSC_NULL_OBJECT ,PETSC_NULL_OBJECT,
    ierrcode)<br>
        call MatMult(a, x, u, ierrcode)<br>
        call VecAXPY(u, neg_one, b, ierrcode)<br>
        call VecNormBegin(u, Norm_2, rnorm_true, ierrcode)<br>
        call VecNormEnd(u, Norm_2, rnorm_true, ierrcode)<br>
    <br>
    The residual in the first few time steps are as follows:<br>
    <br>
    Residual by KSPDefaultConverged<br>
    0 KSP preconditioned resid norm 6.641270532099e-002 true resid norm
    3.885510397175e+011 ||r(i)||/||b|| 1.000000000000e+000<br>
    1 KSP preconditioned resid norm 2.813454813221e-007 true resid norm
    4.527102473818e+008 ||r(i)||/||b|| 1.165124272247e-003<br>
    0 KSP preconditioned resid norm 1.797117552956e-002 true resid norm
    1.165522868790e+011 ||r(i)||/||b|| 1.000000000000e+000<br>
    <b><i>1 KSP preconditioned resid norm 7.459908279054e-008 true resid
        norm 1.612654804265e+008 ||r(i)||/||b|| 1.383632056862e-003</i></b><br>
    <b><i>0 KSP preconditioned resid norm 5.245498186783e-003 true resid
        norm 3.496944019232e+010 ||r(i)||/||b|| 1.000000000000e+000</i></b><b><i><br>
      </i></b><b><i>1 KSP preconditioned resid norm 4.876784688167e-008
        true resid norm 9.615426089812e+007 ||r(i)||/||b||
        2.749665432712e-003</i></b><br>
    <br>
    Residual by MYKSPConverged<br>
    0 KSP preconditioned resid norm 6.641270532099e-002 true resid norm
    3.885510397175e+011 ||r(i)||/||b|| 1.000000000000e+000<br>
    1 KSP preconditioned resid norm 2.813454813221e-007 true resid norm
    4.527102473818e+008 ||r(i)||/||b|| 1.165124272247e-003<br>
    0 KSP preconditioned resid norm 1.797117552956e-002 true resid norm
    1.165522868790e+011 ||r(i)||/||b|| 1.000000000000e+000<br>
    <i><b>1 KSP preconditioned resid norm 7.459908279054e-008 true resid
        norm 1.165341354855e+011 ||r(i)||/||b|| 9.998442639434e-001</b></i><br>
    <i><b>0 KSP preconditioned resid norm 7.321104403842e-003 true resid
        norm 4.661428829096e+010 ||r(i)||/||b|| 1.000000000000e+000</b></i><i><b><br>
      </b></i><i><b>1 KSP preconditioned resid norm 2.815380746230e-008
        true resid norm 4.661536958529e+010 ||r(i)||/||b||
        1.000023196628e+000</b></i><br>
    <br>
    Is there anything wrong in MyKSPConverged function because the
    result get even worse later and at last failed in newton iteration.<br>
    <br>
    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. <br>
    <br>
      0 KSP preconditioned resid norm 4.208619583781e-002 true resid
    norm 2.227983625699e-005 ||r(i)||/||b|| 1.000000000000e+000
    <br>
      1 KSP preconditioned resid norm 6.403275610900e-003 true resid
    norm 2.795175726580e-007 ||r(i)||/||b|| 1.254576422528e-002
    <br>
    <i><b>  2 KSP preconditioned resid norm 7.416300263102e-004 true
        resid norm 3.382118379502e-010 ||r(i)||/||b||
        1.518017610404e-005  
      </b></i><i><b>I want to stop iteration here as the true residual
        meet the stop criteria.</b></i><br>
      3 KSP preconditioned resid norm 6.144409100696e-005 true resid
    norm 1.260466100377e-009 ||r(i)||/||b|| 5.657429820568e-005
    <br>
      4 KSP preconditioned resid norm 4.990158094666e-005 true resid
    norm 3.632496726225e-008 ||r(i)||/||b|| 1.630396509349e-003
    <br>
      5 KSP preconditioned resid norm 2.743769015994e-005 true resid
    norm 1.101666758939e-009 ||r(i)||/||b|| 4.944680680017e-005
    <br>
      6 KSP preconditioned resid norm 4.269805798759e-006 true resid
    norm 7.915494521121e-011 ||r(i)||/||b|| 3.552761532813e-006
    <br>
      7 KSP preconditioned resid norm 1.632413041808e-006 true resid
    norm 1.163794439302e-009 ||r(i)||/||b|| 5.223532282187e-005
    <br>
      8 KSP preconditioned resid norm 8.010590587547e-007 true resid
    norm 1.246839309256e-011 ||r(i)||/||b|| 5.596267831030e-007
    <br>
      9 KSP preconditioned resid norm 4.794695759395e-007 true resid
    norm 3.155943984006e-012 ||r(i)||/||b|| 1.416502324166e-007
    <br>
     10 KSP preconditioned resid norm 4.500033799628e-007 true resid
    norm 4.554787575528e-011 ||r(i)||/||b|| 2.044354151884e-006
    <br>
    <br>
    Thanks,<br>
    <br>
    Danyang<br>
    <blockquote
      cite="mid:59CA4974-9471-4C66-B30B-E7DF7F535022@mcs.anl.gov"
      type="cite">
      <pre wrap="">

Don’t include the -ksp_set_pc_side right if you are using CG.

   Barry


</pre>
      <blockquote type="cite">
        <pre wrap="">Thanks and regards,

Danyang
</pre>
      </blockquote>
      <pre wrap="">
</pre>
    </blockquote>
    <br>
  </body>
</html>