Advertisement
Guest User

gas.c

a guest
Dec 3rd, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define MEM_ERROR -2
  5. #define ERROR -9
  6. #define SUCCESS 0
  7.  
  8.  
  9. struct particle {
  10.   int label;
  11.   char *loc;
  12.   int xdist;
  13.   int ydist;
  14. };
  15.  
  16.  
  17. int myRand(int a, int b);
  18.  
  19.  
  20. int main(int argc, char **argv) {
  21.   int i, j, k, steps, L, N, plab;
  22.   char *grill;
  23.   unsigned long long int seed;
  24.   double boh;
  25.   struct particle *gas;
  26.   FILE *fout=fopen("gas.dat", "w");
  27.  
  28.   sscanf(argv[1], "%d", &L);
  29.   sscanf(argv[2], "%Lu", &seed);
  30.   sscanf(argv[3], "%d", &N);
  31.   sscanf(argv[4], "%d", &steps);
  32.  
  33.   gas=(struct particle *) malloc(N*sizeof(struct particle));
  34.   grill=(char *) calloc(L*L, sizeof(int));
  35.   if (gas==NULL || grill==NULL) {
  36.     printf("\n\nErrore nell'assegnazione della memoria!!\n\n");
  37.     exit(MEM_ERROR);
  38.   }
  39.   srand48(seed);
  40.  
  41.   /*
  42.   for(i=0; i<N; i++) {
  43.     do {
  44.       plab=myRand(0, L*L);
  45.     } while (grill[plab]!=0);
  46.     grill[plab]=1;
  47.   }
  48.   */
  49.  
  50.   for (i=0; i<N; i++) {
  51.     gas[i].label=i;
  52.     do {
  53.       plab=myRand(0, L*L);
  54.     } while (grill[plab]!=0);
  55.     grill[plab]=1;
  56.     gas[i].loc=&grill[plab];
  57.   }
  58.  
  59.   for (k=0; k<steps; k++) {
  60.     plab=myRand(0, N);
  61.     boh=drand48();
  62.     if (boh>0.75) {
  63.       if (*(gas[plab].loc-L)==0) {
  64.     *(gas[plab].loc-L)=1;
  65.     *gas[plab].loc=0;
  66.     gas[plab].loc=gas[plab].loc-L;
  67.     gas[plab].ydist+=1;
  68.       }
  69.     } else if (boh>0.5) {
  70.       if (*(gas[plab].loc+1)==0) {
  71.         *(gas[plab].loc+1)=1;
  72.     *gas[plab].loc=0;
  73.     gas[plab].loc=gas[plab].loc+1;
  74.     gas[plab].xdist+=1;
  75.       }
  76.     } else if (boh>0.25) {
  77.       if (*(gas[plab].loc+L)==0) {
  78.     *(gas[plab].loc+L)=1;
  79.     *gas[plab].loc=0;
  80.     gas[plab].loc=gas[plab].loc+L;
  81.     gas[plab].ydist-=1;
  82.       }
  83.     } else {
  84.       if (*(gas[plab].loc-1)==0) {
  85.     *(gas[plab].loc-1)=1;
  86.     *gas[plab].loc=0;
  87.     gas[plab].loc=gas[plab].loc-1;
  88.     gas[plab].xdist-=1;
  89.       }
  90.     }
  91.  
  92.     for (i=0; i<L; i++) {
  93.       for (j=0; j<L; j++) {
  94.     fprintf(fout, "%d ", grill[i*L+j]);
  95.       }
  96.       fprintf(fout, "\n");
  97.     }
  98.     fprintf(fout, "\n");
  99.     fflush(fout);
  100.   }
  101.  
  102.   /*
  103.   for (i=0; i<L; i++) {
  104.     for (j=0; j<L; j++) {
  105.       fprintf(fout, "%d ", grill[i*L+j]);
  106.     }
  107.     fprintf(fout, "\n");
  108.   }
  109.   */
  110.  
  111.   free(grill);
  112.   free(gas);
  113.  
  114. return SUCCESS;
  115. }
  116.  
  117.  
  118.  
  119. int myRand(int a, int b) {
  120.   return (int)(a+(b-a)*(double) lrand48()/RAND_MAX);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement