Advertisement
Guest User

Untitled

a guest
Mar 6th, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. #include <fcntl.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <unistd.h>
  8.  
  9. #define eps 1e-3
  10.  
  11. int readfile(char *filename, void *buf, int count, int size) {
  12. int inputfd = open(filename, O_RDONLY);
  13. if (inputfd == -1) {
  14. printf("File open error!\n");
  15. return 1;
  16. }
  17.  
  18. int expected = count * size;
  19. int result = read(inputfd, buf, expected);
  20. close(inputfd);
  21.  
  22. if (result != expected) {
  23. printf("File read error: expected %d bytes but read %d bytes!\n", expected,
  24. result);
  25. return 1;
  26. }
  27. return 0;
  28. }
  29.  
  30. int writefile(char *filename, void *buf, int count, int size) {
  31. int outputfd = open(filename, O_RDWR | O_CREAT, 0666);
  32. if (outputfd == -1) {
  33. printf("File create error!\n");
  34. return 1;
  35. }
  36.  
  37. int expected = count * size;
  38. int result = write(outputfd, buf, expected);
  39. close(outputfd);
  40.  
  41. if (result != expected) {
  42. printf("File write error: expected %d bytes but wrote %d bytes!\n",
  43. expected, result);
  44. return 1;
  45. }
  46. return 0;
  47. }
  48.  
  49. int main(int ac, char *av[]) {
  50. int N;
  51. char *inputfile;
  52. int timesteps;
  53. double deltaT;
  54.  
  55. if (ac != 6) {
  56. printf("Using default arguments '3000 ../../nbody_input.gal 100 1e-5 0'\n");
  57. // printf("Correct Format: %s N inputfile timesteps deltaT graphics\n",
  58. // av[0]);
  59. N = 3000;
  60. inputfile = "../../nbody_input.gal";
  61. timesteps = 100;
  62. deltaT = 1e-5;
  63. } else {
  64. N = atoi(av[1]);
  65. inputfile = av[2];
  66. timesteps = atoi(av[3]);
  67. deltaT = atof(av[4]);
  68. }
  69.  
  70. const double G = 100.0 / N;
  71.  
  72. clock_t start;
  73. start = clock();
  74.  
  75. // Phase 1: File to Datastucture
  76.  
  77. typedef struct {
  78. double x;
  79. double y;
  80. double m;
  81. double vx;
  82. double vy;
  83. double pad[1];
  84. } part;
  85.  
  86. typedef struct {
  87. double ax;
  88. double ay;
  89. } accel;
  90.  
  91. part *parts = (part *)malloc(N * sizeof(part));
  92. accel *accels = (accel *)malloc(N * sizeof(accel));
  93.  
  94. printf("Reading input...\n");
  95. int err = readfile(inputfile, parts, N, sizeof(part));
  96. if (err != 0) {
  97. return err;
  98. }
  99.  
  100. printf("Time for File to DS(s): %lf\n",
  101. (double)(clock() - start) / CLOCKS_PER_SEC);
  102. start = clock();
  103.  
  104. // Phase 2: Processing
  105. int instant = 0;
  106. while (instant < timesteps) {
  107. memset(accels, 0, N * sizeof(accel));
  108. for (int row = 0; row < N; row++) {
  109. double mi = parts[row].m;
  110. double x = parts[row].x;
  111. double y = parts[row].y;
  112. double ax = 0.0;
  113. double ay = 0.0;
  114.  
  115. for (int col = row + 1; col < N; col++) {
  116. double dx = x - parts[col].x;
  117. double dy = y - parts[col].y;
  118. double mj = parts[col].m;
  119. double rij = sqrt(dx * dx + dy * dy);
  120. double rije = rij + eps;
  121. double radius = rije * rije * rije;
  122. double force = G / radius;
  123.  
  124. // Optimization: Newton's Third Law
  125. double fx = force * dx;
  126. double fy = force * dy;
  127. ax -= fx * mj;
  128. ay -= fy * mj;
  129. accels[col].ax += fx * mi;
  130. accels[col].ay += fy * mi;
  131. }
  132.  
  133. accels[row].ax += ax;
  134. accels[row].ay += ay;
  135.  
  136. parts[row].vx += deltaT * accels[row].ax;
  137. parts[row].vy += deltaT * accels[row].ay;
  138. parts[row].x += deltaT * parts[row].vx;
  139. parts[row].y += deltaT * parts[row].vy;
  140. }
  141.  
  142. instant++;
  143. }
  144.  
  145. printf("Time for Processing(s): %lf\n",
  146. (double)(clock() - start) / CLOCKS_PER_SEC);
  147. start = clock();
  148.  
  149. // Phase 3: Datastucture to File
  150. err = writefile("results.gal", parts, N, sizeof(part));
  151.  
  152. free(parts);
  153. free(accels);
  154.  
  155. printf("Time for DS to File(s): %lf\n",
  156. (double)(clock() - start) / CLOCKS_PER_SEC);
  157.  
  158. return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement