Skip to content

Commit

Permalink
PETScWrappers: allow recoverable errors in NonlinearSolver and TimeSt…
Browse files Browse the repository at this point in the history
…epper
  • Loading branch information
stefanozampini committed Jun 12, 2023
1 parent 00e6fe7 commit 05a5611
Show file tree
Hide file tree
Showing 16 changed files with 3,414 additions and 338 deletions.
13 changes: 13 additions & 0 deletions include/deal.II/lac/petsc_compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ namespace PETScWrappers
void
petsc_increment_state_counter(Mat A);

/**
* Resets internal domain error flags in the SNES object.
*/
void
snes_reset_domain_flags(SNES snes);

/**
* Tell PETSc nonlinear solver to use matrix free finite differencing (MFFD).
*
Expand Down Expand Up @@ -151,6 +157,13 @@ namespace PETScWrappers
*/
unsigned int
ts_get_step_number(TS ts);

/**
* Return true if the TS has a SNES object.
*/
bool
ts_has_snes(TS ts);

} // namespace PETScWrappers

DEAL_II_NAMESPACE_CLOSE
Expand Down
102 changes: 78 additions & 24 deletions include/deal.II/lac/petsc_snes.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,9 @@ namespace PETScWrappers
* @note This variable represents a
* @ref GlossUserProvidedCallBack "user provided callback".
* See there for a description of how to deal with errors and other
* requirements and conventions. PETSc's SNES package can not deal
* with "recoverable" errors, so if a callback
* throws an exception of type RecoverableUserCallbackError, then this
* exception is treated like any other exception.
* requirements and conventions.
* The recovery callback can be customized using
* NonlinearSolver::recoverable_action_residual.
*/
std::function<void(const VectorType &x, VectorType &res)> residual;

Expand All @@ -383,10 +382,9 @@ namespace PETScWrappers
* @note This variable represents a
* @ref GlossUserProvidedCallBack "user provided callback".
* See there for a description of how to deal with errors and other
* requirements and conventions. PETSc's SNES package can not deal
* with "recoverable" errors, so if a callback
* throws an exception of type RecoverableUserCallbackError, then this
* exception is treated like any other exception.
* requirements and conventions.
* The recovery callback can be customized using
* NonlinearSolver::recoverable_action_jacobian.
*/
std::function<void(const VectorType &x, AMatrixType &A, PMatrixType &P)>
jacobian;
Expand All @@ -401,10 +399,9 @@ namespace PETScWrappers
* @note This variable represents a
* @ref GlossUserProvidedCallBack "user provided callback".
* See there for a description of how to deal with errors and other
* requirements and conventions. PETSc's SNES package can not deal
* with "recoverable" errors, so if a callback
* throws an exception of type RecoverableUserCallbackError, then this
* exception is treated like any other exception.
* requirements and conventions.
* The recovery callback can be customized using
* NonlinearSolver::recoverable_action_monitor.
*/
std::function<void(const VectorType & x,
const unsigned int step_number,
Expand All @@ -422,10 +419,9 @@ namespace PETScWrappers
* @note This variable represents a
* @ref GlossUserProvidedCallBack "user provided callback".
* See there for a description of how to deal with errors and other
* requirements and conventions. PETSc's SNES package can not deal
* with "recoverable" errors, so if a callback
* throws an exception of type RecoverableUserCallbackError, then this
* exception is treated like any other exception.
* requirements and conventions.
* The recovery callback can be customized using
* NonlinearSolver::recoverable_action_jacobian.
*/
std::function<void(const VectorType &x)> setup_jacobian;

Expand All @@ -438,10 +434,9 @@ namespace PETScWrappers
* @note This variable represents a
* @ref GlossUserProvidedCallBack "user provided callback".
* See there for a description of how to deal with errors and other
* requirements and conventions. PETSc's SNES package can not deal
* with "recoverable" errors, so if a callback
* throws an exception of type RecoverableUserCallbackError, then this
* exception is treated like any other exception.
* requirements and conventions.
* The recovery callback can be customized using
* NonlinearSolver::recoverable_action_solve_with_jacobian.
*/
std::function<void(const VectorType &src, VectorType &dst)>
solve_with_jacobian;
Expand All @@ -461,19 +456,69 @@ namespace PETScWrappers
* @note This variable represents a
* @ref GlossUserProvidedCallBack "user provided callback".
* See there for a description of how to deal with errors and other
* requirements and conventions. PETSc's SNES package can not deal
* with "recoverable" errors, so if a callback
* throws an exception of type RecoverableUserCallbackError, then this
* exception is treated like any other exception.
* requirements and conventions.
* The recovery callback can be customized using
* NonlinearSolver::recoverable_action_energy.
*/
std::function<void(const VectorType &x, real_type &energy_value)> energy;

/**
* Callback for the recoverable action from a NonlinearSolver::residual.
*
* Deal.II internally communicates the necessary actions to the PETSc
* object. This additional callback returns control to the user
* for additional cleanup.
*/
std::function<void()> recoverable_action_residual;

/**
* Callback for the recoverable action from a NonlinearSolver::energy.
*
* Deal.II internally communicates the necessary actions to the PETSc
* object. This additional callback returns control to the user
* for additional cleanup.
*/
std::function<void()> recoverable_action_energy;

/**
* Callback for the recoverable action from a NonlinearSolver::jacobian
* or NonlinearSolver::setup_jacobian.
*
* Deal.II internally communicates the necessary actions to the PETSc
* object. This additional callback returns control to the user
* for additional cleanup.
*/
std::function<void()> recoverable_action_jacobian;

/**
* Callback for the recoverable action from a
* NonlinearSolver::solve_with_jacobian.
*
* Deal.II internally communicates the necessary actions to the PETSc
* object. This additional callback returns control to the user
* for additional cleanup.
*/
std::function<void()> recoverable_action_solve_with_jacobian;

/**
* Callback for the recoverable action from a
* NonlinearSolver::monitor.
*
* Deal.II internally communicates the necessary actions to the PETSc
* object. This additional callback returns control to the user
* for additional cleanup.
*/
std::function<void()> recoverable_action_monitor;

private:
/**
* The PETSc SNES object.
*/
SNES snes;

/**
* Pointers to the internal PETSc matrix objects.
*/
SmartPointer<AMatrixType, NonlinearSolver> A;
SmartPointer<PMatrixType, NonlinearSolver> P;

Expand All @@ -488,6 +533,15 @@ namespace PETScWrappers
* has returned.
*/
mutable std::exception_ptr pending_exception;

/**
* Internal functions to handle recoverable errors.
*/
std::function<void()> recoverable_action_residual_internal;
std::function<void()> recoverable_action_jacobian_internal;
std::function<void()> recoverable_action_solve_with_jacobian_internal;
std::function<void()> recoverable_action_energy_internal;
std::function<void()> recoverable_action_monitor_internal;
};

} // namespace PETScWrappers
Expand Down

0 comments on commit 05a5611

Please sign in to comment.