Guest User

Untitled

a guest
Jul 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <vector>
  4.  
  5. #include <boost/math/constants/constants.hpp>
  6. using boost::math::constants::two_pi;
  7.  
  8.  
  9. class Particle {
  10. public:
  11. Particle() = delete;
  12. Particle(float q, float p)
  13. : q(q)
  14. , p(p)
  15. {}
  16.  
  17. float q;
  18. float p;
  19. };
  20.  
  21. int main(int argc, char** argv)
  22. {
  23. auto rng = std::mt19937(std::random_device{}());
  24. auto normdist = std::normal_distribution<float>(0, 1);
  25.  
  26. auto fs = 1e3f;
  27. auto tstep = 0.5e-6f;
  28. const auto angle = (fs*tstep)*two_pi<float>();
  29.  
  30. auto tau = 0.001;
  31.  
  32. auto tmax = 0.01f;
  33.  
  34. uint32_t timax = std::ceil(tmax/tstep);
  35.  
  36. auto nparts = 1000;
  37.  
  38. std::vector<Particle> pvec;
  39.  
  40. auto E_p2 = 0.0f;
  41. auto Ep = 0.0f;
  42.  
  43. std::cout << "time" << " "
  44. << "periods" << " "
  45. << "particle" << " "
  46. << "position" << " "
  47. << "length" << std::endl;
  48.  
  49. for (auto i=0; i<nparts; i++) {
  50. auto q = 2*normdist(rng);
  51. auto p = 1+normdist(rng);
  52. pvec.push_back(Particle(q,p));
  53. Ep += p;
  54. E_p2 += p*p;
  55. }
  56. E_p2 /= nparts;
  57. Ep /= nparts;
  58. std::cout << "0" << " "
  59. << "0" << " "
  60. << pvec[0].p << " "
  61. << Ep << " "
  62. << std::sqrt(E_p2-Ep*Ep) << std::endl;
  63.  
  64. for (uint32_t ti=1; ti<=timax; ti++) {
  65. E_p2 = 0;
  66. for (auto &p : pvec) {
  67. auto p_tmp = p.q * std::sin(angle) + p.p * std::cos(angle);
  68. auto q_tmp = p.q * std::cos(angle) - p.p * std::sin(angle);
  69. p.p = p_tmp;
  70. p.q = q_tmp;
  71. p.p -= p.p*2*tstep/(tau)+2*std::sqrt(tstep/tau)*normdist(rng);
  72. Ep += p_tmp;
  73. E_p2 += p_tmp*p_tmp;
  74. }
  75. E_p2 /= nparts;
  76. Ep /= nparts;
  77. std::cout << ti*tstep << " "
  78. << ti*tstep*fs << " "
  79. << pvec[0].p << " "
  80. << Ep << " "
  81. << std::sqrt(E_p2-Ep*Ep) << std::endl;
  82. }
  83.  
  84. return EXIT_SUCCESS;
  85. }
Add Comment
Please, Sign In to add comment