van_der_pol_stiff.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * van_der_pol_stiff.cpp
  3. *
  4. * Created on: Dec 12, 2011
  5. *
  6. * Copyright 2012 Karsten Ahnert
  7. * Copyright 2012-2013 Rajeev Singh
  8. * Copyright 2012-2013 Mario Mulansky
  9. *
  10. * Distributed under the Boost Software License, Version 1.0.
  11. * (See accompanying file LICENSE_1_0.txt or
  12. * copy at http://www.boost.org/LICENSE_1_0.txt)
  13. */
  14. #include <iostream>
  15. #include <fstream>
  16. #include <utility>
  17. #include <boost/numeric/odeint.hpp>
  18. #include <boost/phoenix/core.hpp>
  19. #include <boost/phoenix/operator.hpp>
  20. using namespace std;
  21. using namespace boost::numeric::odeint;
  22. namespace phoenix = boost::phoenix;
  23. const double mu = 1000.0;
  24. typedef boost::numeric::ublas::vector< double > vector_type;
  25. typedef boost::numeric::ublas::matrix< double > matrix_type;
  26. struct vdp_stiff
  27. {
  28. void operator()( const vector_type &x , vector_type &dxdt , double t )
  29. {
  30. dxdt[0] = x[1];
  31. dxdt[1] = -x[0] - mu * x[1] * (x[0]*x[0]-1.0);
  32. }
  33. };
  34. struct vdp_stiff_jacobi
  35. {
  36. void operator()( const vector_type &x , matrix_type &J , const double &t , vector_type &dfdt )
  37. {
  38. J(0, 0) = 0.0;
  39. J(0, 1) = 1.0;
  40. J(1, 0) = -1.0 - 2.0*mu * x[0] * x[1];
  41. J(1, 1) = -mu * ( x[0] * x[0] - 1.0);
  42. dfdt[0] = 0.0;
  43. dfdt[1] = 0.0;
  44. }
  45. };
  46. int main( int argc , char **argv )
  47. {
  48. //[ integrate_stiff_system
  49. vector_type x( 2 );
  50. /* initialize random seed: */
  51. srand ( time(NULL) );
  52. // initial conditions
  53. for (int i=0; i<2; i++)
  54. x[i] = 1.0; //(1.0 * rand()) / RAND_MAX;
  55. size_t num_of_steps = integrate_const( make_dense_output< rosenbrock4< double > >( 1.0e-6 , 1.0e-6 ) ,
  56. make_pair( vdp_stiff() , vdp_stiff_jacobi() ) ,
  57. x , 0.0 , 1000.0 , 1.0
  58. , cout << phoenix::arg_names::arg2 << " " << phoenix::arg_names::arg1[0] << " " << phoenix::arg_names::arg1[1] << "\n"
  59. );
  60. //]
  61. clog << num_of_steps << endl;
  62. //[ integrate_stiff_system_alternative
  63. vector_type x2( 2 );
  64. // initial conditions
  65. for (int i=0; i<2; i++)
  66. x2[i] = 1.0; //(1.0 * rand()) / RAND_MAX;
  67. //size_t num_of_steps2 = integrate_const( make_dense_output< runge_kutta_dopri5< vector_type > >( 1.0e-6 , 1.0e-6 ) ,
  68. // vdp_stiff() , x2 , 0.0 , 1000.0 , 1.0
  69. // , cout << phoenix::arg_names::arg2 << " " << phoenix::arg_names::arg1[0] << " " << phoenix::arg_names::arg1[1] << "\n"
  70. // );
  71. //]
  72. //clog << num_of_steps2 << endl;
  73. return 0;
  74. }