Hello , i am trying to implement this algorithm for 2d grid.(adsbygoogle = window.adsbygoogle || []).push({});

1) i am not sure if my calculations are correct.

2 ) i don't understand how to return my final calculation ( how will i insert to the matrix i want (the 's' in this example) the new coordinates (xup,xdow,yup,ydown)).

I mean , how will i return these new values from the function.

Thank you!Code (Text):int periodic2d(int xpos,int ypos,int stepx,int stepy){

int Nx=4,Ny=4; //grid size

int **s; //holds spins for example

s=new int* [Nx]; //initialize pointer

for (int k=0;k<Nx;k++) s[k]=new int[Ny]; //initialize pointer

//fill the grid

for (int p=0;p<Nx;p++){

for (int k=0;k<Ny;k++){

s[p][k]=p;

}

}

int i=stepx+xpos;

int j=stepy+ypos;

// ----------------------------------------------------//

// Suppose i have this grid (4X4)

//

// 0 1 2 3

// 1 2 3 4

// 2 3 4 5

// 3 4 5 6

// Applying periodic boundary conditions

//

// If i <0 then add Nx to i else subtract stepx from i .

// If i >Nx-1 then subtract Nx from i else add stepx to i.

// The same for y(column) dimension

//

// Doing this ,for example if i put periodic2d(0,0,1,3)

// it should give me 4

//periodic boundary conditions

int xup=i<0?i+Nx:i-stepx;

int xdown=i>Nx-1?i-Nx:i+stepx;

int yup=j<0?Ny-1:j-stepy;

int ydown=j>Ny-1?i-Ny:j+stepy;

return ?????????

}

**Physics Forums | Science Articles, Homework Help, Discussion**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Periodic boundary conditions for 2d grid

**Physics Forums | Science Articles, Homework Help, Discussion**