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

Zongze Yang yangzongze at gmail.com
Sun Mar 5 02:14:12 CST 2023


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?

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20230305/23100b7c/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_error.py
Type: application/octet-stream
Size: 1476 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20230305/23100b7c/attachment.obj>


More information about the petsc-users mailing list