summaryrefslogtreecommitdiff
path: root/libs/numeric/odeint/test/integrate_stepper_refs.cpp
blob: 5d6d34c94ebd015fe1014618a8f8b0ae0d299040 (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
/*
 [auto_generated]
 libs/numeric/odeint/test/integrate_stepper_refs.cpp

 [begin_description]
 Tests the integrate functions with boost::ref( stepper)
 [end_description]

 Copyright 2009-2013 Karsten Ahnert
 Copyright 2009-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)
 */


#define BOOST_TEST_MODULE odeint_integrate_stepper_refs

#include <vector>
#include <cmath>
#include <iostream>

#include <boost/numeric/odeint/config.hpp>

#include <boost/noncopyable.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/iterator/counting_iterator.hpp>

#include <boost/mpl/vector.hpp>

#include <boost/numeric/odeint/integrate/integrate_const.hpp>
#include <boost/numeric/odeint/integrate/integrate_adaptive.hpp>
#include <boost/numeric/odeint/integrate/integrate_times.hpp>
#include <boost/numeric/odeint/integrate/integrate_n_steps.hpp>
#include <boost/numeric/odeint/stepper/controlled_step_result.hpp>


using namespace boost::unit_test;
using namespace boost::numeric::odeint;
namespace mpl = boost::mpl;

typedef double value_type;
typedef std::vector< value_type > state_type;

// minimal non-copyable basic stepper
template<
class State
>
class simple_stepper_nc : boost::noncopyable
{
public :
    typedef State state_type;
    typedef double value_type;
    typedef state_type deriv_type;
    typedef double time_type;
    typedef size_t order_type;
    typedef stepper_tag stepper_category;

    template< class System  >
    void do_step( System system , state_type &in , time_type t , time_type dt )
    {
        // empty

    }
};

// minimal non-copyable controlled stepper
template<
class State
>
class controlled_stepper_nc : boost::noncopyable
{
public :
    typedef State state_type;
    typedef double value_type;
    typedef state_type deriv_type;
    typedef double time_type;
    typedef size_t order_type;
    typedef controlled_stepper_tag stepper_category;

    template< class System  >
    controlled_step_result try_step( System system , state_type &in , time_type &t , time_type &dt )
    {
        std::cout << "dense out stepper: " << t << " , " << dt << std::endl;
        t += dt;
        return success;
    }
};

// minimal non-copyable dense_output stepper
template<
class State
>
class dense_out_stepper_nc : boost::noncopyable
{
public :
    typedef State state_type;
    typedef double value_type;
    typedef state_type deriv_type;
    typedef double time_type;
    typedef size_t order_type;
    typedef dense_output_stepper_tag stepper_category;

    void initialize( const state_type &x0 , const time_type t0 , const time_type dt0 )
    {
        m_x = x0;
        m_t = t0;
        m_dt = dt0;
        std::cout << "initialize: " << m_t << " , " << m_dt << std::endl;
    }

    template< class System >
    void do_step( System system )
    {
        std::cout << "dense out stepper: " << m_t << " , " << m_dt << std::endl;
        m_t += m_dt;
    }

    void calc_state( const time_type t_inter , state_type &x )
    {
        x = m_x;
    }

    const state_type& current_state() const
    { return m_x; }

    time_type current_time() const
    { return m_t; }

    time_type current_time_step() const
    { return m_dt; }


private :
    time_type m_t;
    time_type m_dt;
    state_type m_x;
};


void lorenz( const state_type &x , state_type &dxdt , const value_type t )
{
    //const value_type sigma( 10.0 );
    const value_type R( 28.0 );
    const value_type b( value_type( 8.0 ) / value_type( 3.0 ) );

    // first component trivial
    dxdt[0] = 1.0; //sigma * ( x[1] - x[0] );
    dxdt[1] = R * x[0] - x[1] - x[0] * x[2];
    dxdt[2] = -b * x[2] + x[0] * x[1];
}

struct push_back_time
{
    std::vector< double >& m_times;

    state_type& m_x;

    push_back_time( std::vector< double > &times , state_type &x )
    :  m_times( times ) , m_x( x ) { }

    void operator()( const state_type &x , double t )
    {
        m_times.push_back( t );
        boost::numeric::odeint::copy( x , m_x );
    }
};

template< class Stepper >
struct perform_integrate_const_test
{
    void operator()()
    {
        state_type x( 3 , 10.0 ) , x_end( 3 );

        std::vector< value_type > times;

        Stepper stepper;

        integrate_const( boost::ref(stepper) , lorenz , x , 0.0 , 1.0 ,
                         0.1, push_back_time( times , x_end ) );
    }
};

template< class Stepper >
struct perform_integrate_adaptive_test
{
    void operator()()
    {
        state_type x( 3 , 10.0 ) , x_end( 3 );

        std::vector< value_type > times;

        Stepper stepper;

        integrate_adaptive( boost::ref(stepper) , lorenz , x , 0.0 , 1.0 ,
                            0.1, push_back_time( times , x_end ) );
    }
};

template< class Stepper >
struct perform_integrate_n_steps_test
{
    void operator()()
    {
        state_type x( 3 , 10.0 ) , x_end( 3 );

        std::vector< value_type > times;

        Stepper stepper;

        integrate_n_steps( boost::ref(stepper) , lorenz , x , 0.0 , 0.1 ,
                           10 , push_back_time( times , x_end ) );
    }
};

template< class Stepper >
struct perform_integrate_times_test
{
    void operator()()
    {
        state_type x( 3 , 10.0 ) , x_end( 3 );

        std::vector< value_type > times;

        Stepper stepper;

        integrate_times( boost::ref(stepper) , lorenz , x ,
                         boost::counting_iterator<int>(0) , boost::counting_iterator<int>(10) , 0.1 ,
                         push_back_time( times , x_end ) );
    }
};

class stepper_methods : public mpl::vector<
    simple_stepper_nc< state_type > ,
    controlled_stepper_nc< state_type >,
    dense_out_stepper_nc< state_type > > { };

BOOST_AUTO_TEST_SUITE( integrate_stepper_refs )

BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_const_test_case , Stepper, stepper_methods )
{
    std::cout << "integrate const" << std::endl;
    perform_integrate_const_test< Stepper > tester;
    tester();
}


BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_adaptive_test_case , Stepper, stepper_methods )
{
    std::cout << "integrate adaptive" << std::endl;
    perform_integrate_adaptive_test< Stepper > tester;
    tester();
}

BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_n_steps_test_case , Stepper, stepper_methods )
{
    std::cout << "integrate n steps" << std::endl;
    perform_integrate_n_steps_test< Stepper > tester;
    tester();
}

BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_times_test_case , Stepper, stepper_methods )
{
    std::cout << "integrate times" << std::endl;
    perform_integrate_times_test< Stepper > tester;
    tester();
}

BOOST_AUTO_TEST_SUITE_END()