Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the PETSc TS interfaces conform to callback conventions. #15269

Merged
merged 5 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
125 changes: 95 additions & 30 deletions include/deal.II/lac/petsc_ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

# include <petscts.h>

# include <exception>

# if defined(DEAL_II_HAVE_CXX20)
# include <concepts>
# endif
Expand Down Expand Up @@ -421,11 +423,19 @@ namespace PETScWrappers

/**
* Callback for the computation of the implicit residual $F(t, y, \dot y)$.
*/
std::function<int(const real_type t,
const VectorType &y,
const VectorType &y_dot,
VectorType & res)>
*
* @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 TS 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.
*/
std::function<void(const real_type t,
const VectorType &y,
const VectorType &y_dot,
VectorType & res)>
implicit_function;

/**
Expand All @@ -436,40 +446,72 @@ namespace PETScWrappers
* All implicit solvers implementations are recast to use the above
* linearization. The $\alpha$ parameter is time-step and solver-type
* specific.
*/
std::function<int(const real_type t,
const VectorType &y,
const VectorType &y_dot,
const real_type alpha,
AMatrixType & A,
PMatrixType & P)>
*
* @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 TS 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.
*/
std::function<void(const real_type t,
const VectorType &y,
const VectorType &y_dot,
const real_type alpha,
AMatrixType & A,
PMatrixType & P)>
implicit_jacobian;

/**
* Callback for the computation of the explicit residual $G(t, y)$.
*/
std::function<int(const real_type t, const VectorType &y, VectorType &res)>
*
* @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 TS 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.
*/
std::function<void(const real_type t, const VectorType &y, VectorType &res)>
explicit_function;

/**
* Callback for the computation of the explicit Jacobian $\dfrac{\partial
* G}{\partial y}$.
*/
std::function<int(const real_type t,
const VectorType &y,
AMatrixType & A,
PMatrixType & P)>
*
* @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 TS 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.
*/
std::function<void(const real_type t,
const VectorType &y,
AMatrixType & A,
PMatrixType & P)>
explicit_jacobian;

/**
* Callback for monitoring the solution process.
*
* This function is called by TimeStepper at the beginning
* of each time step.
*/
std::function<int(const real_type t,
const VectorType & y,
const unsigned int step_number)>
*
* @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 TS 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.
*/
std::function<void(const real_type t,
const VectorType & y,
const unsigned int step_number)>
monitor;

/**
Expand All @@ -485,20 +527,36 @@ namespace PETScWrappers
* specific.
*
* Solvers must be provided via TimeStepper::solve_with_jacobian.
*/
std::function<int(const real_type t,
const VectorType &y,
const VectorType &ydot,
const real_type alpha)>
*
* @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 TS 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.
*/
std::function<void(const real_type t,
const VectorType &y,
const VectorType &ydot,
const real_type alpha)>
setup_jacobian;

/**
* Callback for the solution of the tangent system set up with
* TimeStepper::setup_jacobian.
*
* This is used as a preconditioner inside the Krylov process.
*/
std::function<int(const VectorType &src, VectorType &dst)>
*
* @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 TS 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.
*/
std::function<void(const VectorType &src, VectorType &dst)>
solve_with_jacobian;

/**
Expand Down Expand Up @@ -529,6 +587,13 @@ namespace PETScWrappers
* This flag is used to support versions of PETSc older than 3.13.
*/
bool need_dummy_assemble;

/**
* A pointer to any exception that may have been thrown in user-defined
* call-backs and that we have to deal after the KINSOL function we call
* has returned.
*/
mutable std::exception_ptr pending_exception;
};

} // namespace PETScWrappers
Expand Down