harmonic_oscillator_units.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright 2011-2013 Karsten Ahnert
  3. Copyright 2011-2013 Mario Mulansky
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or
  6. copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #include <iostream>
  9. #include <vector>
  10. /* WARNING: Compilation in debug mode might consume enormous memory
  11. (e.g. ~2GB on gcc 4.4 )
  12. */
  13. // first increase fusion macro variables (now done by odeint itself)
  14. //#define BOOST_FUSION_INVOKE_MAX_ARITY 15
  15. //#define BOOST_RESULT_OF_NUM_ARGS 15
  16. //[ units_define_basic_quantities
  17. #include <boost/numeric/odeint.hpp>
  18. #include <boost/numeric/odeint/algebra/fusion_algebra.hpp>
  19. #include <boost/numeric/odeint/algebra/fusion_algebra_dispatcher.hpp>
  20. #include <boost/units/systems/si/length.hpp>
  21. #include <boost/units/systems/si/time.hpp>
  22. #include <boost/units/systems/si/velocity.hpp>
  23. #include <boost/units/systems/si/acceleration.hpp>
  24. #include <boost/units/systems/si/io.hpp>
  25. #include <boost/fusion/container.hpp>
  26. using namespace std;
  27. using namespace boost::numeric::odeint;
  28. namespace fusion = boost::fusion;
  29. namespace units = boost::units;
  30. namespace si = boost::units::si;
  31. typedef units::quantity< si::time , double > time_type;
  32. typedef units::quantity< si::length , double > length_type;
  33. typedef units::quantity< si::velocity , double > velocity_type;
  34. typedef units::quantity< si::acceleration , double > acceleration_type;
  35. typedef units::quantity< si::frequency , double > frequency_type;
  36. typedef fusion::vector< length_type , velocity_type > state_type;
  37. typedef fusion::vector< velocity_type , acceleration_type > deriv_type;
  38. //]
  39. //[ units_define_ode
  40. struct oscillator
  41. {
  42. frequency_type m_omega;
  43. oscillator( const frequency_type &omega = 1.0 * si::hertz ) : m_omega( omega ) { }
  44. void operator()( const state_type &x , deriv_type &dxdt , time_type t ) const
  45. {
  46. fusion::at_c< 0 >( dxdt ) = fusion::at_c< 1 >( x );
  47. fusion::at_c< 1 >( dxdt ) = - m_omega * m_omega * fusion::at_c< 0 >( x );
  48. }
  49. };
  50. //]
  51. //[ units_observer
  52. struct streaming_observer
  53. {
  54. std::ostream& m_out;
  55. streaming_observer( std::ostream &out ) : m_out( out ) { }
  56. struct write_element
  57. {
  58. std::ostream &m_out;
  59. write_element( std::ostream &out ) : m_out( out ) { };
  60. template< class T >
  61. void operator()( const T &t ) const
  62. {
  63. m_out << "\t" << t;
  64. }
  65. };
  66. template< class State , class Time >
  67. void operator()( const State &x , const Time &t ) const
  68. {
  69. m_out << t;
  70. fusion::for_each( x , write_element( m_out ) );
  71. m_out << "\n";
  72. }
  73. };
  74. //]
  75. int main( int argc , char**argv )
  76. {
  77. // typedef dense_output_runge_kutta
  78. // <
  79. // controlled_runge_kutta
  80. // <
  81. // runge_kutta_dopri5< state_type , double , deriv_type , time_type , fusion_algebra >
  82. // >
  83. // > stepper_type;
  84. //[ units_define_stepper
  85. typedef runge_kutta_dopri5< state_type , double , deriv_type , time_type > stepper_type;
  86. state_type x( 1.0 * si::meter , 0.0 * si::meter_per_second );
  87. integrate_const( make_dense_output( 1.0e-6 , 1.0e-6 , stepper_type() ) , oscillator( 2.0 * si::hertz ) ,
  88. x , 0.0 * si::second , 100.0 * si::second , 0.1 * si::second , streaming_observer( cout ) );
  89. //]
  90. return 0;
  91. }