I am trying to implement periodic boundary conditions for 2d lattice.

I did:

int  boundary2d(int xpos,int ypos,int stepx,int stepy){

int x=4,y=4;       //grid size
int **matrix;      //the grid matrix


int i=stepx+xpos;
int j=stepy+ypos;

if (i<0) {
 do {
    i+=Nx;
 }while (i>Nx-1);
}

if (i>Nx-1) {
    do{
    i-=Nx;
    }while(i>Nx-1);

}

if (j<0) {
 do {
    j+=Ny;
 }while (j>Ny-1);
}

if (j>Ny-1) {
    do{
    j-=Ny;
    }while(j>Ny-1);

}

return matrix[i][j];

}

I don't know if i have the conditions right.

Could you help me?

Thank you!

Why don't you treat this like two 1D boundary conditions (like the other post that was solved)?

If you create a position structure (just contains two ints or doubles for x and y position) then you can return that rather than creating a 4x4 matrix (which you did not allocate memory for).

I'll give you a chance to attempt this before posting any code to help you out because what you need already exists in the other thread and the rest seems pretty straight forward.

Hello ,

1) I don't want to crete a structure because i will need it for a matrix.

2) I allocated memory for the matrix , i just didn't show it because that wasn't the issue.

3) I am giving you full code for testing.The problem is that it doesn't give the right results, so somewhere i have an error.

    int  boundary2d(int xpos,int ypos,int stepx,int stepy){

    int x=4,y=4;       //grid size

    int **matrix;            
    matrix=new int* [x];                            
    for (int k=0;k<x;k++) matrix[k]=new int[y];    //initialize pointer

        //fill the grid          //just for testing purposes
    for (int p=0;p<x;p++){
        for (int k=0;k<y;k++){
            matrix[p][k]=p;
            }
        }

    int i=stepx+xpos;
    int j=stepy+ypos;


    if (i<0) {

       while (i>x-1)  i+=x;
    }

    if (i>x-1) {

       while(i>x-1)  i-=x;

    }

    if (j<0) {

        while (j>y-1) j+=y;
    }

    if (j>y-1) {

       while(j>y-1)  j-=y;

    }

    return matrix[i][j];


    }
      Suppose i have this grid  (4X4)
    
          0    1   2   3
          1    2   3   4
          2    3   4   5
          3    4   5   6

Doing this ,for example if i put boundary2d(0,0,1,2) it should give me 3 but it gives me 1.

Another version (based to the previous post) would be:

int final_posx=xpos+stepx;//final position

    //for step <0
    while (final_posx < - (x/2)) final_posx+=x;

    //step >0
    while (final_posx > (x/2)) final_posx-=x;

    int final_posy=ypos+stepy;//final position

    //for step <0
    while (final_posy < - (y/2)) final_posy+=y;

    //step >0
    while (final_posy > (y/2)) final_posy-=y;

    return matrix[final_posx][final_posy];

but it gives me the same results (which means it's right? but i am missing something here?)

Thank you!

I think the biggest problem you are having is that the matrix you think you have is not the one you set up. As you can see I printed out the contents of the matrix and it is correct because I am adding the values of what row and column the for() loops are on.

I also see that you are kind of accessing the matrix with the x and y values being backwards. If you think about it rows are on the y axis and columns are on the x axis. For this example it will not matter since this matrix is <insert special name that I learned in linear algebra here>, which means that it is mirrored about the diagonal.

If you are going to use the new operator then you will have to store the resulting value that you are at in the matrix to a variable that you will return and then delete the memory allocated for the matrix (otherwise you will have a memory leak).

#include <iostream>
using namespace std;

int boundary2D( int xpos, int ypos, int xstep, int ystep )
{
    const int x = 4, y = 4;
    int matrix[y][x];

    for( int r = 0; r < y; r++ )
        for( int c = 0; c < x; c++ )
            matrix[r][c] = r+c;

    for( int r = 0; r < y; r++ )
    {
        for( int c = 0; c < x; c++ )
            cout << matrix[r][c] << " ";
        cout << endl;
    }
    cout << endl;

    xpos += xstep;
    ypos += ystep;

    while( ypos < 0 )
        ypos += y;
    while( ypos >= y )
        ypos -= y;
    while( xpos < 0 )
        xpos += x;
    while( xpos >= x )
        xpos -= x;

    return matrix[ypos][xpos];
}


int main()
{
    cout << boundary2D(0, 0, 1, 2) << endl;

    cin.get();
    return 0;
}

I also see that you are kind of accessing the matrix with the x and y values being backwards. If you think about it rows are on the y axis and columns are on the x axis

Thanks for that.I totally missed it!

Thanks for the help.Works fine now!

This question has already been answered. Start a new discussion instead.