1 /*
2  [auto_generated]
3  libs/numeric/odeint/test/trivial_state.cpp
4 
5  [begin_description]
6  This file tests if the steppers can integrate the trivial state consisting of a single double.
7  [end_description]
8 
9  Copyright 2012-2013 Mario Mulansky
10  Copyright 2012 Karsten Ahnert
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 <limits>
18 
19 // disable checked iterator warning for msvc
20 #include <boost/config.hpp>
21 #ifdef BOOST_MSVC
22     #pragma warning(disable:4996)
23 #endif
24 
25 #define BOOST_TEST_MODULE odeint_trivial_state
26 
27 #include <boost/test/unit_test.hpp>
28 
29 #include <boost/mpl/vector.hpp>
30 #include <boost/mpl/int.hpp>
31 #include <boost/mpl/at.hpp>
32 
33 #include <boost/numeric/odeint/stepper/euler.hpp>
34 #include <boost/numeric/odeint/stepper/runge_kutta4.hpp>
35 #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54.hpp>
36 #include <boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp>
37 #include <boost/numeric/odeint/stepper/generation.hpp>
38 #include <boost/numeric/odeint/integrate/integrate_adaptive.hpp>
39 #include <boost/numeric/odeint/algebra/vector_space_algebra.hpp>
40 
41 using namespace boost::unit_test;
42 using namespace boost::numeric::odeint;
43 
44 namespace mpl = boost::mpl;
45 
46 struct constant_system
47 {
48     template< typename T >
operator ()constant_system49     void operator()( const T &x , T &dxdt , const T t ) const
50     { dxdt = 1.0; }
51 };
52 
53 
54 BOOST_AUTO_TEST_SUITE( trivial_state_test )
55 
56 /* test different do_step methods of simple steppers */
57 
58 typedef mpl::vector<
59         euler< double > ,
60         runge_kutta4< double > ,
61         // with floats all four parameters have to be given explicitly to override default double
62         euler< float , float , float , float > ,
63         runge_kutta4< float , float , float , float >
64         >::type stepper_types;
65 
66 
BOOST_AUTO_TEST_CASE_TEMPLATE(test_do_step,T,stepper_types)67 BOOST_AUTO_TEST_CASE_TEMPLATE( test_do_step , T, stepper_types )
68 {
69     typedef T stepper_type;
70     stepper_type stepper;
71     typename stepper_type::state_type x = 0.0;
72     typename stepper_type::time_type t = 0.0;
73     typename stepper_type::time_type dt = 0.1;
74     stepper.do_step( constant_system() , x , t , dt );
75     BOOST_CHECK_CLOSE( x , 0.1 , 100*std::numeric_limits< typename stepper_type::state_type >::epsilon() );
76 
77     // this overload is not allowed if the types of dxdt and dt are the same
78     // deriv_type dxdt = 1.0;
79     // stepper.do_step( constant_system , x , dxdt , t , dt );
80 
81     typename stepper_type::state_type x_out;
82     stepper.do_step( constant_system() , x , t , x_out , dt );
83     BOOST_CHECK_CLOSE( x , 0.1 , 100*std::numeric_limits< typename stepper_type::state_type >::epsilon() );
84     BOOST_CHECK_CLOSE( x_out , 0.2 , 100*std::numeric_limits< typename stepper_type::state_type >::epsilon() );
85 }
86 
87 
88 /* test integrate_adaptive with controlled steppers */
89 
90 typedef mpl::vector<
91     runge_kutta_cash_karp54< double > ,
92     runge_kutta_dopri5< double > ,
93     // with floats all four parameters have to be given explicitly to override default double
94     runge_kutta_cash_karp54< float , float , float , float > ,
95     runge_kutta_dopri5< float , float , float , float >
96     > error_stepper_types;
97 
BOOST_AUTO_TEST_CASE_TEMPLATE(test_integrate,T,error_stepper_types)98 BOOST_AUTO_TEST_CASE_TEMPLATE( test_integrate , T , error_stepper_types )
99 {
100     typedef T stepper_type;
101     typename stepper_type::state_type x = 0.0;
102     typename stepper_type::time_type t0 = 0.0;
103     typename stepper_type::time_type t1 = 1.0;
104     typename stepper_type::time_type dt = 0.1;
105     integrate_adaptive( make_controlled< stepper_type >( 1e-6 , 1e-6 ) , constant_system() , x , t0 , t1 , dt );
106     BOOST_CHECK_CLOSE( x , 1.0 , 100*std::numeric_limits< typename stepper_type::state_type >::epsilon() );
107 }
108 
109 BOOST_AUTO_TEST_SUITE_END()
110