3 libs/numeric/odeint/test/times_iterator.cpp
6 This file tests the n-step iterator.
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_times_iterator
25 #include <boost/numeric/odeint/config.hpp>
26 #include <boost/array.hpp>
27 #include <boost/range/algorithm/for_each.hpp>
28 #include <boost/range/algorithm/copy.hpp>
29 #include <boost/mpl/vector.hpp>
31 #include <boost/test/unit_test.hpp>
32 #include <boost/test/floating_point_comparison.hpp>
34 #include <boost/numeric/odeint/iterator/times_iterator.hpp>
35 #include "dummy_steppers.hpp"
36 #include "dummy_odes.hpp"
37 #include "dummy_observers.hpp"
39 namespace mpl
= boost::mpl
;
40 using namespace boost::numeric::odeint
;
42 typedef dummy_stepper::state_type state_type
;
43 typedef dummy_stepper::value_type value_type
;
46 BOOST_AUTO_TEST_SUITE( times_iterator_test
)
50 , dummy_dense_output_stepper
53 boost::array
<double,4> times
= {{ 0.0 , 0.1, 0.2, 0.3 }};
54 typedef boost::array
<double,4>::iterator time_iterator_type
;
56 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_stepper_iterator
, Stepper
, dummy_steppers
)
58 typedef times_iterator
< Stepper
, empty_system
, state_type
, time_iterator_type
> iterator_type
;
59 state_type x
= {{ 1.0 }};
60 iterator_type iter1
= iterator_type( Stepper() , empty_system() , x
, times
.begin() , times
.end() , 0.1 );
61 iterator_type iter2
= iter1
;
62 BOOST_CHECK_EQUAL( &(*iter1
) , &(*iter2
) );
63 BOOST_CHECK_EQUAL( &(*iter1
) , &x
);
64 BOOST_CHECK( iter1
.same( iter2
) );
68 BOOST_AUTO_TEST_CASE_TEMPLATE( assignment_stepper_iterator
, Stepper
, dummy_steppers
)
70 std::cout
<< "assignment" << std::endl
;
71 typedef times_iterator
< Stepper
, empty_system
, state_type
, time_iterator_type
> iterator_type
;
72 state_type x1
= {{ 1.0 }} , x2
= {{ 2.0 }};
73 iterator_type iter1
= iterator_type( Stepper() , empty_system() , x1
, times
.begin() , times
.end() , 0.1 );
74 iterator_type iter2
= iterator_type( Stepper() , empty_system() , x2
, times
.begin() , times
.end() , 0.2 );
75 BOOST_CHECK_EQUAL( &(*iter1
) , &x1
);
76 BOOST_CHECK_EQUAL( &(*iter2
) , &x2
);
77 BOOST_CHECK( !iter1
.same( iter2
) );
79 BOOST_CHECK_EQUAL( &(*iter1
) , &x1
);
80 BOOST_CHECK_EQUAL( &(*iter2
) , &x1
);
81 BOOST_CHECK( iter1
.same( iter2
) );
86 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_factory
, Stepper
, dummy_steppers
)
88 std::cout
<< "factory" << std::endl
;
91 state_type x
= {{ 1.0 }};
94 make_times_iterator_begin( stepper
, boost::ref( system
) , x
, times
.begin(), times
.end() , 0.1 ) ,
95 make_times_iterator_end
<time_iterator_type
>( stepper
, boost::ref( system
) , x
) ,
98 // dummy_steppers just add 0.25 at each step, the above for_each leads to 3 do_step calls so x should be 1.75
99 BOOST_CHECK_CLOSE( x
[0] , 1.75 , 1.0e-13 );
102 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range
, Stepper
, dummy_steppers
)
104 std::cout
<< "range" << std::endl
;
107 state_type x
= {{ 1.0 }};
109 boost::for_each( make_times_range( stepper
, boost::ref( system
) , x
, times
.begin() , times
.end() , 0.1 ) ,
112 BOOST_CHECK_CLOSE( x
[0] , 1.75 , 1.0e-13 );
115 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_with_reference_wrapper_factory
, Stepper
, dummy_steppers
)
119 state_type x
= {{ 1.0 }};
122 make_times_iterator_begin( boost::ref( stepper
) , boost::ref( system
) , x
, times
.begin() , times
.end() , 0.1 ) ,
123 make_times_iterator_end
<time_iterator_type
>( boost::ref( stepper
) , boost::ref( system
) , x
) ,
126 BOOST_CHECK_CLOSE( x
[0] , 1.75 , 1.0e-13 );
129 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range_with_reference_wrapper
, Stepper
, dummy_steppers
)
133 state_type x
= {{ 1.0 }};
135 boost::for_each( make_times_range( boost::ref( stepper
) , boost::ref( system
) , x
, times
.begin() , times
.end(), 0.1 ) ,
138 BOOST_CHECK_CLOSE( x
[0] , 1.75 , 1.0e-13 );
143 BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1
, Stepper
, dummy_steppers
)
145 typedef times_iterator
< Stepper
, empty_system
, state_type
, time_iterator_type
> stepper_iterator
;
146 std::cout
<< "transitivity1" << std::endl
;
147 state_type x
= {{ 1.0 }};
148 stepper_iterator
first1( Stepper() , empty_system() , x
, times
.end() , times
.end() , 0.1 );
149 stepper_iterator
last1( Stepper() , empty_system() , x
);
150 stepper_iterator
last2( Stepper() , empty_system() , x
);
152 BOOST_CHECK( first1
== last1
);
153 BOOST_CHECK( first1
== last2
);
154 BOOST_CHECK( last1
== last2
);
156 first1
= stepper_iterator( Stepper() , empty_system() , x
, times
.end()-1 , times
.end() , 0.1 );
157 last1
= stepper_iterator( Stepper() , empty_system() , x
);
158 BOOST_CHECK( first1
!= last1
);
159 BOOST_CHECK( ++first1
== last1
);
163 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm
, Stepper
, dummy_steppers
)
165 typedef times_iterator
< Stepper
, empty_system
, state_type
, time_iterator_type
> stepper_iterator
;
166 state_type x
= {{ 1.0 }};
167 std::vector
< state_type
> res
;
168 stepper_iterator
first( Stepper() , empty_system() , x
, times
.begin() , times
.end() , 0.1 );
169 stepper_iterator
last( Stepper() , empty_system() , x
);
171 std::copy( first
, last
, std::back_insert_iterator
< std::vector
< state_type
> >( res
) );
173 BOOST_CHECK_EQUAL( res
.size() , size_t( 4 ) );
174 BOOST_CHECK_CLOSE( res
[0][0] , 1.0 , 1.0e-13 );
175 BOOST_CHECK_CLOSE( res
[1][0] , 1.25 , 1.0e-13 );
176 BOOST_CHECK_CLOSE( res
[2][0] , 1.5 , 1.0e-13 );
177 BOOST_CHECK_CLOSE( res
[3][0] , 1.75 , 1.0e-13 );
179 BOOST_CHECK_CLOSE( x
[0] , 1.75 , 1.0e-13 ); // the iterator should not iterate over the end
182 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_negative_time_step
, Stepper
, dummy_steppers
)
184 typedef times_iterator
< Stepper
, empty_system
, state_type
, time_iterator_type
> stepper_iterator
;
185 state_type x
= {{ 1.0 }};
186 std::vector
< state_type
> res
;
187 boost::array
<double,4> neg_times
= {{ 0.0 , -0.1, -0.2, -0.3 }};
188 stepper_iterator
first( Stepper() , empty_system() , x
, neg_times
.begin() , neg_times
.end() , -0.1 );
189 stepper_iterator
last( Stepper() , empty_system() , x
);
191 std::copy( first
, last
, std::back_insert_iterator
< std::vector
< state_type
> >( res
) );
193 BOOST_CHECK_EQUAL( res
.size() , size_t( 4 ) );
194 BOOST_CHECK_CLOSE( res
[0][0] , 1.0 , 1.0e-13 );
195 BOOST_CHECK_CLOSE( res
[1][0] , 1.25 , 1.0e-13 );
196 BOOST_CHECK_CLOSE( res
[2][0] , 1.5 , 1.0e-13 );
197 BOOST_CHECK_CLOSE( res
[3][0] , 1.75 , 1.0e-13 );
199 BOOST_CHECK_CLOSE( x
[0] , 1.75 , 1.0e-13 );
203 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory
, Stepper
, dummy_steppers
)
205 state_type x
= {{ 1.0 }};
206 std::vector
< state_type
> res
;
207 std::copy( make_times_iterator_begin( Stepper() , empty_system() , x
, times
.begin() , times
.end() , 0.1 ) ,
208 make_times_iterator_end
<time_iterator_type
>( Stepper() , empty_system() , x
) ,
209 std::back_insert_iterator
< std::vector
< state_type
> >( res
) );
211 BOOST_CHECK_EQUAL( res
.size() , size_t( 4 ) );
212 BOOST_CHECK_CLOSE( res
[0][0] , 1.0 , 1.0e-13 );
213 BOOST_CHECK_CLOSE( res
[1][0] , 1.25 , 1.0e-13 );
214 BOOST_CHECK_CLOSE( res
[2][0] , 1.5 , 1.0e-13 );
215 BOOST_CHECK_CLOSE( res
[3][0] , 1.75 , 1.0e-13 );
217 BOOST_CHECK_CLOSE( x
[0] , 1.75 , 1.0e-13 );
220 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory
, Stepper
, dummy_steppers
)
222 state_type x
= {{ 1.0 }};
223 std::vector
< state_type
> res
;
224 boost::range::copy( make_times_range( Stepper() , empty_system() , x
, times
.begin() , times
.end() , 0.1 ) ,
225 std::back_insert_iterator
< std::vector
< state_type
> >( res
) );
227 BOOST_CHECK_EQUAL( res
.size() , size_t( 4 ) );
228 BOOST_CHECK_CLOSE( res
[0][0] , 1.0 , 1.0e-13 );
229 BOOST_CHECK_CLOSE( res
[1][0] , 1.25 , 1.0e-13 );
230 BOOST_CHECK_CLOSE( res
[2][0] , 1.5 , 1.0e-13 );
231 BOOST_CHECK_CLOSE( res
[3][0] , 1.75 , 1.0e-13 );
233 BOOST_CHECK_CLOSE( x
[0] , 1.75 , 1.0e-13 );
237 BOOST_AUTO_TEST_SUITE_END()