roadrunner  2.6.0
Fast simulator for SBML models
KinsolSteadyStateSolver.h
1 //
2 // Created by Ciaran on 26/02/2021.
3 //
4 
5 #ifndef ROADRUNNER_KINSOLSTEADYSTATESOLVER_H
6 #define ROADRUNNER_KINSOLSTEADYSTATESOLVER_H
7 
8 #include <nvector/nvector_serial.h> /* access to serial N_Vector */
9 #include <sundials/sundials_types.h> /* defs. of realtype, sunindextype */
10 #include "kinsol/kinsol.h"
11 #include "SteadyStateSolver.h"
12 #include "rrExecutableModel.h"
13 #include "rrException.h"
14 #include "KinsolErrHandler.h"
15 
16 namespace rr {
17  class ExecutableModel;
18 
19  class RoadRunner;
20 
27 #define KinsolCheckForNull(x, fcn, type) \
28  if (x == nullptr){ \
29  std::ostringstream err_; \
30  err_ << "The value of object with type: \"" << type << "\" " \
31  "which is the output of sundials function " << fcn << " is nullptr" << std::endl;\
32  throw NullPointerException(err_.str()); \
33  }; \
34 
35 
45  public:
46  using SteadyStateSolver::SteadyStateSolver;
47 
53  explicit KinsolSteadyStateSolver(ExecutableModel *executableModel);
54 
60 
64  ~KinsolSteadyStateSolver() override = default;
65 
72  void syncWithModel(ExecutableModel *m) override;
73 
77  void setFScale(double value);
78 
83  void setFScale(const std::vector<double> &value);
84 
88  void setUScale(double value);
89 
94  void setUScale(std::vector<double> value);
95 
96 
104  template<class KinsolSteadyStateSolverType>
105  static int kinsolDyDtFcn(N_Vector stateVecIn, N_Vector stateVecOut, void *userData) {
106  // we need to use the state std::vector y (chosen by the solver)
107  // to compute the rates dydt and then update the state std::vector
108  // using the rates.
109  double *y = NV_DATA_S(stateVecIn);
110 
111  // initialize the out NVector with numerical limits for double
112  N_VConst((std::numeric_limits<double>::max)(), stateVecOut);
113  double *dydt = NV_DATA_S(stateVecOut);
114 
115  // cast user data back into our solver type
116  auto solver = (KinsolSteadyStateSolverType *) userData;
117 
118  assert(solver && "userData pointer is nullptr in callback kinsolDyDtFcn");
119 
120  ExecutableModel *model = solver->mModel;
121 
122  assert(model && "model is nullptr");
123 
124  // collect the number of states in this model
125  int numStates = stateVecIn->ops->nvgetlength(stateVecIn);
126 
127  model->getStateVectorRate(model->getTime(), y, dydt);
128 
129  for (int i = 0; i < numStates; i++) {
130  if (dydt[i] == (std::numeric_limits<double>::max)()) {
131  std::ostringstream err;
132  err << __FILE__":" << __LINE__ << ":" << "kinsolDyDtFcn";
133  err << ": steady state solver \"" << solver->getName()
134  << "\". The rate for variable \"" << model->getStateVectorId(i)
135  << "\" is equal to the numerical ";
136  err << "limits for a double." << std::endl;
137  throw KinsolException(err.str());
138  }
139  }
140  rrLog(Logger::LOG_TRACE) << __FUNCTION__ << ", model: " << model;
141 
142  return KIN_SUCCESS;
143  };
144 
166  double solveForSteadyState(KinsolSteadyStateSolver* solverInstance, int kinsolStrategy);
167 
173  void *getKinsolMemory() const;
174 
181  std::unordered_map<std::string, Setting>& getSolverStats();
182 
186  void printSolverStats();
187 
188  protected:
189 
195  virtual void freeKinsol();
196 
203  virtual void updateKinsol();
204 
210  virtual void createKinsol();
211 
218  void resetSettings() override;
219 
220 
224  void *mKinsol_Memory = nullptr;
225 
229  N_Vector mStateVector = nullptr;
230 
240  N_Vector fscale = nullptr;
241 
249  N_Vector uscale = nullptr;
250 
256  N_Vector constraints = nullptr;
257 
258 
264  virtual void getSolverStatsFromKinsol();
265 
270  long int numFuncEvals;
271  long int numNolinSolvIters;
272  long int numBetaCondFails;
273  long int numBacktrackOps;
274  double funcNorm;
275  double stepLength;
276  long int numJacEvals;
277  long int numJtimesEvals;
278  long int numLinConvFails;
279  long int numLinFuncEvals;
280  long int numLinIters;
281  long int numNonlinSolvIters;
282  long int numPrecEvals;
283  long int numPrecSolves;
284 
291  long int nSolverIterations = 0;
292 
293  std::unordered_map<std::string, Setting> solverStats;
294 
295 
296  };
297 
298 
299 }
300 
301 
302 #endif //ROADRUNNER_KINSOLSTEADYSTATESOLVER_H
Contains the base class for RoadRunner solvers.
Base class for all code generation systems; allows compiling and evaluating the model.
Definition: rrExecutableModel.h:118
virtual void getStateVectorRate(double time, const double *y, double *dydt=0)=0
the state std::vector y is the rate rule values and floating species concentrations concatenated.
virtual std::string getStateVectorId(size_t index)=0
get the id of an element of the state std::vector.
Exception class for scanners.
Definition: rrException.h:100
base class to steady state solvers from the Sundials package.
Definition: KinsolSteadyStateSolver.h:44
void printSolverStats()
display solver stats to console
Definition: KinsolSteadyStateSolver.cpp:299
virtual void updateKinsol()
Calls methods for updating options in kinsol.
Definition: KinsolSteadyStateSolver.cpp:243
void syncWithModel(ExecutableModel *m) override
implementation of Solver::syncWithModel.
Definition: KinsolSteadyStateSolver.cpp:18
long int nSolverIterations
stores the number of iterations required by the solver to converge.
Definition: KinsolSteadyStateSolver.h:291
void * getKinsolMemory() const
getter for the underlying memory block that is used by kinsol.
Definition: KinsolSteadyStateSolver.cpp:229
N_Vector mStateVector
a sundials N_Vector for storing system state variables
Definition: KinsolSteadyStateSolver.h:229
void * mKinsol_Memory
Pointer to the kinsol memory block.
Definition: KinsolSteadyStateSolver.h:224
void setUScale(double value)
set all elements of the uscale variable to
Definition: KinsolSteadyStateSolver.cpp:212
virtual void getSolverStatsFromKinsol()
after solve() has been called, this method collects some details such as number of function evals
Definition: KinsolSteadyStateSolver.cpp:185
double solveForSteadyState(KinsolSteadyStateSolver *solverInstance, int kinsolStrategy)
Generic solver method.
Definition: KinsolSteadyStateSolver.cpp:306
virtual void createKinsol()
Initialize kinsol objects.
Definition: KinsolSteadyStateSolver.cpp:26
KinsolSteadyStateSolver()=default
The default constructor for constructing without a model.
N_Vector constraints
vector of constraints used to ensure positive solution.
Definition: KinsolSteadyStateSolver.h:256
long int numFuncEvals
kinsol output variables.
Definition: KinsolSteadyStateSolver.h:270
std::unordered_map< std::string, Setting > & getSolverStats()
return a mapping containing solver statistics.
Definition: KinsolSteadyStateSolver.cpp:280
virtual void freeKinsol()
Destory kinsol objects.
Definition: KinsolSteadyStateSolver.cpp:92
static int kinsolDyDtFcn(N_Vector stateVecIn, N_Vector stateVecOut, void *userData)
kinsol callback function that implements system equations
Definition: KinsolSteadyStateSolver.h:105
N_Vector uscale
std::vector containing diagonal elements of scaling matrix Du for std::vector u chosen so that the co...
Definition: KinsolSteadyStateSolver.h:249
~KinsolSteadyStateSolver() override=default
virtual destructor
N_Vector fscale
std::vector containing diagonal elements of scaling matrix DF for F(u) chosen so that the components ...
Definition: KinsolSteadyStateSolver.h:240
void resetSettings() override
add settings to the Solver::settings std::map when called for the first time or reset the settings st...
Definition: KinsolSteadyStateSolver.cpp:117
void setFScale(double value)
set all elements of the fscale variable to
Definition: KinsolSteadyStateSolver.cpp:195
SteadyStateSolver is an abstract base class that provides an interface to specific steady-state solve...
Definition: SteadyStateSolver.h:38
Base class for all code generators in RoadRunner.