Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. // NewtonianC.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdlib.h>
  6. #include <../glut-3.7.6-bin/glut.h>
  7. #include <math.h>
  8. #include <conio.h>
  9. #include <omp.h>
  10.  
  11. #define G 6.67428E-11
  12. #define TIMESTEP 250
  13. #define INTERVAL 20
  14. #define DIMENSION 400
  15. #define ITERATION 1000
  16. #define SCALING_FACTOR 10000
  17.  
  18. typedef struct ParticleProperty {
  19.  
  20. unsigned char r;
  21. unsigned char g;
  22. unsigned char b;
  23. unsigned char a;
  24. }Property;
  25.  
  26. int SIZE;
  27. int loop = 0;
  28.  
  29. double * x;
  30. double * y;
  31.  
  32. Property * property;
  33.  
  34. /*
  35. void timer(int value) {
  36.  
  37. int i = loop * SIZE;
  38. int bound = (loop +1) * SIZE;
  39. for (i ; i < bound; i++) {
  40.  
  41. //particlesToDisplay[i*2] = particle[i].x * SCALING_FACTOR;
  42. //particlesToDisplay[i*2 +1] = particle[i].y * SCALING_FACTOR;
  43. }
  44.  
  45. loop++;
  46. glutPostRedisplay();
  47. glutTimerFunc(INTERVAL, timer, 1);
  48. }
  49.  
  50. void display(void) {
  51.  
  52. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  53.  
  54. glMatrixMode(GL_PROJECTION);
  55. glLoadIdentity();
  56. glOrtho(-DIMENSION, DIMENSION, -DIMENSION, DIMENSION, -1, 1);
  57.  
  58. glMatrixMode(GL_MODELVIEW);
  59. glLoadIdentity();
  60.  
  61. glColor3ub(255, 255, 255);
  62. glEnableClientState(GL_VERTEX_ARRAY);
  63. glEnableClientState(GL_COLOR_ARRAY);
  64. glVertexPointer(2, GL_DOUBLE, sizeof(Particle), &particlesToDisplay[0]);
  65. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Particle), &particlesToDisplay[0]);
  66. glPointSize(3.0);
  67. glDrawArrays(GL_POINTS, 0, SIZE);
  68. glDisableClientState(GL_VERTEX_ARRAY);
  69. glDisableClientState(GL_COLOR_ARRAY);
  70. glFlush();
  71. glutSwapBuffers();
  72. }
  73. */
  74. int main(int argc, char *argv[]) {
  75. /*
  76. glutInit(&argc, argv);
  77. glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
  78. glutInitWindowSize(800, 600);
  79. glutCreateWindow("N-Body Simulation");
  80.  
  81. glutDisplayFunc(display);
  82. glutTimerFunc(INTERVAL, timer, 1);
  83. */
  84. int i;
  85. int j;
  86. int k;
  87.  
  88. double ax;
  89. double ay;
  90. double rx;
  91. double ry;
  92. double r_square;
  93.  
  94. double *mass;
  95. double *ux;
  96. double *uy;
  97.  
  98. FILE *infile;
  99. FILE *outfile;
  100.  
  101. fopen_s(&infile, "input.txt", "r");
  102.  
  103. fscanf_s(infile, "%d", &SIZE);
  104.  
  105. property = (Property*)malloc(SIZE * sizeof(Property));
  106. mass = (double*)malloc(SIZE * sizeof(double));
  107. ux = (double*)malloc(SIZE * sizeof(double));
  108. uy = (double*)malloc(SIZE * sizeof(double));
  109.  
  110. x = (double*)malloc(SIZE * ITERATION * sizeof(double));
  111. y = (double*)malloc(SIZE * ITERATION * sizeof(double));
  112.  
  113. // Set up the particle properties
  114. for (i = 0; i<SIZE; i++) {
  115.  
  116. fscanf_s(infile, "%lf %lf %lf", &x[i], &y[i], &mass[i]);
  117.  
  118. property[i].r = rand() % 255;
  119. property[i].g = rand() % 255;
  120. property[i].b = rand() % 255;
  121. property[i].a = rand() % 100 + 125;
  122.  
  123. ux[i] = 0;
  124. uy[i] = 0;
  125. }
  126.  
  127. // Simulation Data
  128.  
  129. #pragma omp parallel for private()
  130.  
  131. for (i = 1; i < ITERATION; i++) {
  132.  
  133. for (j = SIZE; j--;) {
  134.  
  135. ax = 0;
  136. ay = 0;
  137.  
  138. for (k = SIZE; k--;) {
  139.  
  140. if (j == k) continue;
  141.  
  142. rx = x[SIZE*i + k] - x[SIZE*i + j];
  143. ry = y[SIZE*i + k] - y[SIZE*i + j];
  144. r_square = rx*rx + ry*ry;
  145.  
  146. ax += G * mass[j] * rx / r_square;
  147. ay += G * mass[j] * ry / r_square;
  148. }
  149.  
  150. x[SIZE*i + j] += (ux[j] + ax * TIMESTEP / 2) * TIMESTEP;
  151. y[SIZE*i + j] += (uy[j] + ay * TIMESTEP / 2) * TIMESTEP;
  152.  
  153. ux[j] += ax * TIMESTEP;
  154. uy[j] += ay * TIMESTEP;
  155. }
  156. }
  157.  
  158. //glutMainLoop();
  159.  
  160. printf_s("finished");
  161. _getch();
  162.  
  163. free(x);
  164. free(y);
  165. free(ux);
  166. free(uy);
  167. free(mass);
  168.  
  169. return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement