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.

> On May 11, 2025, at 11:50 AM, Matthew Knepley <knep...@gmail.com> wrote:
> 
> On Sun, May 11, 2025 at 11:39 AM hj2000--- via petsc-users 
> <petsc-users@mcs.anl.gov <mailto:petsc-users@mcs.anl.gov>> wrote:
>> Dear Developer:
>> 
>>    I am a user of PETSc, and I am using SNESLineSearchSetPostCheck to 
>> perform some actions when the line search fails. 
>> 
>> However, it seems that this function is only called when the line search is 
>> successful. Why is that?
>> 
> PostCheck is intended to work on the solution found by the line search.
>  
>> Do I need to do something else? 
>> 
> 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.
> 
>   Thanks,
> 
>      Matt
>  
>> My PETSc version is 3.18.6. Below is my code.
>> 
>>  
>>         auto LineSearchPostCheck = []( SNESLineSearch ls, Vec x, Vec y, Vec 
>> w, PetscBool* changed_Y,
>> 
>>                                        PetscBool* changed_W, void* ) {
>> 
>>             SNESLineSearchReason lscnv;
>> 
>>             SNESLineSearchGetReason( ls, &lscnv );
>> 
>>             SNES snes = nullptr;
>> 
>>             SNESLineSearchGetSNES( ls, &snes );
>> 
>>             SNESConvergedReason cnv;
>> 
>>             SNESGetConvergedReason( snes, &cnv );
>> 
>>             double lambda = 0.0;
>> 
>>             SNESLineSearchGetLambda( ls, &lambda );
>> 
>>             PetscPrintf( MPI_COMM_WORLD, "lambda=%lf\n", lambda );
>> 
>>             if ( lscnv != SNES_LINESEARCH_SUCCEEDED || lambda < 1e-4 )
>> 
>>             {
>> 
>>                 VecWAXPY( w, -0.1, y, x );
>> 
>>                 SNESLineSearchSetReason( ls, SNES_LINESEARCH_SUCCEEDED );
>> 
>>                 *changed_W = PETSC_TRUE;
>> 
>>             }
>> 
>>             return 0;
>> 
>>         };
>> 
>>         SNESLineSearchSetPostCheck( ls, LineSearchPostCheck, nullptr);
>> 
>> 
>> 
> 
> 
> 
> --
> 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
> 
> https://urldefense.us/v3/__https://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!Z0il6G4Id0NXRUQd4JbC4hlU6LO5ceYj5jMmzoFHYBCPBWWyTvEkjNbe9jg3fc9Vo-zY-NaRmg9F_ywWSi_0kO8$
>   
> <https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!Y6XIK823eTx-iRg60ThDREJsmpqY4jNxIxr-y3_Li6lAA2rs8JvxNjeeUrgfpgbcSCSaRzhBaYiBH_soe98y$>

Reply via email to