list_lattice.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright 2011-2012 Mario Mulansky
  3. Copyright 2012-2013 Karsten Ahnert
  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. /* example showing how odeint can be used with std::list */
  9. #include <iostream>
  10. #include <cmath>
  11. #include <list>
  12. #include <boost/numeric/odeint.hpp>
  13. //[ list_bindings
  14. typedef std::list< double > state_type;
  15. namespace boost { namespace numeric { namespace odeint {
  16. template< >
  17. struct is_resizeable< state_type >
  18. { // declare resizeability
  19. typedef boost::true_type type;
  20. const static bool value = type::value;
  21. };
  22. template< >
  23. struct same_size_impl< state_type , state_type >
  24. { // define how to check size
  25. static bool same_size( const state_type &v1 ,
  26. const state_type &v2 )
  27. {
  28. return v1.size() == v2.size();
  29. }
  30. };
  31. template< >
  32. struct resize_impl< state_type , state_type >
  33. { // define how to resize
  34. static void resize( state_type &v1 ,
  35. const state_type &v2 )
  36. {
  37. v1.resize( v2.size() );
  38. }
  39. };
  40. } } }
  41. //]
  42. void lattice( const state_type &x , state_type &dxdt , const double /* t */ )
  43. {
  44. state_type::const_iterator x_begin = x.begin();
  45. state_type::const_iterator x_end = x.end();
  46. state_type::iterator dxdt_begin = dxdt.begin();
  47. x_end--; // stop one before last
  48. while( x_begin != x_end )
  49. {
  50. *(dxdt_begin++) = std::sin( *(x_begin) - *(x_begin++) );
  51. }
  52. *dxdt_begin = sin( *x_begin - *(x.begin()) ); // periodic boundary
  53. }
  54. using namespace boost::numeric::odeint;
  55. int main()
  56. {
  57. const int N = 32;
  58. state_type x;
  59. for( int i=0 ; i<N ; ++i )
  60. x.push_back( 1.0*i/N );
  61. integrate_const( runge_kutta4< state_type >() , lattice , x , 0.0 , 10.0 , 0.1 );
  62. }