Advertisement
Guest User

Untitled

a guest
Mar 6th, 2025
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <math.h>
  6. #include <time.h>
  7. #include <string.h>
  8.  
  9. #define eps 1e-3
  10.  
  11. int main(int ac, char * av[]) {
  12.     if(ac != 6){
  13.         printf("Correct Format: %s N inputfile timesteps deltaT graphics\n",av[0]);
  14.     }
  15.  
  16.     const int N = atoi(av[1]);
  17.     const double G = 100.0/N;
  18.  
  19.     const char * inputfile = av[2];
  20.     const int timesteps = atoi(av[3]);
  21.     const double deltaT = atof(av[4]);
  22.  
  23.     clock_t start;
  24.     start = clock();
  25.  
  26.     //Phase 1: File to Datastucture
  27.     int dsize = N * sizeof(double);
  28.     int dssize = (dsize << 1) + (dsize << 2);
  29.     double * particles = (double *) malloc(dssize);
  30.    
  31.     int inputfd=open(inputfile,O_RDONLY);
  32.     if(inputfd == -1) {printf("File open error!\n"); return 1;}
  33.    
  34.     int inputsize = read(inputfd, particles, dssize);
  35.     close(inputfd);
  36.  
  37.     printf("Time for File to DS(s): %lf\n", (double) (clock() - start) / CLOCKS_PER_SEC);
  38.     start = clock();
  39.  
  40.     //Phase 2: Processing
  41.     int instant = 0, i = 0, j = 0;
  42.     double * accel = (double *) calloc(N << 1, sizeof(double));
  43.     while(instant < timesteps){
  44.         for(i = 0; i < N; i++){
  45.             int row2 = i << 1;
  46.             int row = row2 + (i << 2);
  47.             double mi = particles[row + 2];
  48.             double x = particles[row], y = particles[row + 1];
  49.             double v_x = particles[row + 3], v_y = particles[row + 4];
  50.             double a_x = 0.0, a_y = 0.0;
  51.  
  52.             for(j = i + 1; j < N; j++){
  53.                 int col2 = j << 1;
  54.                 int col = col2 + (j << 2);
  55.                 double dx = x - particles[col];
  56.                 double dy = y - particles[col + 1];
  57.                 double mj = particles[col + 2];
  58.                 double rij = sqrt(dx * dx + dy * dy);
  59.                 double rije = rij + eps;
  60.                 double radius = rije * rije * rije;
  61.                 double force = G / radius;
  62.                
  63.                 //Optimization: Newton's Third Law
  64.                 double fx = force * dx;
  65.                 double fy = force * dy;
  66.                 a_x -= fx * mj;
  67.                 a_y -= fy * mj;
  68.                 accel[col2] += fx * mi;
  69.                 accel[col2 + 1] += fy * mi;
  70.             }
  71.  
  72.             accel[row2] += a_x;
  73.             accel[row2 + 1] += a_y;
  74.  
  75.             particles[row + 4] += deltaT * accel[row2 + 1];
  76.             particles[row + 3] += deltaT * accel[row2];
  77.             particles[row] += deltaT * particles[row + 3];
  78.             particles[row + 1] += deltaT * particles[row + 4];
  79.  
  80.         }
  81.  
  82.  
  83.         //reset accel
  84.         memset(accel, 0, N * sizeof(double) << 1);
  85.  
  86.         instant++;
  87.     }
  88.  
  89.     printf("Time for Processing(s): %lf\n", (double) (clock() - start) / CLOCKS_PER_SEC);
  90.     start = clock();
  91.  
  92.     //Phase 3: Datastucture to File
  93.     int outputfd = open("results.gal",O_RDWR | O_CREAT, 0666);
  94.     if(outputfd == -1) {printf("File create error!\n"); return 1;}
  95.  
  96.     int outputsize = write(outputfd, particles, dssize);
  97.     close(outputfd);
  98.  
  99.     free(particles);
  100.     free(accel);
  101.  
  102.     printf("Time for DS to File(s): %lf\n", (double) (clock() - start) / CLOCKS_PER_SEC);
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement