Periodic boundary conditions for 2d grid

Click For Summary

Discussion Overview

The discussion revolves around implementing an algorithm for a 2D grid that incorporates periodic boundary conditions. Participants are exploring how to correctly calculate new positions within the grid based on given steps and how to return these values from a function.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses uncertainty about the correctness of their calculations and how to return final values to a matrix.
  • Another participant suggests returning a pointer to a structure or passing a pointer to a pointer to manage data return.
  • A participant questions the clarity of the algorithm and requests a plain English explanation of the intended functionality.
  • Further clarification is sought on how to relate calculated boundary values (xup, xdown, yup, ydown) back to the original positions (xpos, ypos).
  • One participant attempts to explain the grid setup and the intended calculations for periodic boundary conditions, but expresses uncertainty about the clarity of their explanation.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus, as there are multiple competing views on how to implement the function and return values, along with varying levels of understanding of the algorithm's requirements.

Contextual Notes

There is a lack of clarity regarding the definitions of variables and the overall algorithm, which may affect the implementation of the periodic boundary conditions.

ggeo1
Messages
61
Reaction score
0
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!
 
Technology news on Phys.org
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. :)
 
ggeo1 said:
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..
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
Replies
1
Views
2K
Replies
1
Views
3K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K