| New Reply |
periodic boundary conditions for 2d grid |
Share Thread |
| Aug6-12, 08:29 AM | #1 |
|
|
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 ?????????
}
|
| Aug7-12, 03:47 AM | #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). |
| Aug7-12, 06:52 AM | #3 |
|
|
Hello ,
ok for all you say but you don't answer to my questions. :) |
| Aug7-12, 06:55 AM | #4 |
|
|
periodic boundary conditions for 2d grid |
| Aug7-12, 07:00 AM | #5 |
|
|
Ok , use a structure but how to relate xup and xdown with xpos? Thank you |
| Aug7-12, 07:03 AM | #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). |
| Aug7-12, 07:10 AM | #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 |
| New Reply |
| Tags |
| boundary conditions, c++ |
Similar discussions for: periodic boundary conditions for 2d grid
|
||||
| Thread | Forum | Replies | ||
| crystal model with periodic boundary conditions | Atomic, Solid State, Comp. Physics | 2 | ||
| Periodic Boundary Conditions on non sq lattice | Atomic, Solid State, Comp. Physics | 1 | ||
| Periodic boundary conditions and Bloch's theorem | Atomic, Solid State, Comp. Physics | 8 | ||
| Momentum eigenfunctions with periodic boundary conditions | Advanced Physics Homework | 0 | ||
| periodic boundary conditions | Atomic, Solid State, Comp. Physics | 16 | ||