Skip to content

Add option for resetting failing Newton solver #38

@finsberg

Description

@finsberg

Sometimes when you solve a nonlinear problem, the solver might diverge which leaves the solver in a bad state.

Consider the following scenario

solver = NewtonSolver(...)
target_value = 1.0
step = 0.1
while parameter.value < target_value:
    parameter.value += step
    try:
        solver.solve()
    except RuntimeError:
        # Do smaller step and reset solver
        parameter.value -= step
        step /= 2.0

However, when the solver fails it might be in a bad state which needs to be reset to the last good state. My suggestion would be to allow for resetting the state in a seamless way, e.g

solver = NewtonSolver(...)
target_value = 1.0
step = 0.1
while parameter.value < target_value:
    good_state = solver.state
    parameter.value += step
    try:
        solver.solve()
    except RuntimeError:
        parameter.value -= step
        step /= 2.0
        solver.reset_state(good_state)

or even implement a method called try_to_solve_or_reset (or just make it default to reset the state).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions