5 #ifndef ROADRUNNER_KINSOLSTEADYSTATESOLVER_H
6 #define ROADRUNNER_KINSOLSTEADYSTATESOLVER_H
8 #include <nvector/nvector_serial.h>
9 #include <sundials/sundials_types.h>
10 #include "kinsol/kinsol.h"
13 #include "rrException.h"
14 #include "KinsolErrHandler.h"
17 class ExecutableModel;
27 #define KinsolCheckForNull(x, fcn, type) \
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()); \
46 using SteadyStateSolver::SteadyStateSolver;
83 void setFScale(
const std::vector<double> &value);
94 void setUScale(std::vector<double> value);
104 template<
class KinsolSteadyStateSolverType>
105 static int kinsolDyDtFcn(N_Vector stateVecIn, N_Vector stateVecOut,
void *userData) {
109 double *y = NV_DATA_S(stateVecIn);
112 N_VConst((std::numeric_limits<double>::max)(), stateVecOut);
113 double *dydt = NV_DATA_S(stateVecOut);
116 auto solver = (KinsolSteadyStateSolverType *) userData;
118 assert(solver &&
"userData pointer is nullptr in callback kinsolDyDtFcn");
122 assert(model &&
"model is nullptr");
125 int numStates = stateVecIn->ops->nvgetlength(stateVecIn);
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()
135 <<
"\" is equal to the numerical ";
136 err <<
"limits for a double." << std::endl;
140 rrLog(Logger::LOG_TRACE) << __FUNCTION__ <<
", model: " << model;
271 long int numNolinSolvIters;
272 long int numBetaCondFails;
273 long int numBacktrackOps;
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;
293 std::unordered_map<std::string, Setting> solverStats;
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.