summaryrefslogtreecommitdiff
path: root/libs/numeric/odeint/test/stepper_with_ranges.cpp
blob: 59f007f2dfb98580412a7c427322ac0c6002a229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 [auto_generated]
 libs/numeric/odeint/test/stepper_with_ranges.cpp

 [begin_description]
 This file tests if the steppers play well with Boost.Range.
 [end_description]

 Copyright 2011-2012 Karsten Ahnert
 Copyright 2011-2013 Mario Mulansky

 Distributed under the Boost Software License, Version 1.0.
 (See accompanying file LICENSE_1_0.txt or
 copy at http://www.boost.org/LICENSE_1_0.txt)
 */

// disable checked iterator warning for msvc
#include <boost/config.hpp>
#ifdef BOOST_MSVC
    #pragma warning(disable:4996)
#endif

#define BOOST_TEST_MODULE odeint_stepper_with_ranges

#include <boost/test/unit_test.hpp>

#include <vector>
#include <utility>
#include <iostream>

#include <boost/array.hpp>
#include <boost/range.hpp>
#include <boost/ref.hpp>

#include <boost/numeric/odeint/stepper/euler.hpp>
#include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54_classic.hpp>
#include <boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp>
#include <boost/numeric/odeint/stepper/controlled_runge_kutta.hpp>
#include <boost/numeric/odeint/stepper/symplectic_euler.hpp>
#include <boost/numeric/odeint/stepper/dense_output_runge_kutta.hpp>

typedef std::vector< double > state_type;
typedef boost::array< double , 3 > state_type2;

/* explicitly force range algebra for this array! */
namespace boost { namespace numeric { namespace odeint {

template<>
struct algebra_dispatcher< state_type2 >
{ typedef range_algebra algebra_type; };

} } }


/*
 * The two systems are needed, since for steppers with more than
 * one internal step it is difficult to calculate the exact result
 *
 * system1 is suited for euler
 */
struct system1
{
    template< class State , class Deriv >
    void operator()( const State &x_ , Deriv &dxdt_ , double t )
    {
        typename boost::range_iterator< const State >::type x = boost::begin( x_ );
        typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ );

        dxdt[0] = x[0];
        dxdt[1] = 2.0;
        dxdt[2] = 3.0;
    }

    template< class State , class Deriv >
    void operator()( const State &x_ , const Deriv &dxdt_ , double t )
    {
        typename boost::range_iterator< const State >::type x = boost::begin( x_ );
        typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ );

        dxdt[0] = x[0];
        dxdt[1] = 2.0;
        dxdt[2] = 3.0;
    }
};

/*
 * system2 is suited for all steppers, it allows you to calculate the result analytically.
 */
struct system2
{
    template< class State , class Deriv >
    void operator()( const State &x_ , Deriv &dxdt_ , double t )
    {
        typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ );

        dxdt[0] = 1.0;
        dxdt[1] = 2.0;
        dxdt[2] = 3.0;
    }

    template< class State , class Deriv >
    void operator()( const State &x_ , const Deriv &dxdt_ , double t )
    {
        typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ );

        dxdt[0] = 1.0;
        dxdt[1] = 2.0;
        dxdt[2] = 3.0;
    }
};


/*
 * Useful for Hamiltonian systems
 */
struct ham_sys
{
    template< class State , class Deriv >
    void operator()( const State &x_ , Deriv &dxdt_ )
    {
        typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ );
        dxdt[0] = 1.0;
        dxdt[1] = 2.0;
        dxdt[2] = 3.0;
    }

    template< class State , class Deriv >
    void operator()( const State &x_ , const Deriv &dxdt_ )
    {
        typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ );
        dxdt[0] = 1.0;
        dxdt[1] = 2.0;
        dxdt[2] = 3.0;
    }
};


struct vector_fixture
{
    const static size_t dim = 6;
    boost::array< double , dim > in;
    boost::array< double , dim > q;
    boost::array< double , dim > p;
    state_type err;

    vector_fixture( void )
        : in() , err( 3 )
    {
        for( size_t i=0 ; i<dim ; ++i )
        {
            in[ i ] = q[i] = p[i] = double( i );
        }
        for( size_t i=0 ; i<3 ; ++i )
        {
            err[i] = double( i ) * 10.0;
        }
    }

    ~vector_fixture( void )
    {
    }
};

#define CHECK_VALUES( x , x0 , x1 , x2 , x3 , x4 , x5 ) \
    BOOST_CHECK_CLOSE( x[0] , x0 , 1.0e-8 );            \
    BOOST_CHECK_CLOSE( x[1] , x1 , 1.0e-8 );            \
    BOOST_CHECK_CLOSE( x[2] , x2 , 1.0e-8 );            \
    BOOST_CHECK_CLOSE( x[3] , x3 , 1.0e-8 );            \
    BOOST_CHECK_CLOSE( x[4] , x4 , 1.0e-8 );            \
    BOOST_CHECK_CLOSE( x[5] , x5 , 1.0e-8 )



BOOST_AUTO_TEST_SUITE( stepper_with_ranges )

BOOST_AUTO_TEST_CASE( explicit_euler_with_range_v1 )
{
    vector_fixture f;
    boost::numeric::odeint::euler< state_type > euler;
    euler.do_step( system1() , std::make_pair( f.in.begin() + 1 , f.in.begin() + 4 ) , 0.1 , 0.1 );
    CHECK_VALUES( f.in , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
}



BOOST_AUTO_TEST_CASE( explicit_error_k54_with_range_v1 )
{
    vector_fixture f;
    boost::numeric::odeint::runge_kutta_cash_karp54_classic< state_type > rk54;
    rk54.do_step( system2() , std::make_pair( f.in.begin() + 1 , f.in.begin() + 4 ) , 0.1 , 0.1 );
    CHECK_VALUES( f.in , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
}

BOOST_AUTO_TEST_CASE( explicit_error_k54_with_range_v5 )
{
    vector_fixture f;
    boost::numeric::odeint::runge_kutta_cash_karp54_classic< state_type > rk54;
    rk54.do_step( system2() , std::make_pair( f.in.begin() + 1 , f.in.begin() + 4 ) , 0.1 , 0.1 , f.err );
    CHECK_VALUES( f.in , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
}


BOOST_AUTO_TEST_CASE( runge_kutta_dopri5_with_range_v1 )
{
    vector_fixture f;
    boost::numeric::odeint::runge_kutta_dopri5< state_type > dopri5;
    dopri5.do_step( system2() , std::make_pair( f.in.begin() + 1 , f.in.begin() + 4 ) , 0.1 , 0.1 );
    CHECK_VALUES( f.in , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
}


BOOST_AUTO_TEST_CASE( runge_kutta_dopri5_with_range_v5 )
{
    vector_fixture f;
    boost::numeric::odeint::runge_kutta_dopri5< state_type > dopri5;
    dopri5.do_step( system2() , std::make_pair( f.in.begin() + 1 , f.in.begin() + 4 ) , 0.1 , 0.1 , f.err );
    CHECK_VALUES( f.in , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
}


BOOST_AUTO_TEST_CASE( controlled_error_stepper_rk54 )
{
    double t = 0.0 , dt = 0.1;
    vector_fixture f;
    boost::numeric::odeint::controlled_runge_kutta< boost::numeric::odeint::runge_kutta_cash_karp54_classic< state_type > > stepper;
    stepper.try_step( system2() , std::make_pair( f.in.begin() + 1 , f.in.begin() + 4 ) , t , dt );
    CHECK_VALUES( f.in , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
}

BOOST_AUTO_TEST_CASE( controlled_error_stepper_dopri5 )
{
    double t = 0.0 , dt = 0.1;
    vector_fixture f;
    boost::numeric::odeint::controlled_runge_kutta< boost::numeric::odeint::runge_kutta_dopri5< state_type > > stepper;
    stepper.try_step( system2() , std::make_pair( f.in.begin() + 1 , f.in.begin() + 4 ) , t , dt );
    CHECK_VALUES( f.in , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
}


BOOST_AUTO_TEST_CASE( symplectic_euler_coor_func )
{
    vector_fixture f;
    boost::numeric::odeint::symplectic_euler< state_type > euler;
    euler.do_step( ham_sys() ,
                   std::make_pair( f.q.begin() + 1 , f.q.begin() + 4 ) ,
                   std::make_pair( f.p.begin() + 3 , f.p.begin() + 6 ) ,
                   0.0 , 0.1 );
    CHECK_VALUES( f.q , 0.0 , 1.3 , 2.4 , 3.5 , 4.0 , 5.0 );
    CHECK_VALUES( f.p , 0.0 , 1.0 , 2.0 , 3.1 , 4.2 , 5.3 );
}

BOOST_AUTO_TEST_CASE( symplectic_euler_coor_and_mom_func )
{
    vector_fixture f;
    boost::numeric::odeint::symplectic_euler< state_type > euler;
    euler.do_step( std::make_pair( ham_sys() , ham_sys() ) ,
                   std::make_pair( f.q.begin() + 1 , f.q.begin() + 4 ) ,
                   std::make_pair( f.p.begin() + 3 , f.p.begin() + 6 ) ,
                   0.0 , 0.1 );
    CHECK_VALUES( f.q , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
    CHECK_VALUES( f.p , 0.0 , 1.0 , 2.0 , 3.1 , 4.2 , 5.3 );
}


BOOST_AUTO_TEST_CASE( dense_output_euler_with_ranges )
{
    using namespace boost::numeric::odeint;
    vector_fixture f;
    dense_output_runge_kutta< euler< state_type > > stepper;
    stepper.initialize( std::make_pair( f.in.begin() + 1, f.in.begin() + 4 ) , 0.0 , 0.1 );
    stepper.do_step( system1() );
    stepper.calc_state( 0.05 , std::make_pair( f.in.begin() + 1 ,f.in.begin() +4 ) );
    CHECK_VALUES( f.in , 0.0 , 1.05 , 2.1 , 3.15 , 4.0 , 5.0 );
}

BOOST_AUTO_TEST_CASE( dense_output_dopri5_with_ranges )
{
    using namespace boost::numeric::odeint;
    vector_fixture f;
    dense_output_runge_kutta<
        controlled_runge_kutta<
            runge_kutta_dopri5< state_type >
            > > stepper;
    stepper.initialize( std::make_pair( f.in.begin() + 1, f.in.begin() + 4 ) , 0.0 , 0.1 );
    stepper.do_step( system2() );
    stepper.calc_state( 0.05 , std::make_pair( f.in.begin() + 1 ,f.in.begin() +4 ) );
    CHECK_VALUES( f.in , 0.0 , 1.05 , 2.1 , 3.15 , 4.0 , 5.0 );
}



BOOST_AUTO_TEST_SUITE_END()