Skip to content

Sampling

Stephen Berry edited this page Sep 18, 2017 · 1 revision

Ascent provides a Sampler class, which changes the time step according to desired sampling rates and events.

Example

#include "ascent/Ascent.h"

using namespace asc;

struct ODE
{
   void operator()(const state_t& x, state_t& xd, const double t)
   {
      xd[0] = cos(t);
   }
};

int main()
{
   state_t x = { 0.0 };
   double t = 0.0;
   double dt = 0.1;
   double t_end = 10.0;

   RK4 integrator;
   ODE system;

   Recorder recorder;

   while (t < t_end)
   {
      Sampler sampler(t, dt);

      // We force the system to be evaluated at all increments of 0.33 and 0.41, as well as trigger a single event evaluation at 0.617
      if (sampler(0.33) || sampler(0.41) || sampler.event(0.617))
         recorder({ t, x[0] });

      integrator(system, x, t, dt);
   }

   recorder.csv("sampling", { "t", "x0" }); // generate a file of comma separated values

   return 0;
}

Clone this wiki locally