• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  [auto_generated]
3  libs/numeric/odeint/test/rosenbrock4.cpp
4 
5  [begin_description]
6  This file tests the Rosenbrock 4 stepper and its controller and dense output stepper.
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 // disable checked iterator warning for msvc
18 #include <boost/config.hpp>
19 #ifdef BOOST_MSVC
20     #pragma warning(disable:4996)
21 #endif
22 
23 #define BOOST_TEST_MODULE odeint_rosenbrock4
24 
25 #include <utility>
26 #include <iostream>
27 
28 #include <boost/test/unit_test.hpp>
29 
30 #include <boost/numeric/odeint/stepper/rosenbrock4.hpp>
31 #include <boost/numeric/odeint/stepper/rosenbrock4_controller.hpp>
32 #include <boost/numeric/odeint/stepper/rosenbrock4_dense_output.hpp>
33 
34 #include <boost/numeric/ublas/vector.hpp>
35 #include <boost/numeric/ublas/matrix.hpp>
36 
37 using namespace boost::unit_test;
38 using namespace boost::numeric::odeint;
39 
40 typedef double value_type;
41 typedef boost::numeric::ublas::vector< value_type > state_type;
42 typedef boost::numeric::ublas::matrix< value_type > matrix_type;
43 
44 
45 struct sys
46 {
operator ()sys47     void operator()( const state_type &x , state_type &dxdt , const value_type &t ) const
48     {
49         dxdt( 0 ) = x( 0 ) + 2 * x( 1 );
50         dxdt( 1 ) = x( 1 );
51     }
52 };
53 
54 struct jacobi
55 {
operator ()jacobi56     void operator()( const state_type &x , matrix_type &jacobi , const value_type &t , state_type &dfdt ) const
57     {
58         jacobi( 0 , 0 ) = 1;
59         jacobi( 0 , 1 ) = 2;
60         jacobi( 1 , 0 ) = 0;
61         jacobi( 1 , 1 ) = 1;
62         dfdt( 0 ) = 0.0;
63         dfdt( 1 ) = 0.0;
64     }
65 };
66 
67 BOOST_AUTO_TEST_SUITE( rosenbrock4_test )
68 
BOOST_AUTO_TEST_CASE(test_rosenbrock4_stepper)69 BOOST_AUTO_TEST_CASE( test_rosenbrock4_stepper )
70 {
71     typedef rosenbrock4< value_type > stepper_type;
72     stepper_type stepper;
73 
74     typedef stepper_type::state_type state_type;
75     typedef stepper_type::value_type stepper_value_type;
76     typedef stepper_type::deriv_type deriv_type;
77     typedef stepper_type::time_type time_type;
78 
79     state_type x( 2 ) , xerr( 2 );
80     x(0) = 0.0; x(1) = 1.0;
81 
82     stepper.do_step( std::make_pair( sys() , jacobi() ) , x , 0.0 , 0.1 , xerr );
83 
84     stepper.do_step( std::make_pair( sys() , jacobi() ) , x , 0.0 , 0.1 );
85 
86 //    using std::abs;
87 //    value_type eps = 1E-12;
88 //
89 //    // compare with analytic solution of above system
90 //    BOOST_CHECK_SMALL( abs( x(0) - 20.0/81.0 ) , eps );
91 //    BOOST_CHECK_SMALL( abs( x(1) - 10.0/9.0 ) , eps );
92 
93 }
94 
BOOST_AUTO_TEST_CASE(test_rosenbrock4_controller)95 BOOST_AUTO_TEST_CASE( test_rosenbrock4_controller )
96 {
97     typedef rosenbrock4_controller< rosenbrock4< value_type > > stepper_type;
98     stepper_type stepper;
99 
100     typedef stepper_type::state_type state_type;
101     typedef stepper_type::value_type stepper_value_type;
102     typedef stepper_type::deriv_type deriv_type;
103     typedef stepper_type::time_type time_type;
104 
105     state_type x( 2 );
106     x( 0 ) = 0.0 ; x(1) = 1.0;
107 
108     value_type t = 0.0 , dt = 0.01;
109     stepper.try_step( std::make_pair( sys() , jacobi() ) , x , t , dt );
110 }
111 
BOOST_AUTO_TEST_CASE(test_rosenbrock4_dense_output)112 BOOST_AUTO_TEST_CASE( test_rosenbrock4_dense_output )
113 {
114     typedef rosenbrock4_dense_output< rosenbrock4_controller< rosenbrock4< value_type > > > stepper_type;
115     typedef rosenbrock4_controller< rosenbrock4< value_type > > controlled_stepper_type;
116     controlled_stepper_type  c_stepper;
117     stepper_type stepper( c_stepper );
118 
119     typedef stepper_type::state_type state_type;
120     typedef stepper_type::value_type stepper_value_type;
121     typedef stepper_type::deriv_type deriv_type;
122     typedef stepper_type::time_type time_type;
123     state_type x( 2 );
124     x( 0 ) = 0.0 ; x(1) = 1.0;
125     stepper.initialize( x , 0.0 , 0.1 );
126     std::pair< value_type , value_type > tr = stepper.do_step( std::make_pair( sys() , jacobi() ) );
127     stepper.calc_state( 0.5 * ( tr.first + tr.second ) , x );
128 }
129 
130 class rosenbrock4_controller_max_dt_adaptable : public rosenbrock4_controller< rosenbrock4< value_type > >
131 {
132     public:
set_max_dt(value_type max_dt)133         void set_max_dt(value_type max_dt)
134         {
135             m_max_dt = max_dt;
136         }
137 };
138 
BOOST_AUTO_TEST_CASE(test_rosenbrock4_dense_output_ref)139 BOOST_AUTO_TEST_CASE( test_rosenbrock4_dense_output_ref )
140 {
141     typedef rosenbrock4_dense_output< boost::reference_wrapper< rosenbrock4_controller_max_dt_adaptable > > stepper_type;
142     rosenbrock4_controller_max_dt_adaptable  c_stepper;
143     stepper_type stepper( boost::ref( c_stepper ) );
144 
145     typedef stepper_type::state_type state_type;
146     typedef stepper_type::value_type stepper_value_type;
147     typedef stepper_type::deriv_type deriv_type;
148     typedef stepper_type::time_type time_type;
149     state_type x( 2 );
150     x( 0 ) = 0.0 ; x(1) = 1.0;
151     stepper.initialize( x , 0.0 , 0.1 );
152     std::pair< value_type , value_type > tr = stepper.do_step( std::make_pair( sys() , jacobi() ) );
153     stepper.calc_state( 0.5 * ( tr.first + tr.second ) , x );
154 
155     // adapt the maximal step size to a small value (smaller than the step size) and make a step
156     const double max_dt = 1e-8;
157     c_stepper.set_max_dt(max_dt);
158     stepper.do_step( std::make_pair( sys() , jacobi() ) );
159     // check if the step was done with the requested maximal step size
160     BOOST_CHECK_CLOSE(max_dt, stepper.current_time_step(), 1e-14);
161 }
162 
BOOST_AUTO_TEST_CASE(test_rosenbrock4_copy_dense_output)163 BOOST_AUTO_TEST_CASE( test_rosenbrock4_copy_dense_output )
164 {
165     typedef rosenbrock4_controller< rosenbrock4< value_type > > controlled_stepper_type;
166     typedef rosenbrock4_dense_output< controlled_stepper_type > stepper_type;
167 
168     controlled_stepper_type  c_stepper;
169     stepper_type stepper( c_stepper );
170     stepper_type stepper2( stepper );
171 }
172 
173 BOOST_AUTO_TEST_SUITE_END()
174