3 libs/numeric/odeint/test/integrate_implicit.cpp
6 This file tests the integrate function and its variants with the implicit steppers.
9 Copyright 2011 Mario Mulansky
10 Copyright 2011-2013 Karsten Ahnert
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)
17 #define BOOST_TEST_MODULE odeint_integrate_functions_implicit
23 #include <boost/numeric/odeint/config.hpp>
25 #include <boost/array.hpp>
26 #include <boost/ref.hpp>
27 #include <boost/iterator/counting_iterator.hpp>
28 #include <boost/numeric/ublas/vector.hpp>
29 #include <boost/numeric/ublas/matrix.hpp>
31 #include <boost/test/unit_test.hpp>
33 #include <boost/mpl/vector.hpp>
35 #ifndef ODEINT_INTEGRATE_ITERATOR
36 #include <boost/numeric/odeint/integrate/integrate_const.hpp>
37 #include <boost/numeric/odeint/integrate/integrate_adaptive.hpp>
38 #include <boost/numeric/odeint/integrate/integrate_times.hpp>
39 #include <boost/numeric/odeint/integrate/integrate_n_steps.hpp>
41 #include <boost/numeric/odeint/iterator/integrate/integrate_const.hpp>
42 #include <boost/numeric/odeint/iterator/integrate/integrate_adaptive.hpp>
43 #include <boost/numeric/odeint/iterator/integrate/integrate_times.hpp>
44 #include <boost/numeric/odeint/iterator/integrate/integrate_n_steps.hpp>
46 #include <boost/numeric/odeint/stepper/rosenbrock4.hpp>
47 #include <boost/numeric/odeint/stepper/rosenbrock4_controller.hpp>
48 #include <boost/numeric/odeint/stepper/rosenbrock4_dense_output.hpp>
51 using namespace boost::unit_test
;
52 using namespace boost::numeric::odeint
;
53 namespace mpl
= boost::mpl
;
54 namespace ublas
= boost::numeric::ublas
;
56 typedef double value_type
;
57 typedef ublas::vector
< value_type
> state_type
;
58 typedef boost::numeric::ublas::matrix
< value_type
> matrix_type
;
62 void operator()( const state_type
&x
, state_type
&dxdt
, const value_type
&t
) const
64 dxdt( 0 ) = x( 0 ) + 2 * x( 1 );
71 void operator()( const state_type
&x
, matrix_type
&jacobi
, const value_type
&t
, state_type
&dfdt
) const
84 std::vector
< double >& m_times
;
86 push_back_time( std::vector
< double > ×
)
87 : m_times( times
) { }
89 void operator()( const state_type
&x
, double t
)
91 m_times
.push_back( t
);
95 template< class Stepper
>
96 struct perform_integrate_const_test
98 void operator()( void )
100 state_type
x( 2 , 1.0 );
101 const value_type dt
= 0.03;
102 const value_type t_end
= 10.0;
104 std::vector
< value_type
> times
;
106 integrate_const( Stepper() , std::make_pair( sys() , jacobi() ) , x
, 0.0 , t_end
,
107 dt
, push_back_time( times
) );
109 BOOST_CHECK_EQUAL( static_cast<int>(times
.size()) , static_cast<int>(std::floor(t_end
/dt
))+1 );
111 for( size_t i
=0 ; i
<times
.size() ; ++i
)
113 //std::cout << i << std::endl;
114 // check if observer was called at times 0,1,2,...
115 BOOST_CHECK_SMALL( times
[i
] - static_cast< value_type
>(i
)*dt
, (i
+1) * 2.0e-16 );
120 template< class Stepper
>
121 struct perform_integrate_adaptive_test
123 void operator()( void )
125 state_type
x( 2 , 1.0 );
126 const value_type dt
= 0.03;
127 const value_type t_end
= 10.0;
129 std::vector
< value_type
> times
;
131 size_t steps
= integrate_adaptive( Stepper() , std::make_pair( sys() , jacobi() ) , x
, 0.0 , t_end
,
132 dt
, push_back_time( times
) );
134 BOOST_CHECK_EQUAL( times
.size() , steps
+1 );
136 BOOST_CHECK_SMALL( times
[0] - 0.0 , 2.0e-16 );
137 BOOST_CHECK_SMALL( times
[times
.size()-1] - t_end
, times
.size() * 3.0e-16 );
142 template< class Stepper
>
143 struct perform_integrate_times_test
145 void operator()( void )
147 state_type
x( 2 , 1.0 );
149 const value_type dt
= 0.03;
151 std::vector
< double > times
;
154 integrate_times( Stepper() , std::make_pair( sys() , jacobi() ) , x
,
155 boost::counting_iterator
<int>(0) , boost::counting_iterator
<int>(10) ,
156 dt
, push_back_time( times
) );
158 BOOST_CHECK_EQUAL( static_cast<int>(times
.size()) , 10 );
160 for( size_t i
=0 ; i
<times
.size() ; ++i
)
161 // check if observer was called at times 0,1,2,...
162 BOOST_CHECK_EQUAL( times
[i
] , static_cast<double>(i
) );
166 template< class Stepper
>
167 struct perform_integrate_n_steps_test
169 void operator()( void )
171 state_type
x( 2 , 1.0 );
173 const value_type dt
= 0.03;
176 std::vector
< double > times
;
179 value_type end_time
= integrate_n_steps( Stepper() , std::make_pair( sys() , jacobi() ) , x
, 0.0 , dt
, n
, push_back_time( times
) );
181 BOOST_CHECK_SMALL( end_time
- n
*dt
, 3.0e-16 );
182 BOOST_CHECK_EQUAL( static_cast<int>(times
.size()) , n
+1 );
184 for( size_t i
=0 ; i
<times
.size() ; ++i
)
185 // check if observer was called at times 0,1,2,...
186 BOOST_CHECK_SMALL( times
[i
] - static_cast< value_type
>(i
)*dt
, (i
+1) * 2.0e-16 );
192 class stepper_methods
: public mpl::vector
<
193 rosenbrock4
< value_type
> ,
194 rosenbrock4_controller
< rosenbrock4
< value_type
> > ,
195 rosenbrock4_dense_output
< rosenbrock4_controller
< rosenbrock4
< value_type
> > >
200 BOOST_AUTO_TEST_SUITE( integrate_test
)
202 BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_const_test_case
, Stepper
, stepper_methods
)
204 perform_integrate_const_test
< Stepper
> tester
;
209 BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_adaptive_test_case
, Stepper
, stepper_methods
)
211 perform_integrate_adaptive_test
< Stepper
> tester
;
216 BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_times_test_case
, Stepper
, stepper_methods
)
218 perform_integrate_times_test
< Stepper
> tester
;
223 class simple_stepper_methods
: public mpl::vector
<
224 rosenbrock4
< value_type
>
227 BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_n_steps_test_case
, Stepper
, simple_stepper_methods
)
229 perform_integrate_n_steps_test
< Stepper
> tester
;
233 BOOST_AUTO_TEST_SUITE_END()