1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/*
* phase_oscillator_ensemble.cpp
*
* Demonstrates the phase transition from an unsynchronized to an synchronized state.
*
* Copyright 2011-2012 Karsten Ahnert
* Copyright 2011-2012 Mario Mulansky
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
*/
#include <iostream>
#include <utility>
#include <boost/numeric/odeint.hpp>
#ifndef M_PI //not there on windows
#define M_PI 3.141592653589793 //...
#endif
#include <boost/random.hpp>
using namespace std;
using namespace boost::numeric::odeint;
//[ phase_oscillator_ensemble_system_function
typedef vector< double > container_type;
pair< double , double > calc_mean_field( const container_type &x )
{
size_t n = x.size();
double cos_sum = 0.0 , sin_sum = 0.0;
for( size_t i=0 ; i<n ; ++i )
{
cos_sum += cos( x[i] );
sin_sum += sin( x[i] );
}
cos_sum /= double( n );
sin_sum /= double( n );
double K = sqrt( cos_sum * cos_sum + sin_sum * sin_sum );
double Theta = atan2( sin_sum , cos_sum );
return make_pair( K , Theta );
}
struct phase_ensemble
{
container_type m_omega;
double m_epsilon;
phase_ensemble( const size_t n , double g = 1.0 , double epsilon = 1.0 )
: m_omega( n , 0.0 ) , m_epsilon( epsilon )
{
create_frequencies( g );
}
void create_frequencies( double g )
{
boost::mt19937 rng;
boost::cauchy_distribution<> cauchy( 0.0 , g );
boost::variate_generator< boost::mt19937&, boost::cauchy_distribution<> > gen( rng , cauchy );
generate( m_omega.begin() , m_omega.end() , gen );
}
void set_epsilon( double epsilon ) { m_epsilon = epsilon; }
double get_epsilon( void ) const { return m_epsilon; }
void operator()( const container_type &x , container_type &dxdt , double /* t */ ) const
{
pair< double , double > mean = calc_mean_field( x );
for( size_t i=0 ; i<x.size() ; ++i )
dxdt[i] = m_omega[i] + m_epsilon * mean.first * sin( mean.second - x[i] );
}
};
//]
//[ phase_oscillator_ensemble_observer
struct statistics_observer
{
double m_K_mean;
size_t m_count;
statistics_observer( void )
: m_K_mean( 0.0 ) , m_count( 0 ) { }
template< class State >
void operator()( const State &x , double t )
{
pair< double , double > mean = calc_mean_field( x );
m_K_mean += mean.first;
++m_count;
}
double get_K_mean( void ) const { return ( m_count != 0 ) ? m_K_mean / double( m_count ) : 0.0 ; }
void reset( void ) { m_K_mean = 0.0; m_count = 0; }
};
//]
int main( int argc , char **argv )
{
//[ phase_oscillator_ensemble_integration
const size_t n = 16384;
const double dt = 0.1;
container_type x( n );
boost::mt19937 rng;
boost::uniform_real<> unif( 0.0 , 2.0 * M_PI );
boost::variate_generator< boost::mt19937&, boost::uniform_real<> > gen( rng , unif );
// gamma = 1, the phase transition occurs at epsilon = 2
phase_ensemble ensemble( n , 1.0 );
statistics_observer obs;
for( double epsilon = 0.0 ; epsilon < 5.0 ; epsilon += 0.1 )
{
ensemble.set_epsilon( epsilon );
obs.reset();
// start with random initial conditions
generate( x.begin() , x.end() , gen );
// calculate some transients steps
integrate_const( runge_kutta4< container_type >() , boost::ref( ensemble ) , x , 0.0 , 10.0 , dt );
// integrate and compute the statistics
integrate_const( runge_kutta4< container_type >() , boost::ref( ensemble ) , x , 0.0 , 100.0 , dt , boost::ref( obs ) );
cout << epsilon << "\t" << obs.get_K_mean() << endl;
}
//]
return 0;
}
|