From 084c4ff8f8a960f19ce37c06ec012b237944f75f Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Wed, 24 May 2023 10:15:02 -0600 Subject: [PATCH] Avoid making a variable 'public'. --- include/deal.II/sundials/ida.h | 1 - source/sundials/ida.cc | 165 ++++++++++++++------------------- 2 files changed, 72 insertions(+), 94 deletions(-) diff --git a/include/deal.II/sundials/ida.h b/include/deal.II/sundials/ida.h index ae65a5f43e..8048fbdeba 100644 --- a/include/deal.II/sundials/ida.h +++ b/include/deal.II/sundials/ida.h @@ -906,7 +906,6 @@ namespace SUNDIALS */ GrowingVectorMemory mem; - public: /** * 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 diff --git a/source/sundials/ida.cc b/source/sundials/ida.cc index c149169af0..8b8597a477 100644 --- a/source/sundials/ida.cc +++ b/source/sundials/ida.cc @@ -116,95 +116,6 @@ namespace SUNDIALS } // namespace - namespace - { - template - int - residual_callback(realtype tt, - N_Vector yy, - N_Vector yp, - N_Vector rr, - void * user_data) - { - IDA &solver = *static_cast *>(user_data); - - auto *src_yy = internal::unwrap_nvector_const(yy); - auto *src_yp = internal::unwrap_nvector_const(yp); - auto *residual = internal::unwrap_nvector(rr); - - return call_and_possibly_capture_exception(solver.residual, - solver.pending_exception, - tt, - *src_yy, - *src_yp, - *residual); - } - - - - template - int - setup_jacobian_callback(realtype tt, - realtype cj, - N_Vector yy, - N_Vector yp, - N_Vector /* residual */, - SUNMatrix /* ignored */, - void *user_data, - N_Vector /* tmp1 */, - N_Vector /* tmp2 */, - N_Vector /* tmp3 */) - { - Assert(user_data != nullptr, ExcInternalError()); - IDA &solver = *static_cast *>(user_data); - - auto *src_yy = internal::unwrap_nvector_const(yy); - auto *src_yp = internal::unwrap_nvector_const(yp); - - return call_and_possibly_capture_exception(solver.setup_jacobian, - solver.pending_exception, - tt, - *src_yy, - *src_yp, - cj); - } - - - - template - int - solve_with_jacobian_callback(SUNLinearSolver LS, - SUNMatrix /*ignored*/, - N_Vector x, - N_Vector b, - realtype tol) - { - IDA &solver = *static_cast *>(LS->content); - - auto *src_b = internal::unwrap_nvector_const(b); - auto *dst_x = internal::unwrap_nvector(x); - if (solver.solve_with_jacobian) - return call_and_possibly_capture_exception(solver.solve_with_jacobian, - solver.pending_exception, - *src_b, - *dst_x, - tol); - else if (solver.solve_jacobian_system) - return call_and_possibly_capture_exception(solver.solve_jacobian_system, - solver.pending_exception, - *src_b, - *dst_x); - else - { - // We have already checked this outside, so we should never get here. - Assert(false, ExcInternalError()); - return -1; - } - } - } // namespace - - - template IDA::IDA(const AdditionalData &data) : IDA(data, MPI_COMM_SELF) @@ -385,8 +296,26 @@ namespace SUNDIALS # endif ); - status = - IDAInit(ida_mem, residual_callback, current_time, yy, yp); + status = IDAInit( + ida_mem, + [](realtype tt, N_Vector yy, N_Vector yp, N_Vector rr, void *user_data) + -> int { + IDA &solver = *static_cast *>(user_data); + + auto *src_yy = internal::unwrap_nvector_const(yy); + auto *src_yp = internal::unwrap_nvector_const(yp); + auto *residual = internal::unwrap_nvector(rr); + + return call_and_possibly_capture_exception(solver.residual, + solver.pending_exception, + tt, + *src_yy, + *src_yp, + *residual); + }, + current_time, + yy, + yp); AssertIDA(status); if (get_local_tolerances) { @@ -482,7 +411,33 @@ namespace SUNDIALS AssertThrow(solve_jacobian_system || solve_with_jacobian, ExcFunctionNotProvided( "solve_jacobian_system or solve_with_jacobian")); - LS->ops->solve = solve_with_jacobian_callback; + LS->ops->solve = [](SUNLinearSolver LS, + SUNMatrix /*ignored*/, + N_Vector x, + N_Vector b, + realtype tol) -> int { + IDA &solver = *static_cast *>(LS->content); + + auto *src_b = internal::unwrap_nvector_const(b); + auto *dst_x = internal::unwrap_nvector(x); + if (solver.solve_with_jacobian) + return call_and_possibly_capture_exception(solver.solve_with_jacobian, + solver.pending_exception, + *src_b, + *dst_x, + tol); + else if (solver.solve_jacobian_system) + return call_and_possibly_capture_exception(solver.solve_jacobian_system, + solver.pending_exception, + *src_b, + *dst_x); + else + { + // We have already checked this outside, so we should never get here. + Assert(false, ExcInternalError()); + return -1; + } + }; // When we set an iterative solver IDA requires that resid is provided. From // SUNDIALS docs If an iterative method computes the preconditioned initial @@ -537,7 +492,31 @@ namespace SUNDIALS // Finally tell IDA about // it as well. The manual says that this must happen *after* // calling IDASetLinearSolver - status = IDASetJacFn(ida_mem, &setup_jacobian_callback); + status = IDASetJacFn( + ida_mem, + [](realtype tt, + realtype cj, + N_Vector yy, + N_Vector yp, + N_Vector /* residual */, + SUNMatrix /* ignored */, + void *user_data, + N_Vector /* tmp1 */, + N_Vector /* tmp2 */, + N_Vector /* tmp3 */) -> int { + Assert(user_data != nullptr, ExcInternalError()); + IDA &solver = *static_cast *>(user_data); + + auto *src_yy = internal::unwrap_nvector_const(yy); + auto *src_yp = internal::unwrap_nvector_const(yp); + + return call_and_possibly_capture_exception(solver.setup_jacobian, + solver.pending_exception, + tt, + *src_yy, + *src_yp, + cj); + }); AssertIDA(status); status = IDASetMaxOrd(ida_mem, data.maximum_order); AssertIDA(status); -- 2.39.5