<div dir="ltr"><div dir="ltr">On Sun, Mar 5, 2023 at 3:14 AM Zongze Yang <<a href="mailto:yangzongze@gmail.com">yangzongze@gmail.com</a>> wrote:<br></div><div class="gmail_quote"><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"><div><br></div><div><br></div>Hello,<br><br><div>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?</div></div></blockquote><div><br></div><div>When an error is raised, we do not guarantee a consistent state for recovery, so errors terminate the program. If you want</div><div>to do something useful with non-convergence, then you do not set -ksp_error_if_not_converged. Rather you check the convergence</div><div>code, and if it is not convergence, you take your action.</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"><div>I have included a code snippet of the loop below, and the complete code is attached:</div><div>```python<br></div><div>for i in range(3):<br> printf(f"Loop i = {i}")<br> try:<br> solver.solve()<br> except ConvergenceError:<br> printf(f" Error from Firedrake: solver did not converged: {get_ksp_reason(solver)}")<br> except PETSc.Error as e:<br> if e.ierr == 91:<br> printf(f" Error from PETSc: solver did not converged: {get_ksp_reason(solver)}")<br> else:<br> raise<br><div><div dir="ltr"><div dir="ltr">```</div><div dir="ltr"><br></div><div dir="ltr"><div>The output of the code looks like this:</div><div>```python</div><div>(complex-int32-mkl) $ python test_error.py<br>Loop i = 0<br> Linear solve did not converge due to DIVERGED_ITS iterations 4<br> Error from PETSc: solver did not converged: DIVERGED_MAX_IT<br>Loop i = 1<br> Linear solve did not converge due to DIVERGED_ITS iterations 4<br> Error from Firedrake: solver did not converged: DIVERGED_MAX_IT<br>Loop i = 2<br> Linear solve did not converge due to DIVERGED_ITS iterations 4<br> Error from Firedrake: solver did not converged: DIVERGED_MAX_IT<br></div></div><div dir="ltr">```</div><div dir="ltr"><br></div><div dir="ltr">Best wishes,<div>Zongze</div></div></div></div></div></div>
</blockquote></div><br clear="all"><div><br></div>-- <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="http://www.cse.buffalo.edu/~knepley/" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div></div></div></div>