lorenz_ensemble_simple.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Boost libs/numeric/odeint/examples/openmp/lorenz_ensemble_simple.cpp
  2. Copyright 2013 Karsten Ahnert
  3. Copyright 2013 Mario Mulansky
  4. Copyright 2013 Pascal Germroth
  5. Parallelized Lorenz ensembles
  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 <boost/numeric/odeint.hpp>
  14. #include <boost/numeric/odeint/external/openmp/openmp.hpp>
  15. #include "point_type.hpp"
  16. using namespace std;
  17. typedef vector<double> vector_type;
  18. typedef point<double, 3> point_type;
  19. typedef vector<point_type> state_type;
  20. const double sigma = 10.0;
  21. const double b = 8.0 / 3.0;
  22. struct sys_func {
  23. const vector_type &R;
  24. sys_func( const vector_type &_R ) : R( _R ) { }
  25. void operator()( const state_type &x , state_type &dxdt , double t ) const {
  26. const size_t n = x.size();
  27. # pragma omp parallel for
  28. for(size_t i = 0 ; i < n ; i++) {
  29. const point_type &xi = x[i];
  30. point_type &dxdti = dxdt[i];
  31. dxdti[0] = -sigma * (xi[0] - xi[1]);
  32. dxdti[1] = R[i] * xi[0] - xi[1] - xi[0] * xi[2];
  33. dxdti[2] = -b * xi[2] + xi[0] * xi[1];
  34. }
  35. }
  36. };
  37. int main() {
  38. using namespace boost::numeric::odeint;
  39. const size_t n = 1024;
  40. vector_type R(n);
  41. const double Rmin = 0.1, Rmax = 50.0;
  42. # pragma omp parallel for
  43. for(size_t i = 0 ; i < n ; i++)
  44. R[i] = Rmin + (Rmax - Rmin) / (n - 1) * i;
  45. state_type X(n, point_type(10, 10, 10));
  46. const double t_max = 10.0, dt = 0.01;
  47. // Simple stepper with constant step size
  48. // typedef runge_kutta4<state_type, double, state_type, double,
  49. // openmp_range_algebra> stepper;
  50. // integrate_const(stepper(), sys_func(R), X, 0.0, t_max, dt);
  51. // Controlled stepper with variable step size
  52. typedef runge_kutta_fehlberg78<state_type, double, state_type, double,
  53. openmp_range_algebra> error_stepper_type;
  54. typedef controlled_runge_kutta<error_stepper_type> controlled_stepper_type;
  55. controlled_stepper_type controlled_stepper;
  56. integrate_adaptive(controlled_stepper, sys_func(R), X, 0.0, t_max, dt);
  57. copy( X.begin(), X.end(), ostream_iterator<point_type>(cout, "\n") );
  58. return 0;
  59. }