Brian Silverman | 7c33ab2 | 2018-08-04 17:14:51 -0700 | [diff] [blame^] | 1 | /* 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 | |
| 23 | using namespace std; |
| 24 | using namespace boost::numeric::odeint; |
| 25 | |
| 26 | typedef point<double, 3> point_type; |
| 27 | typedef vector< point_type > inner_state_type; |
| 28 | typedef openmp_state<point_type> state_type; |
| 29 | |
| 30 | const double sigma = 10.0; |
| 31 | const double b = 8.0 / 3.0; |
| 32 | |
| 33 | |
| 34 | struct 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 | |
| 57 | int 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 | } |