chaotic_system.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * chaotic_system.cpp
  3. *
  4. * This example demonstrates how one can use odeint to determine the Lyapunov
  5. * exponents of a chaotic system namely the well known Lorenz system. Furthermore,
  6. * it shows how odeint interacts with boost.range.
  7. *
  8. * Copyright 2011-2012 Karsten Ahnert
  9. * Copyright 2011-2013 Mario Mulansky
  10. *
  11. * Distributed under the Boost Software License, Version 1.0.
  12. * (See accompanying file LICENSE_1_0.txt or
  13. * copy at http://www.boost.org/LICENSE_1_0.txt)
  14. */
  15. #include <iostream>
  16. #include <boost/array.hpp>
  17. #include <boost/numeric/odeint.hpp>
  18. #include "gram_schmidt.hpp"
  19. using namespace std;
  20. using namespace boost::numeric::odeint;
  21. const double sigma = 10.0;
  22. const double R = 28.0;
  23. const double b = 8.0 / 3.0;
  24. //[ system_function_without_perturbations
  25. struct lorenz
  26. {
  27. template< class State , class Deriv >
  28. void operator()( const State &x_ , Deriv &dxdt_ , double t ) const
  29. {
  30. typename boost::range_iterator< const State >::type x = boost::begin( x_ );
  31. typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ );
  32. dxdt[0] = sigma * ( x[1] - x[0] );
  33. dxdt[1] = R * x[0] - x[1] - x[0] * x[2];
  34. dxdt[2] = -b * x[2] + x[0] * x[1];
  35. }
  36. };
  37. //]
  38. //[ system_function_with_perturbations
  39. const size_t n = 3;
  40. const size_t num_of_lyap = 3;
  41. const size_t N = n + n*num_of_lyap;
  42. typedef boost::array< double , N > state_type;
  43. typedef boost::array< double , num_of_lyap > lyap_type;
  44. void lorenz_with_lyap( const state_type &x , state_type &dxdt , double t )
  45. {
  46. lorenz()( x , dxdt , t );
  47. for( size_t l=0 ; l<num_of_lyap ; ++l )
  48. {
  49. const double *pert = x.begin() + 3 + l * 3;
  50. double *dpert = dxdt.begin() + 3 + l * 3;
  51. dpert[0] = - sigma * pert[0] + 10.0 * pert[1];
  52. dpert[1] = ( R - x[2] ) * pert[0] - pert[1] - x[0] * pert[2];
  53. dpert[2] = x[1] * pert[0] + x[0] * pert[1] - b * pert[2];
  54. }
  55. }
  56. //]
  57. int main( int argc , char **argv )
  58. {
  59. state_type x;
  60. lyap_type lyap;
  61. fill( x.begin() , x.end() , 0.0 );
  62. x[0] = 10.0 ; x[1] = 10.0 ; x[2] = 5.0;
  63. const double dt = 0.01;
  64. //[ integrate_transients_with_range
  65. // explicitly choose range_algebra to override default choice of array_algebra
  66. runge_kutta4< state_type , double , state_type , double , range_algebra > rk4;
  67. // perform 10000 transient steps
  68. integrate_n_steps( rk4 , lorenz() , std::make_pair( x.begin() , x.begin() + n ) , 0.0 , dt , 10000 );
  69. //]
  70. //[ lyapunov_full_code
  71. fill( x.begin()+n , x.end() , 0.0 );
  72. for( size_t i=0 ; i<num_of_lyap ; ++i ) x[n+n*i+i] = 1.0;
  73. fill( lyap.begin() , lyap.end() , 0.0 );
  74. double t = 0.0;
  75. size_t count = 0;
  76. while( true )
  77. {
  78. t = integrate_n_steps( rk4 , lorenz_with_lyap , x , t , dt , 100 );
  79. gram_schmidt< num_of_lyap >( x , lyap , n );
  80. ++count;
  81. if( !(count % 100000) )
  82. {
  83. cout << t;
  84. for( size_t i=0 ; i<num_of_lyap ; ++i ) cout << "\t" << lyap[i] / t ;
  85. cout << endl;
  86. }
  87. }
  88. //]
  89. return 0;
  90. }