Skip to content

Commit 1dc9592

Browse files
Export FiniteCylinder and FiniteStrip (#49)
* fix typos * unpack `nearest_neighbours` iterators * add generic `next_nearest_neighbours` for 2D `AbstractLattice`s Co-authored-by: Lukas Devos <[email protected]> * format * force formatcheck CI to use JuliaFormatter v1.0.62, v2 seems broken * add test for `FiniteCylinder` * export `FiniteCylinder` * relax version requirement for JuliaFormatter in formatcheck CI * clarify docstring of `FiniteStrip` * update docstring for `FiniteHelix` and `FiniteCylinder` * add FiniteStrip Tests * export `FiniteStrip` * format --------- Co-authored-by: Lukas Devos <[email protected]>
1 parent a06d8cf commit 1dc9592

File tree

4 files changed

+68
-10
lines changed

4 files changed

+68
-10
lines changed

.github/workflows/FormatCheck.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@ jobs:
2828

2929
- uses: actions/checkout@v4
3030
- name: Install JuliaFormatter and format
31-
# This will use the latest version by default but you can set the version like so:
32-
#
33-
# julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter", version="0.13.0"))'
3431
run: |
35-
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
32+
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter", version="v1"))'
3633
julia -e 'using JuliaFormatter; format(".", verbose=true)'
3734
- name: Format check
3835
id: format

src/MPSKitModels.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ using LinearAlgebra: LinearAlgebra
1111
export AbstractLattice
1212
export InfiniteChain, FiniteChain
1313
export InfiniteCylinder, InfiniteHelix, InfiniteStrip, InfiniteLadder
14+
export FiniteCylinder, FiniteStrip
1415
export HoneycombXC, HoneycombYC
1516
export LatticePoint, linearize_index
1617
export vertices, nearest_neighbours, next_nearest_neighbours, bipartition

src/lattices/squarelattice.jl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""
22
FiniteStrip(L::Int, N::Int)
33
4-
An finite strip with `L` sites per rung and `N` sites per unit cell.
4+
A finite strip with a width of `L` and a total number of `N` sites.
5+
6+
This representes an `L` by `N÷L` rectangular patch.
57
"""
68
struct FiniteStrip <: AbstractLattice{2}
79
L::Int
@@ -39,7 +41,7 @@ Base.isfinite(::Type{InfiniteStrip}) = false
3941
"""
4042
FiniteCylinder(L::Int, N::Int)
4143
42-
An finite cylinder with `L` sites per rung and `N` sites per unit cell.
44+
A cylinder with circumference `L` and `N` sites in total.
4345
"""
4446
struct FiniteCylinder <: AbstractLattice{2}
4547
L::Int
@@ -79,7 +81,7 @@ Base.isfinite(::Type{InfiniteCylinder}) = false
7981
"""
8082
FiniteHelix(L::Integer, N::Integer)
8183
82-
An finite helix with `L` sites per rung and `N` sites per unit cell.
84+
A finite helix with `L` sites per rung and `N` sites in total.
8385
"""
8486
struct FiniteHelix <: AbstractLattice{2}
8587
L::Int
@@ -153,7 +155,7 @@ function nearest_neighbours(lattice::FiniteStrip)
153155
for i in 1:rows, j in 1:(cols - 1))
154156
vertical = (LatticePoint((i, j), lattice) => LatticePoint((i + 1, j), lattice)
155157
for i in 1:(rows - 1), j in 1:cols)
156-
return Iterators.flatten((horizontal, vertical))
158+
return [horizontal..., vertical...]
157159
end
158160
function nearest_neighbours(lattice::FiniteCylinder)
159161
rows = lattice.L
@@ -162,7 +164,7 @@ function nearest_neighbours(lattice::FiniteCylinder)
162164
for i in 1:rows, j in 1:(cols - 1))
163165
vertical = (LatticePoint((i, j), lattice) => LatticePoint((i + 1, j), lattice)
164166
for i in 1:rows, j in 1:cols)
165-
return Iterators.flatten((horizontal, vertical))
167+
return [horizontal..., vertical...]
166168
end
167169
function nearest_neighbours(lattice::FiniteHelix)
168170
rows = lattice.L
@@ -171,7 +173,7 @@ function nearest_neighbours(lattice::FiniteHelix)
171173
for i in 1:rows, j in 1:(cols - 1))
172174
vertical = (LatticePoint((i, j), lattice) => LatticePoint((i + 1, j), lattice)
173175
for i in 1:rows, j in 1:cols if (i != rows && j != cols))
174-
return Iterators.flatten((horizontal, vertical))
176+
return [horizontal..., vertical...]
175177
end
176178
function nearest_neighbours(lattice::Union{InfiniteStrip,InfiniteCylinder,InfiniteHelix})
177179
V = vertices(lattice)
@@ -187,6 +189,15 @@ function nearest_neighbours(lattice::Union{InfiniteStrip,InfiniteCylinder,Infini
187189
return neighbours
188190
end
189191

192+
function next_nearest_neighbours(lattice::AbstractLattice{2})
193+
diag1 = (i => i + (1, 1) for i in vertices(lattice) if checkbounds(Bool, lattice,
194+
(i.coordinates .+
195+
(1, 1))...))
196+
diag2 = (i => i + (1, -1) for i in vertices(lattice) if checkbounds(Bool, lattice,
197+
(i.coordinates .+
198+
(1, -1))...))
199+
return [diag1..., diag2...]
200+
end
190201
function next_nearest_neighbours(lattice::Union{InfiniteStrip,InfiniteCylinder,
191202
InfiniteHelix})
192203
V = vertices(lattice)

test/lattices.jl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,52 @@ end
152152
@test_throws ArgumentError HoneycombYC(3)
153153
@test_throws ArgumentError HoneycombYC(4, 6)
154154
end
155+
156+
@testset "FiniteCylinder" begin
157+
for L in 2:5
158+
lattice = FiniteCylinder(L)
159+
@test length(nearest_neighbours(lattice)) == L
160+
@test length(next_nearest_neighbours(lattice)) == 0
161+
for n in 2:4
162+
lattice = FiniteCylinder(L, n * L)
163+
V = vertices(lattice)
164+
@test length(lattice) == length(V) == n * L
165+
@test lattice[1, 1] == first(V)
166+
167+
NN = nearest_neighbours(lattice)
168+
@test length(NN) == 2 * n * L - L
169+
@test allunique(NN)
170+
171+
NNN = next_nearest_neighbours(lattice)
172+
@test length(NNN) == 2 * (n - 1) * L
173+
174+
@test allunique(NNN)
175+
176+
@test_throws ArgumentError FiniteCylinder(L, n * L + 1)
177+
end
178+
end
179+
end
180+
181+
@testset "FiniteStrip" begin
182+
for L in 2:8, n in 2:4
183+
N = n * L
184+
lattice = FiniteStrip(L, N)
185+
V = vertices(lattice)
186+
187+
# Test the number of vertices
188+
@test length(lattice) == length(V) == N
189+
190+
# Test the first vertex
191+
@test lattice[1, 1] == first(V)
192+
193+
# Test nearest neighbors
194+
NN = nearest_neighbours(lattice)
195+
@test length(NN) == 2N - L - n # coordination number 4 - edge effects
196+
@test allunique(NN)
197+
198+
# Test next-nearest neighbors
199+
NNN = next_nearest_neighbours(lattice)
200+
@test length(NNN) == 2N - 2L - (2n - 2) # coordination number 4 - edge effects
201+
@test allunique(NNN)
202+
end
203+
end

0 commit comments

Comments
 (0)