Hi! I'm working on the Monte Carlo simulation of the NO-CO reaction on a square surface, and when I was writing the code, I needed to specify the states of "empty" or "occupied" of the sites on the surface that CO or NO can occupy.
In my code I defined those states as follows
#define empty 0
#define occupied 1
So, if the site is "occupied", the chosen site is written as
grid[x][y] = occupied;
But when we go to check if this is effectively true, I can't do something like
if(grid[x][y] = occupied)
{
thing 1;
thing 2;
}
Since I will obtain a warning or error message. I know that if, while statements work with "==", "!=", "<", ">", but I really need to check if a chosen site of my simulation is or not occupied and I'm not sure how to write it in C
What I have tried:
The following is the code that I've been writing so far.
I've defined some booleans but I don't know if I'am using them correctly. All that I need is to know how to check properly if a chosen site of my simulation is or not occupied.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define xmax 3
#define ymax 3
#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, upleft, upright, downleft, downright;
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] = 0;
}
}
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",x,y);
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", x, y, 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");
check: if (grid[y][x] == occupied){
printf("It's occupied, the trial ends. Choose another site again\n\n");
goto step1;
}
else{
if (grid[y][x] == empty){
printf("It's empty. Let's go to generate a random number to choose the adsorbates\n\n");
goto step2;
}
}
seekneighbors: printf("FIND THE NEIGHBORS\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);
goto step3;
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);
goto seekneighbors;
step3:
printf("****THIS PART WORKS****\n\n");
right = o_occupied;
left = o_occupied;
up = o_occupied;
down = o_occupied;
bool flag1 = 0;
bool flag2 = 0;
bool flag3 = 0;
bool flag4 = 0;
flag1 = (right == o_occupied);
flag2 = (left == o_occupied);
flag3 = (up == o_occupied);
flag4 = (down == o_occupied);
if(flag1 || flag2 || flag3 || flag4){
pco2 = pco2 + 1;
v = v + 2;
printf("produced co2 = %d\n\n",pco2);
} else{
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);
bool flag5 = 0;
bool flag6 = 0;
bool flag7 = 0;
bool flag8 = 0;
flag5 = (right == empty);
flag6 = (left == empty);
flag7 = (up == empty);
flag8 = (down == empty);
if( flag5 || flag6 || flag7 || flag8 ){
n = n + 1;
o = o + 1;
v = v - 2;
printf("adsorbed N ---> n= %d\n adsorbed O ---> o= %d\n",n, o);
}else{
printf("****THE TRIAL ENDS, START AGAIN****");
goto step1;
}
}
}
fclose(fp);
return 0;
}
It seems that my code doesn't even passes through the following chunk:
printf("IS IT OCCUPIED???\n\n\n");
check: if (grid[y][x] == occupied){
printf("It's occupied, the trial ends. Choose another site again\n\n");
goto step1;
}
else{
if (grid[y][x] == empty){
printf("It's empty. Let's go to generate a random number to choose the adsorbates\n\n");
goto step2;
}
}
Why could that happen?
I'll greatly appreciate if you could give me some hints to continue. Thanks for your time :)