• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  [auto_generated]
3  libs/numeric/odeint/test/times_iterator.cpp
4 
5  [begin_description]
6  This file tests the n-step iterator.
7  [end_description]
8 
9  Copyright 2009-2013 Karsten Ahnert
10  Copyright 2009-2013 Mario Mulansky
11 
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)
15  */
16 
17 
18 #define BOOST_TEST_MODULE odeint_times_iterator
19 
20 #include <iterator>
21 #include <algorithm>
22 #include <vector>
23 #include <iostream>
24 
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>
30 
31 #include <boost/test/unit_test.hpp>
32 #include <boost/test/floating_point_comparison.hpp>
33 
34 #include <boost/numeric/odeint/iterator/times_iterator.hpp>
35 #include "dummy_steppers.hpp"
36 #include "dummy_odes.hpp"
37 #include "dummy_observers.hpp"
38 
39 namespace mpl = boost::mpl;
40 using namespace boost::numeric::odeint;
41 
42 typedef dummy_stepper::state_type state_type;
43 typedef dummy_stepper::value_type value_type;
44 
45 
46 BOOST_AUTO_TEST_SUITE( times_iterator_test )
47 
48 typedef mpl::vector<
49     dummy_stepper
50     , dummy_dense_output_stepper
51     > dummy_steppers;
52 
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 
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_stepper_iterator,Stepper,dummy_steppers)56 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_stepper_iterator , Stepper , dummy_steppers )
57 {
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 ) );
65 }
66 
67 
BOOST_AUTO_TEST_CASE_TEMPLATE(assignment_stepper_iterator,Stepper,dummy_steppers)68 BOOST_AUTO_TEST_CASE_TEMPLATE( assignment_stepper_iterator , Stepper , dummy_steppers )
69 {
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 ) );
78     iter2 = iter1;
79     BOOST_CHECK_EQUAL( &(*iter1) , &x1 );
80     BOOST_CHECK_EQUAL( &(*iter2) , &x1 );
81     BOOST_CHECK( iter1.same( iter2 ) );
82 }
83 
84 
85 
BOOST_AUTO_TEST_CASE_TEMPLATE(stepper_iterator_factory,Stepper,dummy_steppers)86 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_factory , Stepper , dummy_steppers )
87 {
88     std::cout << "factory" << std::endl;
89     Stepper stepper;
90     empty_system system;
91     state_type x = {{ 1.0 }};
92 
93     std::for_each(
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 ) ,
96         dummy_observer() );
97 
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 );
100 }
101 
BOOST_AUTO_TEST_CASE_TEMPLATE(stepper_range,Stepper,dummy_steppers)102 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range , Stepper , dummy_steppers )
103 {
104     std::cout << "range" << std::endl;
105     Stepper stepper;
106     empty_system system;
107     state_type x = {{ 1.0 }};
108 
109     boost::for_each( make_times_range( stepper , boost::ref( system ) , x , times.begin() , times.end() , 0.1 ) ,
110                      dummy_observer() );
111 
112     BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
113 }
114 
BOOST_AUTO_TEST_CASE_TEMPLATE(stepper_iterator_with_reference_wrapper_factory,Stepper,dummy_steppers)115 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_with_reference_wrapper_factory , Stepper , dummy_steppers )
116 {
117     Stepper stepper;
118     empty_system system;
119     state_type x = {{ 1.0 }};
120 
121     std::for_each(
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 ) ,
124         dummy_observer() );
125 
126     BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
127 }
128 
BOOST_AUTO_TEST_CASE_TEMPLATE(stepper_range_with_reference_wrapper,Stepper,dummy_steppers)129 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range_with_reference_wrapper , Stepper , dummy_steppers )
130 {
131     Stepper stepper;
132     empty_system system;
133     state_type x = {{ 1.0 }};
134 
135     boost::for_each( make_times_range( boost::ref( stepper ) , boost::ref( system ) , x , times.begin() , times.end(), 0.1 ) ,
136                      dummy_observer() );
137 
138     BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
139 }
140 
141 
142 
BOOST_AUTO_TEST_CASE_TEMPLATE(transitivity1,Stepper,dummy_steppers)143 BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1 , Stepper , dummy_steppers )
144 {
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 );
151 
152     BOOST_CHECK( first1 == last1 );
153     BOOST_CHECK( first1 == last2 );
154     BOOST_CHECK( last1 == last2 );
155 
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 );
160 }
161 
162 
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_algorithm,Stepper,dummy_steppers)163 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm , Stepper , dummy_steppers )
164 {
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 );
170 
171     std::copy( first , last , std::back_insert_iterator< std::vector< state_type > >( res ) );
172 
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 );
178 
179     BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );     // the iterator should not iterate over the end
180 }
181 
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_algorithm_negative_time_step,Stepper,dummy_steppers)182 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_negative_time_step , Stepper , dummy_steppers )
183 {
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 );
190 
191     std::copy( first , last , std::back_insert_iterator< std::vector< state_type > >( res ) );
192 
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 );
198 
199     BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
200 }
201 
202 
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_algorithm_with_factory,Stepper,dummy_steppers)203 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory , Stepper , dummy_steppers )
204 {
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 ) );
210 
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 );
216 
217     BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
218 }
219 
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_algorithm_with_range_factory,Stepper,dummy_steppers)220 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory , Stepper , dummy_steppers )
221 {
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 ) );
226 
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 );
232 
233     BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
234 }
235 
236 
237 BOOST_AUTO_TEST_SUITE_END()
238