1 /*
2 [auto_generated]
3 libs/numeric/odeint/test/adaptive_time_iterator.cpp
4
5 [begin_description]
6 This file tests the adaptive time iterator.
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_time_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_time_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
42
43 typedef dummy_stepper::state_type state_type;
44 typedef dummy_stepper::value_type value_type;
45 typedef dummy_stepper::time_type time_type;
46 typedef std::vector< std::pair< state_type , time_type > > result_vector;
47
48 BOOST_AUTO_TEST_SUITE( adaptive_time_iterator_test )
49
50 typedef mpl::vector<
51 dummy_controlled_stepper
52 , dummy_dense_output_stepper
53 > dummy_steppers;
54
55
BOOST_AUTO_TEST_CASE(copy_stepper_iterator)56 BOOST_AUTO_TEST_CASE( copy_stepper_iterator )
57 {
58 typedef adaptive_time_iterator< dummy_controlled_stepper , empty_system , state_type > iterator_type;
59 state_type x = {{ 1.0 }};
60 iterator_type iter1 = iterator_type( dummy_controlled_stepper() , empty_system() , x , 0.0 , 1.0 , 0.1 );
61 iterator_type iter2 = iter1;
62 BOOST_CHECK_EQUAL( &( iter1->first ) , &( iter2->first ) );
63 BOOST_CHECK_EQUAL( &( iter1->first ) , &x );
64 BOOST_CHECK( iter1.same( iter2 ) );
65 }
66
BOOST_AUTO_TEST_CASE(copy_dense_output_stepper_iterator)67 BOOST_AUTO_TEST_CASE( copy_dense_output_stepper_iterator )
68 {
69 typedef adaptive_time_iterator< dummy_dense_output_stepper , empty_system , state_type > iterator_type;
70 state_type x = {{ 1.0 }};
71 iterator_type iter1 = iterator_type( dummy_dense_output_stepper() , empty_system() , x , 0.0 , 1.0 , 0.1 );
72 iterator_type iter2 = iter1;
73 BOOST_CHECK_NE( &( iter1->first ) , &( iter2->first ) );
74 BOOST_CHECK( iter1.same( iter2 ) );
75 }
76
BOOST_AUTO_TEST_CASE(copy_dense_output_stepper_iterator_with_reference_wrapper)77 BOOST_AUTO_TEST_CASE( copy_dense_output_stepper_iterator_with_reference_wrapper )
78 {
79 typedef adaptive_time_iterator< boost::reference_wrapper< dummy_dense_output_stepper > , empty_system , state_type > iterator_type;
80 state_type x = {{ 1.0 }};
81 dummy_dense_output_stepper stepper;
82 iterator_type iter1 = iterator_type( boost::ref( stepper ) , empty_system() , x , 0.0 , 1.0 , 0.1 );
83 iterator_type iter2 = iter1;
84 BOOST_CHECK_EQUAL( &( iter1->first ) , &( iter2->first ) );
85 BOOST_CHECK( iter1.same( iter2 ) );
86 }
87
88
BOOST_AUTO_TEST_CASE(assignment_stepper_iterator)89 BOOST_AUTO_TEST_CASE( assignment_stepper_iterator )
90 {
91 typedef adaptive_time_iterator< dummy_controlled_stepper , empty_system , state_type > iterator_type;
92 state_type x1 = {{ 1.0 }} , x2 = {{ 2.0 }};
93 iterator_type iter1 = iterator_type( dummy_controlled_stepper() , empty_system() , x1 , 0.0 , 1.0 , 0.1 );
94 iterator_type iter2 = iterator_type( dummy_controlled_stepper() , empty_system() , x2 , 0.0 , 1.0 , 0.2 );
95 BOOST_CHECK_EQUAL( &( iter1->first ) , &x1 );
96 BOOST_CHECK_EQUAL( &( iter2->first ) , &x2 );
97 BOOST_CHECK( !iter1.same( iter2 ) );
98 iter2 = iter1;
99 BOOST_CHECK_EQUAL( &( iter1->first ) , &x1 );
100 BOOST_CHECK_EQUAL( &( iter2->first ) , &x1 );
101 BOOST_CHECK( iter1.same( iter2 ) );
102 }
103
BOOST_AUTO_TEST_CASE(assignment_dense_output_stepper_iterator)104 BOOST_AUTO_TEST_CASE( assignment_dense_output_stepper_iterator )
105 {
106 typedef adaptive_time_iterator< dummy_dense_output_stepper , empty_system , state_type > iterator_type;
107 state_type x1 = {{ 1.0 }} , x2 = {{ 2.0 }};
108 iterator_type iter1 = iterator_type( dummy_dense_output_stepper() , empty_system() , x1 , 0.0 , 1.0 , 0.1 );
109 iterator_type iter2 = iterator_type( dummy_dense_output_stepper() , empty_system() , x2 , 0.0 , 1.0 , 0.2 );
110 BOOST_CHECK_NE( &( iter1->first ) , &x1 );
111 BOOST_CHECK_NE( &( iter2->first ) , &x2 );
112 BOOST_CHECK( !iter1.same( iter2 ) );
113 iter2 = iter1;
114 BOOST_CHECK_NE( &( iter1->first ) , &x1 );
115 BOOST_CHECK_NE( &( iter2->first ) , &x1 );
116 BOOST_CHECK( iter1.same( iter2 ) );
117 BOOST_CHECK_EQUAL( (iter1->first)[0] , (iter1->first)[0] );
118 }
119
BOOST_AUTO_TEST_CASE(assignment_dense_output_stepper_iterator_with_reference_wrapper)120 BOOST_AUTO_TEST_CASE( assignment_dense_output_stepper_iterator_with_reference_wrapper )
121 {
122 typedef adaptive_time_iterator< boost::reference_wrapper< dummy_dense_output_stepper > , empty_system , state_type > iterator_type;
123 state_type x1 = {{ 1.0 }} , x2 = {{ 2.0 }};
124 dummy_dense_output_stepper stepper;
125 iterator_type iter1 = iterator_type( boost::ref( stepper ) , empty_system() , x1 , 0.0 , 1.0 , 0.1 );
126 iterator_type iter2 = iterator_type( boost::ref( stepper ) , empty_system() , x2 , 0.0 , 1.0 , 0.2 );
127
128 BOOST_CHECK_NE( &( iter1->first ) , &x1 );
129 BOOST_CHECK_NE( &( iter2->first ) , &x2 );
130 // same stepper instance -> same internal state
131 BOOST_CHECK_EQUAL( &( iter1->first ) , &( iter2->first ) );
132 BOOST_CHECK( !iter1.same( iter2 ) );
133 iter2 = iter1;
134 BOOST_CHECK_NE( &( iter1->first ) , &x1 );
135 BOOST_CHECK_NE( &( iter2->first ) , &x1 );
136 BOOST_CHECK( iter1.same( iter2 ) );
137 BOOST_CHECK_EQUAL( &( iter1->first ) , &( iter2->first ) );
138 }
139
140
BOOST_AUTO_TEST_CASE(stepper_iterator_factory)141 BOOST_AUTO_TEST_CASE( stepper_iterator_factory )
142 {
143 dummy_controlled_stepper stepper;
144 empty_system system;
145 state_type x = {{ 1.0 }};
146
147 std::for_each(
148 make_adaptive_time_iterator_begin( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
149 make_adaptive_time_iterator_end( stepper , boost::ref( system ) , x ) ,
150 dummy_observer() );
151
152 BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
153 }
154
155 // just test if it compiles
BOOST_AUTO_TEST_CASE(dense_output_stepper_iterator_factory)156 BOOST_AUTO_TEST_CASE( dense_output_stepper_iterator_factory )
157 {
158 dummy_dense_output_stepper stepper;
159 empty_system system;
160 state_type x = {{ 1.0 }};
161
162 std::for_each(
163 make_adaptive_time_iterator_begin( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
164 make_adaptive_time_iterator_end( stepper , boost::ref( system ) , x ) ,
165 dummy_observer() );
166 }
167
168
BOOST_AUTO_TEST_CASE(stepper_range)169 BOOST_AUTO_TEST_CASE( stepper_range )
170 {
171 dummy_controlled_stepper stepper;
172 empty_system system;
173 state_type x = {{ 1.0 }};
174
175 boost::for_each( make_adaptive_time_range( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
176 dummy_observer() );
177
178 BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
179 }
180
181 // just test if it compiles
BOOST_AUTO_TEST_CASE(dense_output_stepper_range)182 BOOST_AUTO_TEST_CASE( dense_output_stepper_range )
183 {
184 dummy_dense_output_stepper stepper;
185 empty_system system;
186 state_type x = {{ 1.0 }};
187
188 boost::for_each( make_adaptive_time_range( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
189 dummy_observer() );
190 }
191
192
BOOST_AUTO_TEST_CASE(stepper_iterator_with_reference_wrapper_factory)193 BOOST_AUTO_TEST_CASE( stepper_iterator_with_reference_wrapper_factory )
194 {
195 dummy_controlled_stepper stepper;
196 empty_system system;
197 state_type x = {{ 1.0 }};
198
199 std::for_each(
200 make_adaptive_time_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
201 make_adaptive_time_iterator_end( boost::ref( stepper ) , boost::ref( system ) , x ) ,
202 dummy_observer() );
203
204 BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
205 }
206
207 // just test if it compiles
BOOST_AUTO_TEST_CASE(dense_output_stepper_iterator_with_reference_wrapper_factory)208 BOOST_AUTO_TEST_CASE( dense_output_stepper_iterator_with_reference_wrapper_factory )
209 {
210 dummy_dense_output_stepper stepper;
211 empty_system system;
212 state_type x = {{ 1.0 }};
213
214 std::for_each(
215 make_adaptive_time_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
216 make_adaptive_time_iterator_end( boost::ref( stepper ) , boost::ref( system ) , x ) ,
217 dummy_observer() );
218 }
219
220
221
BOOST_AUTO_TEST_CASE(stepper_range_with_reference_wrapper)222 BOOST_AUTO_TEST_CASE( stepper_range_with_reference_wrapper )
223 {
224 dummy_controlled_stepper stepper;
225 empty_system system;
226 state_type x = {{ 1.0 }};
227
228 boost::for_each( make_adaptive_time_range( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
229 dummy_observer() );
230
231 BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
232 }
233
234 // just test if it compiles
BOOST_AUTO_TEST_CASE(dense_output_stepper_range_with_reference_wrapper)235 BOOST_AUTO_TEST_CASE( dense_output_stepper_range_with_reference_wrapper )
236 {
237 dummy_dense_output_stepper stepper;
238 empty_system system;
239 state_type x = {{ 1.0 }};
240
241 boost::for_each( make_adaptive_time_range( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
242 dummy_observer() );
243 }
244
245
246
247
248
BOOST_AUTO_TEST_CASE_TEMPLATE(transitivity1,Stepper,dummy_steppers)249 BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1 , Stepper , dummy_steppers )
250 {
251 typedef adaptive_time_iterator< Stepper , empty_system , state_type > stepper_iterator;
252
253 state_type x = {{ 1.0 }};
254 stepper_iterator first1( Stepper() , empty_system() , x , 1.5 , 1.0 , 0.1 );
255 stepper_iterator last1( Stepper() , empty_system() , x );
256 stepper_iterator last2( Stepper() , empty_system() , x );
257
258 BOOST_CHECK( first1 == last1 );
259 BOOST_CHECK( first1 == last2 );
260 BOOST_CHECK( last1 == last2 );
261 }
262
263
264
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_algorithm,Stepper,dummy_steppers)265 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm , Stepper , dummy_steppers )
266 {
267 typedef adaptive_time_iterator< Stepper , empty_system , state_type > stepper_iterator;
268 state_type x = {{ 1.0 }};
269 result_vector res;
270 stepper_iterator first( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 );
271 stepper_iterator last( Stepper() , empty_system() , x );
272
273 std::copy( first , last , std::back_insert_iterator< result_vector >( res ) );
274
275 BOOST_CHECK_EQUAL( res.size() , size_t( 5 ) );
276 BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
277 BOOST_CHECK_CLOSE( res[0].second , 0.0 , 1.0e-13 );
278 BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
279 BOOST_CHECK_CLOSE( res[1].second , 0.1 , 1.0e-13 );
280 BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
281 BOOST_CHECK_CLOSE( res[2].second , 0.2 , 1.0e-13 );
282 BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
283 BOOST_CHECK_CLOSE( res[3].second , 0.3 , 1.0e-13 );
284 BOOST_CHECK_CLOSE( res[4].first[0] , 2.0 , 1.0e-13 );
285 BOOST_CHECK_CLOSE( res[4].second , 0.35 , 1.0e-13 );
286 }
287
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_algorithm_with_factory,Stepper,dummy_steppers)288 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory , Stepper , dummy_steppers )
289 {
290 state_type x = {{ 1.0 }};
291 result_vector res;
292 std::copy( make_adaptive_time_iterator_begin( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 ) ,
293 make_adaptive_time_iterator_end( Stepper() , empty_system() , x ) ,
294 std::back_insert_iterator< result_vector >( res ) );
295
296 BOOST_CHECK_EQUAL( res.size() , size_t( 5 ) );
297 BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
298 BOOST_CHECK_CLOSE( res[0].second , 0.0 , 1.0e-13 );
299 BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
300 BOOST_CHECK_CLOSE( res[1].second , 0.1 , 1.0e-13 );
301 BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
302 BOOST_CHECK_CLOSE( res[2].second , 0.2 , 1.0e-13 );
303 BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
304 BOOST_CHECK_CLOSE( res[3].second , 0.3 , 1.0e-13 );
305 BOOST_CHECK_CLOSE( res[4].first[0] , 2.0 , 1.0e-13 );
306 BOOST_CHECK_CLOSE( res[4].second , 0.35 , 1.0e-13 );
307 }
308
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_algorithm_with_range_factory,Stepper,dummy_steppers)309 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory , Stepper , dummy_steppers )
310 {
311 state_type x = {{ 1.0 }};
312 result_vector res;
313 boost::range::copy( make_adaptive_time_range( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 ) ,
314 std::back_insert_iterator< result_vector >( res ) );
315
316 BOOST_CHECK_EQUAL( res.size() , size_t( 5 ) );
317 BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
318 BOOST_CHECK_CLOSE( res[0].second , 0.0 , 1.0e-13 );
319 BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
320 BOOST_CHECK_CLOSE( res[1].second , 0.1 , 1.0e-13 );
321 BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
322 BOOST_CHECK_CLOSE( res[2].second , 0.2 , 1.0e-13 );
323 BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
324 BOOST_CHECK_CLOSE( res[3].second , 0.3 , 1.0e-13 );
325 BOOST_CHECK_CLOSE( res[4].first[0] , 2.0 , 1.0e-13 );
326 BOOST_CHECK_CLOSE( res[4].second , 0.35 , 1.0e-13 );
327 }
328
329
330
331
332 BOOST_AUTO_TEST_SUITE_END()
333