From a59e4036f9bb43d1afaee83d543dfe7bf7df4794 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Mon, 4 Mar 2024 21:27:16 +0100 Subject: [PATCH] Add Solver::get_current_time Prerequisite for #2246. --- include/amici/solver.h | 6 ++++++ include/amici/solver_cvodes.h | 2 ++ include/amici/solver_idas.h | 2 ++ src/solver_cvodes.cpp | 11 +++++++++++ src/solver_idas.cpp | 11 +++++++++++ 5 files changed, 32 insertions(+) diff --git a/include/amici/solver.h b/include/amici/solver.h index ab4f8ef427..8ddff86763 100644 --- a/include/amici/solver.h +++ b/include/amici/solver.h @@ -964,6 +964,12 @@ class Solver { */ int getMaxConvFails() const; + /** + * @brief Get the current time of the solver. + * @return internal time of the solver + */ + virtual realtype get_current_time() const = 0; + /** * @brief Serialize Solver (see boost::serialization::serialize) * @param ar Archive to serialize to diff --git a/include/amici/solver_cvodes.h b/include/amici/solver_cvodes.h index 592298b7ec..28b7b61014 100644 --- a/include/amici/solver_cvodes.h +++ b/include/amici/solver_cvodes.h @@ -91,6 +91,8 @@ class CVodeSolver : public Solver { void setNonLinearSolverB(int which) const override; + realtype get_current_time() const override; + protected: void calcIC(realtype tout1) const override; diff --git a/include/amici/solver_idas.h b/include/amici/solver_idas.h index 079cee6399..ffcc5368b5 100644 --- a/include/amici/solver_idas.h +++ b/include/amici/solver_idas.h @@ -106,6 +106,8 @@ class IDASolver : public Solver { void setNonLinearSolverB(int which) const override; + realtype get_current_time() const override; + protected: /** * @brief Postprocessing of the solver memory after a discontinuity diff --git a/src/solver_cvodes.cpp b/src/solver_cvodes.cpp index 4fe97720d8..42671e3d96 100644 --- a/src/solver_cvodes.cpp +++ b/src/solver_cvodes.cpp @@ -386,6 +386,17 @@ void CVodeSolver::setNonLinearSolverB(int const which) const { throw CvodeException(status, "CVodeSetNonlinearSolverB"); } +realtype CVodeSolver::get_current_time() const { + if (!solver_memory_) + return std::numeric_limits::quiet_NaN(); + + realtype time; + int status = CVodeGetCurrentTime(solver_memory_.get(), &time); + if (status != CV_SUCCESS) + throw CvodeException(status, "CVodeGetCurrentTime"); + return time; +} + void CVodeSolver::setErrHandlerFn() const { int status = CVodeSetErrHandlerFn( solver_memory_.get(), wrapErrHandlerFn, diff --git a/src/solver_idas.cpp b/src/solver_idas.cpp index 1c69a041db..aa4a22bbd0 100644 --- a/src/solver_idas.cpp +++ b/src/solver_idas.cpp @@ -876,6 +876,17 @@ void IDASolver::setNonLinearSolverB(int which) const { throw CvodeException(status, "CVodeSetNonlinearSolverB"); } +realtype IDASolver::get_current_time() const { + if (!solver_memory_) + return std::numeric_limits::quiet_NaN(); + + realtype time; + int status = IDAGetCurrentTime(solver_memory_.get(), &time); + if (status != IDA_SUCCESS) + throw CvodeException(status, "IDAGetCurrentTime"); + return time; +} + /** * @brief Jacobian of xdot with respect to states x * @param N number of state variables