[petsc-users] petsc4py did not raise for a second time with option `ksp_error_if_not_converged`

Zongze Yang yangzongze at gmail.com
Tue Mar 7 00:09:52 CST 2023


Thank you for your suggestion.

Best wishes,
Zongze


On Mon, 6 Mar 2023 at 02:40, Matthew Knepley <knepley at gmail.com> wrote:

> On Sun, Mar 5, 2023 at 3:14 AM Zongze Yang <yangzongze at gmail.com> wrote:
>
>>
>>
>> Hello,
>>
>> I am trying to catch the "not converged" error in a loop with the
>> `ksp_error_if_not_converged` option on. However, it seems that PETSc only
>> raises the exception once, even though the solver does not converge after
>> that. Is this expected behavior? Can I make it raise an exception every
>> time?
>>
>
> When an error is raised, we do not guarantee a consistent state for
> recovery, so errors terminate the program. If you want
> to do something useful with non-convergence, then you do not set
> -ksp_error_if_not_converged. Rather you check the convergence
> code, and if it is not convergence, you take your action.
>
>   Thanks,
>
>      Matt
>
>
>> I have included a code snippet of the loop below, and the complete code
>> is attached:
>> ```python
>> for i in range(3):
>>     printf(f"Loop i = {i}")
>>     try:
>>         solver.solve()
>>     except ConvergenceError:
>>         printf(f"  Error from Firedrake: solver did not converged:
>> {get_ksp_reason(solver)}")
>>     except PETSc.Error as e:
>>         if e.ierr == 91:
>>             printf(f"  Error from PETSc: solver did not converged:
>> {get_ksp_reason(solver)}")
>>         else:
>>             raise
>> ```
>>
>> The output of the code looks like this:
>> ```python
>> (complex-int32-mkl) $ python test_error.py
>> Loop i = 0
>>   Linear  solve did not converge due to DIVERGED_ITS iterations 4
>>   Error from PETSc: solver did not converged: DIVERGED_MAX_IT
>> Loop i = 1
>>   Linear  solve did not converge due to DIVERGED_ITS iterations 4
>>   Error from Firedrake: solver did not converged: DIVERGED_MAX_IT
>> Loop i = 2
>>   Linear  solve did not converge due to DIVERGED_ITS iterations 4
>>   Error from Firedrake: solver did not converged: DIVERGED_MAX_IT
>> ```
>>
>> Best wishes,
>> Zongze
>>
>
>
> --
> 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://www.cse.buffalo.edu/~knepley/
> <http://www.cse.buffalo.edu/~knepley/>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20230307/3c753837/attachment.html>


More information about the petsc-users mailing list