Periodic boundary conditions for 2d grid

Click For Summary
SUMMARY

This discussion focuses on implementing periodic boundary conditions for a 2D grid in C++. The user is attempting to calculate new coordinates based on initial positions and step sizes, specifically using a grid of size 4x4. The algorithm involves adjusting the coordinates when they exceed grid boundaries by wrapping around, which is achieved through conditional statements. The main challenge discussed is how to return the new coordinates (xup, xdown, yup, ydown) back to the calling function.

PREREQUISITES
  • Understanding of C++ pointer manipulation and memory management
  • Familiarity with 2D arrays and grid structures
  • Knowledge of conditional statements for boundary checks
  • Basic understanding of periodic boundary conditions in computational physics
NEXT STEPS
  • Research C++ memory allocation techniques using malloc() and new
  • Learn about passing pointers to functions in C++
  • Study the implementation of periodic boundary conditions in simulations
  • Explore data structures in C++ for managing grid data
USEFUL FOR

Software developers, computational physicists, and anyone implementing simulations that require handling of grid-based data and 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