I'm new to programming, and I've been trying to make functions (in C) that compute the nearest neighbors of a site
i in a square lattice of NxN sites and, in a triangular lattice.
So far, for the
square lattice , we have 4 nearest neighbors is easy, the first four nearest neighbors of a site
i can be written as:
i+1, for the right neighbor,
i-1, for the left neighbor, and so on for the upper and bottom neighbors.
The problem is when I consider a
triangular lattice example. We have 6 nearest neighbors. I can't find a similar formula using the label
Ci, for the other sites' neighbors than the left and right respectively. Should I include some angles in my formulation?
What I have tried:
The following is the code corresponding to the neighbors of a square lattice. There, I'm not considering still periodic boundary conditions. This program gives us the nearest four neighbors of a site (introduced by the user)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define L 5 // The lattice is LxL
int site;
int calculate_neighbours(int i){
int nr;
nr=i+1;
for(int k=0;k<L+1;k++){
if(site==k*L){
nr=L*(k-1)+1;
}
}
printf("\n right= %d",nr);
int nl;
nl=i-1;
for(int k=1;k<L+1;k++){
if(site==(k-1)*L+1){
nl=L*k;
}
}
printf("\n left= %d",nl);
int nu;
nu=i-L;
for(int p=L-1;p>-1;p--){
if(site==L-p){
nu=L*L-p;
}
}
printf("\n up= %d",nu);
int nd;
nd=i+L;
for(int p=L-1;p>-1;p--){
if(site==L*L-p){
nd=L-p;
}
}
printf("\n down= %d",nd);
int main(void){
int cont, M[L][L];
cont=1;
while(cont<L){
printf("The M-matrix is:\n");
for (int m=0;m<L;m++){
printf("\n\n");
for(int n=0;n<L;n++){
M[m][n]=cont++;
printf("%5d",M[m][n]);
}
}
}
printf("\n\nTamaño de la red L= %d",L);
printf("\nNumero de sitios LxL=%d",L*L);
printf("\n\nintroduzca un sitio= ");
scanf("%d",&site);
if(site==0 || site<0){printf("\nDebes introducir un numero mayor a cero!\n");}
else if(site>0 && site<L*L+1){
calculate_neighbours(site);
printf("\n");
}
else {
printf("\nExcediste el tamaño de la red\n");
}
}
I still don't know how to apply something similar to a triangular lattice. That is, introducing some angles? I'm a bit confused.
Someone recommended that I need to consider (X,Y) coordinate of square lattice:
Y=0 : 0 1 2 3
Y=1 : 0 1 2 3
Y=2 : 0 1 2 3
Y=3 : 0 1 2 3
Then, let's consider this 4x4 data as a Triangle lattice. If you shift the rows with odd Y values to the right by 0.5, the resulting shape will look like a Triangle lattice. And, to consider the neighborhood problem, introduce another coordinate here (U,V) as:
V=0 : 0 2 4 6
V=1 : 1 3 5 7
V=2 : 0 2 4 6
V=3 : 1 3 5 7
On this (U,V) coordinate system, enumerating the 6-neighbors should be:
(U+2,V)
(U-2,V)
(U-1,V-1)
(U+1,V-1)
(U-1,V+1)
(U+1,V+1)
All that is left is the coordinate transformation between (X,Y) and (U,V). This should be:
inline void XY2UV( int X, int Y, int &U, int &V )
{
U = X*2 + ( Y%2 );
V = Y;
}
inline void UV2XY( int U, int V, int &X, int &Y )
{
X = U / 2;
Y = V;
}
BUT...I've been struggling with incorporating this info into the code I already have for a square lattice. Could anyone give me some advice regarding this? Thank you very much in advance :)