## periodic boundary conditions for 2d grid

Hello , i am trying to implement this algorithm for 2d grid.

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.

Code:
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 ?????????

}
Thank you!
 Hey ggeo1 and welcome to the forums. To return the data to the previous scope, you have a few options. One option is to return a pointer to some structure which includes your information. Typically what happens in this instance, is that you allocate a structure on the heap (in C its malloc(), in C++ its new) and then return the pointer to that data structure. Alternatively you could pass the pointer to a pointer as an argument to the function (example StructureType** pointerofpointer) and then set the value of the pointer (using *pointerofpointer = blah and making sure that pointerofpointer is valid and not zero!). If the structure is pre-allocated and the right size then you can allocate the structure beforehand and set the memory contents of the structure in the function itself (this depends on the nature of the structure: for example its a single array then you could use (*pointerofpointer)[index] = blah)). These are the only decent ways to do this (using the stack is not a good idea even though technically you can do it).
 Hello , ok for all you say but you don't answer to my questions. :)

## periodic boundary conditions for 2d grid

 Quote by ggeo1 Hello , ok for all you say but you don't answer to my questions. :)
I answered the last line (how to return values back to function). I'll take a look at the others now.

 I answered the last line (how to return values back to function). I'll take a look at the others now.
I mean , i am confused at how to return xup,xdown to new xpos and yup ,ydown to new ypos.

Ok , use a structure but how to relate xup and xdown with xpos?

Thank you
 For the 2nd question you should really look at the first reply I made where your pointer will point to the matrix data structure. For your last part of the code with the boundary conditions, what is the range for i? is it over all rows? all columns? Can you specify your algorithm in plain english just so that the readers who don't know what you are trying to do (i.e. like me) are trying to do? Right now, it looks like you are calculating variables, but it seems like you are trying to implement something you don't completely understand (and I don't either based on the variables and their definitions).
 Ok, i use in this exmaple a grid size with dimensions Nx,Ny (4x4). The 'particle' will be initial at position xpos,ypos.Then you choose a step in both sirections (stepx,stepy) and you must calculate the new position (xpos,ypos) after applying the periodic boundary conditions). i=stepx+xpos ,is the final position in x direction ( rows) j=stepy+ypos ,is the final position in y direction (columns) I am trying to explain here: Code: // ----------------------------------------------------// // 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 I am not sure if can explain it better.Sorry..

 Tags boundary conditions, c++