File: symplectic_steppers.cpp

package info (click to toggle)
boost1.74 1.74.0%2Bds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 463,588 kB
  • sloc: cpp: 3,338,117; xml: 131,293; python: 33,088; ansic: 14,292; asm: 4,038; sh: 3,353; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (391 lines) | stat: -rw-r--r-- 14,223 bytes parent folder | download | duplicates (13)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 [auto_generated]
 libs/numeric/odeint/test/symplectic_steppers.cpp

 [begin_description]
 tba.
 [end_description]

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

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

#define BOOST_TEST_MODULE odeint_symplectic_steppers

#define BOOST_FUSION_INVOKE_MAX_ARITY 15
#define BOOST_RESULT_OF_NUM_ARGS 15
#define FUSION_MAX_VECTOR_SIZE 15


#include <boost/test/unit_test.hpp>

#include <boost/array.hpp>
#include <boost/static_assert.hpp>
#include <boost/type_traits/is_same.hpp>

#include <boost/mpl/vector.hpp>
#include <boost/mpl/zip_view.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/insert_range.hpp>
#include <boost/mpl/end.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/inserter.hpp>
#include <boost/mpl/at.hpp>

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

#include <boost/numeric/odeint/algebra/vector_space_algebra.hpp>
#include <boost/numeric/odeint/algebra/fusion_algebra.hpp>
#include <boost/numeric/odeint/stepper/symplectic_euler.hpp>
#include <boost/numeric/odeint/stepper/symplectic_rkn_sb3a_mclachlan.hpp>
#include <boost/numeric/odeint/stepper/symplectic_rkn_sb3a_m4_mclachlan.hpp>
#include <boost/numeric/odeint/integrate/integrate_const.hpp>

#include "diagnostic_state_type.hpp"
#include "const_range.hpp"
#include "dummy_odes.hpp"
#include "boost_units_helpers.hpp"


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

class custom_range_algebra : public range_algebra { };
class custom_default_operations : public default_operations { };


template< class Coor , class Mom , class Value , class CoorDeriv , class MomDeriv , class Time ,
          class Algebra , class Operations , class Resizer >
class complete_steppers : public mpl::vector<
    symplectic_euler< Coor , Mom , Value , CoorDeriv , MomDeriv , Time ,
                      Algebra , Operations , Resizer >
    , symplectic_rkn_sb3a_mclachlan< Coor , Mom , Value , CoorDeriv , MomDeriv , Time ,
                                     Algebra , Operations , Resizer >
    , symplectic_rkn_sb3a_m4_mclachlan< Coor , Mom , Value , CoorDeriv , MomDeriv , Time ,
                                        Algebra , Operations , Resizer >
    > {};

template< class Resizer >
class vector_steppers : public complete_steppers<
    diagnostic_state_type , diagnostic_state_type2 , double ,
    diagnostic_deriv_type , diagnostic_deriv_type2 , double ,
    custom_range_algebra , custom_default_operations , Resizer
    > { };


typedef mpl::vector< initially_resizer , always_resizer , never_resizer > resizers;
typedef mpl::vector_c< int , 1 , 3 , 0 > resizer_multiplicities ;


typedef mpl::copy<
    resizers ,
    mpl::inserter<
        mpl::vector0<> ,
        mpl::insert_range<
            mpl::_1 ,
            mpl::end< mpl::_1 > ,
            vector_steppers< mpl::_2 >
            >
        >
    >::type all_stepper_methods;


typedef mpl::size< vector_steppers< initially_resizer > >::type num_steppers;
typedef mpl::copy<
    resizer_multiplicities ,
    mpl::inserter<
        mpl::vector0<> ,
        mpl::insert_range<
            mpl::_1 , 
            mpl::end< mpl::_1 > ,
            const_range< num_steppers , mpl::_2 >
            >
        >
    >::type all_multiplicities;
                                                                                 






BOOST_AUTO_TEST_SUITE( symplectic_steppers_test )


BOOST_AUTO_TEST_CASE_TEMPLATE( test_assoc_types , Stepper , vector_steppers< initially_resizer > )
{
    BOOST_STATIC_ASSERT_MSG(
        ( boost::is_same< typename Stepper::coor_type , diagnostic_state_type >::value ) ,
        "Coordinate type" );
    BOOST_STATIC_ASSERT_MSG(
        ( boost::is_same< typename Stepper::momentum_type , diagnostic_state_type2 >::value ) ,
        "Momentum type" );
    BOOST_STATIC_ASSERT_MSG(
        ( boost::is_same< typename Stepper::coor_deriv_type , diagnostic_deriv_type >::value ) ,
        "Coordinate deriv type" );
    BOOST_STATIC_ASSERT_MSG(
        ( boost::is_same< typename Stepper::momentum_deriv_type , diagnostic_deriv_type2 >::value ) ,
        "Momentum deriv type" );

    BOOST_STATIC_ASSERT_MSG(
        ( boost::is_same< typename Stepper::state_type , std::pair< diagnostic_state_type , diagnostic_state_type2 > >::value ) ,
        "State type" );
    BOOST_STATIC_ASSERT_MSG(
        ( boost::is_same< typename Stepper::deriv_type , std::pair< diagnostic_deriv_type , diagnostic_deriv_type2 > >::value ) ,
        "Deriv type" );

    BOOST_STATIC_ASSERT_MSG( ( boost::is_same< typename Stepper::value_type , double >::value ) , "Value type" );
    BOOST_STATIC_ASSERT_MSG( ( boost::is_same< typename Stepper::time_type , double >::value ) , "Time type" );
    BOOST_STATIC_ASSERT_MSG( ( boost::is_same< typename Stepper::algebra_type , custom_range_algebra >::value ) , "Algebra type" );
    BOOST_STATIC_ASSERT_MSG( ( boost::is_same< typename Stepper::operations_type , custom_default_operations >::value ) , "Operations type" );

    BOOST_STATIC_ASSERT_MSG( ( boost::is_same< typename Stepper::resizer_type , initially_resizer >::value ) , "Resizer type" );
    BOOST_STATIC_ASSERT_MSG( ( boost::is_same< typename Stepper::stepper_category , stepper_tag >::value ) , "Stepper category" );
}


BOOST_AUTO_TEST_CASE_TEMPLATE( test_adjust_size , Stepper , vector_steppers< initially_resizer > )
{
    counter_state::init_counter();
    counter_deriv::init_counter();
    counter_state2::init_counter();
    counter_deriv2::init_counter();

    {
        Stepper stepper;
        diagnostic_state_type x;
        stepper.adjust_size( x );
    }

    TEST_COUNTERS( counter_state , 0 , 0 , 0 , 0 );
    TEST_COUNTERS( counter_state2 , 0 , 0 , 0 , 0 );
    TEST_COUNTERS( counter_deriv , 1 , 1 , 0 , 1 );
    TEST_COUNTERS( counter_deriv2 , 1 , 1 , 0 , 1 );
}


typedef mpl::zip_view< mpl::vector< all_stepper_methods , all_multiplicities > >::type zipped_steppers;
BOOST_AUTO_TEST_CASE_TEMPLATE( test_resizing , Stepper , zipped_steppers )
{
    typedef typename mpl::at_c< Stepper , 0 >::type stepper_type;
    const size_t multiplicity = mpl::at_c< Stepper , 1 >::type::value;

    counter_state::init_counter();
    counter_deriv::init_counter();
    counter_state2::init_counter();
    counter_deriv2::init_counter();

    {
        stepper_type stepper;
        std::pair< diagnostic_state_type , diagnostic_state_type2 > x;
        stepper.do_step( constant_mom_func() , x , 0.0 , 0.1 );
        stepper.do_step( constant_mom_func() , x , 0.0 , 0.1 );
        stepper.do_step( constant_mom_func() , x , 0.0 , 0.1 );
    }

    TEST_COUNTERS( counter_state , 0 , 0 , 0 , 0 );
    TEST_COUNTERS( counter_state2 , 0 , 0 , 0 , 0 );
    // dqdt is not needed when called with mom func only, so no resizing
    // TEST_COUNTERS( counter_deriv , multiplicity , 1 , 0 , 1 );
    TEST_COUNTERS( counter_deriv2 , multiplicity , 1 , 0 , 1 );
}


BOOST_AUTO_TEST_CASE_TEMPLATE( test_copying1 , Stepper , vector_steppers< initially_resizer > )
{
    counter_state::init_counter();
    counter_deriv::init_counter();
    counter_state2::init_counter();
    counter_deriv2::init_counter();

    {
        Stepper stepper;
        Stepper stepper2( stepper );
    }

    TEST_COUNTERS( counter_state , 0 , 0 , 0 , 0 );
    TEST_COUNTERS( counter_state2 , 0 , 0 , 0 , 0 );
    TEST_COUNTERS( counter_deriv , 0 , 2 , 1 , 2 );
    TEST_COUNTERS( counter_deriv2 , 0 , 2 , 1 , 2 );
}


BOOST_AUTO_TEST_CASE_TEMPLATE( test_copying2 , Stepper , vector_steppers< initially_resizer > )
{
    counter_state::init_counter();
    counter_deriv::init_counter();
    counter_state2::init_counter();
    counter_deriv2::init_counter();

    {
        Stepper stepper;
        std::pair< diagnostic_state_type , diagnostic_state_type2 > x;
        stepper.do_step( constant_mom_func() , x , 0.0 , 0.1 );
        Stepper stepper2( stepper );
    }

    TEST_COUNTERS( counter_state , 0 , 0 , 0 , 0 );
    TEST_COUNTERS( counter_state2 , 0 , 0 , 0 , 0 );
    // dqdt is not needed when called with mom func only, so no resizing
    //TEST_COUNTERS( counter_deriv , 1 , 2 , 1 , 2 );
    TEST_COUNTERS( counter_deriv2 , 1 , 2 , 1 , 2 );
}


BOOST_AUTO_TEST_CASE_TEMPLATE( test_do_step_v1 , Stepper , vector_steppers< initially_resizer > )
{
    Stepper s;
    std::pair< diagnostic_state_type , diagnostic_state_type2 > x1 , x2 , x3 , x4;
    x1.first[0] = 1.0;
    x1.second[0] = 2.0;
    x2 = x3 = x4 = x1;
    diagnostic_state_type x5_coor , x5_mom;
    x5_coor[0] = x1.first[0];
    x5_mom[0] = x1.second[0];

    s.do_step( constant_mom_func() , x1 , 0.0 , 0.1 );

    s.do_step( std::make_pair( default_coor_func() , constant_mom_func() ) , x2 , 0.0 , 0.1 );

    default_coor_func cf;
    constant_mom_func mf;
    s.do_step( std::make_pair( boost::ref( cf ) , boost::ref( mf ) ) , x3 , 0.0 , 0.1 );

    std::pair< default_coor_func , constant_mom_func > pf;
    s.do_step( boost::ref( pf ) , x4 , 0.0 , 0.1 );

    s.do_step( constant_mom_func() , std::make_pair( boost::ref( x5_coor ) , boost::ref( x5_mom ) ) , 0.0 , 0.1 );

    // checking for absolute values is not possible here, since the steppers are to different
    BOOST_CHECK_CLOSE( x1.first[0] , x2.first[0] , 1.0e-14 );
    BOOST_CHECK_CLOSE( x2.first[0] , x3.first[0] , 1.0e-14 );
    BOOST_CHECK_CLOSE( x3.first[0] , x4.first[0] , 1.0e-14 );
    BOOST_CHECK_CLOSE( x4.first[0] , x5_coor[0] , 1.0e-14 );

    BOOST_CHECK_CLOSE( x1.second[0] , x2.second[0] , 1.0e-14 );
    BOOST_CHECK_CLOSE( x2.second[0] , x3.second[0] , 1.0e-14 );
    BOOST_CHECK_CLOSE( x3.second[0] , x4.second[0] , 1.0e-14 );
    BOOST_CHECK_CLOSE( x4.second[0] , x5_mom[0] , 1.0e-14 );
}

BOOST_AUTO_TEST_CASE_TEMPLATE( test_do_step_range , Stepper , vector_steppers< initially_resizer > )
{
    Stepper s;
    diagnostic_state_type q , p ;
    q[0] = 1.0;
    p[0] = 2.0;

    std::vector< double > x;
    x.push_back( 1.0 );
    x.push_back( 2.0 );
    s.do_step( constant_mom_func() ,
               std::make_pair( x.begin() , x.begin() + 1 ) ,
               std::make_pair( x.begin() + 1 , x.begin() + 2 ) ,
               0.0 , 0.1 );

    s.do_step( constant_mom_func() , q , p , 0.0 , 0.1 );

    BOOST_CHECK_CLOSE( q[0] , x[0] , 1.0e-14 );
    BOOST_CHECK_CLOSE( p[0] , x[1] , 1.0e-14 );
}

BOOST_AUTO_TEST_CASE_TEMPLATE( test_do_step_v2 , Stepper , vector_steppers< initially_resizer > )
{
    Stepper s;
    diagnostic_state_type q , p ;
    q[0] = 1.0;
    p[0] = 2.0;
    diagnostic_state_type q2 = q , p2 = p;


    s.do_step( constant_mom_func() , q , p , 0.0 , 0.1 );
    s.do_step( constant_mom_func() , std::make_pair( boost::ref( q2 ) , boost::ref( p2 ) ) , 0.0 , 0.1 );
    
    BOOST_CHECK_CLOSE( q[0] , q2[0] , 1.0e-14 );
    BOOST_CHECK_CLOSE( p[0] , p2[0] , 1.0e-14 );
}

BOOST_AUTO_TEST_CASE_TEMPLATE( test_do_step_v3 , Stepper , vector_steppers< initially_resizer > )
{
    Stepper s;
    std::pair< diagnostic_state_type , diagnostic_state_type2 > x_in , x_out;
    x_in.first[0] = 1.0;
    x_in.second[0] = 2.0;
    diagnostic_state_type q2 , p2;
    q2[0] = x_in.first[0];
    p2[0] = x_in.second[0];


    s.do_step( constant_mom_func() , x_in , 0.0 , x_out , 0.1 );
    s.do_step( constant_mom_func() , std::make_pair( boost::ref( q2 ) , boost::ref( p2 ) ) , 0.0 , 0.1 );
    
    BOOST_CHECK_CLOSE( x_in.first[0] , 1.0 , 1.0e-14 );
    BOOST_CHECK_CLOSE( x_in.second[0] , 2.0 , 1.0e-14 );
    BOOST_CHECK_CLOSE( x_out.first[0] , q2[0] , 1.0e-14 );
    BOOST_CHECK_CLOSE( x_out.second[0] , p2[0] , 1.0e-14 );
}


typedef double vector_space;
typedef complete_steppers< vector_space , vector_space , double , 
                           vector_space , vector_space , double , 
                           vector_space_algebra , default_operations , initially_resizer > vector_space_steppers;
BOOST_AUTO_TEST_CASE_TEMPLATE( test_with_vector_space_algebra , Stepper , vector_space_steppers )
{
    Stepper s;
    std::pair< vector_space , vector_space > x;
    s.do_step( constant_mom_func_vector_space_1d() , x , 0.0 , 0.1 );

    s.do_step( std::make_pair( default_coor_func_vector_space_1d() , constant_mom_func_vector_space_1d() ) , x , 0.0 , 0.1 );
}


typedef boost::fusion::vector< length_type > coor_type;
typedef boost::fusion::vector< velocity_type > mom_type;
typedef boost::fusion::vector< acceleration_type > acc_type;
typedef complete_steppers< coor_type , mom_type , double , 
                           mom_type , acc_type , time_type, 
                           fusion_algebra , default_operations , initially_resizer > boost_unit_steppers;
BOOST_AUTO_TEST_CASE_TEMPLATE( test_with_boost_units , Stepper , boost_unit_steppers )
{
    namespace fusion = boost::fusion;
    namespace si = boost::units::si;
    Stepper s;

    coor_type q = fusion::make_vector( 1.0 * si::meter );
    mom_type p = fusion::make_vector( 2.0 * si::meter_per_second );
    time_type t = 0.0 * si::second;
    time_type dt = 0.1 * si::second;

    coor_type q1 = q , q2 = q;
    mom_type p1 = p , p2 = p;

    s.do_step( oscillator_mom_func_units() , std::make_pair( boost::ref( q ) , boost::ref( p ) ) , t , dt );

    s.do_step( std::make_pair( oscillator_coor_func_units() , oscillator_mom_func_units() ) ,
               std::make_pair( boost::ref( q1 ) , boost::ref( p1 ) ) , t , dt );

    s.do_step( oscillator_mom_func_units() , q2 , p2 , t , dt );

    BOOST_CHECK_CLOSE( ( fusion::at_c< 0 >( q  ).value() ) , ( fusion::at_c< 0 >( q1 ).value() ) , 1.0e-14 );
    BOOST_CHECK_CLOSE( ( fusion::at_c< 0 >( q1 ).value() ) , ( fusion::at_c< 0 >( q2 ).value() ) , 1.0e-14 );
    BOOST_CHECK_CLOSE( ( fusion::at_c< 0 >( p  ).value() ) , ( fusion::at_c< 0 >( p1 ).value() ) , 1.0e-14 );
    BOOST_CHECK_CLOSE( ( fusion::at_c< 0 >( p1 ).value() ) , ( fusion::at_c< 0 >( p2 ).value() ) , 1.0e-14 );
}


BOOST_AUTO_TEST_SUITE_END()