Commit 951358e0 authored by Vladislav Perepelkin's avatar Vladislav Perepelkin
Browse files

fixed bug with averaging

parent 750cfd3b
......@@ -4,6 +4,16 @@
#include <time.h>
#define FIELD(i, j) field[(i)*(WIDTH) + j]
#define NEW_FIELD(i, j) new_field[(i)*(WIDTH) + j]
#define RIGHT 1
#define DOWN 2
#define LEFT 4
#define UP 8
#define RP1 16
#define RP2 32
#define RP3 64
#define RP4 128
char *field, *new_field;
void fill(int i0, int j0, int i1, int j1,
......@@ -16,20 +26,20 @@ void fill(int i0, int j0, int i1, int j1,
inline int get_move(char cell)
{
int mass=0;
if (cell&1) mass+=1;
if (cell&2) mass+=1;
if (cell&4) mass+=1;
if (cell&8) mass+=1;
if (cell&RIGHT) mass+=1;
if (cell&DOWN) mass+=1;
if (cell&LEFT) mass+=1;
if (cell&UP) mass+=1;
return mass;
}
inline int get_rest(char cell)
{
int mass=0;
if (cell&16) mass+=2;
if (cell&32) mass+=4;
if (cell&64) mass+=8;
if (cell&128) mass+=16;
if (cell&RP1) mass+=2;
if (cell&RP2) mass+=4;
if (cell&RP3) mass+=8;
if (cell&RP4) mass+=16;
return mass;
}
......@@ -41,14 +51,14 @@ void fill(int i0, int j0, int i1, int j1,
for (i=i0; i<i1; i++) {
for (j=j0; j<j1; j++) {
char cell=0;
if (drand48() < r) { cell += 1; }
if (drand48() < d) { cell += 2; }
if (drand48() < l) { cell += 4; }
if (drand48() < u) { cell += 8; }
if (drand48() < r1) { cell += 16; }
if (drand48() < r2) { cell += 32; }
if (drand48() < r3) { cell += 64; }
if (drand48() < r4) { cell += 128; }
if (drand48() < r) { cell += RIGHT; }
if (drand48() < d) { cell += DOWN; }
if (drand48() < l) { cell += LEFT; }
if (drand48() < u) { cell += UP; }
if (drand48() < r1) { cell += RP1; }
if (drand48() < r2) { cell += RP2; }
if (drand48() < r3) { cell += RP3; }
if (drand48() < r4) { cell += RP4; }
FIELD(i, j)=cell;
}
}
......@@ -85,9 +95,9 @@ void save(int iter)
sum_mass(i, j, &rest, &move);
}
fprintf(fl_density, "%d\t%lf\n", j, (rest+move)/(HEIGHT-AVERAGING_RADIUS*2-1)/square);
fprintf(fl_move, "%d\t%lf\n", j, move/(HEIGHT-AVERAGING_RADIUS*2-1)/square);
fprintf(fl_rest, "%d\t%lf\n", j, rest/(HEIGHT-AVERAGING_RADIUS*2-1)/square);
fprintf(fl_density, "%d\t%lf\n", j, 1.0*(rest+move)/(HEIGHT-AVERAGING_RADIUS*2-1)/square);
fprintf(fl_move, "%d\t%lf\n", j, 1.0*move/(HEIGHT-AVERAGING_RADIUS*2-1)/square);
fprintf(fl_rest, "%d\t%lf\n", j, 1.0*rest/(HEIGHT-AVERAGING_RADIUS*2-1)/square);
}
fclose(fl_density);
......@@ -142,10 +152,10 @@ int main()
for (j=0; j<WIDTH; j++) {
char cell=FIELD(i, j)&0xf0; // keep rest mass
if (FIELD(i, (j+WIDTH-1)%WIDTH)&1) cell+=1; // right
if (FIELD(i, (j+1)%WIDTH)&4) cell+=4; // left
if (FIELD((i+HEIGHT-1)%HEIGHT, j)&8) cell+=8; // up
if (FIELD((i+1)%HEIGHT, j)&2) cell+=2; // down
if (FIELD(i, (j+WIDTH-1)%WIDTH)&RIGHT) cell+=RIGHT; // right
if (FIELD(i, (j+1)%WIDTH)&LEFT) cell+=LEFT; // left
if (FIELD((i+HEIGHT-1)%HEIGHT, j)&UP) cell+=UP; // up
if (FIELD((i+1)%HEIGHT, j)&DOWN) cell+=DOWN; // down
NEW_FIELD(i, j)=cell;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment