2 * Simulation of an ensemble of Roessler attractors
4 * Copyright 2014 Mario Mulansky
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)
17 #include <boost/timer.hpp>
18 #include <boost/array.hpp>
20 #include <boost/numeric/odeint.hpp>
22 namespace odeint
= boost::numeric::odeint
;
24 typedef boost::timer timer_type
;
26 typedef double fp_type
;
27 //typedef float fp_type;
29 typedef boost::array
<fp_type
, 3> state_type
;
30 typedef std::vector
<state_type
> state_vec
;
32 //---------------------------------------------------------------------------
33 struct roessler_system
{
34 const fp_type m_a
, m_b
, m_c
;
36 roessler_system(const fp_type a
, const fp_type b
, const fp_type c
)
37 : m_a(a
), m_b(b
), m_c(c
)
40 void operator()(const state_type
&x
, state_type
&dxdt
, const fp_type t
) const
42 dxdt
[0] = -x
[1] - x
[2];
43 dxdt
[1] = x
[0] + m_a
* x
[1];
44 dxdt
[2] = m_b
+ x
[2] * (x
[0] - m_c
);
48 //---------------------------------------------------------------------------
49 int main(int argc
, char *argv
[]) {
52 std::cerr
<< "Expected size and steps as parameter" << std::endl
;
55 const size_t n
= atoi(argv
[1]);
56 const size_t steps
= atoi(argv
[2]);
57 //const size_t steps = 50;
59 const fp_type dt
= 0.01;
61 const fp_type a
= 0.2;
62 const fp_type b
= 1.0;
63 const fp_type c
= 9.0;
65 // random initial conditions on the device
66 std::vector
<fp_type
> x(n
), y(n
), z(n
);
67 std::default_random_engine generator
;
68 std::uniform_real_distribution
<fp_type
> distribution_xy(-8.0, 8.0);
69 std::uniform_real_distribution
<fp_type
> distribution_z(0.0, 20.0);
70 auto rand_xy
= std::bind(distribution_xy
, std::ref(generator
));
71 auto rand_z
= std::bind(distribution_z
, std::ref(generator
));
72 std::generate(x
.begin(), x
.end(), rand_xy
);
73 std::generate(y
.begin(), y
.end(), rand_xy
);
74 std::generate(z
.begin(), z
.end(), rand_z
);
77 for(size_t i
=0; i
<n
; ++i
)
84 std::cout
.precision(16);
86 std::cout
<< "# n: " << n
<< std::endl
;
88 std::cout
<< x
[0] << std::endl
;
91 // Stepper type - use never_resizer for slight performance improvement
92 odeint::runge_kutta4_classic
<state_type
, fp_type
, state_type
, fp_type
,
93 odeint::array_algebra
,
94 odeint::default_operations
,
95 odeint::never_resizer
> stepper
;
97 roessler_system
sys(a
, b
, c
);
103 for (int step
= 0; step
< steps
; step
++)
105 for(size_t i
=0; i
<n
; ++i
)
107 stepper
.do_step(sys
, state
[i
], t
, dt
);
112 std::cout
<< "Integration finished, runtime for " << steps
<< " steps: ";
113 std::cout
<< timer
.elapsed() << " s" << std::endl
;
115 // compute some accumulation to make sure all results have been computed
117 for(size_t i
= 0; i
< n
; ++i
)
122 std::cout
<< state
[0][0] << std::endl
;
123 std::cout
<< s
/n
<< std::endl
;