Brian Silverman | 7c33ab2 | 2018-08-04 17:14:51 -0700 | [diff] [blame^] | 1 | /* |
| 2 | [auto_generated] |
| 3 | libs/numeric/odeint/test/euler_stepper.cpp |
| 4 | |
| 5 | [begin_description] |
| 6 | This file tests explicit Euler stepper. |
| 7 | [end_description] |
| 8 | |
| 9 | Copyright 2011 Mario Mulansky |
| 10 | Copyright 2012 Karsten Ahnert |
| 11 | |
| 12 | Distributed under the Boost Software License, Version 1.0. |
| 13 | (See accompanying file LICENSE_1_0.txt or |
| 14 | copy at http://www.boost.org/LICENSE_1_0.txt) |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #define BOOST_TEST_MODULE odeint_explicit_euler |
| 19 | |
| 20 | #include <boost/test/unit_test.hpp> |
| 21 | |
| 22 | #include <utility> |
| 23 | #include <iostream> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <boost/numeric/odeint/stepper/euler.hpp> |
| 27 | #include <boost/numeric/odeint/algebra/range_algebra.hpp> |
| 28 | |
| 29 | using namespace boost::unit_test; |
| 30 | using namespace boost::numeric::odeint; |
| 31 | |
| 32 | // test with own vector implementation |
| 33 | |
| 34 | class my_vec : public std::vector< double > { |
| 35 | |
| 36 | public: |
| 37 | |
| 38 | my_vec() : std::vector< double >() |
| 39 | { } |
| 40 | |
| 41 | my_vec( const my_vec &x ) : std::vector< double >( x ) |
| 42 | { } |
| 43 | |
| 44 | |
| 45 | my_vec( size_t dim ) |
| 46 | : std::vector< double >( dim ) |
| 47 | { } |
| 48 | |
| 49 | }; |
| 50 | |
| 51 | namespace boost { |
| 52 | namespace numeric { |
| 53 | namespace odeint { |
| 54 | |
| 55 | template<> |
| 56 | struct is_resizeable< my_vec > |
| 57 | { |
| 58 | //struct type : public boost::true_type { }; |
| 59 | typedef boost::true_type type; |
| 60 | const static bool value = type::value; |
| 61 | }; |
| 62 | } } } |
| 63 | |
| 64 | typedef double value_type; |
| 65 | //typedef std::vector< value_type > state_type; |
| 66 | typedef my_vec state_type; |
| 67 | |
| 68 | /* use functors, because functions don't work with msvc 10, I guess this is a bug */ |
| 69 | struct sys |
| 70 | { |
| 71 | void operator()( const state_type &x , state_type &dxdt , const value_type t ) const |
| 72 | { |
| 73 | std::cout << "sys start " << dxdt.size() << std::endl; |
| 74 | dxdt[0] = x[0] + 2 * x[1]; |
| 75 | dxdt[1] = x[1]; |
| 76 | std::cout << "sys done" << std::endl; |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | |
| 81 | BOOST_AUTO_TEST_SUITE( explicit_euler_test ) |
| 82 | |
| 83 | BOOST_AUTO_TEST_CASE( test_euler ) |
| 84 | { |
| 85 | range_algebra algebra; |
| 86 | euler< state_type > stepper( algebra ); |
| 87 | state_type x( 2 ); |
| 88 | x[0] = 0.0; x[1] = 1.0; |
| 89 | |
| 90 | std::cout << "initialized" << std::endl; |
| 91 | |
| 92 | const value_type eps = 1E-12; |
| 93 | const value_type dt = 0.1; |
| 94 | |
| 95 | stepper.do_step( sys() , x , 0.0 , dt ); |
| 96 | |
| 97 | using std::abs; |
| 98 | |
| 99 | // compare with analytic solution of above system |
| 100 | BOOST_CHECK_MESSAGE( abs( x[0] - 2.0*1.0*dt ) < eps , x[0] - 2.0*1.0*dt ); |
| 101 | BOOST_CHECK_MESSAGE( abs( x[1] - (1.0 + dt) ) < eps , x[1] - (1.0+dt) ); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | BOOST_AUTO_TEST_SUITE_END() |