Explicit Time Integration for Known Types

We demonstate how to integrate in time the following system of ODEs:

\[ \frac{d \mathbf{y}}{d t} = 10 \mathbf{y} \]

where $\mathbf{y} \in R^3$ is the state and $t$ is time.

Include

#include "pressio/ode_advancers.hpp"
#include "pressio/ode_steppers_explicit.hpp"
#include <Eigen/Core>

Problem class

template<class scalar_t>
struct MySystem{
  using scalar_type   = scalar_t;
  using state_type    = Eigen::Matrix<scalar_t,-1,1>;
  using velocity_type = state_type;

public:
  void velocity(const state_type & y,
                const scalar_type t,
                velocity_type & rhs) const
  {
    constexpr scalar_type ten{10};
    rhs(0) = ten * y(0);
    rhs(1) = ten * y(1);
    rhs(2) = ten * y(2);
  };

  velocity_type createVelocity() const{
    return velocity_type(3);
  };
};

Main

int main(int argc, char *argv[])
{
  pressio::log::initialize(pressio::logto::terminal);

  using scalar_type = double;

  // create the system/app object
  using app_t = MySystem<scalar_type>;
  app_t appObj;

  // create and initialize the state
  using state_t = typename app_t::state_type;
  state_t y(3);
  y(0) = 1.; y(1) = 2.; y(2) = 3.;

  // instantiate the stepper
  namespace pode = pressio::ode;
  constexpr auto scheme = pode::StepScheme::ForwardEuler;
  auto stepperObj = pode::create_explicit_stepper(scheme, y, appObj);

  // integrate in time
  const scalar_type dt = 0.1;
  const scalar_type startTime = 0.0;
  const pode::step_count_type numSteps = 1;
  pode::advance_n_steps(stepperObj, y, startTime, dt, numSteps);

  // check solution
  std::cout << "Computed solution: ["
            << y(0) << " " << y(1) << " " << y(2) << "] "
            << "Expected solution: [2,4,6] "
            << std::endl;

  pressio::log::finalize();
  return 0;
}

Full Code

The full code is available here.