blob: 4e6cc4229ce64b3ae59db32ecd8a9291895e4595 [file] [log] [blame]
Brian Silverman7c33ab22018-08-04 17:14:51 -07001/*
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
12
13#include <iostream>
14#include <vector>
15#include <random>
16
17#include <boost/timer.hpp>
18#include <boost/array.hpp>
19
20#include <boost/numeric/odeint.hpp>
21
22namespace odeint = boost::numeric::odeint;
23
24typedef boost::timer timer_type;
25
26typedef double fp_type;
27//typedef float fp_type;
28
29typedef boost::array<fp_type, 3> state_type;
30typedef std::vector<state_type> state_vec;
31
32//---------------------------------------------------------------------------
33struct roessler_system {
34 const fp_type m_a, m_b, m_c;
35
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)
38 {}
39
40 void operator()(const state_type &x, state_type &dxdt, const fp_type t) const
41 {
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);
45 }
46};
47
48//---------------------------------------------------------------------------
49int main(int argc, char *argv[]) {
50if(argc<3)
51{
52 std::cerr << "Expected size and steps as parameter" << std::endl;
53 exit(1);
54}
55const size_t n = atoi(argv[1]);
56const size_t steps = atoi(argv[2]);
57//const size_t steps = 50;
58
59const fp_type dt = 0.01;
60
61const fp_type a = 0.2;
62const fp_type b = 1.0;
63const fp_type c = 9.0;
64
65// random initial conditions on the device
66std::vector<fp_type> x(n), y(n), z(n);
67std::default_random_engine generator;
68std::uniform_real_distribution<fp_type> distribution_xy(-8.0, 8.0);
69std::uniform_real_distribution<fp_type> distribution_z(0.0, 20.0);
70auto rand_xy = std::bind(distribution_xy, std::ref(generator));
71auto rand_z = std::bind(distribution_z, std::ref(generator));
72std::generate(x.begin(), x.end(), rand_xy);
73std::generate(y.begin(), y.end(), rand_xy);
74std::generate(z.begin(), z.end(), rand_z);
75
76state_vec state(n);
77for(size_t i=0; i<n; ++i)
78{
79 state[i][0] = x[i];
80 state[i][1] = y[i];
81 state[i][2] = z[i];
82}
83
84std::cout.precision(16);
85
86std::cout << "# n: " << n << std::endl;
87
88std::cout << x[0] << std::endl;
89
90
91// Stepper type - use never_resizer for slight performance improvement
92odeint::runge_kutta4_classic<state_type, fp_type, state_type, fp_type,
93 odeint::array_algebra,
94 odeint::default_operations,
95 odeint::never_resizer> stepper;
96
97roessler_system sys(a, b, c);
98
99timer_type timer;
100
101fp_type t = 0.0;
102
103for (int step = 0; step < steps; step++)
104{
105 for(size_t i=0; i<n; ++i)
106 {
107 stepper.do_step(sys, state[i], t, dt);
108 }
109 t += dt;
110}
111
112std::cout << "Integration finished, runtime for " << steps << " steps: ";
113std::cout << timer.elapsed() << " s" << std::endl;
114
115// compute some accumulation to make sure all results have been computed
116fp_type s = 0.0;
117for(size_t i = 0; i < n; ++i)
118{
119 s += state[i][0];
120}
121
122std::cout << state[0][0] << std::endl;
123std::cout << s/n << std::endl;
124
125}