Brian Silverman | 7c33ab2 | 2018-08-04 17:14:51 -0700 | [diff] [blame^] | 1 | /* |
| 2 | [auto_generated] |
| 3 | libs/numeric/odeint/test/fusion_algebra.cpp |
| 4 | |
| 5 | [begin_description] |
| 6 | This file tests the Fusion algebra. |
| 7 | [end_description] |
| 8 | |
| 9 | Copyright 2011-2012 Karsten Ahnert |
| 10 | Copyright 2011-2013 Mario Mulansky |
| 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_fusion_algebra |
| 19 | |
| 20 | // using fusion vectors as state types requires increased macro variables |
| 21 | #define BOOST_FUSION_INVOKE_MAX_ARITY 15 |
| 22 | #define BOOST_RESULT_OF_NUM_ARGS 15 |
| 23 | |
| 24 | #include <cmath> |
| 25 | #include <complex> |
| 26 | #include <utility> |
| 27 | #include <functional> |
| 28 | |
| 29 | #include <boost/numeric/odeint/config.hpp> |
| 30 | |
| 31 | #include <boost/test/unit_test.hpp> |
| 32 | #include <boost/test/floating_point_comparison.hpp> |
| 33 | |
| 34 | #include <boost/units/systems/si/length.hpp> |
| 35 | #include <boost/units/systems/si/time.hpp> |
| 36 | #include <boost/units/systems/si/velocity.hpp> |
| 37 | #include <boost/units/systems/si/acceleration.hpp> |
| 38 | #include <boost/units/systems/si/io.hpp> |
| 39 | |
| 40 | #include <boost/fusion/container.hpp> |
| 41 | |
| 42 | #include <boost/numeric/odeint/algebra/default_operations.hpp> |
| 43 | #include <boost/numeric/odeint/algebra/fusion_algebra.hpp> |
| 44 | |
| 45 | namespace units = boost::units; |
| 46 | namespace si = boost::units::si; |
| 47 | namespace fusion = boost::fusion; |
| 48 | |
| 49 | using boost::numeric::odeint::default_operations; |
| 50 | using boost::numeric::odeint::fusion_algebra; |
| 51 | |
| 52 | typedef double value_type; |
| 53 | typedef units::quantity< si::time , value_type > time_type; |
| 54 | typedef units::quantity< si::length , value_type > length_type; |
| 55 | typedef units::quantity< si::velocity , value_type > velocity_type; |
| 56 | typedef units::quantity< si::acceleration , value_type > acceleration_type; |
| 57 | typedef fusion::vector< length_type , velocity_type > state_type; |
| 58 | typedef fusion::vector< velocity_type , acceleration_type > deriv_type; |
| 59 | |
| 60 | const time_type dt = 0.1 * si::second; |
| 61 | |
| 62 | |
| 63 | struct fusion_fixture |
| 64 | { |
| 65 | fusion_fixture( void ) |
| 66 | : res( 0.0 * si::meter , 0.0 * si::meter_per_second ) , |
| 67 | x( 1.0 * si::meter , 1.0 * si::meter_per_second ) , |
| 68 | k1( 1.0 * si::meter_per_second , 1.0 * si::meter_per_second_squared ) , |
| 69 | k2( 2.0 * si::meter_per_second , 2.0 * si::meter_per_second_squared ) , |
| 70 | k3( 3.0 * si::meter_per_second , 3.0 * si::meter_per_second_squared ) , |
| 71 | k4( 4.0 * si::meter_per_second , 4.0 * si::meter_per_second_squared ) , |
| 72 | k5( 5.0 * si::meter_per_second , 5.0 * si::meter_per_second_squared ) , |
| 73 | k6( 6.0 * si::meter_per_second , 6.0 * si::meter_per_second_squared ) { } |
| 74 | |
| 75 | ~fusion_fixture( void ) |
| 76 | { |
| 77 | BOOST_CHECK_CLOSE( fusion::at_c< 0 >( x ).value() , 1.0 , 1.0e-10 ); |
| 78 | BOOST_CHECK_CLOSE( fusion::at_c< 1 >( x ).value() , 1.0 , 1.0e-10 ); |
| 79 | BOOST_CHECK_CLOSE( fusion::at_c< 0 >( k1 ).value() , 1.0 , 1.0e-10 ); |
| 80 | BOOST_CHECK_CLOSE( fusion::at_c< 1 >( k1 ).value() , 1.0 , 1.0e-10 ); |
| 81 | BOOST_CHECK_CLOSE( fusion::at_c< 0 >( k2 ).value() , 2.0 , 1.0e-10 ); |
| 82 | BOOST_CHECK_CLOSE( fusion::at_c< 1 >( k2 ).value() , 2.0 , 1.0e-10 ); |
| 83 | BOOST_CHECK_CLOSE( fusion::at_c< 0 >( k3 ).value() , 3.0 , 1.0e-10 ); |
| 84 | BOOST_CHECK_CLOSE( fusion::at_c< 1 >( k3 ).value() , 3.0 , 1.0e-10 ); |
| 85 | BOOST_CHECK_CLOSE( fusion::at_c< 0 >( k4 ).value() , 4.0 , 1.0e-10 ); |
| 86 | BOOST_CHECK_CLOSE( fusion::at_c< 1 >( k4 ).value() , 4.0 , 1.0e-10 ); |
| 87 | BOOST_CHECK_CLOSE( fusion::at_c< 0 >( k5 ).value() , 5.0 , 1.0e-10 ); |
| 88 | BOOST_CHECK_CLOSE( fusion::at_c< 1 >( k5 ).value() , 5.0 , 1.0e-10 ); |
| 89 | BOOST_CHECK_CLOSE( fusion::at_c< 0 >( k6 ).value() , 6.0 , 1.0e-10 ); |
| 90 | BOOST_CHECK_CLOSE( fusion::at_c< 1 >( k6 ).value() , 6.0 , 1.0e-10 ); |
| 91 | } |
| 92 | |
| 93 | state_type res , x; |
| 94 | deriv_type k1 , k2 , k3 , k4 , k5 , k6 ; |
| 95 | }; |
| 96 | |
| 97 | |
| 98 | |
| 99 | BOOST_AUTO_TEST_SUITE( fusion_algebra_test ) |
| 100 | |
| 101 | fusion_algebra algebra; |
| 102 | |
| 103 | BOOST_AUTO_TEST_CASE( for_each2 ) |
| 104 | { |
| 105 | fusion_fixture f; |
| 106 | algebra.for_each2( f.res , f.k1 , |
| 107 | default_operations::scale_sum1< time_type >( dt ) ); |
| 108 | BOOST_CHECK_CLOSE( fusion::at_c< 0 >( f.res ).value() , 0.1 , 1.0e-10 ); |
| 109 | BOOST_CHECK_CLOSE( fusion::at_c< 1 >( f.res ).value() , 0.1 , 1.0e-10 ); |
| 110 | } |
| 111 | |
| 112 | BOOST_AUTO_TEST_CASE( for_each3 ) |
| 113 | { |
| 114 | fusion_fixture f; |
| 115 | algebra.for_each3( f.res , f.x , f.k1 , |
| 116 | default_operations::scale_sum2< value_type , time_type >( 1.0 , dt ) ); |
| 117 | BOOST_CHECK_CLOSE( fusion::at_c< 0 >( f.res ).value() , 1.1 , 1.0e-10 ); |
| 118 | BOOST_CHECK_CLOSE( fusion::at_c< 1 >( f.res ).value() , 1.1 , 1.0e-10 ); |
| 119 | } |
| 120 | |
| 121 | BOOST_AUTO_TEST_CASE( for_each4 ) |
| 122 | { |
| 123 | fusion_fixture f; |
| 124 | algebra.for_each4( f.res , f.x , f.k1 , f.k2 , |
| 125 | default_operations::scale_sum3< value_type , time_type , time_type >( 1.0 , dt , dt ) ); |
| 126 | BOOST_CHECK_CLOSE( fusion::at_c< 0 >( f.res ).value() , 1.3 , 1.0e-10 ); |
| 127 | BOOST_CHECK_CLOSE( fusion::at_c< 1 >( f.res ).value() , 1.3 , 1.0e-10 ); |
| 128 | } |
| 129 | |
| 130 | BOOST_AUTO_TEST_CASE( for_each5 ) |
| 131 | { |
| 132 | fusion_fixture f; |
| 133 | algebra.for_each5( f.res , f.x , f.k1 , f.k2 , f.k3 , |
| 134 | default_operations::scale_sum4< value_type , time_type , time_type , time_type >( 1.0 , dt , dt , dt ) ); |
| 135 | BOOST_CHECK_CLOSE( fusion::at_c< 0 >( f.res ).value() , 1.6 , 1.0e-10 ); |
| 136 | BOOST_CHECK_CLOSE( fusion::at_c< 1 >( f.res ).value() , 1.6 , 1.0e-10 ); |
| 137 | } |
| 138 | |
| 139 | BOOST_AUTO_TEST_CASE( for_each6 ) |
| 140 | { |
| 141 | fusion_fixture f; |
| 142 | algebra.for_each6( f.res , f.x , f.k1 , f.k2 , f.k3 , f.k4 , |
| 143 | default_operations::scale_sum5< value_type , time_type , time_type , time_type , time_type >( 1.0 , dt , dt , dt , dt ) ); |
| 144 | BOOST_CHECK_CLOSE( fusion::at_c< 0 >( f.res ).value() , 2.0 , 1.0e-10 ); |
| 145 | BOOST_CHECK_CLOSE( fusion::at_c< 1 >( f.res ).value() , 2.0 , 1.0e-10 ); |
| 146 | } |
| 147 | |
| 148 | BOOST_AUTO_TEST_CASE( for_each7 ) |
| 149 | { |
| 150 | fusion_fixture f; |
| 151 | algebra.for_each7( f.res , f.x , f.k1 , f.k2 , f.k3 , f.k4 , f.k5 , |
| 152 | default_operations::scale_sum6< value_type , time_type , time_type , time_type , time_type , time_type >( 1.0 , dt , dt , dt , dt , dt ) ); |
| 153 | BOOST_CHECK_CLOSE( fusion::at_c< 0 >( f.res ).value() , 2.5 , 1.0e-10 ); |
| 154 | BOOST_CHECK_CLOSE( fusion::at_c< 1 >( f.res ).value() , 2.5 , 1.0e-10 ); |
| 155 | } |
| 156 | |
| 157 | BOOST_AUTO_TEST_CASE( for_each8 ) |
| 158 | { |
| 159 | fusion_fixture f; |
| 160 | algebra.for_each8( f.res , f.x , f.k1 , f.k2 , f.k3 , f.k4 , f.k5 , f.k6 , |
| 161 | default_operations::scale_sum7< value_type , time_type , time_type , time_type , time_type , time_type , time_type >( 1.0 , dt , dt , dt , dt , dt , dt ) ); |
| 162 | BOOST_CHECK_CLOSE( fusion::at_c< 0 >( f.res ).value() , 3.1 , 1.0e-10 ); |
| 163 | BOOST_CHECK_CLOSE( fusion::at_c< 1 >( f.res ).value() , 3.1 , 1.0e-10 ); |
| 164 | } |
| 165 | |
| 166 | BOOST_AUTO_TEST_CASE( for_each15 ) |
| 167 | { |
| 168 | fusion_fixture f; |
| 169 | algebra.for_each15( f.res , f.x , f.k1 , f.k2 , f.k3 , f.k4 , f.k5 , f.k6 , f.k1 , f.k2 , f.k3 , f.k4 , f.k5 , f.k6 , f.k1 , |
| 170 | default_operations::scale_sum14< value_type , time_type , time_type , time_type , time_type , time_type , time_type , |
| 171 | time_type , time_type , time_type , time_type , time_type , time_type , time_type >( 1.0 , dt , dt , dt , dt , dt , dt , dt , dt , dt , dt , dt , dt , dt ) ); |
| 172 | //BOOST_CHECK_CLOSE( fusion::at_c< 0 >( f.res ).value() , 3.1 , 1.0e-10 ); |
| 173 | //BOOST_CHECK_CLOSE( fusion::at_c< 1 >( f.res ).value() , 3.1 , 1.0e-10 ); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | BOOST_AUTO_TEST_CASE( norm_inf ) |
| 178 | { |
| 179 | double nrm = algebra.norm_inf( fusion::make_vector( 1.0 , 2.0 , 3.0 ) ); |
| 180 | BOOST_CHECK_CLOSE( nrm , 3.0 , 1.0e-10 ); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | BOOST_AUTO_TEST_SUITE_END() |