roadrunner  2.6.0
Fast simulator for SBML models
CVODEIntegrator.h
Go to the documentation of this file.
1 // == PREAMBLE ================================================
2 
3 // * Licensed under the Apache License, Version 2.0; see README
4 
5 // == FILEDOC =================================================
6 
13 //todo replace the include guards to match the filename
14 #ifndef rrCvodeInterfaceH
15 #define rrCvodeInterfaceH
16 
17 #include "Integrator.h"
18 #include "rrRoadRunnerOptions.h"
19 
20 #include <string>
21 #include <vector>
22 #include <sundials/sundials_linearsolver.h>
23 #include <sundials/sundials_nonlinearsolver.h>
24 #include <cvodes/cvodes.h>
25 #include <sundials/sundials_nvector.h>
26 #include "ForwardSensitivitySolver.h"
27 
28 
29 namespace rr {
30  using std::string;
31 
32  class ExecutableModel;
33 
34  class RoadRunner;
35 
36  class ForwardSensitivitySolver;
37 
46  class CVODEIntegrator : public Integrator {
47 
48  public:
49 
50  friend class ForwardSensitivitySolver;
51 
52  using Integrator::Integrator;
53 
58  explicit CVODEIntegrator(ExecutableModel *oModel);
59 
64  ~CVODEIntegrator() override;
65 
71  void syncWithModel(ExecutableModel *m) override;
72 
73  // ** Loading Settings *************************************************
74 
79  void loadConfigSettings() override;
80 
86  void loadSBMLSettings(const std::string &filename) override;
87 
88  // ** Meta Info ********************************************************
89 
95  std::string getName() const override;
96 
102  std::string getDescription() const override;
103 
109  std::string getHint() const override;
110 
116  Solver *construct(ExecutableModel *executableModel) const override;
117 
118  // ** Getters / Setters ************************************************
119 
124  IntegrationMethod getIntegrationMethod() const override;
125 
130  void setValue(const std::string &setting, Setting value) override;
131 
139  void setMaxOrder(int newValue);
140 
145  void setIndividualTolerance(std::string sid, double value) override;
146 
151  void resetSettings() override;
152 
153 
162  void tweakTolerances() override;
163 
164 
165  // ** Integration Routines *********************************************
166 
171  double integrate(double t0, double hstep) override;
172 
179  void restart(double timeStart) override;
180 
181  // ** Listeners ********************************************************
182 
187  IntegratorListenerPtr getListener() override;
188 
193  void setListener(IntegratorListenerPtr) override;
194 
199  void checkType() const;
200 
201 
206  void checkVectorSize(int expected, size_t real) const;
207 
212  void checkIndex(int index, int size) const;
213 
218  std::string ToString(int val) const;
219 
224  std::string ToString(size_t val) const;
225 
229  N_Vector getStateVector() const;
230 
234  SUNNonlinearSolver getSolver() const;
235 
239  void *getCvodeMemory() const;
240 
246  std::vector<double> getAbsoluteToleranceVector() override;
247 
248 
249  private:
250  // defaults directly from CVODE docs
251  static const int mDefaultMaxNumSteps;
252  static const int mDefaultMaxAdamsOrder;
253  static const int mDefaultMaxBDFOrder;
254 
255  // cvode components
256  void *mCVODE_Memory = nullptr;
257  N_Vector mStateVector = nullptr;
258  SUNMatrix jac = nullptr;
259  SUNNonlinearSolver nonLinSolver = nullptr;
260  SUNLinearSolver linSolver = nullptr;
261 
262  IntegratorListenerPtr listener;
263  double mLastEventTime;
264  bool variableStepPendingEvent;
265  bool variableStepTimeEndEvent;
266  std::vector<double> variableStepPostEventState;
267  std::vector<unsigned char> eventStatus;
268 
269  void testRootsAtInitialTime();
270 
271  bool haveVariables() const;
272 
273  void assignResultsToModel() const;
274 
275 
281  void setCVODETolerances();
282 
286  void reInit(double t0);
287 
298  void updateCVODE();
299 
300  void applyPendingEvents(double timeEnd);
301 
302  void applyEvents(double timeEnd, std::vector<unsigned char> &previousEventStatus);
303 
304  double applyVariableStepPendingEvents();
305 
306  void create();
307 
308  void freeSundialsMemory();
309 
310  bool stateVectorVariables;
311 
312  unsigned long typecode_;
313 
314  friend int cvodeDyDtFcn(double t, N_Vector cv_y, N_Vector cv_ydot, void *f_data);
315 
316  friend int cvodeEventAndPiecewiseRootFcn(double t, N_Vector y, double *gout, void *g_data);
317 
318  };
319 
320  template <class SundialsType = CVODEIntegrator>
321  std::string decodeSundialsError(SundialsType* solver, int cvodeError, bool exInfo) {
322  std::string result;
323  std::stringstream ss;
324  ss << (int) solver->getValue("maximum_num_steps");
325  std::string max_steps = ss.str();
326 
327  switch (cvodeError) {
328  case CV_TOO_MUCH_WORK:
329  result = "CV_TOO_MUCH_WORK";
330  if (exInfo) {
331  result += ": The solver took mxstep (" + max_steps + ") internal steps but " +
332  "could not reach tout.";
333  }
334  break;
335  case CV_TOO_MUCH_ACC:
336  result = "CV_TOO_MUCH_ACC";
337  if (exInfo) {
338  result += ": The solver could not satisfy the accuracy "
339  "demanded by the user for some internal step.";
340  }
341  break;
342  case CV_ERR_FAILURE:
343  result = "CV_ERR_FAILURE";
344  if (exInfo) {
345  result += ": Error test failures occurred too many times "
346  "(= MXNEF = 7) during one internal time step or"
347  "occurred with |h| = hmin.";
348  }
349  break;
350  case CV_CONV_FAILURE:
351  result = "CV_CONV_FAILURE";
352  if (exInfo) {
353  result += ": Convergence test failures occurred too many "
354  "times (= MXNCF = 10) during one internal time"
355  "step or occurred with |h| = hmin.";
356  }
357  break;
358  case CV_LINIT_FAIL:
359  result = "CV_LINIT_FAIL";
360  if (exInfo) {
361  result += ": The linear solver's initialization function "
362  "failed.";
363  }
364  break;
365  case CV_LSETUP_FAIL:
366  result = "CV_LSETUP_FAIL";
367  if (exInfo) {
368  result += ": The linear solver's setup routine failed in an "
369  "unrecoverable manner.";
370  }
371  break;
372  case CV_LSOLVE_FAIL:
373  result = "CV_LSOLVE_FAIL";
374  if (exInfo) {
375  result += ": The linear solver's solve routine failed in an "
376  "unrecoverable manner.";
377  }
378  break;
379  case CV_RHSFUNC_FAIL:
380  result = "CV_RHSFUNC_FAIL";
381  break;
382  case CV_FIRST_RHSFUNC_ERR:
383  result = "CV_FIRST_RHSFUNC_ERR";
384  break;
385  case CV_REPTD_RHSFUNC_ERR:
386  result = "CV_REPTD_RHSFUNC_ERR";
387  break;
388  case CV_UNREC_RHSFUNC_ERR:
389  result = "CV_UNREC_RHSFUNC_ERR";
390  break;
391  case CV_RTFUNC_FAIL:
392  result = "CV_RTFUNC_FAIL";
393  break;
394  case CV_MEM_FAIL:
395  result = "CV_MEM_FAIL";
396  break;
397  case CV_MEM_NULL:
398  result = "CV_MEM_NULL";
399  if (exInfo) {
400  result += ": The cvode_mem argument was NULL.";
401  }
402  break;
403  case CV_ILL_INPUT:
404  result = "CV_ILL_INPUT";
405  if (exInfo) {
406  result += ": One of the inputs to CVode is illegal. This "
407  "includes the situation when a component of the "
408  "error weight vectors becomes < 0 during "
409  "internal time-stepping. It also includes the "
410  "situation where a root of one of the root "
411  "functions was found both at t0 and very near t0. "
412  "The ILL_INPUT flag will also be returned if the "
413  "linear solver routine CV--- (called by the user "
414  "after calling CVodeCreate) failed to set one of "
415  "the linear solver-related fields in cvode_mem or "
416  "if the linear solver's init routine failed. In "
417  "any case, the user should see the printed "
418  "error message for more details.";
419  }
420  break;
421  case CV_NO_MALLOC:
422  result = "CV_NO_MALLOC";
423  if (exInfo) {
424  result += ": indicating that cvode_mem has not been "
425  "allocated (i.e., CVodeInit has not been "
426  "called).";
427  }
428  break;
429  case CV_BAD_K:
430  result = "CV_BAD_K";
431  if (exInfo) {
432  result += ": k is not in the range 0, 1, ..., qu.";
433  }
434  break;
435  case CV_BAD_T:
436  result = "CV_BAD_T";
437  if (exInfo) {
438  result += ": t is not in the interval [tn-hu,tn].";
439  }
440  break;
441  case CV_BAD_DKY:
442  result = "CV_BAD_DKY";
443  if (exInfo) {
444  result += ": The dky argument was NULL.";
445  }
446  break;
447  case CV_TOO_CLOSE:
448  result = "CV_TOO_CLOSE:";
449  break;
450  default:
451  result = "UNKNOWN_CODE";
452  break;
453  }
454  return result;
455  }
456 
457 }
458 
459 #endif
RoadRunner's Gillespie SSA integrator.
A RoadRunner integrator based on CVODE; serves as RoadRunner's main integrator for ODEs.
Definition: CVODEIntegrator.h:46
std::vector< double > getAbsoluteToleranceVector() override
Get the absolute tolerance vector for the solver.
Definition: CVODEIntegrator.cpp:797
void tweakTolerances() override
Fix tolerances for SBML tests.
Definition: CVODEIntegrator.cpp:578
std::string getDescription() const override
Get the description for this integrator.
Definition: CVODEIntegrator.cpp:294
double integrate(double t0, double hstep) override
Main integration routine.
Definition: CVODEIntegrator.cpp:436
N_Vector getStateVector() const
getter for the internal state std::vector
Definition: CVODEIntegrator.cpp:1092
std::string ToString(int val) const
Converts integer to std::string for error print.
Definition: CVODEIntegrator.cpp:141
IntegratorListenerPtr getListener() override
Gets the integrator listener.
Definition: CVODEIntegrator.cpp:118
void setIndividualTolerance(std::string sid, double value) override
Sets tolerance for individual species.
Definition: CVODEIntegrator.cpp:314
void setMaxOrder(int newValue)
sets the value of maximum order, which defaults to 12 for Adams (non-stiff) and 5 for BDF (Stiff).
Definition: CVODEIntegrator.cpp:346
void syncWithModel(ExecutableModel *m) override
Called whenever a new model is loaded to allow integrator to reset internal state.
Definition: CVODEIntegrator.cpp:198
SUNNonlinearSolver getSolver() const
getter for the internal Sundials linear solver object
Definition: CVODEIntegrator.cpp:1096
IntegrationMethod getIntegrationMethod() const override
Always deterministic for CVODE.
Definition: CVODEIntegrator.cpp:310
void checkType() const
Does a RT type check which throws if it fails, EVEN IF RTTI IS DISABLED.
Definition: CVODEIntegrator.cpp:122
void checkIndex(int index, int size) const
Does a index check which throws if it is out of bound.
Definition: CVODEIntegrator.cpp:134
Solver * construct(ExecutableModel *executableModel) const override
construct an instance of type CVODEIntegrator.
Definition: CVODEIntegrator.cpp:305
std::string getName() const override
Get the name for this integrator.
Definition: CVODEIntegrator.cpp:290
void resetSettings() override
Reset all integrator settings to their respective default values.
Definition: CVODEIntegrator.cpp:154
void loadConfigSettings() override
It looks like this method only get used inside resetSettings.
Definition: CVODEIntegrator.cpp:221
void setListener(IntegratorListenerPtr) override
Sets the integrator listener.
Definition: CVODEIntegrator.cpp:114
void loadSBMLSettings(const std::string &filename) override
Load an SBML settings file and apply the configuration options.
Definition: CVODEIntegrator.cpp:243
void restart(double timeStart) override
Reset time to zero and reinitialize model.
Definition: CVODEIntegrator.cpp:920
void * getCvodeMemory() const
getter for the internal CVode memory buffer
Definition: CVODEIntegrator.cpp:1088
~CVODEIntegrator() override
Destructor.
Definition: CVODEIntegrator.cpp:108
CVODEIntegrator(ExecutableModel *oModel)
Constructor: takes an executable model, does not own the pointer.
Definition: CVODEIntegrator.cpp:83
std::string getHint() const override
Get the hint for this integrator.
Definition: CVODEIntegrator.cpp:301
void checkVectorSize(int expected, size_t real) const
Does a size check which throws if it fails.
Definition: CVODEIntegrator.cpp:127
Base class for all code generation systems; allows compiling and evaluating the model.
Definition: rrExecutableModel.h:118
Time based sensivitity solver.
Definition: ForwardSensitivitySolver.h:31
Integrator is an abstract base class that provides an interface to specific integrator class implemen...
Definition: Integrator.h:60
virtual void setValue(const std::string &key, Setting value)
Pull down the setValue from superclass.
Definition: Solver.cpp:125
Store a roadrunner option (or setting) as a Variant type.
Definition: Setting.h:78
Base class for all integrators and steady state solvers.
Definition: Solver.h:39