Skip to content

balance_solver

BalanceSolver

BalanceSolver(
    perturb=0.0001,
    stop_condition=0.01,
    relax_ratio=0.8,
    relax_power=3,
    max_iter=100,
)

Solver for balance models using a Newton-Raphson method. Takes a model implementing IModelForSolver as input, and solves it using.

The main difference with a classic Newton-Raphson method is that the correction of the state vector is using a custom formula, using a relaxation value that decreases with the number of iterations.

The jacobian matrix is computed using finite differences.

1
2
3
4
5
>>> balance_model = ...  # some model implementing IModelForSolver
>>> solver = BalanceSolver()
>>> solver.solve(balance_model)
>>> balance_model.state_vector  # updated state vector after solving
np.array([...])

Raises:

Type Description
ConvergenceError

if the solver fails to converge within max_iter iterations.

Source code in src/mechaphlowers/core/models/balance/solvers/balance_solver.py
40
41
42
43
44
45
46
47
48
49
50
51
52
def __init__(
    self,
    perturb=0.0001,
    stop_condition=1e-2,
    relax_ratio=0.8,
    relax_power=3,
    max_iter=100,
) -> None:
    self.perturb = perturb
    self.stop_condition = stop_condition
    self.relax_ratio = relax_ratio
    self.relax_power = relax_power
    self.max_iter = max_iter