8 #ifndef EULERINTEGRATOR_H_
9 #define EULERINTEGRATOR_H_
13 #include "rrRoadRunnerOptions.h"
22 using std::invalid_argument;
23 using std::stringstream;
50 using Integrator::Integrator;
67 eventStatus(std::vector<unsigned char>(m->getNumEvents(), false)),
68 previousEventStatus(std::vector<unsigned char>(m->getNumEvents(), false)) {
72 exampleParameter1 = 3.14;
73 exampleParameter2 =
"hello";
74 rrLog(Logger::LOG_WARNING) <<
"Euler integrator is inaccurate";
81 rateBuffer =
new double[stateVectorSize];
82 stateBufferBegin =
new double[stateVectorSize];
83 stateBufferEnd =
new double[stateVectorSize];
86 stateBufferBegin = NULL;
87 stateBufferEnd = NULL;
97 delete[] stateBufferBegin;
98 delete[] stateBufferEnd;
100 stateBufferBegin =
nullptr;
101 stateBufferEnd =
nullptr;
114 virtual double integrate(
double t0,
double h)
override {
115 int internal_steps =
getValue(
"subdivision_steps");
121 for (
int subdiv = 0; subdiv < internal_steps; ++subdiv) {
133 for (
int i = 0; i < stateVectorSize; ++i) {
134 stateBufferEnd[i] = stateBufferBegin[i] + h * rateBuffer[i];
141 double timeEnd = t0 + h;
147 listener->onTimeStep(
this,
mModel, timeEnd);
151 bool triggered =
false;
154 for (
int k_ = 0; k_ < eventStatus.size(); ++k_) {
155 if (eventStatus.at(k_)) {
164 applyEvents(timeEnd, previousEventStatus);
167 if (!eventStatus.empty()) {
168 previousEventStatus = eventStatus;
171 finalTimeEnd = timeEnd;
177 void applyEvents(
double timeEnd, std::vector<unsigned char> &previousEventStatus) {
180 mModel->
applyEvents(timeEnd, previousEventStatus.size() == 0 ? NULL : &previousEventStatus[0],
181 stateBufferEnd, NULL);
211 std::stringstream ss;
212 ss <<
"< roadrunner.EulerIntegrator() " << std::endl;
213 ss <<
"{ 'this' : " << (
void *)
this << std::endl;
214 ss <<
"'exampleParameter1' : " << exampleParameter1 << std::endl;
215 ss <<
"'exampleParameter2' : " << exampleParameter2 << std::endl;
223 virtual std::string
toRepr()
const override {
224 std::stringstream ss;
225 ss <<
"< roadrunner.EulerIntegrator() { 'this' : "
226 << (
void *)
this <<
" }>";
243 return "The Euler method is one of the simplest approaches to "
244 "solving a first order ODE. Given the rate of change of "
245 "function f at time t, it computes the new value of f as "
246 "f(t+h) = f(t) + h*f'(t), where h is the time step. "
247 "Euler's method is rarely used in practice due to poor "
248 "numerical robustness.";
257 return "A simple Euler integrator";
269 return Integrator::Deterministic;
279 if (key ==
"exampleParameter1") {
280 exampleParameter1 = value.
get<
double>();
284 if (key ==
"exampleParameter2") {
286 exampleParameter2 = value.
get<std::string>();
291 throw std::invalid_argument(
"Error, attempt to set invalid key: " + key);
301 if (key ==
"exampleParameter1") {
302 return Setting(exampleParameter1);
305 if (key ==
"exampleParameter2") {
306 return Setting(exampleParameter2);
310 throw std::invalid_argument(
"Error, attempt to read invalid key: " + key);
316 virtual bool hasKey(
const std::string &key)
const {
317 if (key ==
"exampleParameter1" || key ==
"exampleParameter2") {
329 throw std::invalid_argument(
330 "Error, the EulerIntegrator does not support deleting keys");
336 virtual std::vector<std::string>
getKeys()
const {
337 std::vector<std::string> keys;
338 keys.push_back(
"exampleParameter1");
339 keys.push_back(
"exampleParameter2");
347 addSetting(
"subdivision_steps",
Setting(1),
349 "The number of subdivisions of the Euler step size (int).",
350 "(int) For each point, up to this many extra steps will be taken as smaller steps within each step, although their values are not saved");
357 double exampleParameter1;
362 std::string exampleParameter2;
368 double *rateBuffer, *stateBufferBegin, *stateBufferEnd;
375 std::vector<unsigned char> eventStatus;
376 std::vector<unsigned char> previousEventStatus;
387 IntegratorListenerPtr listener;
RoadRunner's Gillespie SSA integrator.
An example class intended to show how to create an Integrator.
Definition: EulerIntegrator.h:47
std::string getName() const override
get the name of this integrator
Definition: EulerIntegrator.h:233
virtual void setItem(const std::string &key, const rr::Setting &value)
sets the value for a key.
Definition: EulerIntegrator.h:278
virtual Setting getItem(const std::string &key) const
Get a value.
Definition: EulerIntegrator.h:300
Solver * construct(ExecutableModel *executableModel) const override
Constructs a new Solver of a given type.
Definition: EulerIntegrator.h:260
void resetSettings() override
Reset all settings to their respective default values.
Definition: EulerIntegrator.h:343
virtual int deleteItem(const std::string &key)
remove a value, this example class does not support deleting keys, so just raise an exception if some...
Definition: EulerIntegrator.h:328
virtual bool hasKey(const std::string &key) const
is there a key matching this name.
Definition: EulerIntegrator.h:316
virtual IntegratorListenerPtr getListener() override
get the integrator listener
Definition: EulerIntegrator.h:203
virtual double integrate(double t0, double h) override
integrates the model from t0 to t0 + hstep
Definition: EulerIntegrator.h:114
std::string getDescription() const override
Get the description for this integrator.
Definition: EulerIntegrator.h:242
virtual void setListener(IntegratorListenerPtr p) override
Clients may register a listener to listen for sbml events and time step events.
Definition: EulerIntegrator.h:196
virtual std::vector< std::string > getKeys() const
list of keys in this object.
Definition: EulerIntegrator.h:336
virtual std::string toString() const override
get a description of this object, compatable with python str
Definition: EulerIntegrator.h:210
virtual void restart(double t0) override
This simple integrator has nothing to reset, so do nothing here.
Definition: EulerIntegrator.h:190
IntegrationMethod getIntegrationMethod() const override
Always deterministic for Euler.
Definition: EulerIntegrator.h:268
std::string getHint() const override
Get the hint for this integrator.
Definition: EulerIntegrator.h:256
virtual std::string toRepr() const override
get a short descriptions of this object, compatable with python repr.
Definition: EulerIntegrator.h:223
~EulerIntegrator() override
delete any memory we allocated
Definition: EulerIntegrator.h:94
EulerIntegrator(ExecutableModel *m)
Creates a new EulerIntegrator.
Definition: EulerIntegrator.h:65
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 int setStateVector(const double *stateVector)=0
sets the internal model state to the provided packed state std::vector.
virtual int getEventTriggers(size_t len, const int *indx, unsigned char *values)=0
get the event status, false if the even is not triggered, true if it is.
virtual int getStateVector(double *stateVector)=0
The state std::vector is a std::vector of elements that are defined by differential equations (rate r...
virtual int applyEvents(double timeEnd, const unsigned char *previousEventStatus, const double *initialState, double *finalState)=0
Itterate through all of the current and pending events and apply them.
Integrator is an abstract base class that provides an interface to specific integrator class implemen...
Definition: Integrator.h:60
Store a roadrunner option (or setting) as a Variant type.
Definition: Setting.h:78
SettingType get()
return the value held by this Setting as a type SettingType.
Definition: Setting.h:197
Base class for all integrators and steady state solvers.
Definition: Solver.h:39
virtual void resetSettings()
Reset all settings to their respective default values.
Definition: Solver.cpp:67
virtual Setting getValue(const std::string &key) const
Get the value of an integrator setting.
Definition: Solver.cpp:104
ExecutableModel * mModel
non-owning pointer to model
Definition: Solver.h:208
Base class for all code generators in RoadRunner.