• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  [auto_generated]
3  libs/numeric/odeint/test/adaptive_iterator.cpp
4 
5  [begin_description]
6  This file tests the adaptive iterators.
7  [end_description]
8 
9  Copyright 2012-2013 Karsten Ahnert
10  Copyright 2012-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_adaptive_iterator
19 
20 #include <iterator>
21 #include <algorithm>
22 #include <vector>
23 
24 #include <boost/numeric/odeint/config.hpp>
25 #include <boost/array.hpp>
26 #include <boost/range/algorithm/copy.hpp>
27 #include <boost/range/algorithm/for_each.hpp>
28 #include <boost/mpl/vector.hpp>
29 
30 #include <boost/test/unit_test.hpp>
31 #include <boost/test/floating_point_comparison.hpp>
32 
33 #include <boost/numeric/odeint/iterator/adaptive_iterator.hpp>
34 #include "dummy_steppers.hpp"
35 #include "dummy_odes.hpp"
36 #include "dummy_observers.hpp"
37 
38 namespace mpl = boost::mpl;
39 using namespace boost::numeric::odeint;
40 
41 typedef dummy_stepper::state_type state_type;
42 typedef dummy_stepper::value_type value_type;
43 
44 BOOST_AUTO_TEST_SUITE( adaptive_iterator_test )
45 
46 typedef mpl::vector<
47     dummy_controlled_stepper
48     , dummy_dense_output_stepper
49     > dummy_steppers;
50 
51 
BOOST_AUTO_TEST_CASE(copy_controlled_stepper_iterator)52 BOOST_AUTO_TEST_CASE( copy_controlled_stepper_iterator )
53 {
54     typedef adaptive_iterator< dummy_controlled_stepper , empty_system , state_type > iterator_type;
55 
56     state_type x = {{ 1.0 }};
57     iterator_type iter1( dummy_controlled_stepper() , empty_system() , x );
58     iterator_type iter2( iter1 );
59 
60     BOOST_CHECK_EQUAL( &( *iter1 ) , &x );
61     BOOST_CHECK_EQUAL( &( *iter2 ) , &x );
62     BOOST_CHECK_EQUAL( &( *iter1 ) , &( *iter2 ) );
63     BOOST_CHECK( iter1.same( iter2 ) );
64 
65     ++iter1;
66     ++iter2;
67 
68     BOOST_CHECK_EQUAL( &( *iter1 ) , &x );
69     BOOST_CHECK_EQUAL( &( *iter2 ) , &x );
70     BOOST_CHECK_EQUAL( &( *iter1 ) , &( *iter2 ) );
71     BOOST_CHECK( iter1.same( iter2 ) );
72 
73 }
74 
BOOST_AUTO_TEST_CASE(copy_dense_output_stepper_iterator)75 BOOST_AUTO_TEST_CASE( copy_dense_output_stepper_iterator )
76 {
77     typedef adaptive_iterator< dummy_dense_output_stepper , empty_system , state_type > iterator_type;
78 
79     state_type x = {{ 1.0 }};
80     // fix by mario: do not dereference iterators at the end - made iter1 start iterator
81     iterator_type iter1( dummy_dense_output_stepper() , empty_system() , x , 0.0 , 1.0 , 0.1 );
82     iterator_type iter2( iter1 );
83 
84     // fix by mario: iterator dereference now always gives internal state also for dense output, consistent with other iterator implementations
85     // changed: iterators with dense output stepper do not have an internal state now to avoid a copy
86     BOOST_CHECK_NE( & (*iter1) , & (*iter2) );
87     BOOST_CHECK( iter1.same( iter2 ) );
88 
89     ++iter1;
90     ++iter2;
91 
92     BOOST_CHECK_NE( & (*iter1) , & (*iter2) );
93     BOOST_CHECK( iter1.same( iter2 ) );
94 }
95 
BOOST_AUTO_TEST_CASE(copy_dense_output_stepper_iterator_with_reference_wrapper)96 BOOST_AUTO_TEST_CASE( copy_dense_output_stepper_iterator_with_reference_wrapper )
97 {
98     // bad use case, the same stepper is iterated twice
99     typedef adaptive_iterator< boost::reference_wrapper< dummy_dense_output_stepper > , empty_system , state_type > iterator_type;
100 
101     state_type x = {{ 1.0 }};
102     dummy_dense_output_stepper stepper;
103     iterator_type iter1( boost::ref( stepper ) , empty_system() , x , 0.0 , 0.9 , 0.1 );
104     iterator_type iter2( iter1 );
105 
106     BOOST_CHECK_EQUAL( & (*iter1) , & (*iter2) );
107     BOOST_CHECK( iter1.same( iter2 ) );
108 
109     ++iter1;
110     ++iter2;
111 
112     BOOST_CHECK_EQUAL( & (*iter1) , & (*iter2) );
113     BOOST_CHECK( !iter1.same( iter2 ) );         // they point to the same stepper, there the times will be different
114 }
115 
116 
117 
BOOST_AUTO_TEST_CASE(assignment_controlled_stepper_iterator)118 BOOST_AUTO_TEST_CASE( assignment_controlled_stepper_iterator )
119 {
120     typedef adaptive_iterator< dummy_controlled_stepper , empty_system , state_type > iterator_type;
121     state_type x1 = {{ 1.0 }} , x2 = {{ 2.0 }};
122     iterator_type iter1 = iterator_type( dummy_controlled_stepper() , empty_system() , x1 , 0.0 , 1.0 , 0.1 );
123     iterator_type iter2 = iterator_type( dummy_controlled_stepper() , empty_system() , x2 , 0.0 , 1.0 , 0.2 );
124     BOOST_CHECK_EQUAL( &(*iter1) , &x1 );
125     BOOST_CHECK_EQUAL( &(*iter2) , &x2 );
126     // the iterators are indeed the same as this only checks the time values
127     BOOST_CHECK( !iter1.same( iter2 ) );
128     iter2 = iter1;
129     BOOST_CHECK_EQUAL( &(*iter1) , &x1 );
130     BOOST_CHECK_EQUAL( &(*iter2) , &x1 );
131     BOOST_CHECK( iter1.same( iter2 ) );
132 }
133 
134 
135 
BOOST_AUTO_TEST_CASE(assignment_dense_output_stepper_iterator)136 BOOST_AUTO_TEST_CASE( assignment_dense_output_stepper_iterator )
137 {
138     typedef adaptive_iterator< dummy_dense_output_stepper , empty_system , state_type > iterator_type;
139     state_type x1 = {{ 1.0 }};
140     iterator_type iter1 = iterator_type( dummy_dense_output_stepper() , empty_system() , x1 , 0.0 , 1.0 , 0.1 );
141     iterator_type iter2 = iterator_type( dummy_dense_output_stepper() , empty_system() , x1 , 0.0 , 1.0 , 0.2 );
142 
143     BOOST_CHECK_NE( & (*iter1) , & (*iter2) );
144     BOOST_CHECK( !iter1.same( iter2 ) );
145 
146     iter2 = iter1;
147     // fix by mario: iterator dereference now always gives internal state also for dense output, consistent with other iterator implementations
148     // changed: iterators with dense output stepper do not have an internal state now to avoid a copy
149     BOOST_CHECK_NE( & (*iter1) , & (*iter2) );
150     BOOST_CHECK( iter1.same( iter2 ) );
151 }
152 
BOOST_AUTO_TEST_CASE(assignment_dense_output_stepper_iterator_with_reference_wrapper)153 BOOST_AUTO_TEST_CASE( assignment_dense_output_stepper_iterator_with_reference_wrapper )
154 {
155     typedef adaptive_iterator< boost::reference_wrapper< dummy_dense_output_stepper > , empty_system , state_type > iterator_type;
156     state_type x1 = {{ 1.0 }};
157 
158     dummy_dense_output_stepper stepper;
159     iterator_type iter1 = iterator_type( boost::ref( stepper )  , empty_system() , x1 , 0.0 , 1.0 , 0.1 );
160     iterator_type iter2 = iterator_type( boost::ref( stepper ) , empty_system() , x1 , 0.0 , 1.0 , 0.2 );
161 
162     BOOST_CHECK_EQUAL( & (*iter1) , & (*iter2) );
163     BOOST_CHECK( !iter1.same( iter2 ) );
164 
165     iter2 = iter1;
166 
167     BOOST_CHECK_EQUAL( & (*iter1) , & (*iter2) );
168     BOOST_CHECK( iter1.same( iter2 ) );
169 }
170 
171 
172 
BOOST_AUTO_TEST_CASE(controlled_stepper_iterator_factory)173 BOOST_AUTO_TEST_CASE( controlled_stepper_iterator_factory )
174 {
175     dummy_controlled_stepper stepper;
176     empty_system system;
177     state_type x = {{ 1.0 }};
178 
179     std::for_each(
180          make_adaptive_iterator_begin( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
181          make_adaptive_iterator_end( stepper , boost::ref( system ) , x ) ,
182          dummy_observer() );
183 
184     BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
185 }
186 
187 // just test if it compiles
BOOST_AUTO_TEST_CASE(dense_output_stepper_iterator_factory)188 BOOST_AUTO_TEST_CASE( dense_output_stepper_iterator_factory )
189 {
190     dummy_dense_output_stepper stepper;
191     empty_system system;
192     state_type x = {{ 1.0 }};
193 
194     std::for_each(
195          make_adaptive_iterator_begin( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
196          make_adaptive_iterator_end( stepper , boost::ref( system ) , x ) ,
197          dummy_observer() );
198 }
199 
200 
201 
BOOST_AUTO_TEST_CASE(controlled_stepper_range)202 BOOST_AUTO_TEST_CASE( controlled_stepper_range )
203 {
204     dummy_controlled_stepper stepper;
205     empty_system system;
206     state_type x = {{ 1.0 }};
207 
208     boost::for_each( make_adaptive_range( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
209                      dummy_observer() );
210 
211     BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
212 }
213 
214 // just test if it compiles
BOOST_AUTO_TEST_CASE(dense_output_stepper_range)215 BOOST_AUTO_TEST_CASE( dense_output_stepper_range )
216 {
217     dummy_dense_output_stepper stepper;
218     empty_system system;
219     state_type x = {{ 1.0 }};
220 
221     boost::for_each( make_adaptive_range( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
222                      dummy_observer() );
223 }
224 
225 
BOOST_AUTO_TEST_CASE(controlled_stepper_iterator_with_reference_wrapper_factory)226 BOOST_AUTO_TEST_CASE( controlled_stepper_iterator_with_reference_wrapper_factory )
227 {
228     dummy_controlled_stepper stepper;
229     empty_system system;
230     state_type x = {{ 1.0 }};
231 
232     std::for_each(
233         make_adaptive_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
234         make_adaptive_iterator_end( boost::ref( stepper ) , boost::ref( system ) , x ) ,
235         dummy_observer() );
236 
237     BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
238 }
239 
240 // just test if it compiles
BOOST_AUTO_TEST_CASE(dense_output_stepper_iterator_with_reference_wrapper_factory)241 BOOST_AUTO_TEST_CASE( dense_output_stepper_iterator_with_reference_wrapper_factory )
242 {
243     dummy_dense_output_stepper stepper;
244     empty_system system;
245     state_type x = {{ 1.0 }};
246 
247     std::for_each(
248         make_adaptive_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
249         make_adaptive_iterator_end( boost::ref( stepper ) , boost::ref( system ) , x ) ,
250         dummy_observer() );
251 }
252 
BOOST_AUTO_TEST_CASE(controlled_stepper_range_with_reference_wrapper)253 BOOST_AUTO_TEST_CASE( controlled_stepper_range_with_reference_wrapper )
254 {
255     dummy_controlled_stepper stepper;
256     empty_system system;
257     state_type x = {{ 1.0 }};
258 
259     boost::for_each( make_adaptive_range( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
260                      dummy_observer() );
261 
262     BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
263 }
264 
265 // just test if it compiles
BOOST_AUTO_TEST_CASE(dense_output_stepper_range_with_reference_wrapper)266 BOOST_AUTO_TEST_CASE( dense_output_stepper_range_with_reference_wrapper )
267 {
268     dummy_dense_output_stepper stepper;
269     empty_system system;
270     state_type x = {{ 1.0 }};
271 
272     boost::for_each( make_adaptive_range( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
273                      dummy_observer() );
274 }
275 
276 
277 
BOOST_AUTO_TEST_CASE_TEMPLATE(transitivity1,Stepper,dummy_steppers)278 BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1 , Stepper , dummy_steppers )
279 {
280     typedef adaptive_iterator< Stepper , empty_system , state_type > stepper_iterator;
281 
282     state_type x = {{ 1.0 }};
283     stepper_iterator first1( Stepper() , empty_system() , x , 2.5 , 2.0 , 0.1 );
284     stepper_iterator last1( Stepper() , empty_system() , x );
285     stepper_iterator last2( Stepper() , empty_system() , x );
286 
287     BOOST_CHECK( first1 == last1 );
288     BOOST_CHECK( first1 == last2 );
289     BOOST_CHECK( last1 == last2 );
290 }
291 
292 
293 
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_algorithm,Stepper,dummy_steppers)294 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm , Stepper , dummy_steppers )
295 {
296     typedef adaptive_iterator< Stepper , empty_system , state_type > stepper_iterator;
297     state_type x = {{ 1.0 }};
298     std::vector< state_type > res;
299     stepper_iterator first( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 );
300     stepper_iterator last( Stepper() , empty_system() , x );
301 
302     std::copy( first , last , std::back_insert_iterator< std::vector< state_type > >( res ) );
303 
304     BOOST_CHECK_EQUAL( res.size() , size_t( 5 ) );
305     BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-14 );
306     BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-14 );
307     BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-14 );
308     BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-14 );
309     BOOST_CHECK_CLOSE( res[4][0] , 2.0 , 1.0e-14 );
310 }
311 
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_algorithm_with_factory,Stepper,dummy_steppers)312 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory , Stepper , dummy_steppers )
313 {
314     state_type x = {{ 1.0 }};
315     std::vector< state_type > res;
316     std::copy( make_adaptive_iterator_begin( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 ) ,
317                make_adaptive_iterator_end( Stepper() , empty_system() , x ) ,
318                std::back_insert_iterator< std::vector< state_type > >( res ) );
319 
320     BOOST_CHECK_EQUAL( res.size() , size_t( 5 ) );
321     BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-14 );
322     BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-14 );
323     BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-14 );
324     BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-14 );
325     BOOST_CHECK_CLOSE( res[4][0] , 2.0 , 1.0e-14 );
326 }
327 
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_algorithm_with_range_factory,Stepper,dummy_steppers)328 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory , Stepper , dummy_steppers )
329 {
330     state_type x = {{ 1.0 }};
331     std::vector< state_type > res;
332     boost::range::copy( make_adaptive_range( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 ) ,
333                         std::back_insert_iterator< std::vector< state_type > >( res ) );
334 
335     BOOST_CHECK_EQUAL( res.size() , size_t( 5 ) );
336     BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-14 );
337     BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-14 );
338     BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-14 );
339     BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-14 );
340     BOOST_CHECK_CLOSE( res[4][0] , 2.0 , 1.0e-14 );
341 }
342 
343 
344 
345 
346 BOOST_AUTO_TEST_SUITE_END()
347