summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/numeric/odeint/test/velocity_verlet.cpp
blob: 93a2e97ca2371643283ea0c0619be523f2045bab (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
293
294
295
296
297
298
/*
 [auto_generated]
 libs/numeric/odeint/test/velocity_verlet.cpp

 [begin_description]
 tba.
 [end_description]

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

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

#define BOOST_TEST_MODULE odeint_velocity_verlet

#define BOOST_FUSION_INVOKE_MAX_ARITY 15
#define BOOST_RESULT_OF_NUM_ARGS 15

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

#include "resizing_test_state_type.hpp"

#include <boost/numeric/odeint/stepper/velocity_verlet.hpp>
#include <boost/numeric/odeint/algebra/fusion_algebra.hpp>

#include <boost/array.hpp>
#include <boost/test/unit_test.hpp>

#include <boost/units/systems/si/length.hpp>
#include <boost/units/systems/si/time.hpp>
#include <boost/units/systems/si/velocity.hpp>
#include <boost/units/systems/si/acceleration.hpp>
#include <boost/units/systems/si/io.hpp>

#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/vector20.hpp>
#include <boost/fusion/container.hpp>

namespace fusion = boost::fusion;
namespace units = boost::units;
namespace si = boost::units::si;

typedef double value_type;
typedef units::quantity< si::time , value_type > time_type;
typedef units::unit< units::derived_dimension< units::time_base_dimension , 2 >::type , si::system > time_2;
typedef units::quantity< time_2 , value_type > time_2_type;
typedef units::quantity< si::length , value_type > length_type;
typedef units::quantity< si::velocity , value_type > velocity_type;
typedef units::quantity< si::acceleration , value_type > acceleration_type;
typedef fusion::vector< length_type , length_type > coor_vector;
typedef fusion::vector< velocity_type , velocity_type > velocity_vector;
typedef fusion::vector< acceleration_type , acceleration_type > accelartion_vector;



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

size_t ode_call_count;

struct velocity_verlet_fixture
{
    velocity_verlet_fixture( void ) { ode_call_count = 0; adjust_size_count = 0; }
};

struct ode
{
    template< class CoorIn , class MomentumIn , class AccelerationOut , class Time >
    void operator()( const CoorIn &q , const MomentumIn &p , AccelerationOut &a , Time t ) const
    {
        a[0] = -q[0] - p[0];
        a[1] = -q[1] - p[1];
        ++ode_call_count;
    }
};

struct ode_units
{
    void operator()( coor_vector const &q , velocity_vector const &p , accelartion_vector &a , time_type t ) const
    {
        const units::quantity< si::frequency , value_type > omega = 1.0 * si::hertz;
        const units::quantity< si::frequency , value_type > friction = 0.001 * si::hertz;
        fusion::at_c< 0 >( a ) = omega * omega * fusion::at_c< 0 >( q ) - friction * fusion::at_c< 0 >( p );
        fusion::at_c< 1 >( a ) = omega * omega * fusion::at_c< 1 >( q ) - friction * fusion::at_c< 0 >( p );
        ++ode_call_count;
    }
};

template< class Q , class P >
void init_state( Q &q , P &p )
{
    q[0] = 1.0 ; q[1] = 0.5;
    p[0] = 2.0 ; p[1] = -1.0;
}

typedef boost::array< double , 2 > array_type;
typedef std::vector< double > vector_type;

typedef velocity_verlet< array_type > array_stepper;
typedef velocity_verlet< vector_type > vector_stepper;

template< typename Resizer >
struct get_resizer_test_stepper
{
    typedef velocity_verlet< test_array_type , test_array_type , double , test_array_type , 
        double , double , range_algebra , default_operations , Resizer > type;
};





BOOST_AUTO_TEST_SUITE( velocity_verlet_test )

BOOST_FIXTURE_TEST_CASE( test_with_array_ref , velocity_verlet_fixture )
{
    array_stepper stepper;
    array_type q , p ;
    init_state( q , p );
    stepper.do_step( ode() , std::make_pair( boost::ref( q ) , boost::ref( p ) ) , 0.0 , 0.01 );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 2 ) );
}

BOOST_FIXTURE_TEST_CASE( test_with_array_pair , velocity_verlet_fixture )
{
    array_stepper stepper;
    std::pair< array_type , array_type > xxx;
    init_state( xxx.first , xxx.second );
    stepper.do_step( ode() , xxx , 0.0 , 0.01 );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 2 ) );
}

BOOST_FIXTURE_TEST_CASE( test_with_vector_ref , velocity_verlet_fixture )
{
    vector_stepper stepper;
    vector_type q( 2 ) , p( 2 );
    init_state( q , p );
    stepper.do_step( ode() , std::make_pair( boost::ref( q ) , boost::ref( p ) ) , 0.0 , 0.01 );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 2 ) );
}

BOOST_FIXTURE_TEST_CASE( test_with_vector_pair , velocity_verlet_fixture )
{
    vector_stepper stepper;
    std::pair< vector_type , vector_type > x;
    x.first.resize( 2 ) ; x.second.resize( 2 );
    init_state( x.first , x.second );
    stepper.do_step( ode() , x , 0.0 , 0.01 );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 2 ) );
}

BOOST_FIXTURE_TEST_CASE( test_initial_resizer , velocity_verlet_fixture )
{
    typedef get_resizer_test_stepper< initially_resizer >::type stepper_type;
    std::pair< test_array_type , test_array_type > x;
    init_state( x.first , x.second );
    stepper_type stepper;
    stepper.do_step( ode() , x , 0.0 , 0.01 );
    stepper.do_step( ode() , x , 0.0 , 0.01 );
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 2 ) );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 3 ) );
}

BOOST_FIXTURE_TEST_CASE( test_always_resizer , velocity_verlet_fixture )
{
    typedef get_resizer_test_stepper< always_resizer >::type stepper_type;
    std::pair< test_array_type , test_array_type > x;
    init_state( x.first , x.second );
    stepper_type stepper;
    stepper.do_step( ode() , x , 0.0 , 0.01 );
    stepper.do_step( ode() , x , 0.0 , 0.01 );
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 4 ) );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 4 ) );    // attention: one more system call, since the size of the state has been changed
}

BOOST_FIXTURE_TEST_CASE( test_with_never_resizer , velocity_verlet_fixture )
{
    typedef get_resizer_test_stepper< never_resizer >::type stepper_type;
    std::pair< test_array_type , test_array_type > x;
    init_state( x.first , x.second );
    stepper_type stepper;
    stepper.do_step( ode() , x , 0.0 , 0.01 );
    stepper.do_step( ode() , x , 0.0 , 0.01 );
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 0 ) );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 3 ) );
}

BOOST_FIXTURE_TEST_CASE( test_reset , velocity_verlet_fixture )
{
    typedef get_resizer_test_stepper< initially_resizer >::type stepper_type;
    std::pair< test_array_type , test_array_type > x;
    init_state( x.first , x.second );
    stepper_type stepper;
    stepper.do_step( ode() , x , 0.0 , 0.01 );
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 2 ) );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 2 ) );
    stepper.do_step( ode() , x , 0.0 , 0.01 );
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 2 ) );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 3 ) );
    stepper.reset();
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 2 ) );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 3 ) );
    stepper.do_step( ode() , x , 0.0 , 0.01 );
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 2 ) );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 5 ) );    
}

BOOST_FIXTURE_TEST_CASE( test_initialize1 , velocity_verlet_fixture )
{
    typedef get_resizer_test_stepper< initially_resizer >::type stepper_type;
    std::pair< test_array_type , test_array_type > x;
    init_state( x.first , x.second );
    stepper_type stepper;
    test_array_type ain;
    ode()( x.first , x.second , ain , 0.0 );
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 0 ) );
    stepper.initialize( ain );
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 2 ) );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 1 ) );
    stepper.do_step( ode() , x , 0.0 , 0.01 );
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 2 ) );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 2 ) );
}

BOOST_FIXTURE_TEST_CASE( test_initialize2 , velocity_verlet_fixture )
{
    typedef get_resizer_test_stepper< initially_resizer >::type stepper_type;
    std::pair< test_array_type , test_array_type > x;
    init_state( x.first , x.second );
    stepper_type stepper;
    stepper.initialize( ode() , x.first , x.second , 0.0 );
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 2 ) );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 1 ) );
    stepper.do_step( ode() , x , 0.0 , 0.01 );
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 2 ) );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 2 ) );
}


BOOST_FIXTURE_TEST_CASE( test_adjust_size , velocity_verlet_fixture )
{
    typedef get_resizer_test_stepper< initially_resizer >::type stepper_type;
    std::pair< test_array_type , test_array_type > x;
    init_state( x.first , x.second );
    stepper_type stepper;
    stepper.do_step( ode() , x , 0.0 , 0.01 );
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 2 ) );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 2 ) );
    stepper.adjust_size( x.first );
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 4 ) );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 2 ) );    
    stepper.do_step( ode() , x , 0.0 , 0.01 );    
    BOOST_CHECK_EQUAL( adjust_size_count , size_t( 4 ) );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 4 ) );
}

BOOST_FIXTURE_TEST_CASE( test_with_unit_pair , velocity_verlet_fixture )
{
    typedef velocity_verlet< coor_vector , velocity_vector , value_type , accelartion_vector ,
        time_type , time_2_type , fusion_algebra , default_operations > stepper_type;
    
    std::pair< coor_vector , velocity_vector > x;
    fusion::at_c< 0 >( x.first ) = 1.0 * si::meter;
    fusion::at_c< 1 >( x.first ) = 0.5 * si::meter;
    fusion::at_c< 0 >( x.second ) = 2.0 * si::meter_per_second;
    fusion::at_c< 1 >( x.second ) = -1.0 * si::meter_per_second;
    stepper_type stepper;
    stepper.do_step( ode_units() , x , 0.0 * si::second , 0.01 * si::second );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 2 ) );
}


BOOST_FIXTURE_TEST_CASE( test_with_unit_ref , velocity_verlet_fixture )
{
    typedef velocity_verlet< coor_vector , velocity_vector , value_type , accelartion_vector ,
        time_type , time_2_type , fusion_algebra , default_operations > stepper_type;
    
    coor_vector q;
    velocity_vector p;
    fusion::at_c< 0 >( q ) = 1.0 * si::meter;
    fusion::at_c< 1 >( q ) = 0.5 * si::meter;
    fusion::at_c< 0 >( p ) = 2.0 * si::meter_per_second;
    fusion::at_c< 1 >( p ) = -1.0 * si::meter_per_second;
    stepper_type stepper;
    stepper.do_step( ode_units() , std::make_pair( boost::ref( q ) , boost::ref( p ) ) , 0.0 * si::second , 0.01 * si::second );
    BOOST_CHECK_EQUAL( ode_call_count , size_t( 2 ) );
}


BOOST_AUTO_TEST_SUITE_END()