<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div><br></div>   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?<div><br></div><div>    This is done with <a href="https://petsc.org/release/manualpages/SNES/SNESSetFunctionDomainError/#snessetfunctiondomainerror">https://petsc.org/release/manualpages/SNES/SNESSetFunctionDomainError/#snessetfunctiondomainerror</a> or <a href="https://petsc.org/release/manualpages/SNES/SNESSetJacobianDomainError/#snessetjacobiandomainerror">https://petsc.org/release/manualpages/SNES/SNESSetJacobianDomainError/#snessetjacobiandomainerror</a></div><div><br></div><div>    In conjunction with using this, you must call <a href="https://petsc.org/release/manualpages/SNES/SNESGetConvergedReason/#snesgetconvergedreason">https://petsc.org/release/manualpages/SNES/SNESGetConvergedReason/#snesgetconvergedreason</a> immediately after each SNESSolve() and check if the reason is negative. The specific values <span class="w" style="caret-color: rgb(50, 50, 50); font-family: var(--pst-font-family-monospace); font-size: 13.125px; box-sizing: border-box; color: rgb(248, 248, 248);"> </span><span class="n" style="caret-color: rgb(50, 50, 50); font-family: var(--pst-font-family-monospace); box-sizing: border-box;"><a href="https://petsc.org/release/manualpages/SNES/SNESConvergedReason/" style="box-sizing: border-box; color: var(--pst-color-link); text-decoration: none;">SNES_DIVERGED_FUNCTION_DOMAIN</a> will be set if you had called SNESSetFunctionDomainError() and </span><span class="w" style="caret-color: rgb(50, 50, 50); font-family: var(--pst-font-family-monospace); box-sizing: border-box; color: rgb(248, 248, 248);"></span><a href="https://petsc.org/release/manualpages/SNES/SNESConvergedReason/" style="caret-color: rgb(50, 50, 50); box-sizing: border-box; color: var(--pst-color-link); text-decoration: none;">SNES_DIVERGED_JACOBIAN_DOMAIN</a><span style="caret-color: rgb(50, 50, 50);"> will be set if you had called SNESSetJacobianDomainError().</span></div><div><span style="caret-color: rgb(50, 50, 50);"><br></span></div><div><span style="caret-color: rgb(50, 50, 50);">   Also take a look at the Note in </span><a href="https://petsc.org/release/manualpages/SNES/SNESSetFunctionDomainError/#snessetfunctiondomainerror">https://petsc.org/release/manualpages/SNES/SNESSetFunctionDomainError/#snessetfunctiondomainerror</a>. 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.</div><div><br></div><div>   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.</div><div><br></div><div>   Good luck.</div><div><span style="caret-color: rgb(50, 50, 50); font-size: 13.125px;"><br></span></div><div><div><br><blockquote type="cite"><div>On Jul 5, 2023, at 8:54 PM, Hongrui Yu <yuhongrui@utexas.edu> wrote:</div><br class="Apple-interchange-newline"><div><meta charset="UTF-8"><div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 18px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Hello PETSc users,<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">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.<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Thanks in advance for any help or advice!<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Hongrui</div></div></div></blockquote></div><br></div></body></html>