9
3

phase_oscillator_ensemble.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //==============================================================================
  2. // Copyright 2011-2014 Karsten Ahnert
  3. // Copyright 2011-2014 Mario Mulansky
  4. // Copyright 2014 LRI UMR 8623 CNRS/Univ Paris Sud XI
  5. // Copyright 2014 NumScale SAS
  6. //
  7. // Distributed under the Boost Software License, Version 1.0.
  8. // See accompanying file LICENSE.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt
  10. //==============================================================================
  11. #include <iostream>
  12. #include <utility>
  13. #include <boost/numeric/odeint.hpp>
  14. #ifndef M_PI //not there on windows
  15. #define M_PI 3.141592653589793 //...
  16. #endif
  17. #include <boost/random.hpp>
  18. #include <boost/dispatch/meta/as_integer.hpp>
  19. #include <nt2/include/functions/cos.hpp>
  20. #include <nt2/include/functions/sin.hpp>
  21. #include <nt2/include/functions/atan2.hpp>
  22. #include <nt2/table.hpp>
  23. #include <nt2/include/functions/zeros.hpp>
  24. #include <nt2/include/functions/sum.hpp>
  25. #include <nt2/include/functions/mean.hpp>
  26. #include <nt2/arithmetic/include/functions/hypot.hpp>
  27. #include <nt2/include/functions/tie.hpp>
  28. #include <boost/numeric/odeint/external/nt2/nt2_algebra_dispatcher.hpp>
  29. using namespace std;
  30. using namespace boost::numeric::odeint;
  31. template <typename container_type, typename T>
  32. pair< T, T > calc_mean_field( const container_type &x )
  33. {
  34. T cos_sum = 0.0 , sin_sum = 0.0;
  35. nt2::tie(cos_sum,sin_sum) = nt2::tie(nt2::mean( nt2::cos(x) ), nt2::mean( nt2::sin(x) ));
  36. T K = nt2::hypot(sin_sum,cos_sum);
  37. T Theta = nt2::atan2( sin_sum , cos_sum );
  38. return make_pair( K , Theta );
  39. }
  40. template <typename container_type, typename T>
  41. struct phase_ensemble
  42. {
  43. typedef typename boost::dispatch::meta::as_integer<T,unsigned>::type int_type;
  44. container_type m_omega;
  45. T m_epsilon;
  46. phase_ensemble( const int_type n , T g = 1.0 , T epsilon = 1.0 )
  47. : m_epsilon( epsilon )
  48. {
  49. m_omega = nt2::zeros(nt2::of_size(n), nt2::meta::as_<T>());
  50. create_frequencies( g );
  51. }
  52. void create_frequencies( T g )
  53. {
  54. boost::mt19937 rng;
  55. boost::cauchy_distribution<> cauchy( 0.0 , g );
  56. boost::variate_generator< boost::mt19937&, boost::cauchy_distribution<> > gen( rng , cauchy );
  57. generate( m_omega.begin() , m_omega.end() , gen );
  58. }
  59. void set_epsilon( T epsilon ) { m_epsilon = epsilon; }
  60. T get_epsilon( void ) const { return m_epsilon; }
  61. void operator()( const container_type &x , container_type &dxdt , T ) const
  62. {
  63. pair< T, T > mean = calc_mean_field<container_type,T>( x );
  64. dxdt = m_omega + m_epsilon * mean.first * nt2::sin( mean.second - x );
  65. }
  66. };
  67. template<typename T>
  68. struct statistics_observer
  69. {
  70. typedef typename boost::dispatch::meta::as_integer<T,unsigned>::type int_type;
  71. T m_K_mean;
  72. int_type m_count;
  73. statistics_observer( void )
  74. : m_K_mean( 0.0 ) , m_count( 0 ) { }
  75. template< class State >
  76. void operator()( const State &x , T t )
  77. {
  78. pair< T, T > mean = calc_mean_field<State,T>( x );
  79. m_K_mean += mean.first;
  80. ++m_count;
  81. }
  82. T get_K_mean( void ) const { return ( m_count != 0 ) ? m_K_mean / T( m_count ) : 0.0 ; }
  83. void reset( void ) { m_K_mean = 0.0; m_count = 0; }
  84. };
  85. template<typename T>
  86. struct test_ode_table
  87. {
  88. typedef nt2::table<T> array_type;
  89. typedef void experiment_is_immutable;
  90. typedef typename boost::dispatch::meta::as_integer<T,unsigned>::type int_type;
  91. test_ode_table ( )
  92. : size_(16384), ensemble( size_ , 1.0 ), unif( 0.0 , 2.0 * M_PI ), gen( rng , unif ), obs()
  93. {
  94. x.resize(nt2::of_size(size_));
  95. }
  96. void operator()()
  97. {
  98. for( T epsilon = 0.0 ; epsilon < 5.0 ; epsilon += 0.1 )
  99. {
  100. ensemble.set_epsilon( epsilon );
  101. obs.reset();
  102. // start with random initial conditions
  103. generate( x.begin() , x.end() , gen );
  104. // calculate some transients steps
  105. integrate_const( runge_kutta4< array_type, T >() , boost::ref( ensemble ) , x , T(0.0) , T(10.0) , dt );
  106. // integrate and compute the statistics
  107. integrate_const( runge_kutta4< array_type, T >() , boost::ref( ensemble ) , x , T(0.0) , T(100.0) , dt , boost::ref( obs ) );
  108. cout << epsilon << "\t" << obs.get_K_mean() << endl;
  109. }
  110. }
  111. friend std::ostream& operator<<(std::ostream& os, test_ode_table<T> const& p)
  112. {
  113. return os << "(" << p.size() << ")";
  114. }
  115. std::size_t size() const { return size_; }
  116. private:
  117. std::size_t size_;
  118. phase_ensemble<array_type,T> ensemble;
  119. boost::uniform_real<> unif;
  120. array_type x;
  121. boost::mt19937 rng;
  122. boost::variate_generator< boost::mt19937&, boost::uniform_real<> > gen;
  123. statistics_observer<T> obs;
  124. static const T dt = 0.1;
  125. };
  126. int main()
  127. {
  128. std::cout<< " With T = [double] \n";
  129. test_ode_table<double> test_double;
  130. test_double();
  131. std::cout<< " With T = [float] \n";
  132. test_ode_table<float> test_float;
  133. test_float();
  134. }