I assume you want to stop the SNES solve, but you do not want the program to
end immediately? Instead, you want SNESSolve() to end immediately with a very
useful message?
This is done with
https://petsc.org/release/manualpages/SNES/SNESSetFunctionDomainError/#snessetfunctiondomainerror
or
https://petsc.org/release/manualpages/SNES/SNESSetJacobianDomainError/#snessetjacobiandomainerror
In conjunction with using this, you must call
https://petsc.org/release/manualpages/SNES/SNESGetConvergedReason/#snesgetconvergedreason
immediately after each SNESSolve() and check if the reason is negative. The
specific values SNES_DIVERGED_FUNCTION_DOMAIN
<https://petsc.org/release/manualpages/SNES/SNESConvergedReason/> will be set
if you had called SNESSetFunctionDomainError() and
SNES_DIVERGED_JACOBIAN_DOMAIN
<https://petsc.org/release/manualpages/SNES/SNESConvergedReason/> will be set
if you had called SNESSetJacobianDomainError().
Also take a look at the Note in
https://petsc.org/release/manualpages/SNES/SNESSetFunctionDomainError/#snessetfunctiondomainerror.
This provides two alternatives to simply giving up on the nonlinear solve but
provides ways to either bound the tentative solutions into their physical
regime or to "nudge" the tentative solutions back into their physical regime.
PETSc errors are hard, meaning when one is encountered the program ends!
Thus one cannot use the error handler infrastructure to recover from errors,
even user created errors; that is why we provide the SNES domain "error"
mechanism since it does not involve the PETSc error handlers.
Good luck.
> On Jul 5, 2023, at 8:54 PM, Hongrui Yu <[email protected]> wrote:
>
> Hello PETSc users,
>
> I’d like to use the error handling mechanism to abort SNES mid-iteration if
> certain quantities become nonphysical by calling SETERRQ() inside
> FormFunction() or FormJacobian(). I’m wondering if users can define their own
> error handler routines to use with PetscPushErrorHandler() and
> PetscPopErrorHandler()? I’m also open to any suggestion on better ways to
> quit SNES mid-iteration.
>
> Thanks in advance for any help or advice!
>
> Hongrui