<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 suppose we could call the postcheck even on failure. Then your first check in your postcheck would be on SNESLineSearchReason. What line search are you using? You could add the postcheck to all the early PetscFunctionReturns() in the line search routine.<br id="lineBreakAtBeginningOfMessage"><div><br><blockquote type="cite"><div>On May 11, 2025, at 11:50 AM, Matthew Knepley <knepley@gmail.com> wrote:</div><br class="Apple-interchange-newline"><div><div dir="ltr"><div dir="ltr">On Sun, May 11, 2025 at 11:39 AM hj2000--- via petsc-users <<a href="mailto:petsc-users@mcs.anl.gov">petsc-users@mcs.anl.gov</a>> wrote:</div><div class="gmail_quote gmail_quote_container"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><p><span>Dear Developer:</span></p><p><span>   I am a user of PETSc, and I am using <code>SNESLineSearchSetPostCheck</code> to perform some actions when the line search fails. </span></p><p><span>However, it seems that this function is only called when the line search is successful. Why is that? </span></p></div></blockquote><div>PostCheck is intended to work on the solution found by the line search.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><p><span>Do I need to do something else? </span></p></div></blockquote><div>Yes. We do not have a specific hook for failure, because that would make it a different line search (one which may not obey the advertised properties). Thus we would want your scheme to be its own line search.</div><div><br></div><div>  Thanks,</div><div><br></div><div>     Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><p><span>My PETSc version is 3.18.6. Below is my code.</span></p><blockquote style="padding-left:5px;margin-left:5px;border-left:2px solid rgb(182,182,182)"><div><span></span><span> </span><br class="webkit-block-placeholder"></div><p>        auto LineSearchPostCheck = []( SNESLineSearch ls, Vec x, Vec y, Vec w, PetscBool* changed_Y,</p><p>                                       PetscBool* changed_W, void* ) {</p><p>            SNESLineSearchReason lscnv;</p><p>            SNESLineSearchGetReason( ls, &lscnv );</p><p>            SNES snes = nullptr;</p><p>            SNESLineSearchGetSNES( ls, &snes );</p><p>            SNESConvergedReason cnv;</p><p>            SNESGetConvergedReason( snes, &cnv );</p><p>            double lambda = 0.0;</p><p>            SNESLineSearchGetLambda( ls, &lambda );</p><p>            PetscPrintf( MPI_COMM_WORLD, "lambda=%lf\n", lambda );</p><p>            if ( lscnv != SNES_LINESEARCH_SUCCEEDED || lambda < 1e-4 )</p><p>            {</p><p>                VecWAXPY( w, -0.1, y, x );</p><p>                SNESLineSearchSetReason( ls, SNES_LINESEARCH_SUCCEEDED );</p><p>                *changed_W = PETSC_TRUE;</p><p>            }</p><p>            return 0;</p><p>        };</p><p>        SNESLineSearchSetPostCheck( ls, LineSearchPostCheck, nullptr);</p><p><span></span><br></p></blockquote></div></blockquote></div><div><br clear="all"></div><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener</div><div><br></div><div><a href="https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!Y6XIK823eTx-iRg60ThDREJsmpqY4jNxIxr-y3_Li6lAA2rs8JvxNjeeUrgfpgbcSCSaRzhBaYiBH_soe98y$" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div></div></div></div>
</div></blockquote></div><br></body></html>