3 libs/numeric/odeint/test/integrate_stepper_refs.cpp
6 Tests the integrate functions with boost::ref( stepper)
9 Copyright 2009-2013 Karsten Ahnert
10 Copyright 2009-2013 Mario Mulansky
12 Distributed under the Boost Software License, Version 1.0.
13 (See accompanying file LICENSE_1_0.txt or
14 copy at http://www.boost.org/LICENSE_1_0.txt)
18 #define BOOST_TEST_MODULE odeint_integrate_stepper_refs
24 #include <boost/numeric/odeint/config.hpp>
26 #include <boost/noncopyable.hpp>
27 #include <boost/test/unit_test.hpp>
28 #include <boost/iterator/counting_iterator.hpp>
30 #include <boost/mpl/vector.hpp>
32 #include <boost/numeric/odeint/integrate/integrate_const.hpp>
33 #include <boost/numeric/odeint/integrate/integrate_adaptive.hpp>
34 #include <boost/numeric/odeint/integrate/integrate_times.hpp>
35 #include <boost/numeric/odeint/integrate/integrate_n_steps.hpp>
36 #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
39 using namespace boost::unit_test
;
40 using namespace boost::numeric::odeint
;
41 namespace mpl
= boost::mpl
;
43 typedef double value_type
;
44 typedef std::vector
< value_type
> state_type
;
46 // minimal non-copyable basic stepper
50 class simple_stepper_nc
: boost::noncopyable
53 typedef State state_type
;
54 typedef double value_type
;
55 typedef state_type deriv_type
;
56 typedef double time_type
;
57 typedef size_t order_type
;
58 typedef stepper_tag stepper_category
;
60 template< class System
>
61 void do_step( System system
, state_type
&in
, time_type t
, time_type dt
)
68 // minimal non-copyable controlled stepper
72 class controlled_stepper_nc
: boost::noncopyable
75 typedef State state_type
;
76 typedef double value_type
;
77 typedef state_type deriv_type
;
78 typedef double time_type
;
79 typedef size_t order_type
;
80 typedef controlled_stepper_tag stepper_category
;
82 template< class System
>
83 controlled_step_result
try_step( System system
, state_type
&in
, time_type
&t
, time_type
&dt
)
85 std::cout
<< "dense out stepper: " << t
<< " , " << dt
<< std::endl
;
91 // minimal non-copyable dense_output stepper
95 class dense_out_stepper_nc
: boost::noncopyable
98 typedef State state_type
;
99 typedef double value_type
;
100 typedef state_type deriv_type
;
101 typedef double time_type
;
102 typedef size_t order_type
;
103 typedef dense_output_stepper_tag stepper_category
;
105 void initialize( const state_type
&x0
, const time_type t0
, const time_type dt0
)
110 std::cout
<< "initialize: " << m_t
<< " , " << m_dt
<< std::endl
;
113 template< class System
>
114 void do_step( System system
)
116 std::cout
<< "dense out stepper: " << m_t
<< " , " << m_dt
<< std::endl
;
120 void calc_state( const time_type t_inter
, state_type
&x
)
125 const state_type
& current_state() const
128 time_type
current_time() const
131 time_type
current_time_step() const
142 void lorenz( const state_type
&x
, state_type
&dxdt
, const value_type t
)
144 //const value_type sigma( 10.0 );
145 const value_type
R( 28.0 );
146 const value_type
b( value_type( 8.0 ) / value_type( 3.0 ) );
148 // first component trivial
149 dxdt
[0] = 1.0; //sigma * ( x[1] - x[0] );
150 dxdt
[1] = R
* x
[0] - x
[1] - x
[0] * x
[2];
151 dxdt
[2] = -b
* x
[2] + x
[0] * x
[1];
154 struct push_back_time
156 std::vector
< double >& m_times
;
160 push_back_time( std::vector
< double > ×
, state_type
&x
)
161 : m_times( times
) , m_x( x
) { }
163 void operator()( const state_type
&x
, double t
)
165 m_times
.push_back( t
);
166 boost::numeric::odeint::copy( x
, m_x
);
170 template< class Stepper
>
171 struct perform_integrate_const_test
175 state_type
x( 3 , 10.0 ) , x_end( 3 );
177 std::vector
< value_type
> times
;
181 integrate_const( boost::ref(stepper
) , lorenz
, x
, 0.0 , 1.0 ,
182 0.1, push_back_time( times
, x_end
) );
186 template< class Stepper
>
187 struct perform_integrate_adaptive_test
191 state_type
x( 3 , 10.0 ) , x_end( 3 );
193 std::vector
< value_type
> times
;
197 integrate_adaptive( boost::ref(stepper
) , lorenz
, x
, 0.0 , 1.0 ,
198 0.1, push_back_time( times
, x_end
) );
202 template< class Stepper
>
203 struct perform_integrate_n_steps_test
207 state_type
x( 3 , 10.0 ) , x_end( 3 );
209 std::vector
< value_type
> times
;
213 integrate_n_steps( boost::ref(stepper
) , lorenz
, x
, 0.0 , 0.1 ,
214 10 , push_back_time( times
, x_end
) );
218 template< class Stepper
>
219 struct perform_integrate_times_test
223 state_type
x( 3 , 10.0 ) , x_end( 3 );
225 std::vector
< value_type
> times
;
229 integrate_times( boost::ref(stepper
) , lorenz
, x
,
230 boost::counting_iterator
<int>(0) , boost::counting_iterator
<int>(10) , 0.1 ,
231 push_back_time( times
, x_end
) );
235 class stepper_methods
: public mpl::vector
<
236 simple_stepper_nc
< state_type
> ,
237 controlled_stepper_nc
< state_type
>,
238 dense_out_stepper_nc
< state_type
> > { };
240 BOOST_AUTO_TEST_SUITE( integrate_stepper_refs
)
242 BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_const_test_case
, Stepper
, stepper_methods
)
244 std::cout
<< "integrate const" << std::endl
;
245 perform_integrate_const_test
< Stepper
> tester
;
250 BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_adaptive_test_case
, Stepper
, stepper_methods
)
252 std::cout
<< "integrate adaptive" << std::endl
;
253 perform_integrate_adaptive_test
< Stepper
> tester
;
257 BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_n_steps_test_case
, Stepper
, stepper_methods
)
259 std::cout
<< "integrate n steps" << std::endl
;
260 perform_integrate_n_steps_test
< Stepper
> tester
;
264 BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_times_test_case
, Stepper
, stepper_methods
)
266 std::cout
<< "integrate times" << std::endl
;
267 perform_integrate_times_test
< Stepper
> tester
;
271 BOOST_AUTO_TEST_SUITE_END()