lorenz_ensemble_nested.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Boost libs/numeric/odeint/examples/openmp/lorenz_ensemble_nested.cpp
  2. Copyright 2013 Karsten Ahnert
  3. Copyright 2013 Pascal Germroth
  4. Copyright 2013 Mario Mulansky
  5. Parallelized Lorenz ensembles using nested omp algebra
  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. #include <omp.h>
  11. #include <vector>
  12. #include <iostream>
  13. #include <iterator>
  14. #include <boost/numeric/odeint.hpp>
  15. #include <boost/numeric/odeint/external/openmp/openmp.hpp>
  16. #include <boost/lexical_cast.hpp>
  17. #include "point_type.hpp"
  18. using namespace std;
  19. using namespace boost::numeric::odeint;
  20. typedef point<double, 3> point_type;
  21. typedef vector< point_type > state_type;
  22. const double sigma = 10.0;
  23. const double b = 8.0 / 3.0;
  24. struct sys_func {
  25. const vector<double> &R;
  26. sys_func( vector<double> &R ) : R(R) {}
  27. void operator()( const state_type &x , state_type &dxdt , double t ) const {
  28. # pragma omp parallel for
  29. for(size_t i = 0 ; i < x.size() ; i++) {
  30. dxdt[i][0] = -sigma * (x[i][0] - x[i][1]);
  31. dxdt[i][1] = R[i] * x[i][0] - x[i][1] - x[i][0] * x[i][2];
  32. dxdt[i][2] = -b * x[i][2] + x[i][0] * x[i][1];
  33. }
  34. }
  35. };
  36. int main(int argc, char **argv) {
  37. size_t n = 1024;
  38. if(argc > 1) n = boost::lexical_cast<size_t>(argv[1]);
  39. vector<double> R(n);
  40. const double Rmin = 0.1, Rmax = 50.0;
  41. # pragma omp parallel for
  42. for(size_t i = 0 ; i < n ; i++)
  43. R[i] = Rmin + (Rmax - Rmin) / (n - 1) * i;
  44. state_type state( n , point_type(10, 10, 10) );
  45. typedef runge_kutta4< state_type, double , state_type , double ,
  46. openmp_nested_algebra<vector_space_algebra> > stepper;
  47. const double t_max = 10.0, dt = 0.01;
  48. integrate_const(
  49. stepper(),
  50. sys_func(R),
  51. state,
  52. 0.0, t_max, dt
  53. );
  54. std::copy( state.begin(), state.end(), ostream_iterator<point_type>(cout, "\n") );
  55. return 0;
  56. }