4 * This example demonstrates how one can use odeint to solve the Fermi-Pasta-Ulam system.
6 * Created on: July 13, 2011
8 * Copyright 2011-2012 Karsten Ahnert
9 * Copyright 2011 Mario Mulansky
10 * Distributed under the Boost Software License, Version 1.0. (See
11 * accompanying file LICENSE_1_0.txt or copy at
12 * http://www.boost.org/LICENSE_1_0.txt)
20 #include <boost/numeric/odeint.hpp>
22 #ifndef M_PI //not there on windows
23 #define M_PI 3.1415927 //...
27 using namespace boost::numeric::odeint;
29 //[ fpu_system_function
30 typedef vector< double > container_type;
36 fpu( const double beta = 1.0 ) : m_beta( beta ) { }
38 // system function defining the ODE
39 void operator()( const container_type &q , container_type &dpdt ) const
42 double tmp = q[0] - 0.0;
43 double tmp2 = tmp + m_beta * tmp * tmp * tmp;
45 for( size_t i=0 ; i<n-1 ; ++i )
48 tmp2 = tmp + m_beta * tmp * tmp * tmp;
53 tmp2 = tmp + m_beta * tmp * tmp * tmp;
57 // calculates the energy of the system
58 double energy( const container_type &q , const container_type &p ) const
66 energy += 0.5 * tmp * tmp + 0.25 * m_beta * tmp * tmp * tmp * tmp;
67 for( size_t i=0 ; i<n-1 ; ++i )
70 energy += 0.5 * ( p[i] * p[i] + tmp * tmp ) + 0.25 * m_beta * tmp * tmp * tmp * tmp;
72 energy += 0.5 * p[n-1] * p[n-1];
74 energy += 0.5 * tmp * tmp + 0.25 * m_beta * tmp * tmp * tmp * tmp;
80 // calculates the local energy of the system
81 void local_energy( const container_type &q , const container_type &p , container_type &e ) const
87 double tmp2 = 0.5 * tmp * tmp + 0.25 * m_beta * tmp * tmp * tmp * tmp;
89 for( size_t i=0 ; i<n-1 ; ++i )
92 tmp2 = 0.25 * tmp * tmp + 0.125 * m_beta * tmp * tmp * tmp * tmp;
93 e[i] += 0.5 * p[i] * p[i] + tmp2 ;
97 tmp2 = 0.5 * tmp * tmp + 0.25 * m_beta * tmp * tmp * tmp * tmp;
98 e[n-1] += 0.5 * p[n-1] * p[n-1] + tmp2;
107 struct streaming_observer
111 size_t m_write_every;
114 streaming_observer( std::ostream &out , const fpu &f , size_t write_every = 100 )
115 : m_out( out ) , m_fpu( f ) , m_write_every( write_every ) , m_count( 0 ) { }
117 template< class State >
118 void operator()( const State &x , double t )
120 if( ( m_count % m_write_every ) == 0 )
122 container_type &q = x.first;
123 container_type &p = x.second;
124 container_type energy( q.size() );
125 m_fpu.local_energy( q , p , energy );
126 for( size_t i=0 ; i<q.size() ; ++i )
128 m_out << t << "\t" << i << "\t" << q[i] << "\t" << p[i] << "\t" << energy[i] << "\n";
131 clog << t << "\t" << accumulate( energy.begin() , energy.end() , 0.0 ) << "\n";
145 int main( int argc , char **argv )
149 container_type q( n , 0.0 ) , p( n , 0.0 );
151 for( size_t i=0 ; i<n ; ++i )
154 q[i] = 32.0 * sin( double( i + 1 ) / double( n + 1 ) * M_PI );
158 const double dt = 0.1;
160 typedef symplectic_rkn_sb3a_mclachlan< container_type > stepper_type;
161 fpu fpu_instance( 8.0 );
163 integrate_const( stepper_type() , fpu_instance ,
164 make_pair( boost::ref( q ) , boost::ref( p ) ) ,
165 0.0 , 1000.0 , dt , streaming_observer( cout , fpu_instance , 10 ) );