123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- /*
- [auto_generated]
- libs/numeric/odeint/test/integrate_stepper_refs.cpp
- [begin_description]
- Tests the integrate functions with boost::ref( stepper)
- [end_description]
- Copyright 2009-2013 Karsten Ahnert
- Copyright 2009-2013 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)
- */
- #define BOOST_TEST_MODULE odeint_integrate_stepper_refs
- #include <vector>
- #include <cmath>
- #include <iostream>
- #include <boost/numeric/odeint/config.hpp>
- #include <boost/noncopyable.hpp>
- #include <boost/test/unit_test.hpp>
- #include <boost/iterator/counting_iterator.hpp>
- #include <boost/mpl/vector.hpp>
- #include <boost/numeric/odeint/integrate/integrate_const.hpp>
- #include <boost/numeric/odeint/integrate/integrate_adaptive.hpp>
- #include <boost/numeric/odeint/integrate/integrate_times.hpp>
- #include <boost/numeric/odeint/integrate/integrate_n_steps.hpp>
- #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
- using namespace boost::unit_test;
- using namespace boost::numeric::odeint;
- namespace mpl = boost::mpl;
- typedef double value_type;
- typedef std::vector< value_type > state_type;
- // minimal non-copyable basic stepper
- template<
- class State
- >
- class simple_stepper_nc : boost::noncopyable
- {
- public :
- typedef State state_type;
- typedef double value_type;
- typedef state_type deriv_type;
- typedef double time_type;
- typedef size_t order_type;
- typedef stepper_tag stepper_category;
- template< class System >
- void do_step( System system , state_type &in , time_type t , time_type dt )
- {
- // empty
- }
- };
- // minimal non-copyable controlled stepper
- template<
- class State
- >
- class controlled_stepper_nc : boost::noncopyable
- {
- public :
- typedef State state_type;
- typedef double value_type;
- typedef state_type deriv_type;
- typedef double time_type;
- typedef size_t order_type;
- typedef controlled_stepper_tag stepper_category;
- template< class System >
- controlled_step_result try_step( System system , state_type &in , time_type &t , time_type &dt )
- {
- std::cout << "dense out stepper: " << t << " , " << dt << std::endl;
- t += dt;
- return success;
- }
- };
- // minimal non-copyable dense_output stepper
- template<
- class State
- >
- class dense_out_stepper_nc : boost::noncopyable
- {
- public :
- typedef State state_type;
- typedef double value_type;
- typedef state_type deriv_type;
- typedef double time_type;
- typedef size_t order_type;
- typedef dense_output_stepper_tag stepper_category;
- void initialize( const state_type &x0 , const time_type t0 , const time_type dt0 )
- {
- m_x = x0;
- m_t = t0;
- m_dt = dt0;
- std::cout << "initialize: " << m_t << " , " << m_dt << std::endl;
- }
- template< class System >
- void do_step( System system )
- {
- std::cout << "dense out stepper: " << m_t << " , " << m_dt << std::endl;
- m_t += m_dt;
- }
- void calc_state( const time_type t_inter , state_type &x )
- {
- x = m_x;
- }
- const state_type& current_state() const
- { return m_x; }
- time_type current_time() const
- { return m_t; }
- time_type current_time_step() const
- { return m_dt; }
- private :
- time_type m_t;
- time_type m_dt;
- state_type m_x;
- };
- void lorenz( const state_type &x , state_type &dxdt , const value_type t )
- {
- //const value_type sigma( 10.0 );
- const value_type R( 28.0 );
- const value_type b( value_type( 8.0 ) / value_type( 3.0 ) );
- // first component trivial
- dxdt[0] = 1.0; //sigma * ( x[1] - x[0] );
- dxdt[1] = R * x[0] - x[1] - x[0] * x[2];
- dxdt[2] = -b * x[2] + x[0] * x[1];
- }
- struct push_back_time
- {
- std::vector< double >& m_times;
- state_type& m_x;
- push_back_time( std::vector< double > × , state_type &x )
- : m_times( times ) , m_x( x ) { }
- void operator()( const state_type &x , double t )
- {
- m_times.push_back( t );
- boost::numeric::odeint::copy( x , m_x );
- }
- };
- template< class Stepper >
- struct perform_integrate_const_test
- {
- void operator()()
- {
- state_type x( 3 , 10.0 ) , x_end( 3 );
- std::vector< value_type > times;
- Stepper stepper;
- integrate_const( boost::ref(stepper) , lorenz , x , 0.0 , 1.0 ,
- 0.1, push_back_time( times , x_end ) );
- }
- };
- template< class Stepper >
- struct perform_integrate_adaptive_test
- {
- void operator()()
- {
- state_type x( 3 , 10.0 ) , x_end( 3 );
- std::vector< value_type > times;
- Stepper stepper;
- integrate_adaptive( boost::ref(stepper) , lorenz , x , 0.0 , 1.0 ,
- 0.1, push_back_time( times , x_end ) );
- }
- };
- template< class Stepper >
- struct perform_integrate_n_steps_test
- {
- void operator()()
- {
- state_type x( 3 , 10.0 ) , x_end( 3 );
- std::vector< value_type > times;
- Stepper stepper;
- integrate_n_steps( boost::ref(stepper) , lorenz , x , 0.0 , 0.1 ,
- 10 , push_back_time( times , x_end ) );
- }
- };
- template< class Stepper >
- struct perform_integrate_times_test
- {
- void operator()()
- {
- state_type x( 3 , 10.0 ) , x_end( 3 );
- std::vector< value_type > times;
- Stepper stepper;
- integrate_times( boost::ref(stepper) , lorenz , x ,
- boost::counting_iterator<int>(0) , boost::counting_iterator<int>(10) , 0.1 ,
- push_back_time( times , x_end ) );
- }
- };
- class stepper_methods : public mpl::vector<
- simple_stepper_nc< state_type > ,
- controlled_stepper_nc< state_type >,
- dense_out_stepper_nc< state_type > > { };
- BOOST_AUTO_TEST_SUITE( integrate_stepper_refs )
- BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_const_test_case , Stepper, stepper_methods )
- {
- std::cout << "integrate const" << std::endl;
- perform_integrate_const_test< Stepper > tester;
- tester();
- }
- BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_adaptive_test_case , Stepper, stepper_methods )
- {
- std::cout << "integrate adaptive" << std::endl;
- perform_integrate_adaptive_test< Stepper > tester;
- tester();
- }
- BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_n_steps_test_case , Stepper, stepper_methods )
- {
- std::cout << "integrate n steps" << std::endl;
- perform_integrate_n_steps_test< Stepper > tester;
- tester();
- }
- BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_times_test_case , Stepper, stepper_methods )
- {
- std::cout << "integrate times" << std::endl;
- perform_integrate_times_test< Stepper > tester;
- tester();
- }
- BOOST_AUTO_TEST_SUITE_END()
|