/* * fpu.cpp * * This example demonstrates how one can use odeint to solve the Fermi-Pasta-Ulam system. * Created on: July 13, 2011 * * Copyright 2011-2012 Karsten Ahnert * Copyright 2011 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 #include #include #include #include #ifndef M_PI //not there on windows #define M_PI 3.1415927 //... #endif using namespace std; using namespace boost::numeric::odeint; //[ fpu_system_function typedef vector< double > container_type; struct fpu { const double m_beta; fpu( const double beta = 1.0 ) : m_beta( beta ) { } // system function defining the ODE void operator()( const container_type &q , container_type &dpdt ) const { size_t n = q.size(); double tmp = q[0] - 0.0; double tmp2 = tmp + m_beta * tmp * tmp * tmp; dpdt[0] = -tmp2; for( size_t i=0 ; i } // calculates the local energy of the system void local_energy( const container_type &q , const container_type &p , container_type &e ) const { // ... //<- size_t n = q.size(); double tmp = q[0]; double tmp2 = 0.5 * tmp * tmp + 0.25 * m_beta * tmp * tmp * tmp * tmp; e[0] = tmp2; for( size_t i=0 ; i } }; //] //[ fpu_observer struct streaming_observer { std::ostream& m_out; const fpu &m_fpu; size_t m_write_every; size_t m_count; streaming_observer( std::ostream &out , const fpu &f , size_t write_every = 100 ) : m_out( out ) , m_fpu( f ) , m_write_every( write_every ) , m_count( 0 ) { } template< class State > void operator()( const State &x , double t ) { if( ( m_count % m_write_every ) == 0 ) { container_type &q = x.first; container_type &p = x.second; container_type energy( q.size() ); m_fpu.local_energy( q , p , energy ); for( size_t i=0 ; i stepper_type; fpu fpu_instance( 8.0 ); integrate_const( stepper_type() , fpu_instance , make_pair( boost::ref( q ) , boost::ref( p ) ) , 0.0 , 1000.0 , dt , streaming_observer( cout , fpu_instance , 10 ) ); //] return 0; }