Working example in 
  https://gitlab.com/petsc/petsc/-/merge_requests/6430

On May 7, 2023, at 11:47 AM, Barry Smith <bsmith@petsc.dev> wrote:


  Note, you must call the default test on iteration 0. This is how it determines the initial residual for relative residual tests etc. 

  I recommend not having the if () test at all. Instead, always call the default convergence test first and then change the flag value it provides if needed before doing any custom checking.

On May 7, 2023, at 11:38 AM, Barry Smith <bsmith@petsc.dev> wrote:


   The code will not work as written because KSPConvergedDefault() requires a context created with KSPConvergedDefaultCreate(). 

   Here is a starting point for what you need, in main

   integer*8 defaultctx
   extern MyKSPConverged, KSPConvergedDefaultDestroy

KSPDefaultConvergedCreate(defaultctx,ierr)
KSPSetConvergenceTest(ksp,MyKSPConverged,defaultctx,KSPConvergedDefaultDestroy, ierr)


subroutine MyKSPConverged(ksp,n,rnorm,flag,defaultctx,ierr)

       KSP              ksp
       PetscErrorCode ierr
       PetscInt n
       integer*8 defaultctx
       KSPConvergedReason flag
       PetscReal rnorm

       if (n>1) then
         call KSPConvergedDefault(ksp, n, rnorm, flag,defaultctx, ierr)
       else
         flag = 0
       endif
       ierr = 0

    end subroutine MyKSPConverged



On May 7, 2023, at 10:09 AM, Matthew Knepley <knepley@gmail.com> wrote:

On Sun, May 7, 2023 at 10:02 AM Edoardo alinovi <edoardo.alinovi@gmail.com> wrote:
Thanks, 

Is this a reasonable thing to do if I want to replicate what KSP is doing by default?

Yes. The other option is to pass along 'dummy'

  Thanks,

    Matt

--
What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.
-- Norbert Wiener