I wrote a code to simulate the NO-CO reaction on a square surface in C language, but every time my program calculates a new site at random, the matrix doesn't look the same, even when in the code hasn't it is not intended.
What I have tried:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define xmax 2
#define ymax 2
#define MCSS 100
#define empty 0
#define occupied 1
#define co_occupied 2
#define no_occupied 3
#define n_occupied 4
#define o_occupied 5
#define p 0.300000
#define xco 0.4
#define rno 0.6
int printrandomx(int lower, int upper, int cuenta)
{
int m, num1;
for (m = 0; m < cuenta ; m++) {
num1 = (rand() % (upper - lower + 1)) + lower;
}
return num1;
}
int printrandomy(int bajo, int alto, int contar)
{
int n,num2;
for (n = 0; n < contar; n++) {
num2 = (rand() % (alto - bajo + 1)) + bajo;
}
return num2;
}
int get_limited_coord(int coord, int coord_max)
{
if (coord >= 0 && coord < coord_max) {
return coord;
} else if (coord >= coord_max) {
return coord - coord_max;
} else {
return coord + coord_max;
}
}
int main(){
FILE *fp;
fp = fopen ("data.txt", "w");
srand(time(0));
int N = xmax*ymax;
int grid[xmax][ymax];
int lower = 0, bajo=0, upper=xmax-1, alto=ymax-1, cuenta=1, contar=1;
int count = 1, x=0, y=0;
int right, left, up, down;
double random,q,r;
int co = 0, no = 0, n = 0, o = 0, v = N, pco2 = 0, pn2 = 0, po2 = 0;
for (int i = 0; i < xmax; i++) {
for (int j = 0; j < ymax; j++) {
grid[i][j] = empty;
}
}
step1:
for (int i = 0; i < xmax; i++) {
for (int j = 0; j < ymax; j++) {
grid[i][j] = count++;
printf("%d ", grid[i][j]);
}
printf("\n");
}
x = printrandomx(lower, upper, cuenta);
y = printrandomy(bajo, alto, contar);
printf("(x,y)=(%d,%d)\n\n",y,x);
for (int i = 0; i < xmax; i++)
{
for (int j = 0; j < ymax; j++ )
{
if(x == i && y == j){ grid[x][y] == count; }
}
}
printf("The coordinates are (%d,%d) and the solicited matrix entry is: %d\n\n", y, x, grid[y][x]);
if (x > xmax || x < 0 || y > ymax || y < 0) {
printf ("Invalid coordinates given\n");
return 1;
}
printf("IS IT OCCUPIED???\n\n\n");
if (grid[y][x] == occupied){
printf("It's occupied, the trial ends. Choose another site again\n\n");
goto step1;
}
else{
printf("It's empty. Let's go to generate a random number to choose the adsorbates\n\n");
goto step2;
}
step2:
q = (double) rand () / RAND_MAX;
printf("random q= %lf\n\n",q);
if(q < xco || q == xco){
printf("CO is adsorbed\n\n");
grid[y][x] = co_occupied;
co = co + 1;
v = v - 1;
printf("adsorbed CO ---> co = %d\n\n",co);
printf("FIND THE NEIGHBORS OF THE ADSORBED CO\n\n");
right = grid[get_limited_coord(y , ymax)][get_limited_coord(x+1, xmax)];
left = grid[get_limited_coord(y , ymax)][get_limited_coord(x-1, xmax)];
up = grid[get_limited_coord(y-1, ymax)][get_limited_coord(x , xmax)];
down = grid[get_limited_coord(y+1, ymax)][get_limited_coord(x , xmax)];
printf("Right: %d\nLeft: %d\nUp: %d\nDown: %d\n\n",right,left,up,down);
if( (right == o_occupied) || (left == o_occupied) || (up == o_occupied) || (down == o_occupied) ){
printf("It was encountered a site with O!\n\n");
pco2 = pco2 + 1;
v = v + 2;
printf("produced co2 = %d\n\n",pco2);
} else{
printf("It wasn't encountered a site with O :(\n\n");
printf("CO2 IS NOT PRODUCED\n\n");
}
} else {
printf("q = %lf is greater than xco = %lf. We choose NO\n\n", q, xco);
r = (double) rand () / RAND_MAX;
printf("r= %lf\n\n", r);
if(r < rno){
printf("r= %lf is less than rno= %lf\n\n We seek another neighbor to decide if NO can disociate\n\n", r,rno);
printf("FIND THE NEIGHBORS SO THAT NO CAN BE DISOCIATED\n\n");
right = grid[get_limited_coord(y , ymax)][get_limited_coord(x+1, xmax)];
left = grid[get_limited_coord(y , ymax)][get_limited_coord(x-1, xmax)];
up = grid[get_limited_coord(y-1, ymax)][get_limited_coord(x , xmax)];
down = grid[get_limited_coord(y+1, ymax)][get_limited_coord(x , xmax)];
printf("Right: %d\nLeft: %d\nUp: %d\nDown: %d\n\n",right,left,up,down);
if( (right == empty) || (left == empty) || (up == empty) || (down == empty) ){
printf("It was encountered another site so that NO can disociate!\n\n");
n = n + 1;
o = o + 1;
v = v - 2;
printf("adsorbed N ---> n= %d\n adsorbed O ---> o= %d\n",n, o);
}else{
printf("****WE DIDN'T FIND AN EMPTY NEIGHBOR SO,THE TRIAL ENDS, START AGAIN****\n\n");
goto step1;
}
}else{ printf("r= %lf is greater than rno= %lf\n\n So, NO remains in its molecular form\n\n",r, rno);
printf("FIND THE NEIGHBORS OF THE NO IN ITS MOLECULAR FORM\n\n");
right = grid[get_limited_coord(y , ymax)][get_limited_coord(x+1, xmax)];
left = grid[get_limited_coord(y , ymax)][get_limited_coord(x-1, xmax)];
up = grid[get_limited_coord(y-1, ymax)][get_limited_coord(x , xmax)];
down = grid[get_limited_coord(y+1, ymax)][get_limited_coord(x , xmax)];
printf("Right: %d\nLeft: %d\nUp: %d\nDown: %d\n\n",right,left,up,down);
if( (right == n_occupied) || (left == n_occupied) || (up == n_occupied) || (down == n_occupied) ){
printf("se ha encontrado un N, entonces se va a formar N2 y quedara adsorbido O, dejando un sitio vacante\n\n");
pn2 = pn2 +1;
o = o + 1;
v = v + 1;
}else{
printf("****THERE IS NO N TO REACT ---> THE TRIAL ENDS, START AGAIN****\n\n\n");
goto step1;
}
}
}
fclose(fp);
return 0;
}
I think the code is not "remembering" the prior state and that's the reason it gives me a new matrix very time it finds a new site at random. How could I fix this?