Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5.  
  6. typedef struct {
  7. double x, y, z;
  8. } Point;
  9.  
  10.  
  11. typedef struct {
  12. double mass;
  13. Point position, velocity, acceleration;
  14. } Particle;
  15.  
  16.  
  17. const double G = 6.6742367e-11;
  18.  
  19. void integrator_leapfrog_part1(int n_particles, Particle* particles, double half_time_step){
  20. for (int i = 0; i < n_particles; i++) {
  21. particles[i].position.x += half_time_step * particles[i].velocity.x;
  22. particles[i].position.y += half_time_step * particles[i].velocity.y;
  23. particles[i].position.z += half_time_step * particles[i].velocity.z;
  24. }
  25. }
  26.  
  27.  
  28. void integrator_leapfrog_part2(int n_particles, Particle* particles, double time_step, double half_time_step){
  29. for (int i = 0; i < n_particles; i++) {
  30. particles[i].velocity.x += time_step * particles[i].acceleration.x;
  31. particles[i].velocity.y += time_step * particles[i].acceleration.y;
  32. particles[i].velocity.z += time_step * particles[i].acceleration.z;
  33.  
  34. particles[i].position.x += half_time_step * particles[i].velocity.x;
  35. particles[i].position.y += half_time_step * particles[i].velocity.y;
  36. particles[i].position.z += half_time_step * particles[i].velocity.z;
  37. }
  38. }
  39.  
  40.  
  41. void gravity_calculate_acceleration(int n_particles, Particle* particles) {
  42. for (int i = 0; i < n_particles; i++) {
  43. particles[i].acceleration.x = 0.0;
  44. particles[i].acceleration.y = 0.0;
  45. particles[i].acceleration.z = 0.0;
  46. for (int j = 0; j < n_particles; j++) {
  47. if (j == i) {
  48. continue;
  49. }
  50. double dx = particles[i].position.x - particles[j].position.x;
  51. double dy = particles[i].position.y - particles[j].position.y;
  52. double dz = particles[i].position.z - particles[j].position.z;
  53. double r = sqrt(dx*dx + dy*dy + dz*dz);
  54. double prefact = -G/(r*r*r) * particles[j].mass;
  55. particles[i].acceleration.x += prefact * dx;
  56. particles[i].acceleration.y += prefact * dy;
  57. particles[i].acceleration.z += prefact * dz;
  58. }
  59. }
  60. }
  61.  
  62.  
  63. int main() {
  64.  
  65. double time = 0.0;
  66. double time_step = 0.08;
  67. double half_time_step = time_step/2;
  68. double time_limit = 365.25 * 1e4;
  69.  
  70. Particle star = {
  71. .mass = 0.08,
  72. .position = {.x = 0.0, .y = 0.0, .z = 0.0},
  73. .velocity = {.x = 0.0, .y = 0.0, .z = 0.0},
  74. .acceleration = {.x = 0.0, .y = 0.0, .z = 0.0}
  75. };
  76. Particle planet = {
  77. .mass = 3.0e-6,
  78. .position = {.x = 0.0162, .y = 6.57192058353e-15, .z = 5.74968548652e-16},
  79. .velocity = {.x = -1.48427302304e-14, .y = 0.0399408809121, .z = 0.00349437429104},
  80. .acceleration = {.x = 0.0, .y = 0.0, .z = 0.0},
  81. };
  82.  
  83. int num_particles = 2;
  84. Particle* particles = malloc(2*sizeof(Particle));
  85. particles[0] = star;
  86. particles[1] = planet;
  87.  
  88. while (time < time_limit) {
  89. integrator_leapfrog_part1(num_particles, particles, half_time_step);
  90. time += half_time_step;
  91. gravity_calculate_acceleration(num_particles, particles);
  92. integrator_leapfrog_part2(num_particles, particles, time_step, half_time_step);
  93. time += half_time_step;
  94. }
  95.  
  96. printf("%f", particles[1].position.x);
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement