3 libs/numeric/odeint/test/times_time_iterator.cpp
6 This file tests the times 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_time_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_time_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;
44 typedef dummy_stepper::time_type time_type;
46 BOOST_AUTO_TEST_SUITE( times_time_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;
55 typedef std::vector< std::pair< state_type , time_type > > result_vector;
57 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_stepper_iterator , Stepper , dummy_steppers )
59 typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type > iterator_type;
60 state_type x = {{ 1.0 }};
61 iterator_type iter1 = iterator_type( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 );
62 iterator_type iter2 = iter1;
63 BOOST_CHECK_EQUAL( &(iter1->first) , &(iter2->first) );
64 BOOST_CHECK_EQUAL( &(iter1->first) , &x );
65 BOOST_CHECK( iter1.same( iter2 ) );
69 BOOST_AUTO_TEST_CASE_TEMPLATE( assignment_stepper_iterator , Stepper , dummy_steppers )
71 std::cout << "assignment" << std::endl;
72 typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type> iterator_type;
73 state_type x1 = {{ 1.0 }} , x2 = {{ 2.0 }};
74 iterator_type iter1 = iterator_type( Stepper() , empty_system() , x1 , times.begin() , times.end() , 0.1 );
75 iterator_type iter2 = iterator_type( Stepper() , empty_system() , x2 , times.begin() , times.end() , 0.2 );
76 BOOST_CHECK_EQUAL( &(iter1->first) , &x1 );
77 BOOST_CHECK_EQUAL( &(iter2->first) , &x2 );
78 BOOST_CHECK( !iter1.same( iter2 ) );
80 BOOST_CHECK_EQUAL( &(iter1->first) , &x1 );
81 BOOST_CHECK_EQUAL( &(iter2->first) , &x1 );
82 BOOST_CHECK( iter1.same( iter2 ) );
87 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_factory , Stepper , dummy_steppers )
89 std::cout << "factory" << std::endl;
92 state_type x = {{ 1.0 }};
95 make_times_time_iterator_begin( stepper , boost::ref( system ) , x , times.begin(), times.end() , 0.1 ) ,
96 make_times_time_iterator_end<time_iterator_type>( stepper , boost::ref( system ) , x ) ,
99 // 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
100 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
103 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range , Stepper , dummy_steppers )
105 std::cout << "range" << std::endl;
108 state_type x = {{ 1.0 }};
110 boost::for_each( make_times_time_range( stepper , boost::ref( system ) , x , times.begin() , times.end() , 0.1 ) ,
113 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
116 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_with_reference_wrapper_factory , Stepper , dummy_steppers )
120 state_type x = {{ 1.0 }};
123 make_times_time_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , times.begin() , times.end() , 0.1 ) ,
124 make_times_time_iterator_end<time_iterator_type>( boost::ref( stepper ) , boost::ref( system ) , x ) ,
127 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
130 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range_with_reference_wrapper , Stepper , dummy_steppers )
134 state_type x = {{ 1.0 }};
136 boost::for_each( make_times_time_range( boost::ref( stepper ) , boost::ref( system ) , x , times.begin() , times.end(), 0.1 ) ,
139 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
144 BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1 , Stepper , dummy_steppers )
146 typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type > stepper_iterator;
147 std::cout << "transitivity1" << std::endl;
148 state_type x = {{ 1.0 }};
149 stepper_iterator first1( Stepper() , empty_system() , x , times.end() , times.end() , 0.1 );
150 stepper_iterator last1( Stepper() , empty_system() , x );
151 stepper_iterator last2( Stepper() , empty_system() , x );
153 BOOST_CHECK( first1 == last1 );
154 BOOST_CHECK( first1 == last2 );
155 BOOST_CHECK( last1 == last2 );
157 first1 = stepper_iterator( Stepper() , empty_system() , x , times.end()-1 , times.end() , 0.1 );
158 last1 = stepper_iterator( Stepper() , empty_system() , x );
159 BOOST_CHECK( first1 != last1 );
160 BOOST_CHECK( ++first1 == last1 );
164 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm , Stepper , dummy_steppers )
166 typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type> stepper_iterator;
167 state_type x = {{ 1.0 }};
169 stepper_iterator first( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 );
170 stepper_iterator last( Stepper() , empty_system() , x );
172 std::copy( first , last , std::back_insert_iterator< result_vector >( res ) );
174 BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
175 BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
176 BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
177 BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
178 BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
180 for( size_t n=0 ; n<4 ; ++n )
181 BOOST_CHECK_CLOSE( res[n].second , times[n] , 1.0e-13 );
183 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 ); // the iterator should not iterate over the end
186 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_negative_time_step , Stepper , dummy_steppers )
188 typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type > stepper_iterator;
189 state_type x = {{ 1.0 }};
191 boost::array<double,4> neg_times = {{ 0.0 , -0.1, -0.2, -0.3 }};
192 stepper_iterator first( Stepper() , empty_system() , x , neg_times.begin() , neg_times.end() , -0.1 );
193 stepper_iterator last( Stepper() , empty_system() , x );
195 std::copy( first , last , std::back_insert_iterator< result_vector >( res ) );
197 BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
198 BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
199 BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
200 BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
201 BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
203 for( size_t n=0 ; n<4 ; ++n )
204 BOOST_CHECK_CLOSE( res[n].second , neg_times[n] , 1.0e-13 );
206 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
210 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory , Stepper , dummy_steppers )
212 state_type x = {{ 1.0 }};
214 std::copy( make_times_time_iterator_begin( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 ) ,
215 make_times_time_iterator_end<time_iterator_type>( Stepper() , empty_system() , x ) ,
216 std::back_insert_iterator< result_vector >( res ) );
218 BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
219 BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
220 BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
221 BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
222 BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
224 for( size_t n=0 ; n<4 ; ++n )
225 BOOST_CHECK_CLOSE( res[n].second , times[n] , 1.0e-13 );
227 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
230 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory , Stepper , dummy_steppers )
232 state_type x = {{ 1.0 }};
234 boost::range::copy( make_times_time_range( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 ) ,
235 std::back_insert_iterator< result_vector >( res ) );
237 BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
238 BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
239 BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
240 BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
241 BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
243 for( size_t n=0 ; n<4 ; ++n )
244 BOOST_CHECK_CLOSE( res[n].second , times[n] , 1.0e-13 );
246 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
250 BOOST_AUTO_TEST_SUITE_END()