Advertisement
phystota

DRR_ver0.0

May 22nd, 2025
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <fstream>
  4. #include <assert.h>
  5.  
  6. #include <math.h>
  7. #include <iomanip>  // For std::fixed and std::setprecision
  8.  
  9.  
  10.  
  11.  
  12. int main() {
  13.  
  14. double k1b, k1f, k2b, k2f;
  15. double n0, ne;
  16. double error;
  17.  
  18. double dt;
  19. double time;
  20.  
  21. int steps;
  22.  
  23.  
  24.  
  25. error = 1.0E-3;
  26. k1f = 2.24957*1.0E-10; k1b = 4.34413*1.0E-10;
  27. k2f = 7.10391*1.0E-11; k2b = 1.36891*1.0E-9;
  28. ne = 1.0E+15; n0 = 1.0E+20;
  29. dt = 1.0E-8; time = 1.0E-1;
  30.  
  31. steps = static_cast<int>(time/dt);
  32.  
  33. std::vector<std::array<double, 3>> n(steps);
  34.  
  35. //initial condition handling
  36. n[0][0] = n0; n[0][1] = 0.0; n[0][2] = 0.0;
  37.  
  38. for (int i = 1; i < steps; i++) {
  39.    
  40.     n[i][0] = n[i-1][0] + dt*(-(k1f+k2f)*n[i-1][0] + k1b*n[i-1][1] + k2b*n[i-1][2])*ne;
  41.     n[i][1] = n[i-1][1] + dt*(k1f*n[i-1][0] - k1b*n[i-1][1])*ne;
  42.     n[i][2] = n[i-1][2] + dt*(k2f*n[i-1][0] - k2b*n[i-1][2])*ne;
  43.  
  44.     std::cout << n[i][0] << " " << n[i][1] << " " << n[i][2] << "\n";
  45.  
  46.     double rel0, rel1, rel2;
  47.     rel0 = n[i][0]/n[i-1][0]; rel1 = n[i][1]/n[i-1][1]; rel2 = n[i][2]/n[i-1][2];
  48.  
  49.     if (std::abs(n[i-1][1]) < 1e-30) rel1 = 1.0;
  50.     if (std::abs(n[i-1][2]) < 1e-30) rel2 = 1.0;
  51.  
  52.     if ((std::abs(rel0) < error) && (std::abs(rel1) < error) && (std::abs(rel2) < error)) {
  53.         std::cout << "Accuracy reached at step: " << i << "\n";
  54.         break;
  55.     }
  56. }
  57.  
  58.  
  59. return 0;
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement