MUQ  0.4.3
ODE.h
Go to the documentation of this file.
1 #ifndef ODE_H_
2 #define ODE_H_
3 
4 #include <boost/property_tree/ptree.hpp>
5 
6 #include <cvodes/cvodes.h> /* access to CVODE */
7 #include <nvector/nvector_serial.h> /* access to serial N_Vector */
8 #include <sunmatrix/sunmatrix_dense.h> /* access to dense SUNMatrix */
9 #include <sunlinsol/sunlinsol_dense.h> /* access to dense SUNLinearSolver */
10 #include <sunnonlinsol/sunnonlinsol_newton.h> /* access to the newton SUNNonlinearSolver */
11 #include <sunnonlinsol/sunnonlinsol_fixedpoint.h> /* access to the fixed point SUNNonlinearSolver */
12 
13 
14 #include "MUQ/Modeling/ModPiece.h"
15 
16 namespace muq {
17 namespace Modeling {
18 
19 
31  class ODE : public ModPiece {
32  public:
33 
34 
35  ODE(std::shared_ptr<ModPiece> const& rhsIn,
36  Eigen::VectorXd const& evalTimesIn,
37  boost::property_tree::ptree opts = boost::property_tree::ptree()) : ODE(rhsIn, evalTimesIn, true, opts){};
38 
39  ODE(std::shared_ptr<ModPiece> const& rhsIn,
40  Eigen::VectorXd const& evalTimesIn,
41  bool isAuto,
42  boost::property_tree::ptree opts = boost::property_tree::ptree());
43 
44 
45  virtual ~ODE() = default;
46 
48  const bool isAutonomous;
49 
51  const double startTime;
52 
53  protected:
54 
59  virtual void EvaluateImpl(ref_vector<Eigen::VectorXd> const& inputs) override;
60 
61  virtual void GradientImpl(unsigned int outWrt,
62  unsigned int inWrt,
63  ref_vector<Eigen::VectorXd> const& inputs,
64  Eigen::VectorXd const& sens) override;
65 
66  virtual void JacobianImpl(unsigned int outWrt,
67  unsigned int inWrt,
68  ref_vector<Eigen::VectorXd> const& inputs) override;
69 
70  virtual void ApplyJacobianImpl(unsigned int outWrt,
71  unsigned int inWrt,
72  ref_vector<Eigen::VectorXd> const& inputs,
73  Eigen::VectorXd const& vec) override;
74 
87  NonlinearSolverOptions(boost::property_tree::ptree const& opts);
88 
89  void SetOptions(boost::property_tree::ptree const& opts);
90 
91  std::string method; // e.g., Iter, Newton
92  int maxIters;
93  };
94 
105  LinearSolverOptions() = default;
106  LinearSolverOptions(boost::property_tree::ptree const& opts);
107  void SetOptions(boost::property_tree::ptree const& opts);
108 
109  std::string method; // e.g., Dense, SPGMR, SPFGMR, SPBCGS, SPTFQMR, PCG
110  int maxIters; // maximum number of iterations allowed for iterative solvers
111 
112  };
113 
132  IntegratorOptions() = default;
133  IntegratorOptions(boost::property_tree::ptree const& opts);
134 
135  void SetOptions(boost::property_tree::ptree const& opts);
136 
137  std::string method; // e.g., Adams or BDF
138  double reltol; // relative tolerance
139  double abstol; // absolute tolerance
140  double maxStepSize; // maximum allowed step inputSize
141  int maxNumSteps; // maximum allowed steps
142  int checkPtGap; // gap between check points
143 
144  double sensRelTol; // relative tolerance for forward sensitivities
145  double sensAbsTol; // absolute tolerance for forward sensitivities.
146 
147  double adjRelTol; // relative tolerance for adjoint sensitivities.
148  double adjAbsTol; // absolute tolerance for adjoint sensitivities.
149  };
150 
153  struct Interface{
154 
155  static int RHS(realtype time, N_Vector state, N_Vector deriv, void *user_data);
156  static int RHSJacobian(realtype time, N_Vector state, N_Vector rhs, SUNMatrix jac, void *user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3);
157  static int RHSJacobianAction(N_Vector v, N_Vector Jv, realtype time, N_Vector state, N_Vector rhs, void *user_data, N_Vector tmp);
158 
165  static int SensitivityRHS(int Ns, realtype time, N_Vector y, N_Vector ydot, N_Vector *ys, N_Vector *ySdot, void *user_data, N_Vector tmp1, N_Vector tmp2);
166 
173  static int AdjointRHS(double time, N_Vector state, N_Vector lambda, N_Vector deriv, void *user_data);
174  static int AdjointJacobian(double time, N_Vector state, N_Vector lambda, N_Vector rhs, SUNMatrix jac, void *user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3);
175 
176  static int AdjointJacobianAction(N_Vector target, N_Vector output, double time, N_Vector state, N_Vector lambda, N_Vector adjRhs, void *user_data, N_Vector tmp);
177 
181  static int AdjointQuad(realtype time, N_Vector state, N_Vector lambda, N_Vector quadRhs, void *user_data);
182  };
183 
187 
188  // Store the times when where we want to return the solution of the ODE
189  const Eigen::VectorXd evalTimes;
190 
191  // Store a ModPiece that evaluates the RHS function
192  std::shared_ptr<ModPiece> rhs;
193 
194  private:
195 
204  std::pair<SUNLinearSolver, SUNMatrix> CreateLinearSolver(void *cvode_mem, N_Vector const& state);
205 
206  SUNNonlinearSolver CreateNonlinearSolver(void *cvode_mem, N_Vector const& state);
207 
208  std::pair<SUNLinearSolver, SUNMatrix> CreateAdjointLinearSolver(void *cvode_mem, N_Vector const& state, int indexB);
209 
210  SUNNonlinearSolver CreateAdjointNonlinearSolver(void *cvode_mem, N_Vector const& state, int indexB);
211 
212 
213  static Eigen::VectorXi GetInputSizes(std::shared_ptr<ModPiece> const& rhsIn,
214  bool isAuto);
215 
216  static Eigen::VectorXi GetOutputSizes(std::shared_ptr<ModPiece> const& rhsIn,
217  Eigen::VectorXd const& evalTimes);
218 
219  static double GetStartTime(Eigen::VectorXd const& evalTimesIn,
220  boost::property_tree::ptree const& opts);
221 
223  static void CheckFlag(void *returnvalue, const char *funcname, int opt);
224  };
225 
226 } // namespace Modeling
227 } // namespace muq
228 
229 #endif // #ifndef ODE_H_
Provides an abstract interface for defining vector-valued model components.
Definition: ModPiece.h:148
A class for integrating ODEs with CVODES.
Definition: ODE.h:31
const double startTime
The time when this simulation starts – either set in the opts ptree or set to the minimum of evalTime...
Definition: ODE.h:51
static Eigen::VectorXi GetInputSizes(std::shared_ptr< ModPiece > const &rhsIn, bool isAuto)
Definition: ODE.cpp:77
static void CheckFlag(void *returnvalue, const char *funcname, int opt)
Checks the return value produced by CVODES. Throws and exception if error occured.
Definition: ODE.cpp:1212
SUNNonlinearSolver CreateNonlinearSolver(void *cvode_mem, N_Vector const &state)
Definition: ODE.cpp:189
std::pair< SUNLinearSolver, SUNMatrix > CreateAdjointLinearSolver(void *cvode_mem, N_Vector const &state, int indexB)
Definition: ODE.cpp:218
virtual void JacobianImpl(unsigned int outWrt, unsigned int inWrt, ref_vector< Eigen::VectorXd > const &inputs) override
Definition: ODE.cpp:638
static Eigen::VectorXi GetOutputSizes(std::shared_ptr< ModPiece > const &rhsIn, Eigen::VectorXd const &evalTimes)
Definition: ODE.cpp:87
virtual void GradientImpl(unsigned int outWrt, unsigned int inWrt, ref_vector< Eigen::VectorXd > const &inputs, Eigen::VectorXd const &sens) override
Definition: ODE.cpp:399
IntegratorOptions intOpts
Definition: ODE.h:186
std::shared_ptr< ModPiece > rhs
Definition: ODE.h:192
LinearSolverOptions linOpts
Definition: ODE.h:185
std::pair< SUNLinearSolver, SUNMatrix > CreateLinearSolver(void *cvode_mem, N_Vector const &state)
Definition: ODE.cpp:133
virtual void ApplyJacobianImpl(unsigned int outWrt, unsigned int inWrt, ref_vector< Eigen::VectorXd > const &inputs, Eigen::VectorXd const &vec) override
Definition: ODE.cpp:822
SUNNonlinearSolver CreateAdjointNonlinearSolver(void *cvode_mem, N_Vector const &state, int indexB)
Definition: ODE.cpp:273
const bool isAutonomous
Does the RHS explicitly depend on time?
Definition: ODE.h:48
ODE(std::shared_ptr< ModPiece > const &rhsIn, Eigen::VectorXd const &evalTimesIn, boost::property_tree::ptree opts=boost::property_tree::ptree())
Definition: ODE.h:35
const Eigen::VectorXd evalTimes
Definition: ODE.h:189
virtual ~ODE()=default
NonlinearSolverOptions nonlinOpts
Definition: ODE.h:184
static double GetStartTime(Eigen::VectorXd const &evalTimesIn, boost::property_tree::ptree const &opts)
Definition: ODE.cpp:48
virtual void EvaluateImpl(ref_vector< Eigen::VectorXd > const &inputs) override
Integrates the ODE forward in time.
Definition: ODE.cpp:302
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37
int int diyfp diyfp v
Definition: json.h:15163
void SetOptions(boost::property_tree::ptree const &opts)
Definition: ODE.cpp:114
IntegratorOptions(boost::property_tree::ptree const &opts)
static int RHSJacobianAction(N_Vector v, N_Vector Jv, realtype time, N_Vector state, N_Vector rhs, void *user_data, N_Vector tmp)
Definition: ODE.cpp:1018
static int RHSJacobian(realtype time, N_Vector state, N_Vector rhs, SUNMatrix jac, void *user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
Definition: ODE.cpp:1045
static int RHS(realtype time, N_Vector state, N_Vector deriv, void *user_data)
Definition: ODE.cpp:999
void SetOptions(boost::property_tree::ptree const &opts)
Definition: ODE.cpp:108
LinearSolverOptions(boost::property_tree::ptree const &opts)
NonlinearSolverOptions(boost::property_tree::ptree const &opts)
void SetOptions(boost::property_tree::ptree const &opts)
Definition: ODE.cpp:102