- 3.0.2 core module.
IntegratorComparison.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #include <gtest/gtest.h>
9 #include "../system/TestNonlinearSystem.h"
10 
11 
12 using namespace ct::core;
13 using std::shared_ptr;
14 
15 
16 double uniformRandomNumber(double min, double max)
17 {
18  std::random_device rd; // obtain a random number from hardware
19  std::mt19937 eng(rd()); // seed the generator
20  std::uniform_real_distribution<> distr(min, max); // define the range
21 
22  return distr(eng);
23 }
24 
25 
26 TEST(IntegrationTest, derivativeTest)
27 {
29 
30  // create two nonlinear systems, one regular one and one auto-differentiable
31  //
32  size_t nTests = 1;
33 
34  for (size_t i = 0; i < nTests; ++i)
35  {
36  shared_ptr<SecondOrderSystem> oscillator;
37  double w_n = uniformRandomNumber(0, 10);
38  double zeta = uniformRandomNumber(0, 10);
39 
40  // make sure we are not complex
41  while (w_n * w_n - zeta * zeta <= 0)
42  {
43  w_n = uniformRandomNumber(0, 100);
44  zeta = uniformRandomNumber(0, 10);
45  }
46  oscillator = shared_ptr<SecondOrderSystem>(new SecondOrderSystem(w_n, zeta));
47  oscillator->checkParameters();
48 
49  // std::shared_ptr<IntegratorBase<state_dim> > integratorEulerOdeint;
50  Integrator<state_dim> integratorEulerOdeint(oscillator, EULER);
51  // std::shared_ptr<IntegratorBase<state_dim> > integratorRk4Odeint;
52  Integrator<state_dim> integratorRk4Odeint(oscillator, RK4);
53 
54  Integrator<state_dim> integratorEulerCT(oscillator, EULERCT);
55  Integrator<state_dim> integratorRK4CT(oscillator, RK4CT);
56 
57  StateVector<state_dim> stateEulerCT;
58  stateEulerCT << 1.0, 0.0;
59  StateVector<state_dim> stateEulerOdeInt;
60  stateEulerOdeInt << 1.0, 0.0;
61  StateVector<state_dim> stateRK4CT;
62  stateRK4CT << 1.0, 0.0;
63  StateVector<state_dim> stateRK4OdeInt;
64  stateRK4OdeInt << 1.0, 0.0;
65 
66  double dt = 0.00001;
67  double startTime = 0.0;
68  size_t numSteps = 100000;
69 
70  Timer t;
71  t.start();
72  integratorEulerOdeint.integrate_n_steps(stateEulerOdeInt, startTime, numSteps, dt);
73  t.stop();
74 
75  std::cout << "ODEINT Euler took: " << t.getElapsedTime() << " s for integration" << std::endl;
76 
77  t.reset();
78  t.start();
79  integratorEulerCT.integrate_n_steps(stateEulerCT, startTime, numSteps, dt);
80  t.stop();
81  std::cout << "CT Euler took: " << t.getElapsedTime() << " s for integration" << std::endl;
82 
83  t.reset();
84  t.start();
85  integratorRk4Odeint.integrate_n_steps(stateRK4OdeInt, startTime, numSteps, dt);
86  t.stop();
87  std::cout << "ODEINT RK4 took: " << t.getElapsedTime() << " s for integration" << std::endl;
88 
89  t.reset();
90  t.start();
91  integratorRK4CT.integrate_n_steps(stateRK4CT, startTime, numSteps, dt);
92  t.stop();
93  std::cout << "CT RK4 took: " << t.getElapsedTime() << " s for integration" << std::endl;
94 
95  ASSERT_LT((stateRK4CT - stateRK4OdeInt).array().abs().maxCoeff(), 1e-12);
96  ASSERT_LT((stateEulerCT - stateEulerOdeInt).array().abs().maxCoeff(), 1e-12);
97  }
98 }
A timer ("stop watch") to record elapsed time based on the system clock.
Definition: Timer.h:19
void integrate_n_steps(StateVector< STATE_DIM, SCALAR > &state, const SCALAR &startTime, size_t numSteps, SCALAR dt, StateVectorArray< STATE_DIM, SCALAR > &stateTrajectory, tpl::TimeArray< SCALAR > &timeTrajectory)
Equidistant integration based on number of time steps and step length.
Definition: Integrator-impl.h:50
SCALAR getElapsedTime() const
Get the elapsed time between calls to start() and stop()
Definition: Timer.h:40
Definition: Integrator.h:32
const double zeta
double uniformRandomNumber(double min, double max)
Definition: IntegratorComparison.h:16
const double w_n
void stop()
Trigger stop.
Definition: Timer.h:34
clear all close all load ct GNMSLog0 mat reformat t
const double dt
void start()
Trigger start.
Definition: Timer.h:29
tpl::SecondOrderSystem< double > SecondOrderSystem
harmonic oscillator (double)
Definition: SecondOrderSystem.h:220
const size_t state_dim
Definition: SymplecticIntegrationTest.cpp:14
static const size_t STATE_DIM
Definition: TestNonlinearSystem.h:21
Definition: StateVector.h:12
Standard Integrator.
Definition: Integrator.h:62
for i
Definition: Integrator.h:33
Definition: Integrator.h:39
TEST(IntegrationTest, derivativeTest)
Definition: IntegratorComparison.h:26
Definition: Integrator.h:40
void reset()
Resets the clock.
Definition: Timer.h:49