2 Copyright 2010-2012 Karsten Ahnert
3 Copyright 2011-2013 Mario Mulansky
4 Copyright 2013 Pascal Germroth
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE_1_0.txt or
8 copy at http://www.boost.org/LICENSE_1_0.txt)
15 #include <boost/numeric/odeint.hpp>
20 /* The type of container used to hold the state vector */
21 typedef std::vector< double > state_type;
23 const double gam = 0.15;
25 /* The rhs of x' = f(x) */
26 void harmonic_oscillator( const state_type &x , state_type &dxdt , const double /* t */ )
29 dxdt[1] = -x[0] - gam*x[1];
38 /* The rhs of x' = f(x) defined as a class */
44 harm_osc( double gam ) : m_gam(gam) { }
46 void operator() ( const state_type &x , state_type &dxdt , const double /* t */ )
49 dxdt[1] = -x[0] - m_gam*x[1];
58 //[ integrate_observer
59 struct push_back_state_and_time
61 std::vector< state_type >& m_states;
62 std::vector< double >& m_times;
64 push_back_state_and_time( std::vector< state_type > &states , std::vector< double > × )
65 : m_states( states ) , m_times( times ) { }
67 void operator()( const state_type &x , double t )
69 m_states.push_back( x );
70 m_times.push_back( t );
77 void operator()( const state_type &x ) const
79 std::cout << x[0] << "\t" << x[1] << "\n";
84 int main(int /* argc */ , char** /* argv */ )
87 using namespace boost::numeric::odeint;
90 //[ state_initialization
92 x[0] = 1.0; // start at x=1.0, p=0.0
99 size_t steps = integrate( harmonic_oscillator ,
100 x , 0.0 , 10.0 , 0.1 );
105 //[ integration_class
107 steps = integrate( ho ,
108 x , 0.0 , 10.0 , 0.1 );
116 vector<state_type> x_vec;
117 vector<double> times;
119 steps = integrate( harmonic_oscillator ,
120 x , 0.0 , 10.0 , 0.1 ,
121 push_back_state_and_time( x_vec , times ) );
124 for( size_t i=0; i<=steps; i++ )
126 cout << times[i] << '\t' << x_vec[i][0] << '\t' << x_vec[i][1] << '\n';
136 //[ define_const_stepper
137 runge_kutta4< state_type > stepper;
138 integrate_const( stepper , harmonic_oscillator , x , 0.0 , 10.0 , 0.01 );
144 //[ integrate_const_loop
145 const double dt = 0.01;
146 for( double t=0.0 ; t<10.0 ; t+= dt )
147 stepper.do_step( harmonic_oscillator , x , t , dt );
153 //[ define_adapt_stepper
154 typedef runge_kutta_cash_karp54< state_type > error_stepper_type;
160 typedef controlled_runge_kutta< error_stepper_type > controlled_stepper_type;
161 controlled_stepper_type controlled_stepper;
162 integrate_adaptive( controlled_stepper , harmonic_oscillator , x , 0.0 , 10.0 , 0.01 );
166 //[integrate_adapt_full
167 double abs_err = 1.0e-10 , rel_err = 1.0e-6 , a_x = 1.0 , a_dxdt = 1.0;
168 controlled_stepper_type controlled_stepper(
169 default_error_checker< double , range_algebra , default_operations >( abs_err , rel_err , a_x , a_dxdt ) );
170 integrate_adaptive( controlled_stepper , harmonic_oscillator , x , 0.0 , 10.0 , 0.01 );
175 //[integrate_adapt_make_controlled
176 integrate_adaptive( make_controlled< error_stepper_type >( 1.0e-10 , 1.0e-6 ) ,
177 harmonic_oscillator , x , 0.0 , 10.0 , 0.01 );
183 //[integrate_adapt_make_controlled_alternative
184 integrate_adaptive( make_controlled( 1.0e-10 , 1.0e-6 , error_stepper_type() ) ,
185 harmonic_oscillator , x , 0.0 , 10.0 , 0.01 );
188 #ifdef BOOST_NUMERIC_ODEINT_CXX11
189 //[ define_const_stepper_cpp11
191 runge_kutta4< state_type > stepper;
192 integrate_const( stepper , []( const state_type &x , state_type &dxdt , double t ) {
193 dxdt[0] = x[1]; dxdt[1] = -x[0] - gam*x[1]; }
194 , x , 0.0 , 10.0 , 0.01 );
200 //[ harm_iterator_const_step]
201 std::for_each( make_const_step_time_iterator_begin( stepper , harmonic_oscillator, x , 0.0 , 0.1 , 10.0 ) ,
202 make_const_step_time_iterator_end( stepper , harmonic_oscillator, x ) ,
203 []( std::pair< const state_type & , const double & > x ) {
204 cout << x.second << " " << x.first[0] << " " << x.first[1] << "\n"; } );