roessler.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Simulation of an ensemble of Roessler attractors
  3. *
  4. * Copyright 2014 Mario Mulansky
  5. *
  6. * Distributed under the Boost Software License, Version 1.0.
  7. * (See accompanying file LICENSE_1_0.txt or
  8. * copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. #include <iostream>
  12. #include <vector>
  13. #include <random>
  14. #include <boost/timer.hpp>
  15. #include <boost/array.hpp>
  16. #include <boost/numeric/odeint.hpp>
  17. namespace odeint = boost::numeric::odeint;
  18. typedef boost::timer timer_type;
  19. typedef double fp_type;
  20. //typedef float fp_type;
  21. typedef boost::array<fp_type, 3> state_type;
  22. typedef std::vector<state_type> state_vec;
  23. //---------------------------------------------------------------------------
  24. struct roessler_system {
  25. const fp_type m_a, m_b, m_c;
  26. roessler_system(const fp_type a, const fp_type b, const fp_type c)
  27. : m_a(a), m_b(b), m_c(c)
  28. {}
  29. void operator()(const state_type &x, state_type &dxdt, const fp_type t) const
  30. {
  31. dxdt[0] = -x[1] - x[2];
  32. dxdt[1] = x[0] + m_a * x[1];
  33. dxdt[2] = m_b + x[2] * (x[0] - m_c);
  34. }
  35. };
  36. //---------------------------------------------------------------------------
  37. int main(int argc, char *argv[]) {
  38. if(argc<3)
  39. {
  40. std::cerr << "Expected size and steps as parameter" << std::endl;
  41. exit(1);
  42. }
  43. const size_t n = atoi(argv[1]);
  44. const size_t steps = atoi(argv[2]);
  45. //const size_t steps = 50;
  46. const fp_type dt = 0.01;
  47. const fp_type a = 0.2;
  48. const fp_type b = 1.0;
  49. const fp_type c = 9.0;
  50. // random initial conditions on the device
  51. std::vector<fp_type> x(n), y(n), z(n);
  52. std::default_random_engine generator;
  53. std::uniform_real_distribution<fp_type> distribution_xy(-8.0, 8.0);
  54. std::uniform_real_distribution<fp_type> distribution_z(0.0, 20.0);
  55. auto rand_xy = std::bind(distribution_xy, std::ref(generator));
  56. auto rand_z = std::bind(distribution_z, std::ref(generator));
  57. std::generate(x.begin(), x.end(), rand_xy);
  58. std::generate(y.begin(), y.end(), rand_xy);
  59. std::generate(z.begin(), z.end(), rand_z);
  60. state_vec state(n);
  61. for(size_t i=0; i<n; ++i)
  62. {
  63. state[i][0] = x[i];
  64. state[i][1] = y[i];
  65. state[i][2] = z[i];
  66. }
  67. std::cout.precision(16);
  68. std::cout << "# n: " << n << std::endl;
  69. std::cout << x[0] << std::endl;
  70. // Stepper type - use never_resizer for slight performance improvement
  71. odeint::runge_kutta4_classic<state_type, fp_type, state_type, fp_type,
  72. odeint::array_algebra,
  73. odeint::default_operations,
  74. odeint::never_resizer> stepper;
  75. roessler_system sys(a, b, c);
  76. timer_type timer;
  77. fp_type t = 0.0;
  78. for (int step = 0; step < steps; step++)
  79. {
  80. for(size_t i=0; i<n; ++i)
  81. {
  82. stepper.do_step(sys, state[i], t, dt);
  83. }
  84. t += dt;
  85. }
  86. std::cout << "Integration finished, runtime for " << steps << " steps: ";
  87. std::cout << timer.elapsed() << " s" << std::endl;
  88. // compute some accumulation to make sure all results have been computed
  89. fp_type s = 0.0;
  90. for(size_t i = 0; i < n; ++i)
  91. {
  92. s += state[i][0];
  93. }
  94. std::cout << state[0][0] << std::endl;
  95. std::cout << s/n << std::endl;
  96. }