Periodic boundary conditions for 2d grid

In summary, you are trying to apply boundary conditions to a grid, but you are not sure how to return the data back to the function.
  • #1
ggeo1
63
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
  • #2
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).
 
  • #3
Hello ,

ok for all you say but you don't answer to my questions. :)
 
  • #4
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.
 
  • #5
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
 
  • #6
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).
 
  • #7
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..
 

Related to Periodic boundary conditions for 2d grid

1. What are periodic boundary conditions for a 2d grid?

Periodic boundary conditions for a 2d grid are a set of conditions that allow for the simulation of a continuous system on a finite grid. This means that the edges of the grid are connected, creating a virtual loop, so that particles or objects can move from one side of the grid to the other without leaving the simulation.

2. Why are periodic boundary conditions important in simulations?

Periodic boundary conditions are important in simulations because they allow for the study of systems that are too large or complex to be simulated in their entirety. By using a finite grid with periodic boundary conditions, researchers can obtain accurate results for a smaller portion of the system, which can then be extrapolated to represent the entire system.

3. How are periodic boundary conditions implemented in simulations?

Periodic boundary conditions are implemented by setting the opposite edges of the grid to be equivalent. This means that particles or objects that leave the grid on one side will reappear on the opposite side, as if they had wrapped around the edges of the grid. This is typically done by using special equations or algorithms that take into account the periodicity of the system.

4. What are the advantages of using periodic boundary conditions?

There are several advantages to using periodic boundary conditions in simulations. One is that it allows for the study of large or complex systems on a smaller scale, which can save time and resources. Additionally, periodic boundary conditions can help to reduce boundary effects or artifacts that may occur in simulations with fixed boundaries.

5. Are there any limitations or drawbacks to using periodic boundary conditions?

While periodic boundary conditions are useful in many simulations, they may not be appropriate for all systems. In some cases, the behavior of particles or objects at the edges of the grid may not accurately represent the behavior of those in the center of the system. Additionally, periodic boundary conditions may not be suitable for simulations where there is a strong gradient or change in properties across the boundaries of the system.

Similar threads

  • Programming and Computer Science
Replies
4
Views
691
  • Programming and Computer Science
Replies
1
Views
957
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
977
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
666
Back
Top