rosenbrock4_mp.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/rosenbrock4.cpp
  4. [begin_description]
  5. This file tests the Rosenbrock 4 stepper and its controller and dense output stepper.
  6. [end_description]
  7. Copyright 2009-2012 Karsten Ahnert
  8. Copyright 2009-2012 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. // disable checked iterator warning for msvc
  14. #include <boost/config.hpp>
  15. #ifdef BOOST_MSVC
  16. #pragma warning(disable:4996)
  17. #endif
  18. #define BOOST_TEST_MODULE odeint_rosenbrock4
  19. #include <utility>
  20. #include <iostream>
  21. #include <boost/test/unit_test.hpp>
  22. #include <boost/multiprecision/cpp_dec_float.hpp>
  23. #include <boost/numeric/odeint/stepper/rosenbrock4.hpp>
  24. #include <boost/numeric/odeint/stepper/rosenbrock4_controller.hpp>
  25. #include <boost/numeric/odeint/stepper/rosenbrock4_dense_output.hpp>
  26. #include <boost/numeric/ublas/vector.hpp>
  27. #include <boost/numeric/ublas/matrix.hpp>
  28. using namespace boost::unit_test;
  29. using namespace boost::numeric::odeint;
  30. typedef boost::multiprecision::cpp_dec_float_50 value_type;
  31. typedef boost::numeric::ublas::vector< value_type > state_type;
  32. typedef boost::numeric::ublas::matrix< value_type > matrix_type;
  33. struct sys
  34. {
  35. void operator()( const state_type &x , state_type &dxdt , const value_type &t ) const
  36. {
  37. dxdt( 0 ) = x( 0 ) + 2 * x( 1 );
  38. dxdt( 1 ) = x( 1 );
  39. }
  40. };
  41. struct jacobi
  42. {
  43. void operator()( const state_type &x , matrix_type &jacobi , const value_type &t , state_type &dfdt ) const
  44. {
  45. jacobi( 0 , 0 ) = 1;
  46. jacobi( 0 , 1 ) = 2;
  47. jacobi( 1 , 0 ) = 0;
  48. jacobi( 1 , 1 ) = 1;
  49. dfdt( 0 ) = 0.0;
  50. dfdt( 1 ) = 0.0;
  51. }
  52. };
  53. BOOST_AUTO_TEST_SUITE( rosenbrock4_test )
  54. BOOST_AUTO_TEST_CASE( test_rosenbrock4_stepper )
  55. {
  56. typedef rosenbrock4< value_type > stepper_type;
  57. stepper_type stepper;
  58. typedef stepper_type::state_type state_type;
  59. typedef stepper_type::value_type stepper_value_type;
  60. typedef stepper_type::deriv_type deriv_type;
  61. typedef stepper_type::time_type time_type;
  62. state_type x( 2 ) , xerr( 2 );
  63. x(0) = 0.0; x(1) = 1.0;
  64. stepper.do_step( std::make_pair( sys() , jacobi() ) , x ,
  65. static_cast<value_type>(0.0) , static_cast<value_type>(0.1) , xerr );
  66. stepper.do_step( std::make_pair( sys() , jacobi() ) , x ,
  67. static_cast<value_type>(0.0) , static_cast<value_type>(0.1) );
  68. // using std::abs;
  69. // value_type eps = 1E-12;
  70. //
  71. // // compare with analytic solution of above system
  72. // BOOST_CHECK_SMALL( abs( x(0) - 20.0/81.0 ) , eps );
  73. // BOOST_CHECK_SMALL( abs( x(1) - 10.0/9.0 ) , eps );
  74. }
  75. BOOST_AUTO_TEST_CASE( test_rosenbrock4_controller )
  76. {
  77. typedef rosenbrock4_controller< rosenbrock4< value_type > > stepper_type;
  78. stepper_type stepper;
  79. typedef stepper_type::state_type state_type;
  80. typedef stepper_type::value_type stepper_value_type;
  81. typedef stepper_type::deriv_type deriv_type;
  82. typedef stepper_type::time_type time_type;
  83. state_type x( 2 );
  84. x( 0 ) = 0.0 ; x(1) = 1.0;
  85. value_type t = 0.0 , dt = 0.01;
  86. stepper.try_step( std::make_pair( sys() , jacobi() ) , x , t , dt );
  87. }
  88. BOOST_AUTO_TEST_CASE( test_rosenbrock4_dense_output )
  89. {
  90. typedef rosenbrock4_dense_output< rosenbrock4_controller< rosenbrock4< value_type > > > stepper_type;
  91. typedef rosenbrock4_controller< rosenbrock4< value_type > > controlled_stepper_type;
  92. controlled_stepper_type c_stepper;
  93. stepper_type stepper( c_stepper );
  94. typedef stepper_type::state_type state_type;
  95. typedef stepper_type::value_type stepper_value_type;
  96. typedef stepper_type::deriv_type deriv_type;
  97. typedef stepper_type::time_type time_type;
  98. state_type x( 2 );
  99. x( 0 ) = 0.0 ; x(1) = 1.0;
  100. stepper.initialize( x , 0.0 , 0.1 );
  101. std::pair< value_type , value_type > tr = stepper.do_step( std::make_pair( sys() , jacobi() ) );
  102. stepper.calc_state( 0.5 * ( tr.first + tr.second ) , x );
  103. }
  104. BOOST_AUTO_TEST_CASE( test_rosenbrock4_copy_dense_output )
  105. {
  106. typedef rosenbrock4_controller< rosenbrock4< value_type > > controlled_stepper_type;
  107. typedef rosenbrock4_dense_output< controlled_stepper_type > stepper_type;
  108. controlled_stepper_type c_stepper;
  109. stepper_type stepper( c_stepper );
  110. stepper_type stepper2( stepper );
  111. }
  112. BOOST_AUTO_TEST_SUITE_END()