/
lattices.jl
141 lines (128 loc) · 3.51 KB
/
lattices.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
A LatticeBond is a struct which represents
a single bond in a geometrical lattice or
else on interaction graph defining a physical
model such as a quantum Hamiltonian.
LatticeBond has the following data fields:
* s1::Int -- number of site 1
* s2::Int -- number of site 2
* x1::Float64 -- x coordinate of site 1
* y1::Float64 -- y coordinate of site 1
* x2::Float64 -- x coordinate of site 2
* y2::Float64 -- y coordinate of site 2
* type::String -- optional description of bond type
"""
struct LatticeBond
s1::Int
s2::Int
x1::Float64
y1::Float64
x2::Float64
y2::Float64
type::String
end
"""
LatticeBond(s1::Int,s2::Int)
LatticeBond(s1::Int,s2::Int,
x1::Real,y1::Real,
x2::Real,y2::Real,
type::String="")
Construct a LatticeBond struct by
specifying just the numbers of sites
1 and 2, or additional details including
the (x,y) coordinates of the two sites and
an optional type string.
"""
function LatticeBond(s1::Int, s2::Int)
return LatticeBond(s1, s2, 0.0, 0.0, 0.0, 0.0, "")
end
function LatticeBond(
s1::Int, s2::Int, x1::Real, y1::Real, x2::Real, y2::Real, bondtype::String=""
)
cf(x) = convert(Float64, x)
return LatticeBond(s1, s2, cf(x1), cf(y1), cf(x2), cf(y2), bondtype)
end
"""
Lattice is an alias for Vector{LatticeBond}
"""
const Lattice = Vector{LatticeBond}
"""
square_lattice(Nx::Int,
Ny::Int;
kwargs...)::Lattice
Return a Lattice (array of LatticeBond
objects) corresponding to the two-dimensional
square lattice of dimensions (Nx,Ny).
By default the lattice has open boundaries,
but can be made periodic in the y direction
by specifying the keyword argument
`yperiodic=true`.
"""
function square_lattice(Nx::Int, Ny::Int; kwargs...)::Lattice
yperiodic = get(kwargs, :yperiodic, false)
yperiodic = yperiodic && (Ny > 2)
N = Nx * Ny
Nbond = 2N - Ny + (yperiodic ? 0 : -Nx)
latt = Lattice(undef, Nbond)
b = 0
for n in 1:N
x = div(n - 1, Ny) + 1
y = mod(n - 1, Ny) + 1
if x < Nx
latt[b += 1] = LatticeBond(n, n + Ny, x, y, x + 1, y)
end
if Ny > 1
if y < Ny
latt[b += 1] = LatticeBond(n, n + 1, x, y, x, y + 1)
end
if yperiodic && y == 1
latt[b += 1] = LatticeBond(n, n + Ny - 1, x, y, x, y + Ny)
end
end
end
return latt
end
"""
triangular_lattice(Nx::Int,
Ny::Int;
kwargs...)::Lattice
Return a Lattice (array of LatticeBond
objects) corresponding to the two-dimensional
triangular lattice of dimensions (Nx,Ny).
By default the lattice has open boundaries,
but can be made periodic in the y direction
by specifying the keyword argument
`yperiodic=true`.
"""
function triangular_lattice(Nx::Int, Ny::Int; kwargs...)::Lattice
yperiodic = get(kwargs, :yperiodic, false)
yperiodic = yperiodic && (Ny > 2)
N = Nx * Ny
Nbond = 3N - 2Ny + (yperiodic ? 0 : -2Nx + 1)
latt = Lattice(undef, Nbond)
b = 0
for n in 1:N
x = div(n - 1, Ny) + 1
y = mod(n - 1, Ny) + 1
# x-direction bonds
if x < Nx
latt[b += 1] = LatticeBond(n, n + Ny)
end
# 2d bonds
if Ny > 1
# vertical / y-periodic diagonal bond
if (n + 1 <= N) && ((y < Ny) || yperiodic)
latt[b += 1] = LatticeBond(n, n + 1)
end
# periodic vertical bond
if yperiodic && y == 1
latt[b += 1] = LatticeBond(n, n + Ny - 1)
end
# diagonal bonds
if x < Nx && y < Ny
latt[b += 1] = LatticeBond(n, n + Ny + 1)
end
end
end
return latt
end