krypy icon indicating copy to clipboard operation
krypy copied to clipboard

Provide example for catching ConvergenceError

Open andrenarchy opened this issue 9 years ago • 3 comments

We should add an example to the documentation for catching a ConvergenceError. A lot of people are unfamiliar with try/except so we can probably help them here a bit.

@zimoun What do you think?

andrenarchy avatar Mar 21 '16 10:03 andrenarchy

@zimoun: something like this should do the trick:

from krypy.utils import ConvergenceError
import warnings

# run a solver and catch ConvergenceErrors
def solve_unsafe(Solver, linear_system, **kwargs):
    try:
        return Solver(linear_system, **kwargs)
    except ConvergenceError as e:
        # warn the user (probably we should add more information to the message)
        warnings.warn('Solver did not converge.')
        # use solver of exception
        return e.solver

solver = solve_unsafe(Minres, linear_system, maxiter=5)

andrenarchy avatar Mar 21 '16 11:03 andrenarchy

yes, this kind of trick. In my branch converr, I do that in the solver itself but it is not clean.

So, maybe, you could add this snippet of code in linsys.py or just in an example.

zimoun avatar Mar 21 '16 11:03 zimoun

+1 for example, that's the most visible way.

andrenarchy avatar Mar 21 '16 11:03 andrenarchy