default_operations.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/default_operations.cpp
  4. [begin_description]
  5. This file tests default_operations.
  6. [end_description]
  7. Copyright 2011-2012 Karsten Ahnert
  8. Copyright 2011 Mario Mulansky
  9. Distributed under the Boost Software License, Version 1.0.
  10. (See accompanying file LICENSE_1_0.txt or
  11. copy at http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. #define BOOST_TEST_MODULE odeint_standard_operations
  14. #include <cmath>
  15. #include <complex>
  16. #include <boost/test/unit_test.hpp>
  17. #include <boost/test/floating_point_comparison.hpp>
  18. #include <boost/units/systems/si/length.hpp>
  19. #include <boost/units/systems/si/time.hpp>
  20. #include <boost/units/systems/si/velocity.hpp>
  21. #include <boost/units/systems/si/acceleration.hpp>
  22. #include <boost/units/systems/si/io.hpp>
  23. #include <boost/mpl/list.hpp>
  24. #include <boost/numeric/odeint/algebra/default_operations.hpp>
  25. namespace units = boost::units;
  26. namespace si = boost::units::si;
  27. namespace mpl = boost::mpl;
  28. using boost::numeric::odeint::default_operations;
  29. template< class Value > struct internal_value_type { typedef Value type; };
  30. template< class T > struct internal_value_type< std::complex< T > > { typedef T type; };
  31. template< class T > struct default_eps;
  32. template<> struct default_eps< double > { static double def_eps( void ) { return 1.0e-10; } };
  33. template<> struct default_eps< float > { static float def_eps( void ) { return 1.0e-5; } };
  34. typedef units::unit< units::derived_dimension< units::time_base_dimension , 2 >::type , si::system > time_2;
  35. typedef units::unit< units::derived_dimension< units::time_base_dimension , 3 >::type , si::system > time_3;
  36. typedef units::unit< units::derived_dimension< units::time_base_dimension , 4 >::type , si::system > time_4;
  37. typedef units::unit< units::derived_dimension< units::time_base_dimension , 5 >::type , si::system > time_5;
  38. typedef units::unit< units::derived_dimension< units::time_base_dimension , 6 >::type , si::system > time_6;
  39. typedef units::unit< units::derived_dimension< units::time_base_dimension , 7 >::type , si::system > time_7;
  40. const time_2 second2 = si::second * si::second;
  41. const time_3 second3 = second2 * si::second;
  42. const time_4 second4 = second3 * si::second;
  43. const time_5 second5 = second4 * si::second;
  44. const time_6 second6 = second5 * si::second;
  45. const time_7 second7 = second6 * si::second;
  46. template< class Value , class Compare = typename internal_value_type< Value >::type >
  47. struct double_fixture
  48. {
  49. typedef Value value_type;
  50. typedef Compare compare_type;
  51. double_fixture( const compare_type &eps_ = default_eps< compare_type >::def_eps() )
  52. : 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 )
  53. {}
  54. ~double_fixture( void )
  55. {
  56. using std::abs;
  57. BOOST_CHECK_SMALL( abs( x1 - value_type( 1.0 ) ) , m_eps );
  58. BOOST_CHECK_SMALL( abs( x2 - value_type( 2.0 ) ) , m_eps );
  59. BOOST_CHECK_SMALL( abs( x3 - value_type( 3.0 ) ) , m_eps );
  60. BOOST_CHECK_SMALL( abs( x4 - value_type( 4.0 ) ) , m_eps );
  61. BOOST_CHECK_SMALL( abs( x5 - value_type( 5.0 ) ) , m_eps );
  62. BOOST_CHECK_SMALL( abs( x6 - value_type( 6.0 ) ) , m_eps );
  63. BOOST_CHECK_SMALL( abs( x7 - value_type( 7.0 ) ) , m_eps );
  64. }
  65. const compare_type m_eps;
  66. value_type res;
  67. value_type x1 , x2 , x3 , x4 , x5 , x6 , x7 , x8;
  68. };
  69. template< class Value , class Compare = typename internal_value_type< Value >::type >
  70. struct unit_fixture
  71. {
  72. typedef Value value_type;
  73. typedef Compare compare_type;
  74. typedef units::quantity< si::length , value_type > length_type;
  75. typedef units::quantity< si::time , value_type > time_type;
  76. typedef units::quantity< time_2 , value_type > time_2_type;
  77. typedef units::quantity< time_3 , value_type > time_3_type;
  78. typedef units::quantity< time_4 , value_type > time_4_type;
  79. typedef units::quantity< time_5 , value_type > time_5_type;
  80. typedef units::quantity< time_6 , value_type > time_6_type;
  81. typedef units::quantity< time_7 , value_type > time_7_type;
  82. typedef units::quantity< si::velocity , value_type > velocity_type;
  83. typedef units::quantity< si::acceleration , value_type > acceleration_type;
  84. unit_fixture( const compare_type &eps_ = default_eps< compare_type >::def_eps() )
  85. : m_eps( eps_ )
  86. , res( 0.0 * si::meter )
  87. , x( 1.0 * si::meter )
  88. , d1x( 2.0 * si::meter / si::second )
  89. , d2x( 3.0 * si::meter / si::second / si::second )
  90. {}
  91. ~unit_fixture( void )
  92. {
  93. using std::abs;
  94. BOOST_CHECK_SMALL( abs( x.value() - value_type( 1.0 ) ) , m_eps );
  95. BOOST_CHECK_SMALL( abs( d1x.value() - value_type( 2.0 ) ) , m_eps );
  96. BOOST_CHECK_SMALL( abs( d2x.value() - value_type( 3.0 ) ) , m_eps );
  97. }
  98. compare_type m_eps;
  99. length_type res;
  100. length_type x;
  101. velocity_type d1x;
  102. acceleration_type d2x;
  103. };
  104. typedef mpl::list< float , double , std::complex< double > > test_types;
  105. BOOST_AUTO_TEST_SUITE( check_operations_test )
  106. using std::abs;
  107. BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum2_test , T , test_types )
  108. {
  109. typedef double_fixture< T > fix_type;
  110. fix_type f;
  111. typedef typename default_operations::scale_sum2< T , T > Op;
  112. Op op( 1.25 , 9.81 );
  113. op( f.res , f.x1 , f.x2 );
  114. BOOST_CHECK_SMALL( abs( f.res - T( 20.87 ) ) , f.m_eps );
  115. }
  116. BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum3_test , T , test_types )
  117. {
  118. typedef double_fixture< T > fix_type;
  119. fix_type f;
  120. typedef default_operations::scale_sum3< T , T , T > Op;
  121. Op op( 1.25 , 9.81 , 0.87 );
  122. op( f.res , f.x1 , f.x2 , f.x3 );
  123. BOOST_CHECK_SMALL( abs( f.res - T( 23.48 ) ) , f.m_eps );
  124. }
  125. BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum4_test , T , test_types )
  126. {
  127. typedef double_fixture< T > fix_type;
  128. fix_type f;
  129. typedef default_operations::scale_sum4< T , T , T , T > Op;
  130. Op op( 1.25 , 9.81 , 0.87 , -0.15 );
  131. op( f.res , f.x1 , f.x2 , f.x3 , f.x4 );
  132. BOOST_CHECK_SMALL( abs( f.res - T( 22.88 ) ) , f.m_eps );
  133. }
  134. BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum5_test , T , test_types )
  135. {
  136. typedef double_fixture< T > fix_type;
  137. fix_type f;
  138. typedef default_operations::scale_sum5< T , T , T , T , T > Op;
  139. Op op( 1.25 , 9.81 , 0.87 , -0.15 , -3.3 );
  140. op( f.res , f.x1 , f.x2 , f.x3 , f.x4 , f.x5 );
  141. BOOST_CHECK_SMALL( abs( f.res - T( 6.38 ) ) , f.m_eps );
  142. }
  143. BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum6_test , T , test_types )
  144. {
  145. typedef double_fixture< T > fix_type;
  146. fix_type f;
  147. typedef default_operations::scale_sum6< T , T , T , T , T , T > Op;
  148. Op op( 1.25 , 9.81 , 0.87 , -0.15 , -3.3 , 4.2 );
  149. op( f.res , f.x1 , f.x2 , f.x3 , f.x4 , f.x5 , f.x6 );
  150. BOOST_CHECK_SMALL( abs( f.res - T( 31.58 ) ) , f.m_eps );
  151. }
  152. BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum7_test , T , test_types )
  153. {
  154. typedef double_fixture< T > fix_type;
  155. fix_type f;
  156. typedef default_operations::scale_sum7< T , T , T , T , T , T , T > Op;
  157. Op op( 1.25 , 9.81 , 0.87 , -0.15 , -3.3 , 4.2 , -0.22 );
  158. op( f.res , f.x1 , f.x2 , f.x3 , f.x4 , f.x5 , f.x6 , f.x7 );
  159. BOOST_CHECK_SMALL( abs( f.res - T( 30.04 ) ) , f.m_eps );
  160. }
  161. BOOST_AUTO_TEST_CASE_TEMPLATE( rel_error_test , T , test_types )
  162. {
  163. typedef double_fixture< T > fix_type;
  164. fix_type f;
  165. f.res = -1.1;
  166. typedef default_operations::rel_error< T > Op;
  167. Op op( 0.1 , 0.2 , 0.15 , 0.12 );
  168. op( f.res , -f.x1 , -f.x2 );
  169. BOOST_CHECK_SMALL( abs( f.res - T( 6.17978 ) ) , typename fix_type::compare_type( 1.0e-4 ) );
  170. }
  171. BOOST_AUTO_TEST_CASE_TEMPLATE( maximum_test , T , test_types )
  172. {
  173. typedef double_fixture< T > fix_type;
  174. fix_type f;
  175. typedef default_operations::maximum< typename fix_type::compare_type > Op;
  176. Op op;
  177. f.res = op( f.x1 , f.x2 );
  178. BOOST_CHECK_SMALL( abs( f.res - T( 2.0 ) ) , f.m_eps );
  179. }
  180. BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum2_units_test , T , test_types )
  181. {
  182. typedef unit_fixture< T > fix_type;
  183. typedef typename fix_type::value_type value_type;
  184. typedef typename fix_type::time_type time_type;
  185. fix_type f;
  186. typedef default_operations::scale_sum2< value_type , time_type > Op;
  187. Op op( 1.0 , time_type( 1.0 * si::second ) );
  188. op( f.res , f.x , f.d1x );
  189. BOOST_CHECK_SMALL( abs( f.res.value() - T( 3.0 ) ) , f.m_eps );
  190. }
  191. BOOST_AUTO_TEST_CASE_TEMPLATE( scale_sum3_units_test , T , test_types )
  192. {
  193. typedef unit_fixture< T > fix_type;
  194. typedef typename fix_type::value_type value_type;
  195. typedef typename fix_type::time_type time_type;
  196. typedef typename fix_type::time_2_type time_2_type;
  197. fix_type f;
  198. typedef default_operations::scale_sum3< value_type , time_type , time_2_type > Op;
  199. Op op( 1.0 , time_type( 1.0 * si::second ) , time_2_type( 1.0 * second2 ) );
  200. op( f.res , f.x , f.d1x , f.d2x );
  201. BOOST_CHECK_SMALL( abs( f.res.value() - T( 6.0 ) ) , f.m_eps );
  202. }
  203. BOOST_AUTO_TEST_SUITE_END()