blob: 6717a505b500825c3a4977acb11be9a4f2caddfa [file] [log] [blame]
Brian Silverman7c33ab22018-08-04 17:14:51 -07001/* Boost libs/numeric/odeint/examples/openmp/lorenz_ensemble.cpp
2
3 Copyright 2013 Karsten Ahnert
4 Copyright 2013 Mario Mulansky
5 Copyright 2013 Pascal Germroth
6
7 Parallelized Lorenz ensembles
8
9 Distributed under the Boost Software License, Version 1.0.
10(See accompanying file LICENSE_1_0.txt or
11 copy at http://www.boost.org/LICENSE_1_0.txt)
12 */
13
14#include <omp.h>
15#include <vector>
16#include <iostream>
17#include <iterator>
18#include <boost/numeric/odeint.hpp>
19#include <boost/numeric/odeint/external/openmp/openmp.hpp>
20#include <boost/lexical_cast.hpp>
21#include "point_type.hpp"
22
23using namespace std;
24using namespace boost::numeric::odeint;
25
26typedef point<double, 3> point_type;
27typedef vector< point_type > inner_state_type;
28typedef openmp_state<point_type> state_type;
29
30const double sigma = 10.0;
31const double b = 8.0 / 3.0;
32
33
34struct sys_func {
35 const vector<double> &R;
36 sys_func( vector<double> &R ) : R(R) {}
37
38 void operator()( const state_type &x , state_type &dxdt , double t ) const {
39# pragma omp parallel for
40 for(size_t j = 0 ; j < x.size() ; j++) {
41 size_t offset = 0;
42 for(size_t i = 0 ; i < j ; i++)
43 offset += x[i].size();
44
45 for(size_t i = 0 ; i < x[j].size() ; i++) {
46 const point_type &xi = x[j][i];
47 point_type &dxdti = dxdt[j][i];
48 dxdti[0] = -sigma * (xi[0] - xi[1]);
49 dxdti[1] = R[offset + i] * xi[0] - xi[1] - xi[0] * xi[2];
50 dxdti[2] = -b * xi[2] + xi[0] * xi[1];
51 }
52 }
53 }
54};
55
56
57int main(int argc, char **argv) {
58 size_t n = 1024;
59 if(argc > 1) n = boost::lexical_cast<size_t>(argv[1]);
60
61 vector<double> R(n);
62 const double Rmin = 0.1, Rmax = 50.0;
63# pragma omp parallel for
64 for(size_t i = 0 ; i < n ; i++)
65 R[i] = Rmin + (Rmax - Rmin) / (n - 1) * i;
66
67 vector<point_type> inner(n, point_type(10, 10, 10));
68 state_type state;
69 split(inner, state);
70
71 cerr << "openmp_state split " << n << " into";
72 for(size_t i = 0 ; i != state.size() ; i++)
73 cerr << ' ' << state[i].size();
74 cerr << endl;
75
76 typedef runge_kutta4< state_type, double > stepper;
77
78 const double t_max = 10.0, dt = 0.01;
79
80 integrate_const(
81 stepper(),
82 sys_func(R),
83 state,
84 0.0, t_max, dt
85 );
86
87 unsplit(state, inner);
88 std::copy( inner.begin(), inner.end(), ostream_iterator<point_type>(cout, "\n") );
89
90 return 0;
91}