Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #define MEM_ERROR -2
- #define ERROR -9
- #define SUCCESS 0
- struct particle {
- int label;
- char *loc;
- int xdist;
- int ydist;
- };
- int myRand(int a, int b);
- int main(int argc, char **argv) {
- int i, j, k, steps, L, N, plab;
- char *grill;
- unsigned long long int seed;
- double boh;
- struct particle *gas;
- FILE *fout=fopen("gas.dat", "w");
- sscanf(argv[1], "%d", &L);
- sscanf(argv[2], "%Lu", &seed);
- sscanf(argv[3], "%d", &N);
- sscanf(argv[4], "%d", &steps);
- gas=(struct particle *) malloc(N*sizeof(struct particle));
- grill=(char *) calloc(L*L, sizeof(int));
- if (gas==NULL || grill==NULL) {
- printf("\n\nErrore nell'assegnazione della memoria!!\n\n");
- exit(MEM_ERROR);
- }
- srand48(seed);
- /*
- for(i=0; i<N; i++) {
- do {
- plab=myRand(0, L*L);
- } while (grill[plab]!=0);
- grill[plab]=1;
- }
- */
- for (i=0; i<N; i++) {
- gas[i].label=i;
- do {
- plab=myRand(0, L*L);
- } while (grill[plab]!=0);
- grill[plab]=1;
- gas[i].loc=&grill[plab];
- }
- for (k=0; k<steps; k++) {
- plab=myRand(0, N);
- boh=drand48();
- if (boh>0.75) {
- if (*(gas[plab].loc-L)==0) {
- *(gas[plab].loc-L)=1;
- *gas[plab].loc=0;
- gas[plab].loc=gas[plab].loc-L;
- gas[plab].ydist+=1;
- }
- } else if (boh>0.5) {
- if (*(gas[plab].loc+1)==0) {
- *(gas[plab].loc+1)=1;
- *gas[plab].loc=0;
- gas[plab].loc=gas[plab].loc+1;
- gas[plab].xdist+=1;
- }
- } else if (boh>0.25) {
- if (*(gas[plab].loc+L)==0) {
- *(gas[plab].loc+L)=1;
- *gas[plab].loc=0;
- gas[plab].loc=gas[plab].loc+L;
- gas[plab].ydist-=1;
- }
- } else {
- if (*(gas[plab].loc-1)==0) {
- *(gas[plab].loc-1)=1;
- *gas[plab].loc=0;
- gas[plab].loc=gas[plab].loc-1;
- gas[plab].xdist-=1;
- }
- }
- for (i=0; i<L; i++) {
- for (j=0; j<L; j++) {
- fprintf(fout, "%d ", grill[i*L+j]);
- }
- fprintf(fout, "\n");
- }
- fprintf(fout, "\n");
- fflush(fout);
- }
- /*
- for (i=0; i<L; i++) {
- for (j=0; j<L; j++) {
- fprintf(fout, "%d ", grill[i*L+j]);
- }
- fprintf(fout, "\n");
- }
- */
- free(grill);
- free(gas);
- return SUCCESS;
- }
- int myRand(int a, int b) {
- return (int)(a+(b-a)*(double) lrand48()/RAND_MAX);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement