tutorial_chaotic_system.qbk 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. [/============================================================================
  2. Boost.odeint
  3. Copyright 2011-2012 Karsten Ahnert
  4. Copyright 2011-2013 Mario Mulansky
  5. Copyright 2012 Sylwester Arabas
  6. Use, modification and distribution is subject to the Boost Software License,
  7. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. http://www.boost.org/LICENSE_1_0.txt)
  9. =============================================================================/]
  10. [section Chaotic systems and Lyapunov exponents]
  11. [import ../examples/chaotic_system.cpp]
  12. In this example we present application of odeint to investigation of the properties of chaotic
  13. deterministic systems. In mathematical terms chaotic refers to an exponential
  14. growth of perturbations ['__delta x]. In order to observe this exponential growth one usually solves the equations for the tangential dynamics which is again an ordinary differential equation. These equations are linear but time dependent and can be obtained via
  15. ['d __delta x / dt = J(x) __delta x]
  16. where ['J] is the Jacobian of the system under consideration. ['__delta x] can
  17. also be interpreted as a perturbation of the original system. In principle
  18. ['n] of these perturbations exist, they form a hypercube and evolve in the
  19. time. The Lyapunov exponents are then defined as logarithmic growth rates of
  20. the perturbations. If one Lyapunov exponent is larger then zero the nearby
  21. trajectories diverge exponentially hence they are chaotic. If the largest
  22. Lyapunov exponent is zero one is usually faced with periodic motion. In the
  23. case of a largest Lyapunov exponent smaller then zero convergence to a
  24. fixed point is expected. More information's about Lyapunov exponents and nonlinear
  25. dynamical systems can be found in many textbooks, see for example: E. Ott "Chaos is
  26. Dynamical Systems", Cambridge.
  27. To calculate the Lyapunov exponents numerically one usually solves the equations of motion for ['n] perturbations and orthonormalizes them every ['k] steps. The Lyapunov exponent is the average of the logarithm of the stretching factor of each perturbation.
  28. To demonstrate how one can use odeint to determine the Lyapunov exponents we choose the Lorenz system. It is one of the most studied dynamical systems in the nonlinear dynamics community. For the standard parameters it possesses a strange attractor with non-integer dimension. The Lyapunov exponents take values of approximately 0.9, 0 and -12.
  29. The implementation of the Lorenz system is
  30. ``
  31. const double sigma = 10.0;
  32. const double R = 28.0;
  33. const double b = 8.0 / 3.0;
  34. typedef boost::array< double , 3 > lorenz_state_type;
  35. void lorenz( const lorenz_state_type &x , lorenz_state_type &dxdt , double t )
  36. {
  37. dxdt[0] = sigma * ( x[1] - x[0] );
  38. dxdt[1] = R * x[0] - x[1] - x[0] * x[2];
  39. dxdt[2] = -b * x[2] + x[0] * x[1];
  40. }
  41. ``
  42. We need also to integrate the set of the perturbations. This is done in parallel to the original system, hence within one system function. Of course, we want to use the above definition of the Lorenz system, hence the definition of the system function including the Lorenz system itself and the perturbation could look like:
  43. ``
  44. const size_t n = 3;
  45. const size_t num_of_lyap = 3;
  46. const size_t N = n + n*num_of_lyap;
  47. typedef std::tr1::array< double , N > state_type;
  48. typedef std::tr1::array< double , num_of_lyap > lyap_type;
  49. void lorenz_with_lyap( const state_type &x , state_type &dxdt , double t )
  50. {
  51. lorenz( x , dxdt , t );
  52. for( size_t l=0 ; l<num_of_lyap ; ++l )
  53. {
  54. const double *pert = x.begin() + 3 + l * 3;
  55. double *dpert = dxdt.begin() + 3 + l * 3;
  56. dpert[0] = - sigma * pert[0] + 10.0 * pert[1];
  57. dpert[1] = ( R - x[2] ) * pert[0] - pert[1] - x[0] * pert[2];
  58. dpert[2] = x[1] * pert[0] + x[0] * pert[1] - b * pert[2];
  59. }
  60. }
  61. ``
  62. The perturbations are stored linearly in the `state_type` behind the state of the Lorenz system.
  63. The problem of '''lorenz()''' and '''lorenz_with_lyap()''' having different state types may be solved putting the Lorenz system inside a functor with templatized arguments:
  64. ``
  65. struct lorenz
  66. {
  67. template< class StateIn , class StateOut , class Value >
  68. void operator()( const StateIn &x , StateOut &dxdt , Value t )
  69. {
  70. dxdt[0] = sigma * ( x[1] - x[0] );
  71. dxdt[1] = R * x[0] - x[1] - x[0] * x[2];
  72. dxdt[2] = -b * x[2] + x[0] * x[1];
  73. }
  74. };
  75. void lorenz_with_lyap( const state_type &x , state_type &dxdt , double t )
  76. {
  77. lorenz()( x , dxdt , t );
  78. ...
  79. }
  80. ``
  81. This works fine and `lorenz_with_lyap` can be used for example via
  82. ``
  83. state_type x;
  84. // initialize x..
  85. explicit_rk4< state_type > rk4;
  86. integrate_n_steps( rk4 , lorenz_with_lyap , x , 0.0 , 0.01 , 1000 );
  87. ``
  88. This code snippet performs 1000 steps with constant step size 0.01.
  89. A real world use case for the calculation of the Lyapunov exponents of Lorenz system would always include some transient steps, just to ensure that the current state lies on the attractor, hence it would look like
  90. ``
  91. state_type x;
  92. // initialize x
  93. explicit_rk4< state_type > rk4;
  94. integrate_n_steps( rk4 , lorenz , x , 0.0 , 0.01 , 1000 );
  95. ``
  96. The problem is now, that `x` is the full state containing also the
  97. perturbations and `integrate_n_steps` does not know that it should only use 3
  98. elements. In detail, odeint and its steppers determine the length of the
  99. system under consideration by determining the length of the state. In the
  100. classical solvers, e.g. from Numerical Recipes, the problem was solved by
  101. pointer to the state and an appropriate length, something similar to
  102. ``
  103. void lorenz( double* x , double *dxdt , double t, void* params )
  104. {
  105. ...
  106. }
  107. int system_length = 3;
  108. rk4( x , system_length , t , dt , lorenz );
  109. ``
  110. But odeint supports a similar and much more sophisticated concept: __boost_range. To make the steppers and the system ready to work with __boost_range the system has to be changed:
  111. [system_function_without_perturbations]
  112. This is in principle all. Now, we only have to call `integrate_n_steps` with a
  113. range including only the first 3 components of ['x]:
  114. [integrate_transients_with_range]
  115. [note Note that when using __boost_range, we have to explicitly configure the
  116. stepper to use the `range_algebra` as otherwise odeint would automatically
  117. chose the `array_algebra`, which is incompatible with the usage of __boost_range, because the original state_type is an `array`.]
  118. Having integrated a sufficient number of transients steps we are now able to calculate the Lyapunov exponents:
  119. # Initialize the perturbations. They are stored linearly behind the state of the Lorenz system. The perturbations are initialized such that [' p [subl ij] = __delta [subl ij]], where ['p [subl ij]] is the ['j]-component of the ['i].-th perturbation and ['__delta [subl ij]] is the Kronecker symbol.
  120. # Integrate 100 steps of the full system with perturbations
  121. # Orthonormalize the perturbation using Gram-Schmidt orthonormalization algorithm.
  122. # Repeat step 2 and 3. Every 10000 steps write the current Lyapunov exponent.
  123. [lyapunov_full_code]
  124. The full code can be found here: [github_link examples/chaotic_system.cpp chaotic_system.cpp]
  125. [endsect]