lorenz_mp.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * lorenz_mp.cpp
  3. *
  4. * This example demonstrates how odeint can be used with boost.multiprecision.
  5. *
  6. * Copyright 2011-2012 Karsten Ahnert
  7. * Copyright 2013 Mario Mulansky
  8. *
  9. * Distributed under the Boost Software License, Version 1.0.
  10. * (See accompanying file LICENSE_1_0.txt or
  11. * copy at http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. #include <iostream>
  14. //[ mp_lorenz_defs
  15. #include <boost/numeric/odeint.hpp>
  16. #include <boost/multiprecision/cpp_dec_float.hpp>
  17. using namespace std;
  18. using namespace boost::numeric::odeint;
  19. typedef boost::multiprecision::cpp_dec_float_50 value_type;
  20. typedef boost::array< value_type , 3 > state_type;
  21. //]
  22. //[ mp_lorenz_rhs
  23. struct lorenz
  24. {
  25. void operator()( const state_type &x , state_type &dxdt , value_type t ) const
  26. {
  27. const value_type sigma( 10 );
  28. const value_type R( 28 );
  29. const value_type b( value_type( 8 ) / value_type( 3 ) );
  30. dxdt[0] = sigma * ( x[1] - x[0] );
  31. dxdt[1] = R * x[0] - x[1] - x[0] * x[2];
  32. dxdt[2] = -b * x[2] + x[0] * x[1];
  33. }
  34. };
  35. //]
  36. struct streaming_observer
  37. {
  38. std::ostream& m_out;
  39. streaming_observer( std::ostream &out ) : m_out( out ) { }
  40. template< class State , class Time >
  41. void operator()( const State &x , Time t ) const
  42. {
  43. m_out << t;
  44. for( size_t i=0 ; i<x.size() ; ++i ) m_out << "\t" << x[i] ;
  45. m_out << "\n";
  46. }
  47. };
  48. int main( int argc , char **argv )
  49. {
  50. //[ mp_lorenz_int
  51. state_type x = {{ value_type( 10.0 ) , value_type( 10.0 ) , value_type( 10.0 ) }};
  52. cout.precision( 50 );
  53. integrate_const( runge_kutta4< state_type , value_type >() ,
  54. lorenz() , x , value_type( 0.0 ) , value_type( 10.0 ) , value_type( value_type( 1.0 ) / value_type( 10.0 ) ) ,
  55. streaming_observer( cout ) );
  56. //]
  57. return 0;
  58. }