1 /*
2   [auto_generated]
3   libs/numeric/odeint/test/default_operations.cpp
4 
5   [begin_description]
6   This file tests default_operations.
7   [end_description]
8 
9   Copyright 2011-2012 Karsten Ahnert
10   Copyright 2011 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_standard_operations
19 
20 #include <cmath>
21 #include <complex>
22 
23 #include <boost/test/unit_test.hpp>
24 #include <boost/test/floating_point_comparison.hpp>
25 
26 #include <boost/units/systems/si/length.hpp>
27 #include <boost/units/systems/si/time.hpp>
28 #include <boost/units/systems/si/velocity.hpp>
29 #include <boost/units/systems/si/acceleration.hpp>
30 #include <boost/units/systems/si/io.hpp>
31 
32 #include <boost/mpl/list.hpp>
33 
34 #include <boost/numeric/odeint/algebra/default_operations.hpp>
35 
36 namespace units = boost::units;
37 namespace si = boost::units::si;
38 namespace mpl = boost::mpl;
39 using boost::numeric::odeint::default_operations;
40 
41 
42 template< class Value > struct internal_value_type { typedef Value type; };
43 template< class T > struct internal_value_type< std::complex< T > > { typedef T type; };
44 
45 template< class T > struct default_eps;
def_epsdefault_eps46 template<> struct default_eps< double > { static double def_eps( void ) { return 1.0e-10; } };
def_epsdefault_eps47 template<> struct default_eps< float > { static float def_eps( void ) { return 1.0e-5; } };
48 
49 
50 typedef units::unit< units::derived_dimension< units::time_base_dimension , 2 >::type , si::system > time_2;
51 typedef units::unit< units::derived_dimension< units::time_base_dimension , 3 >::type , si::system > time_3;
52 typedef units::unit< units::derived_dimension< units::time_base_dimension , 4 >::type , si::system > time_4;
53 typedef units::unit< units::derived_dimension< units::time_base_dimension , 5 >::type , si::system > time_5;
54 typedef units::unit< units::derived_dimension< units::time_base_dimension , 6 >::type , si::system > time_6;
55 typedef units::unit< units::derived_dimension< units::time_base_dimension , 7 >::type , si::system > time_7;
56 
57 const time_2 second2 = si::second * si::second;
58 const time_3 second3 = second2 * si::second;
59 const time_4 second4 = second3 * si::second;
60 const time_5 second5 = second4 * si::second;
61 const time_6 second6 = second5 * si::second;
62 const time_7 second7 = second6 * si::second;
63 
64 
65 
66 
67 template< class Value , class Compare = typename internal_value_type< Value >::type >
68 struct double_fixture
69 {
70     typedef Value value_type;
71     typedef Compare compare_type;
72 
double_fixturedouble_fixture73     double_fixture( const compare_type &eps_ = default_eps< compare_type >::def_eps() )
74         : m_eps( eps_ ) , res( 0.0 ) , x1( 1.0 ) , x2( 2.0 ) , x3( 3.0 ) , x4( 4.0 ) , x5( 5.0 ) , x6( 6.0 ) , x7( 7.0 ) , x8( 8.0 )
75     {}
76 
~double_fixturedouble_fixture77     ~double_fixture( void )
78     {
79         using std::abs;
80         BOOST_CHECK_SMALL( abs( x1 - value_type( 1.0 ) ) , m_eps );
81         BOOST_CHECK_SMALL( abs( x2 - value_type( 2.0 ) ) , m_eps );
82         BOOST_CHECK_SMALL( abs( x3 - value_type( 3.0 ) ) , m_eps );
83         BOOST_CHECK_SMALL( abs( x4 - value_type( 4.0 ) ) , m_eps );
84         BOOST_CHECK_SMALL( abs( x5 - value_type( 5.0 ) ) , m_eps );
85         BOOST_CHECK_SMALL( abs( x6 - value_type( 6.0 ) ) , m_eps );
86         BOOST_CHECK_SMALL( abs( x7 - value_type( 7.0 ) ) , m_eps );
87     }
88 
89     const compare_type m_eps;
90     value_type res;
91     value_type x1 , x2 , x3 , x4 , x5 , x6 , x7 , x8;
92 };
93 
94 template< class Value , class Compare = typename internal_value_type< Value >::type >
95 struct unit_fixture
96 {
97     typedef Value value_type;
98     typedef Compare compare_type;
99     typedef units::quantity< si::length , value_type > length_type;
100 
101     typedef units::quantity< si::time , value_type > time_type;
102     typedef units::quantity< time_2 , value_type > time_2_type;
103     typedef units::quantity< time_3 , value_type > time_3_type;
104     typedef units::quantity< time_4 , value_type > time_4_type;
105     typedef units::quantity< time_5 , value_type > time_5_type;
106     typedef units::quantity< time_6 , value_type > time_6_type;
107     typedef units::quantity< time_7 , value_type > time_7_type;
108 
109     typedef units::quantity< si::velocity , value_type > velocity_type;
110     typedef units::quantity< si::acceleration , value_type > acceleration_type;
111 
unit_fixtureunit_fixture112     unit_fixture( const compare_type &eps_ = default_eps< compare_type >::def_eps() )
113         : m_eps( eps_ )
114         , res( 0.0 * si::meter )
115         ,   x( 1.0 * si::meter )
116         , d1x( 2.0 * si::meter / si::second )
117         , d2x( 3.0 * si::meter / si::second / si::second )
118     {}
119 
~unit_fixtureunit_fixture120     ~unit_fixture( void )
121     {
122         using std::abs;
123         BOOST_CHECK_SMALL( abs(   x.value() - value_type( 1.0 ) ) , m_eps );
124         BOOST_CHECK_SMALL( abs( d1x.value() - value_type( 2.0 ) ) , m_eps );
125         BOOST_CHECK_SMALL( abs( d2x.value() - value_type( 3.0 ) ) , m_eps );
126     }
127 
128     compare_type m_eps;
129     length_type res;
130     length_type x;
131     velocity_type d1x;
132     acceleration_type d2x;
133 };
134 
135 
136 typedef mpl::list< float , double , std::complex< double > > test_types;
137 
138 BOOST_AUTO_TEST_SUITE( check_operations_test )
139 
140 using std::abs;
141 
BOOST_AUTO_TEST_CASE_TEMPLATE(scale_sum2_test,T,test_types)142 BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum2_test , T , test_types )
143 {
144     typedef double_fixture< T > fix_type;
145     fix_type f;
146     typedef typename default_operations::scale_sum2< T , T > Op;
147     Op op( 1.25 , 9.81 );
148     op( f.res , f.x1 , f.x2 );
149     BOOST_CHECK_SMALL( abs( f.res  - T( 20.87 ) ) , f.m_eps );
150 }
151 
BOOST_AUTO_TEST_CASE_TEMPLATE(scale_sum3_test,T,test_types)152 BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum3_test , T , test_types )
153 {
154     typedef double_fixture< T > fix_type;
155     fix_type f;
156     typedef default_operations::scale_sum3< T , T , T > Op;
157     Op op( 1.25 , 9.81 , 0.87 );
158     op( f.res , f.x1 , f.x2 , f.x3 );
159     BOOST_CHECK_SMALL( abs( f.res - T( 23.48 ) ) , f.m_eps );
160 }
161 
BOOST_AUTO_TEST_CASE_TEMPLATE(scale_sum4_test,T,test_types)162 BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum4_test , T , test_types  )
163 {
164     typedef double_fixture< T > fix_type;
165     fix_type f;
166     typedef default_operations::scale_sum4< T , T , T , T > Op;
167     Op op( 1.25 , 9.81 , 0.87 , -0.15 );
168     op( f.res , f.x1 , f.x2 , f.x3 , f.x4 );
169     BOOST_CHECK_SMALL( abs( f.res  - T( 22.88 ) ) , f.m_eps );
170 }
171 
BOOST_AUTO_TEST_CASE_TEMPLATE(scale_sum5_test,T,test_types)172 BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum5_test , T , test_types )
173 {
174     typedef double_fixture< T > fix_type;
175     fix_type f;
176     typedef default_operations::scale_sum5< T , T , T , T , T > Op;
177     Op op( 1.25 , 9.81 , 0.87 , -0.15 , -3.3 );
178     op( f.res , f.x1 , f.x2 , f.x3 , f.x4 , f.x5 );
179     BOOST_CHECK_SMALL( abs( f.res  - T( 6.38 ) ) , f.m_eps );
180 }
181 
BOOST_AUTO_TEST_CASE_TEMPLATE(scale_sum6_test,T,test_types)182 BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum6_test , T , test_types )
183 {
184     typedef double_fixture< T > fix_type;
185     fix_type f;
186     typedef default_operations::scale_sum6< T , T , T , T , T , T > Op;
187     Op op( 1.25 , 9.81 , 0.87 , -0.15 , -3.3 , 4.2 );
188     op( f.res , f.x1 , f.x2 , f.x3 , f.x4 , f.x5 , f.x6 );
189     BOOST_CHECK_SMALL( abs( f.res - T( 31.58 ) ) , f.m_eps );
190 }
191 
BOOST_AUTO_TEST_CASE_TEMPLATE(scale_sum7_test,T,test_types)192 BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum7_test , T , test_types )
193 {
194     typedef double_fixture< T > fix_type;
195     fix_type f;
196     typedef default_operations::scale_sum7< T , T , T , T , T , T , T > Op;
197     Op op( 1.25 , 9.81 , 0.87 , -0.15 , -3.3 , 4.2 , -0.22 );
198     op( f.res , f.x1 , f.x2 , f.x3 , f.x4 , f.x5 , f.x6 , f.x7 );
199     BOOST_CHECK_SMALL( abs( f.res - T( 30.04 ) ) , f.m_eps );
200 }
201 
BOOST_AUTO_TEST_CASE_TEMPLATE(rel_error_test,T,test_types)202 BOOST_AUTO_TEST_CASE_TEMPLATE( rel_error_test , T , test_types )
203 {
204     typedef double_fixture< T > fix_type;
205     fix_type f;
206     f.res = -1.1;
207     typedef default_operations::rel_error< T > Op;
208     Op op( 0.1 , 0.2 , 0.15 , 0.12 );
209     op( f.res , -f.x1 , -f.x2 );
210     BOOST_CHECK_SMALL( abs( f.res - T( 6.17978 ) ) , typename fix_type::compare_type( 1.0e-4 ) );
211 }
212 
BOOST_AUTO_TEST_CASE_TEMPLATE(maximum_test,T,test_types)213 BOOST_AUTO_TEST_CASE_TEMPLATE( maximum_test , T , test_types )
214 {
215     typedef double_fixture< T > fix_type;
216     fix_type f;
217     typedef default_operations::maximum< typename fix_type::compare_type > Op;
218     Op op;
219     f.res = op( f.x1 , f.x2 );
220     BOOST_CHECK_SMALL( abs( f.res - T( 2.0 ) ) , f.m_eps );
221 }
222 
223 
224 
225 
226 
BOOST_AUTO_TEST_CASE_TEMPLATE(scale_sum2_units_test,T,test_types)227 BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum2_units_test , T , test_types )
228 {
229     typedef unit_fixture< T > fix_type;
230     typedef typename fix_type::value_type value_type;
231     typedef typename fix_type::time_type time_type;
232 
233     fix_type f;
234     typedef default_operations::scale_sum2< value_type , time_type > Op;
235     Op op( 1.0 , time_type( 1.0 * si::second ) );
236     op( f.res , f.x , f.d1x );
237     BOOST_CHECK_SMALL( abs( f.res.value() - T( 3.0 ) ) , f.m_eps );
238 }
239 
BOOST_AUTO_TEST_CASE_TEMPLATE(scale_sum3_units_test,T,test_types)240 BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum3_units_test , T , test_types )
241 {
242     typedef unit_fixture< T > fix_type;
243     typedef typename fix_type::value_type value_type;
244     typedef typename fix_type::time_type time_type;
245     typedef typename fix_type::time_2_type time_2_type;
246 
247     fix_type f;
248     typedef default_operations::scale_sum3< value_type , time_type , time_2_type > Op;
249     Op op( 1.0 , time_type( 1.0 * si::second ) , time_2_type( 1.0 * second2 ) );
250     op( f.res , f.x , f.d1x , f.d2x );
251     BOOST_CHECK_SMALL( abs( f.res.value() - T( 6.0 ) ) , f.m_eps );
252 }
253 
254 
255 
256 
257 BOOST_AUTO_TEST_SUITE_END()
258