Advertisement
azakharov93

two_objects_simple_task

Dec 3rd, 2022
341
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | Science | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. using Time = double;
  8.  
  9. // Number of seconds in one day
  10. const Time DefaultTimeStep{ 24.0 * 60 * 60 };
  11. // Gravitational Constant from the Newtons law
  12. const double G{ 6.67e-11 };
  13. // Mass of the Sun
  14. const double SunMass{ 2.0e30 };
  15. // Mass of the Earth
  16. const double EarthMass{ 5.972e24 };
  17. // Distance between Earth and Sun
  18. const double EarthToSunDistance{ 1.5e11 };
  19. // Velocity of Earth
  20. const double EarthVelocity{ 29290 };
  21.  
  22. struct Position
  23. {
  24.   double x{ 0. };
  25.   double y{ 0. };
  26. };
  27.  
  28. struct Vector {
  29.   double x{ 0. };
  30.   double y{ 0. };
  31.  
  32.   double abs() const {
  33.     return std::sqrt(x * x + y * y);
  34.   }
  35. };
  36.  
  37. // Calculates the difference vector between pair of points in 2D space
  38. Vector calculateDifference(const Position from, const Position to)
  39. {
  40.   Vector difference;
  41.   difference.x = to.x - from.x;
  42.   difference.y = to.y - from.y;
  43.  
  44.   return difference;
  45. }
  46.  
  47. // Calculates the distance between pair of points in 2D space
  48. double calculateDistance(const Position from, const Position to)
  49. {
  50.   Vector direction;
  51.   direction.x = to.x - from.x;
  52.   direction.y = to.y - from.y;
  53.  
  54.   return direction.abs();
  55. }
  56.  
  57. struct SpaceBody
  58. {
  59.   double mass{ 0. };
  60.   Position position;
  61.   Vector velocity;
  62.   // Stores position of the space object on each iteration
  63.   vector<Position> pathway;
  64.  
  65.   void updatePosition(const SpaceBody& other, const Time dt)
  66.   {
  67.     const Vector direction = calculateDifference(position, other.position);
  68.     const double distance = calculateDistance(position, other.position);
  69.  
  70.     // Fx = G * m * M * x / r^3 , but here we divide on mass as well, because it will go :)
  71.     // Pay attention that this is "x" in numerator and "r^3" in denominator
  72.     // (not r^2 - but r^3 because in the numerator we multiply on "x" to find the direction of force action)
  73.     const double acceleration_x = G * other.mass * direction.x / (distance * distance * distance);
  74.     const double acceleration_y = G * other.mass * direction.y / (distance * distance * distance);
  75.  
  76.     // dv/dt = a -> dv = a * dt
  77.     velocity.x += acceleration_x * dt;
  78.     velocity.y += acceleration_y * dt;
  79.  
  80.     // dx/dt = v -> dx = v * dt
  81.     position.x += velocity.x * dt;
  82.     position.y += velocity.y * dt;
  83.  
  84.     pathway.push_back(position);
  85.   }
  86. };
  87.  
  88. // Set all required physical values for the sun
  89. void initSun(SpaceBody& sun)
  90. {
  91.   sun.mass = SunMass;
  92.  
  93.   sun.position.x = 0;
  94.   sun.position.y = 0;
  95.  
  96.   sun.velocity.x = 0;
  97.   sun.velocity.y = 0;
  98. }
  99.  
  100. // Set all required physical values for the earth
  101. void initEarth(SpaceBody& earth)
  102. {
  103.   earth.mass = SunMass;
  104.  
  105.   earth.position.x = 0;
  106.   earth.position.y = EarthToSunDistance;
  107.  
  108.   earth.velocity.x = EarthVelocity;
  109.   earth.velocity.y = 0;
  110. }
  111.  
  112. // !!! OPTIONAL FUNCTION ONLY FOR VISUALIZATION !!! YOU COULD REMOVE IT WITH THE CORRESPONDING CODE IN main() FUNCTION
  113. void printData(const std::vector<Position>& sun_path, const std::vector<Position>& earth_path)
  114. {
  115.   std::ofstream out("planets_pathway.csv");
  116.   out << "sun_x" << "," << "sun_y" << "," << "earth_x" << "," << "earth_y" << '\n';
  117.   for (int id = 0; id < sun_path.size(); ++id)
  118.     out << sun_path[id].x << "," << sun_path[id].y << "," << earth_path[id].x << "," << earth_path[id].y << '\n';
  119. }
  120.  
  121. int main()
  122. {
  123.   SpaceBody sun;
  124.   initSun(sun);
  125.  
  126.   SpaceBody earth;
  127.   initEarth(earth);
  128.  
  129.   int simulation_days{ 0 };
  130.   std::cout << "Input number of days for the simulation: ";
  131.   std::cin >> simulation_days;
  132.  
  133.   try
  134.   {
  135.     for (int day = 0; day < simulation_days; ++day)
  136.     {
  137.       earth.updatePosition(sun, DefaultTimeStep);
  138.       sun.updatePosition(earth, DefaultTimeStep);
  139.     }
  140.  
  141.   }
  142.   catch (const exception& error)
  143.   {
  144.     cout << "Error during simulation: " << error.what() << endl;
  145.   }
  146.  
  147.   printData(sun.pathway, earth.pathway);
  148.  
  149.   return 0;
  150. }
  151.  
Advertisement
Comments
  • azakharov93
    1 year
    # Python 0.49 KB | 0 0
    1. # How to print the orbits
    2. import pandas as pd
    3. import matplotlib.pyplot as plt
    4.  
    5. path = r"D:\other\tom_and_jerry\planet_motion\planets_pathway.csv"
    6.  
    7. if __name__ == '__main__':
    8.     df =pd.read_csv(path)
    9.  
    10.     ax = df.plot(x="sun_x", y="sun_y", color='C3', label='sun')
    11.     df.plot(x="earth_x", y="earth_y", color='C0', label='earth', ax=ax)
    12.  
    13.     plt.xlabel('X-coordinate')
    14.     plt.ylabel('Y-coordinate')
    15.     plt.grid(True)
    16.     plt.title('Rotation of Earth Around the Sun')
    17.     plt.show()
Add Comment
Please, Sign In to add comment
Advertisement