• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  [auto_generated]
3  libs/numeric/odeint/test/runge_kutta_controlled_concepts.cpp
4 
5  [begin_description]
6  This file tests the Stepper concepts of odeint with all Controlled Runge-Kutta steppers.
7  It's one of the main tests of odeint.
8  [end_description]
9 
10  Copyright 2012 Karsten Ahnert
11  Copyright 2012-2013 Mario Mulansky
12 
13  Distributed under the Boost Software License, Version 1.0.
14  (See accompanying file LICENSE_1_0.txt or
15  copy at http://www.boost.org/LICENSE_1_0.txt)
16  */
17 
18 // disable checked iterator warning for msvc
19 #include <boost/config.hpp>
20 #ifdef BOOST_MSVC
21     #pragma warning(disable:4996)
22 #endif
23 
24 #define BOOST_TEST_MODULE odeint_runge_kutta_controlled_concepts
25 
26 #include <vector>
27 #include <cmath>
28 #include <iostream>
29 
30 #include <boost/numeric/odeint/config.hpp>
31 
32 #include <boost/array.hpp>
33 
34 #include <boost/test/unit_test.hpp>
35 
36 #include <boost/ref.hpp>
37 #include <boost/bind.hpp>
38 #include <boost/utility.hpp>
39 #include <boost/type_traits/add_reference.hpp>
40 
41 #include <boost/mpl/vector.hpp>
42 #include <boost/mpl/for_each.hpp>
43 #include <boost/mpl/insert_range.hpp>
44 #include <boost/mpl/end.hpp>
45 #include <boost/mpl/copy.hpp>
46 #include <boost/mpl/placeholders.hpp>
47 #include <boost/mpl/inserter.hpp>
48 
49 #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54_classic.hpp>
50 #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54.hpp>
51 #include <boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp>
52 #include <boost/numeric/odeint/stepper/runge_kutta_fehlberg78.hpp>
53 #include <boost/numeric/odeint/stepper/controlled_runge_kutta.hpp>
54 #include <boost/numeric/odeint/stepper/bulirsch_stoer.hpp>
55 
56 #include "prepare_stepper_testing.hpp"
57 #include "dummy_odes.hpp"
58 
59 using std::vector;
60 
61 using namespace boost::unit_test;
62 using namespace boost::numeric::odeint;
63 namespace mpl = boost::mpl;
64 
65 const double result = 2.2; // two steps
66 
67 const double eps = 1.0e-14;
68 
69 template< class Stepper , class System >
check_controlled_stepper_concept(Stepper & stepper,System system,typename Stepper::state_type & x)70 void check_controlled_stepper_concept( Stepper &stepper , System system , typename Stepper::state_type &x )
71 {
72     typedef Stepper stepper_type;
73     typedef typename stepper_type::deriv_type container_type;
74     //typedef typename stepper_type::order_type order_type;  controlled_error_stepper don't necessarily have a order (burlish-stoer)
75     typedef typename stepper_type::time_type time_type;
76 
77     time_type t = 0.0 , dt = 0.1;
78     controlled_step_result step_result = stepper.try_step( system , x , t , dt );
79 
80     BOOST_CHECK_MESSAGE( step_result == success , "step result: " << step_result ); // error = 0 for constant system -> step size is always too small
81 }
82 
83 
84 template< class ControlledStepper , typename T >
85 struct perform_controlled_stepper_test
86 {
87     typedef T vector_space_type;
operator ()perform_controlled_stepper_test88     void operator()( void ) const
89     {
90         using std::abs;
91         vector_space_type x;
92         x = 2.0;
93         ControlledStepper controlled_stepper;
94         constant_system_functor_vector_space sys;
95 #ifndef _MSC_VER
96         // dont run this for MSVC due to compiler bug 697006
97         check_controlled_stepper_concept( controlled_stepper ,
98                                           constant_system_vector_space< vector_space_type , vector_space_type , typename ControlledStepper::time_type >
99                                           , x );
100 #else
101         check_controlled_stepper_concept( controlled_stepper , boost::cref( sys ) , x );
102 #endif
103         check_controlled_stepper_concept( controlled_stepper , boost::cref( sys ) , x );
104         BOOST_CHECK( (abs( x - result )) < eps );
105     }
106 };
107 
108 template< class ControlledStepper , typename T >
109 struct perform_controlled_stepper_test< ControlledStepper , std::vector<T> >
110 {
111     typedef std::vector<T> vector_type;
operator ()perform_controlled_stepper_test112     void operator()( void )
113     {
114         using std::abs;
115         vector_type x( 1 , 2.0 );
116         ControlledStepper controlled_stepper;
117         constant_system_functor_standard sys;
118 #ifndef _MSC_VER
119         // dont run this for MSVC due to compiler bug 697006
120 
121         check_controlled_stepper_concept( controlled_stepper ,
122                                           constant_system_standard< vector_type , vector_type , typename ControlledStepper::time_type > ,
123                                           x );
124 #else
125         check_controlled_stepper_concept( controlled_stepper , boost::cref( sys ) , x );
126 #endif
127         check_controlled_stepper_concept( controlled_stepper , boost::cref( sys ) , x );
128         BOOST_CHECK( (abs( x[0] - result )) < eps );
129     }
130 };
131 
132 
133 template< class ControlledStepper >
134 struct perform_controlled_stepper_test< ControlledStepper , vector_space_type >
135 {
operator ()perform_controlled_stepper_test136     void operator()( void ) const
137     {
138         using std::abs;
139         vector_space_type x;
140         x = 2.0;
141         ControlledStepper controlled_stepper;
142         constant_system_functor_vector_space sys;
143 #ifndef _MSC_VER
144         // dont run this for MSVC due to compiler bug 697006
145         check_controlled_stepper_concept( controlled_stepper ,
146                                           constant_system_vector_space< vector_space_type , vector_space_type , typename ControlledStepper::time_type >
147                                           , x );
148 #else
149         check_controlled_stepper_concept( controlled_stepper , boost::cref( sys ) , x );
150 #endif
151         check_controlled_stepper_concept( controlled_stepper , boost::cref( sys ) , x );
152         BOOST_CHECK( (abs( x - result )) < eps );
153     }
154 };
155 
156 template< class ControlledStepper , typename T >
157 struct perform_controlled_stepper_test< ControlledStepper , boost::array<T,1> >
158 {
159     typedef boost::array<T,1> array_type;
operator ()perform_controlled_stepper_test160     void operator()( void )
161     {
162         using std::abs;
163         array_type x;
164         x[0] = 2.0;
165         ControlledStepper controlled_stepper;
166         constant_system_functor_standard sys;
167 #ifndef _MSC_VER
168         // dont run this for MSVC due to compiler bug 697006
169         check_controlled_stepper_concept( controlled_stepper , constant_system_standard< array_type , array_type , typename ControlledStepper::time_type > , x );
170 #else
171         check_controlled_stepper_concept( controlled_stepper , boost::cref( sys ) , x );
172 #endif
173         check_controlled_stepper_concept( controlled_stepper , boost::cref( sys ) , x );
174         BOOST_CHECK( (abs( x[0] - result )) < eps );
175     }
176 };
177 
178 
179 template< class State > class controlled_stepper_methods : public mpl::vector<
180     controlled_runge_kutta< runge_kutta_cash_karp54_classic< State , typename detail::extract_value_type<State>::type > > ,
181     controlled_runge_kutta< runge_kutta_dopri5< State , typename detail::extract_value_type<State>::type > > ,
182     controlled_runge_kutta< runge_kutta_fehlberg78< State , typename detail::extract_value_type<State>::type > > ,
183     bulirsch_stoer< State , typename detail::extract_value_type<State>::type >
184     > { };
185 
186 typedef mpl::copy
187 <
188     container_types ,
189     mpl::inserter
190     <
191         mpl::vector0<> ,
192         mpl::insert_range
193         <
194             mpl::_1 ,
195             mpl::end< mpl::_1 > ,
196             controlled_stepper_methods< mpl::_2 >
197             >
198         >
199     >::type all_controlled_stepper_methods;
200 
201 
202 
203 
204 BOOST_AUTO_TEST_SUITE( controlled_runge_kutta_concept_test )
205 
BOOST_AUTO_TEST_CASE_TEMPLATE(controlled_stepper_test,ControlledStepper,all_controlled_stepper_methods)206 BOOST_AUTO_TEST_CASE_TEMPLATE( controlled_stepper_test , ControlledStepper , all_controlled_stepper_methods )
207 {
208     perform_controlled_stepper_test< ControlledStepper , typename ControlledStepper::state_type > tester;
209     tester();
210 }
211 
212 BOOST_AUTO_TEST_SUITE_END()
213