stiff_system.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * rosenbrock4.cpp
  3. *
  4. * Copyright 2010-2012 Mario Mulansky
  5. * Copyright 2011-2012 Karsten Ahnert
  6. * Copyright 2012 Andreas Angelopoulos
  7. *
  8. * Distributed under the Boost Software License, Version 1.0.
  9. * (See accompanying file LICENSE_1_0.txt or
  10. * copy at http://www.boost.org/LICENSE_1_0.txt)
  11. */
  12. #include <iostream>
  13. #include <fstream>
  14. #include <utility>
  15. #include <boost/numeric/odeint.hpp>
  16. #include <boost/phoenix/core.hpp>
  17. #include <boost/phoenix/core.hpp>
  18. #include <boost/phoenix/operator.hpp>
  19. using namespace std;
  20. using namespace boost::numeric::odeint;
  21. namespace phoenix = boost::phoenix;
  22. //[ stiff_system_definition
  23. typedef boost::numeric::ublas::vector< double > vector_type;
  24. typedef boost::numeric::ublas::matrix< double > matrix_type;
  25. struct stiff_system
  26. {
  27. void operator()( const vector_type &x , vector_type &dxdt , double /* t */ )
  28. {
  29. dxdt[ 0 ] = -101.0 * x[ 0 ] - 100.0 * x[ 1 ];
  30. dxdt[ 1 ] = x[ 0 ];
  31. }
  32. };
  33. struct stiff_system_jacobi
  34. {
  35. void operator()( const vector_type & /* x */ , matrix_type &J , const double & /* t */ , vector_type &dfdt )
  36. {
  37. J( 0 , 0 ) = -101.0;
  38. J( 0 , 1 ) = -100.0;
  39. J( 1 , 0 ) = 1.0;
  40. J( 1 , 1 ) = 0.0;
  41. dfdt[0] = 0.0;
  42. dfdt[1] = 0.0;
  43. }
  44. };
  45. //]
  46. /*
  47. //[ stiff_system_alternative_definition
  48. typedef boost::numeric::ublas::vector< double > vector_type;
  49. typedef boost::numeric::ublas::matrix< double > matrix_type;
  50. struct stiff_system
  51. {
  52. template< class State >
  53. void operator()( const State &x , State &dxdt , double t )
  54. {
  55. ...
  56. }
  57. };
  58. struct stiff_system_jacobi
  59. {
  60. template< class State , class Matrix >
  61. void operator()( const State &x , Matrix &J , const double &t , State &dfdt )
  62. {
  63. ...
  64. }
  65. };
  66. //]
  67. */
  68. int main( int argc , char **argv )
  69. {
  70. // typedef rosenbrock4< double > stepper_type;
  71. // typedef rosenbrock4_controller< stepper_type > controlled_stepper_type;
  72. // typedef rosenbrock4_dense_output< controlled_stepper_type > dense_output_type;
  73. //[ integrate_stiff_system
  74. vector_type x( 2 , 1.0 );
  75. size_t num_of_steps = integrate_const( make_dense_output< rosenbrock4< double > >( 1.0e-6 , 1.0e-6 ) ,
  76. make_pair( stiff_system() , stiff_system_jacobi() ) ,
  77. x , 0.0 , 50.0 , 0.01 ,
  78. cout << phoenix::arg_names::arg2 << " " << phoenix::arg_names::arg1[0] << "\n" );
  79. //]
  80. clog << num_of_steps << endl;
  81. // typedef runge_kutta_dopri5< vector_type > dopri5_type;
  82. // typedef controlled_runge_kutta< dopri5_type > controlled_dopri5_type;
  83. // typedef dense_output_runge_kutta< controlled_dopri5_type > dense_output_dopri5_type;
  84. //[ integrate_stiff_system_alternative
  85. vector_type x2( 2 , 1.0 );
  86. size_t num_of_steps2 = integrate_const( make_dense_output< runge_kutta_dopri5< vector_type > >( 1.0e-6 , 1.0e-6 ) ,
  87. stiff_system() , x2 , 0.0 , 50.0 , 0.01 ,
  88. cout << phoenix::arg_names::arg2 << " " << phoenix::arg_names::arg1[0] << "\n" );
  89. //]
  90. clog << num_of_steps2 << endl;
  91. return 0;
  92. }