1 /*
2 [auto_generated]
3 libs/numeric/odeint/test/multi_array.cpp
4
5 [begin_description]
6 tba.
7 [end_description]
8
9 Copyright 2009-2012 Karsten Ahnert
10 Copyright 2009-2012 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 #include <boost/config.hpp>
18 #ifdef BOOST_MSVC
19 #pragma warning(disable:4996)
20 #endif
21
22 #define BOOST_TEST_MODULE odeint_multi_array
23
24 #include <boost/test/unit_test.hpp>
25
26 #include <boost/numeric/odeint/algebra/multi_array_algebra.hpp>
27 #include <boost/numeric/odeint/util/multi_array_adaption.hpp>
28 #include <boost/numeric/odeint/stepper/runge_kutta4.hpp>
29 #include <boost/numeric/odeint/integrate/integrate_const.hpp>
30 #include <boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp>
31 #include <boost/numeric/odeint/stepper/generation.hpp>
32
33 using namespace boost::unit_test;
34 using namespace boost::numeric::odeint;
35
36 typedef boost::multi_array< double , 1 > vector_type;
37 typedef boost::multi_array< double , 2 > matrix_type;
38 typedef boost::multi_array< double , 3 > tensor_type;
39
40
41 BOOST_AUTO_TEST_SUITE( multi_array_test )
42
BOOST_AUTO_TEST_CASE(test_resizeable)43 BOOST_AUTO_TEST_CASE( test_resizeable )
44 {
45 BOOST_CHECK( is_resizeable< vector_type >::value );
46 BOOST_CHECK( is_resizeable< matrix_type >::value );
47 BOOST_CHECK( is_resizeable< tensor_type >::value );
48 }
49
BOOST_AUTO_TEST_CASE(test_same_size_vector1)50 BOOST_AUTO_TEST_CASE( test_same_size_vector1 )
51 {
52 vector_type v1( boost::extents[12] );
53 vector_type v2( boost::extents[12] );
54 BOOST_CHECK( same_size( v1 , v2 ) );
55 }
56
BOOST_AUTO_TEST_CASE(test_same_size_vector2)57 BOOST_AUTO_TEST_CASE( test_same_size_vector2 )
58 {
59 vector_type v1( boost::extents[12] );
60 vector_type v2( boost::extents[13] );
61 BOOST_CHECK( !same_size( v1 , v2 ) );
62 }
63
BOOST_AUTO_TEST_CASE(test_same_size_vector3)64 BOOST_AUTO_TEST_CASE( test_same_size_vector3 )
65 {
66 vector_type v1( boost::extents[12] );
67 vector_type v2( boost::extents[vector_type::extent_range(-1,11)] );
68 BOOST_CHECK( !same_size( v1 , v2 ) );
69 }
70
BOOST_AUTO_TEST_CASE(test_same_size_vector4)71 BOOST_AUTO_TEST_CASE( test_same_size_vector4 )
72 {
73 vector_type v1( boost::extents[12] );
74 vector_type v2( boost::extents[vector_type::extent_range(-1,10)] );
75 BOOST_CHECK( !same_size( v1 , v2 ) );
76 }
77
BOOST_AUTO_TEST_CASE(test_same_size_vector5)78 BOOST_AUTO_TEST_CASE( test_same_size_vector5 )
79 {
80 vector_type v1( boost::extents[vector_type::extent_range(-1,10)] );
81 vector_type v2( boost::extents[vector_type::extent_range(-1,10)] );
82 BOOST_CHECK( same_size( v1 , v2 ) );
83 }
84
85
86
BOOST_AUTO_TEST_CASE(test_same_size_matrix1)87 BOOST_AUTO_TEST_CASE( test_same_size_matrix1 )
88 {
89 matrix_type m1( boost::extents[12][4] );
90 matrix_type m2( boost::extents[12][4] );
91 BOOST_CHECK( same_size( m1 , m2 ) );
92 }
93
BOOST_AUTO_TEST_CASE(test_same_size_matrix2)94 BOOST_AUTO_TEST_CASE( test_same_size_matrix2 )
95 {
96 matrix_type m1( boost::extents[12][4] );
97 matrix_type m2( boost::extents[12][3] );
98 BOOST_CHECK( !same_size( m1 , m2 ) );
99 }
100
BOOST_AUTO_TEST_CASE(test_same_size_matrix3)101 BOOST_AUTO_TEST_CASE( test_same_size_matrix3 )
102 {
103 matrix_type m1( boost::extents[12][matrix_type::extent_range(-1,2)] );
104 matrix_type m2( boost::extents[12][3] );
105 BOOST_CHECK( !same_size( m1 , m2 ) );
106 }
107
BOOST_AUTO_TEST_CASE(test_same_size_matrix4)108 BOOST_AUTO_TEST_CASE( test_same_size_matrix4 )
109 {
110 matrix_type m1( boost::extents[12][matrix_type::extent_range(-1,1)] );
111 matrix_type m2( boost::extents[12][3] );
112 BOOST_CHECK( !same_size( m1 , m2 ) );
113 }
114
BOOST_AUTO_TEST_CASE(test_same_size_tensor1)115 BOOST_AUTO_TEST_CASE( test_same_size_tensor1 )
116 {
117 tensor_type t1( boost::extents[ tensor_type::extent_range( -2 , 9 ) ]
118 [ tensor_type::extent_range( 5 , 11 ) ]
119 [ tensor_type::extent_range( -1 , 2 ) ] );
120 tensor_type t2( boost::extents[ tensor_type::extent_range( 2 , 13 ) ]
121 [ tensor_type::extent_range( -1 , 5 ) ]
122 [ tensor_type::extent_range( 1 , 4 ) ] );
123 BOOST_CHECK( !same_size( t1 , t2 ) );
124 }
125
BOOST_AUTO_TEST_CASE(test_same_size_tensor2)126 BOOST_AUTO_TEST_CASE( test_same_size_tensor2 )
127 {
128 tensor_type t1( boost::extents[ tensor_type::extent_range( -2 , 9 ) ]
129 [ tensor_type::extent_range( 5 , 11 ) ]
130 [ tensor_type::extent_range( -1 , 2 ) ] );
131 tensor_type t2( boost::extents[ tensor_type::extent_range( -2 , 9 ) ]
132 [ tensor_type::extent_range( 5 , 11 ) ]
133 [ tensor_type::extent_range( -1 , 2 ) ] );
134 BOOST_CHECK( same_size( t1 , t2 ) );
135 }
136
137
138 // Tests for tensor_type
139
BOOST_AUTO_TEST_CASE(test_resize_vector1)140 BOOST_AUTO_TEST_CASE( test_resize_vector1 )
141 {
142 vector_type v1( boost::extents[4] );
143 vector_type v2;
144 resize( v2 , v1 );
145 BOOST_CHECK( same_size( v1 , v2 ) );
146 }
147
BOOST_AUTO_TEST_CASE(test_resize_vector2)148 BOOST_AUTO_TEST_CASE( test_resize_vector2 )
149 {
150 vector_type v1( boost::extents[ vector_type::extent_range( -2 , 9 ) ] );
151 vector_type v2;
152 resize( v2 , v1 );
153 BOOST_CHECK( same_size( v1 , v2 ) );
154 }
155
BOOST_AUTO_TEST_CASE(test_resize_vector3)156 BOOST_AUTO_TEST_CASE( test_resize_vector3 )
157 {
158 vector_type v1( boost::extents[ vector_type::extent_range( -2 , 9 ) ] );
159 vector_type v2( boost::extents[ vector_type::extent_range( 2 , 9 ) ] );
160 BOOST_CHECK( !same_size( v1 , v2 ) );
161 resize( v2 , v1 );
162 BOOST_CHECK( same_size( v1 , v2 ) );
163 }
164
165 struct mult4
166 {
operator ()mult4167 void operator()( double &a , double const &b ) const
168 {
169 a = b * 4.0;
170 }
171 };
172
BOOST_AUTO_TEST_CASE(test_for_each2_vector)173 BOOST_AUTO_TEST_CASE( test_for_each2_vector )
174 {
175 vector_type v1( boost::extents[ vector_type::extent_range( -2 , 9 ) ] );
176 vector_type v2( boost::extents[ vector_type::extent_range( 2 , 13 ) ] );
177 for( int i=-2 ; i<9 ; ++i )
178 v1[i] = i * 2;
179 multi_array_algebra::for_each2( v2 , v1 , mult4() );
180 for( int i=2 ; i<13 ; ++i )
181 BOOST_CHECK_EQUAL( v2[i] , v1[i-4]*4.0 );
182 }
183
184 struct vector_ode
185 {
186 const static size_t n = 128;
operator ()vector_ode187 void operator()( const vector_type &x , vector_type &dxdt , double t ) const
188 {
189 dxdt[-1] = x[n] - 2.0 * x[-1] + x[0];
190 for( size_t i=0 ; i<n ; ++i )
191 dxdt[i] = x[i-1] - 2.0 * x[i] + x[i+1];
192 dxdt[n] = x[-1] - 2.0 * x[n] + x[n-1];
193 }
194 };
195
196
BOOST_AUTO_TEST_CASE(test_rk4_vector)197 BOOST_AUTO_TEST_CASE( test_rk4_vector )
198 {
199 vector_type v1( boost::extents[ vector_type::extent_range( -1 , vector_ode::n+1 ) ] );
200 integrate_const( runge_kutta4< vector_type >() , vector_ode() , v1 , 0.0 , 1.0 , 0.01 );
201 }
202
BOOST_AUTO_TEST_CASE(test_dopri5_vector)203 BOOST_AUTO_TEST_CASE( test_dopri5_vector )
204 {
205 vector_type v1( boost::extents[ vector_type::extent_range( -1 , vector_ode::n+1 ) ] );
206 integrate_const( make_dense_output( 1.0e-6 , 1.0e-6 , runge_kutta_dopri5< vector_type >() ) , vector_ode() , v1 , 0.0 , 1.0 , 0.01 );
207 }
208
209
210
211 BOOST_AUTO_TEST_SUITE_END()
212