I am trying to generate an equilateral triangular lattice with fixed vertices and periodic boundary conditions. I am supposed to assign an index (starting from 0) to each vertex in my lattice and then find all the 6 nearest neighbors of each point. In the following you can see what I have tried so far. Here "a" shows the length of the sides of my unit triangle and Lx and Ly indicate the size of my box. I have tried to find the neighbors of each point by measuring its distance from all other points. Now if you run the program and check the "connectionx" file, you can see that some of the points (points near the boundaries) have less than 6 neighbors and it is because I do not know how to impose periodic boundary conditions on my lattice. Do you have any suggestions that can help me find all the 6 neighbors of each point using periodic boundary conditions?!

Thank you in advance!

SetDirectory["my favorite directory"];

x0 = 0;
y0 = 0;
a = 1;
R1x = a;
R2x = a*1/2;
R2y = a*Sqrt[3]/2;
Lx = 10.5;
Ly = 9.5;

Q1 = -16;
Q2 = 32;
r = -1;

points = OpenWrite["points.dat", FormatType -> OutputForm]
x = x0 + m*R1x + n*R2x // N;
y = y0 + n*R2y  // N;
If[0 < x < Lx && 0 < y < Ly, r += 1;
Write[points, r, "\t", x, "\t", y]]
, {m, Q1, Q2}]
, {n, Q1, Q2}]
Close[points]; (*This part gives me the right positions of particles with their x and y coordinates*)

data = ReadList["points.dat", Number, RecordLists -> True];
con = OpenWrite["connectionx.dat", FormatType -> OutputForm]
distance[i_, j_] := 
Sqrt[(data[[i]][[2]] - data[[j]][[2]])^2 + (data[[i]][[3]] - 
WriteString[con, i - 1, " "];
i != j && distance[i, j] <  1.01*a,
WriteString[con, j - 1, " "]
, {j, 1, Length[data]}]
WriteString[con, "\n"]
, {i, 1, Length[data]}];
  • $\begingroup$ Please write down your "periodic boundary conditions" using maths (not Mathematica, since you already said you don't know how) $\endgroup$ Jun 9, 2014 at 2:33


Browse other questions tagged or ask your own question.