1 /* Boost numeric test for orders of quadrature formulas test file
3 Copyright 2015 Gregor de Cillia
4 Copyright 2015 Mario Mulansky <mario.mulansky@gmx.net>
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE_1_0.txt or
8 copy at http://www.boost.org/LICENSE_1_0.txt)
11 // disable checked iterator warning for msvc
12 #include <boost/config.hpp>
15 #pragma warning(disable:4996)
18 #define BOOST_TEST_MODULE order_quadrature_formula
23 #include "boost/format.hpp"
25 #include <boost/test/unit_test.hpp>
27 #include <boost/mpl/vector.hpp>
29 #include <boost/numeric/odeint.hpp>
31 #include <boost/numeric/ublas/vector.hpp>
33 #include <boost/format.hpp>
35 using namespace boost::unit_test
;
36 using namespace boost::numeric::odeint
;
37 namespace mpl
= boost::mpl
;
39 typedef double value_type
;
40 typedef value_type time_type
;
41 typedef value_type state_type
;
43 BOOST_AUTO_TEST_SUITE( order_of_convergence_test
)
45 /* defines the simple monomial f(t) = (p+1) * t^p.*/
50 monomial(int p
= 0) : power( p
){};
52 void operator()( const state_type
&x
, state_type
&dxdt
, const time_type t
)
54 dxdt
= ( 1.0 + power
) * pow( t
, power
);
59 /* generic test for all steppers that support integrate_const */
60 template< class Stepper
>
61 struct stepper_order_test
63 void operator()( int steps
= 1 )
65 const int estimated_order
= estimate_order( steps
);
66 const int defined_order
= Stepper::order_value
;
68 std::cout
<< boost::format( "%-20i%-20i\n" )
69 % estimated_order
% defined_order
;
71 BOOST_REQUIRE_EQUAL( estimated_order
, defined_order
);
75 the order of the stepper is estimated by trying to solve the ODE
77 until the errors are too big to be justified by finite precision.
78 the first value p for which the problem is *not* solved within the
79 finite precision tolerance is the estimate for the order of the scheme.
81 int estimate_order( int steps
)
83 const double dt
= 1.0/steps
;
84 const double tolerance
= steps
*1E-15;
86 for( p
= 0; true; p
++ )
88 // begin with x'(t) = t^0 = 1
90 // then use x'(t) = 2*t^1
95 double t
= integrate_n_steps( Stepper(), monomial( p
), x
, 0.0, dt
,
97 if( fabs( x
- pow( t
, ( 1.0 + p
) ) ) > tolerance
)
100 // the smallest power p for which the test failed is the estimated order,
101 // as the solution for this power is x(t) = t^{p+1}
108 euler
< state_type
> ,
109 modified_midpoint
< state_type
> ,
110 runge_kutta4
< state_type
> ,
111 runge_kutta4_classic
< state_type
> ,
112 runge_kutta_cash_karp54_classic
< state_type
> ,
113 runge_kutta_cash_karp54
< state_type
> ,
114 runge_kutta_dopri5
< state_type
> ,
115 runge_kutta_fehlberg78
< state_type
>
116 > runge_kutta_steppers
;
119 adams_bashforth
< 2, state_type
, double, state_type
, double,
120 vector_space_algebra
, default_operations
,
121 initially_resizer
, runge_kutta_fehlberg78
< state_type
> >,
122 adams_bashforth
< 3, state_type
, double, state_type
, double,
123 vector_space_algebra
, default_operations
,
124 initially_resizer
, runge_kutta_fehlberg78
< state_type
> >,
125 adams_bashforth
< 4, state_type
, double, state_type
, double,
126 vector_space_algebra
, default_operations
,
127 initially_resizer
, runge_kutta_fehlberg78
< state_type
> >,
128 adams_bashforth
< 5, state_type
, double, state_type
, double,
129 vector_space_algebra
, default_operations
,
130 initially_resizer
, runge_kutta_fehlberg78
< state_type
> >,
131 adams_bashforth
< 6, state_type
, double, state_type
, double,
132 vector_space_algebra
, default_operations
,
133 initially_resizer
, runge_kutta_fehlberg78
< state_type
> >,
134 adams_bashforth
< 7, state_type
, double, state_type
, double,
135 vector_space_algebra
, default_operations
,
136 initially_resizer
, runge_kutta_fehlberg78
< state_type
> >,
137 adams_bashforth
< 8, state_type
, double, state_type
, double,
138 vector_space_algebra
, default_operations
,
139 initially_resizer
, runge_kutta_fehlberg78
< state_type
> >
144 adams_bashforth_moulton
< 2, state_type
, double, state_type
, double,
145 vector_space_algebra
, default_operations
,
147 runge_kutta_fehlberg78
< state_type
> >,
148 adams_bashforth_moulton
< 3, state_type
, double, state_type
, double,
149 vector_space_algebra
, default_operations
,
151 runge_kutta_fehlberg78
< state_type
> >,
152 adams_bashforth_moulton
< 4, state_type
, double, state_type
, double,
153 vector_space_algebra
, default_operations
,
155 runge_kutta_fehlberg78
< state_type
> >,
156 adams_bashforth_moulton
< 5, state_type
, double, state_type
, double,
157 vector_space_algebra
, default_operations
,
159 runge_kutta_fehlberg78
< state_type
> >,
160 adams_bashforth_moulton
< 6, state_type
, double, state_type
, double,
161 vector_space_algebra
, default_operations
,
163 runge_kutta_fehlberg78
< state_type
> >,
164 adams_bashforth_moulton
< 7, state_type
, double, state_type
, double,
165 vector_space_algebra
, default_operations
,
167 runge_kutta_fehlberg78
< state_type
> >,
168 adams_bashforth_moulton
< 8, state_type
, double, state_type
, double,
169 vector_space_algebra
, default_operations
,
171 runge_kutta_fehlberg78
< state_type
> >
175 BOOST_AUTO_TEST_CASE_TEMPLATE( runge_kutta_test
, Stepper
, runge_kutta_steppers
)
177 stepper_order_test
< Stepper
> tester
;
182 BOOST_AUTO_TEST_CASE_TEMPLATE( adams_bashforth_test
, Stepper
, ab_steppers
)
184 stepper_order_test
< Stepper
> tester
;
189 BOOST_AUTO_TEST_CASE_TEMPLATE( adams_bashforth_moultion_test
, Stepper
, abm_steppers
)
191 stepper_order_test
< Stepper
> tester
;
195 BOOST_AUTO_TEST_SUITE_END()