diff --git a/ODElibrary/RungeKutta.cpp b/ODElibrary/RungeKutta.cpp index cf7096c..f80177e 100644 --- a/ODElibrary/RungeKutta.cpp +++ b/ODElibrary/RungeKutta.cpp @@ -1,40 +1,43 @@ #include #include "RungeKutta.h" void RungeKutta::step(Eigen::VectorXd &y, double t) { // The update is different depending on the odrer switch (methodOrder) { case 1 : // simple Euler step y += stepSize * (A * y + g(t)); break; case 2 : // midpoint method y += stepSize * (A * (y + 0.5 * stepSize * (A * y + g(t))) + g(t + 0.5 * stepSize)); break; case 3 : {// Kutta's third order method Eigen::VectorXd k1 = stepSize * (A * y + g(t)); Eigen::VectorXd k2 = stepSize * (A * (y + k1 / 2.) + g(t + stepSize / 2.)); Eigen::VectorXd k3 = stepSize * (A * (y - k1 + k2 / 2.) + g(t + stepSize)); y += 1./6. * (k1 + 4 * k2 + k3); break; } case 4 : {//classical RK-4 method Eigen::VectorXd k1 = stepSize * (A * y + g(t)); Eigen::VectorXd k2 = stepSize * (A * (y + k1 / 2.) + g(t + stepSize / 2.)); Eigen::VectorXd k3 = stepSize * (A * (y + k2 / 2.) + g(t + stepSize / 2.)); Eigen::VectorXd k4 = stepSize * (A * (y + k3) + g(t + stepSize)); y += 1./6. * (k1 + 2 * k2 + 2 * k3 + k4); break; } default: throw std::domain_error("Runge-Kutta method order is not valid in step."); } } void RungeKutta::setMethodOrder(int n) { if (n < 1 || n > 4){ // check the bounds of implementation throw std::invalid_argument("Runge-Kutta method is only implemented for order from 1, 2, 3 or 4."); } else { methodOrder = n; } } +int RungeKutta::getMethodOrder(){ + return methodOrder; +}; diff --git a/ODElibrary/RungeKutta.h b/ODElibrary/RungeKutta.h index 6b2af06..836d906 100644 --- a/ODElibrary/RungeKutta.h +++ b/ODElibrary/RungeKutta.h @@ -1,32 +1,34 @@ #ifndef ODELIBRARY_RUNGEKUTTA_H #define ODELIBRARY_RUNGEKUTTA_H #include "AbstractSolver.h" /** * The famous Runge Kutta solver. Solutions for 4 methods order are available. It will compute the 4th order udpate by * default, a.k.a. the RK4 method. */ class RungeKutta : public AbstractSolver{ private: // int methodOrder; public: /** * Default constructor sets the method order to 4. */ RungeKutta() : AbstractSolver() { methodOrder=4; }; /** * Setter for the method order, will throw an exception if the order is invalid. * @param n : method order, integer, can be 1, 2, 3 or 4 */ void setMethodOrder(int n); + /// getter for the method order + int getMethodOrder(); /** * Step method for runge Kutta, it will depend on the method order. * @param y : Eigen vector, the current value of the equation * @param t : double, the current time where the step must be performed */ void step(Eigen::VectorXd &y, double t) override ; }; #endif //ODELIBRARY_RUNGEKUTTA_H \ No newline at end of file