• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  [auto_generated]
3  libs/numeric/odeint/test/runge_kutta_concepts.cpp
4 
5  [begin_description]
6  This file tests the Stepper concepts of odeint with all Runge-Kutta steppers. It's one of the main tests
7  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_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/euler.hpp>
50 #include <boost/numeric/odeint/stepper/modified_midpoint.hpp>
51 #include <boost/numeric/odeint/stepper/runge_kutta4_classic.hpp>
52 #include <boost/numeric/odeint/stepper/runge_kutta4.hpp>
53 #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54_classic.hpp>
54 #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54.hpp>
55 #include <boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp>
56 #include <boost/numeric/odeint/stepper/runge_kutta_fehlberg78.hpp>
57 
58 #include <boost/numeric/odeint/algebra/detail/extract_value_type.hpp>
59 
60 #include "prepare_stepper_testing.hpp"
61 #include "dummy_odes.hpp"
62 
63 using std::vector;
64 
65 using namespace boost::unit_test;
66 using namespace boost::numeric::odeint;
67 namespace mpl = boost::mpl;
68 
69 const double result = 2.2;
70 
71 const double eps = 1.0e-14;
72 
73 template< class Stepper , class System >
check_stepper_concept(Stepper & stepper,System system,typename Stepper::state_type & x)74 void check_stepper_concept( Stepper &stepper , System system , typename Stepper::state_type &x )
75 {
76     typedef Stepper stepper_type;
77     typedef typename stepper_type::deriv_type container_type;
78     typedef typename stepper_type::order_type order_type;
79     typedef typename stepper_type::time_type time_type;
80 
81     stepper.do_step( system , x , static_cast<time_type>(0.0) , static_cast<time_type>(0.1) );
82 }
83 
84 // default case is used for vector space types like plain double
85 template< class Stepper , typename T >
86 struct perform_stepper_test
87 {
88     typedef T vector_space_type;
operator ()perform_stepper_test89     void operator()( void ) const
90     {
91         using std::abs;
92         vector_space_type x;
93         x = 2.0;
94         Stepper stepper;
95         constant_system_functor_vector_space sys;
96 #ifndef _MSC_VER
97         // dont run this for MSVC due to compiler bug 697006
98         check_stepper_concept( stepper , constant_system_vector_space< vector_space_type , vector_space_type , typename Stepper::time_type > , x );
99 #else
100         check_stepper_concept( stepper , boost::cref( sys ) , x );
101 #endif
102         check_stepper_concept( stepper , boost::cref( sys ) , x );
103         std::cout << x << " ?= " << result << std::endl;
104         BOOST_CHECK( (abs( x - result )) < eps );
105     }
106 };
107 
108 template< class Stepper , typename T >
109 struct perform_stepper_test< Stepper , std::vector<T> >
110 {
111     typedef std::vector<T> vector_type;
operator ()perform_stepper_test112     void operator()( void )
113     {
114         using std::abs;
115         vector_type x( 1 , static_cast<T>(2.0) );
116         Stepper stepper;
117         constant_system_functor_standard sys;
118 #ifndef _MSC_VER
119         // dont run this for MSVC due to compiler bug 697006
120         check_stepper_concept( stepper , constant_system_standard< vector_type , vector_type , typename Stepper::time_type > , x );
121 #else
122         check_stepper_concept( stepper , boost::cref( sys ) , x );
123 #endif
124         check_stepper_concept( stepper , boost::cref( sys ) , x );
125         std::cout << x[0] << " ?= " << result << std::endl;
126         BOOST_CHECK( (abs( x[0] - result )) < eps );
127     }
128 };
129 
130 template< class Stepper , typename T >
131 struct perform_stepper_test< Stepper , boost::array<T,1> >
132 {
133     typedef boost::array<T,1> array_type;
operator ()perform_stepper_test134     void operator()( void )
135     {
136         using std::abs;
137         array_type x;
138         x[0] = static_cast<T>(2.0);
139         Stepper stepper;
140         constant_system_functor_standard sys;
141 #ifndef _MSC_VER
142         // dont run this for MSVC due to compiler bug 697006
143         check_stepper_concept( stepper , constant_system_standard< array_type , array_type , typename Stepper::time_type > , x );
144 #else
145         check_stepper_concept( stepper , boost::cref( sys ) , x );
146 #endif
147         check_stepper_concept( stepper , boost::cref( sys ) , x );
148         std::cout << x[0] << " ?= " << result << std::endl;
149         BOOST_CHECK( (abs( x[0] - result )) < eps );
150     }
151 };
152 
153 // split stepper methods to ensure the final vector has less than 30(?) elements
154 // (stepper_methods*container_types) < 30(?)
155 template< class State > class stepper_methods1 : public mpl::vector<
156     euler< State , typename detail::extract_value_type<State>::type > ,
157     modified_midpoint< State , typename detail::extract_value_type<State>::type > ,
158     runge_kutta4< State , typename detail::extract_value_type<State>::type > ,
159     runge_kutta4_classic< State , typename detail::extract_value_type<State>::type >
160     > { };
161 
162 template< class State > class stepper_methods2 : public mpl::vector<
163     runge_kutta_cash_karp54_classic< State , typename detail::extract_value_type<State>::type > ,
164     runge_kutta_cash_karp54< State , typename detail::extract_value_type<State>::type > ,
165     runge_kutta_dopri5< State , typename detail::extract_value_type<State>::type > ,
166     runge_kutta_fehlberg78< State , typename detail::extract_value_type<State>::type >
167     > { };
168 
169 
170 
171 typedef mpl::copy
172 <
173   container_types ,
174   mpl::inserter
175   <
176     mpl::vector0<> ,
177     mpl::insert_range
178     <
179       mpl::_1 ,
180       mpl::end< mpl::_1 > ,
181       stepper_methods1< mpl::_2 >
182     >
183   >
184 >::type stepper_combinations1;
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       stepper_methods2< mpl::_2 >
197     >
198   >
199 >::type stepper_combinations2;
200 
201 
202 BOOST_AUTO_TEST_SUITE( runge_kutta_concept_test )
203 
BOOST_AUTO_TEST_CASE_TEMPLATE(stepper_test1,Stepper,stepper_combinations1)204 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_test1 , Stepper, stepper_combinations1 )
205 {
206     perform_stepper_test< Stepper , typename Stepper::deriv_type > tester;
207     tester();
208 }
209 
BOOST_AUTO_TEST_CASE_TEMPLATE(stepper_test2,Stepper,stepper_combinations2)210 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_test2 , Stepper, stepper_combinations2 )
211 {
212     perform_stepper_test< Stepper , typename Stepper::deriv_type > tester;
213     tester();
214 }
215 
216 
217 BOOST_AUTO_TEST_SUITE_END()
218