mimesis-inria / caribou

Multi-physics computation library
GNU Lesser General Public License v3.0
29 stars 17 forks source link

Add callback mechanism to Newton-Raphson solvers #107

Open jnbrunet opened 2 years ago

jnbrunet commented 2 years ago

Solvers inheriting Caribou's Newton Raphson solver class can now register callbacks at different steps of a Newton iteration.

For example, the following C++ code prints the stiffness matrix just after the latter as been assembled:

using E = SofaCaribou::ode::NewtonRaphsonSolver::Event;
using NR = SofaCaribou::ode::NewtonRaphsonSolver;

solver->register_callback(E::MATRIX_ASSEMBLED, [](const NR & solver) {
    std::cout << solver.A();
});

Or to get the solution vector once solved in python

from SofaCaribou import NewtonRaphsonSolver
E = NewtonRaphsonSolver.Event

root.solver.register_callback(
    E.INCREMENT_SOLVED,   
    lambda s: print(s.dx())
)

The following events are available:

enum class Event : unsigned int {
    /** Event triggered at the very beginning of the Newton iteration. */
    ITERATION_BEGIN = 0,

    /** Event triggered after the system matrix has been assembled. */
    MATRIX_ASSEMBLED,

    /** Event triggered after the system matrix has been analyzed. */
    MATRIX_ANALYZED,

    /** Event triggered after the system matrix has been factorized. */
    MATRIX_FACTORIZED,

    /** Event triggered after the system solution vector has been solved. */
    INCREMENT_SOLVED,

    /** Event triggered after the system solution vector has been propagated through mappings. */
    INCREMENT_PROPAGATED,

    /** Event triggered after the system force vector has been updated. */
    RESIDUAL_UPDATED,

    /** Event triggered at the very end of the Newton iteration. */
    ITERATION_END,
};