Skip to content

Commit

Permalink
Test reshaping check
Browse files Browse the repository at this point in the history
  • Loading branch information
kbarros committed Oct 12, 2024
1 parent a11e305 commit 434b9d9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/Reshaping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ function reshape_supercell(sys::System, shape)

orig = orig_crystal(sys)
check_shape_commensurate(orig, shape)

shape_in_prim = primitive_cell(orig) \ shape
prim_cell = @something primitive_cell(orig) Mat3(I)
shape_in_prim = prim_cell \ shape
@assert all_integer(shape_in_prim; orig.symprec)
shape_in_prim = round.(Int, shape_in_prim)

Expand Down
18 changes: 7 additions & 11 deletions src/Symmetry/Crystal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -499,17 +499,13 @@ end


function check_shape_commensurate(cryst, shape)
# Primitive lattice vectors in global Cartesian coordinates
prim_cell = primitive_cell(cryst)

if isnothing(prim_cell)
if !all_integer(shape; cryst.symprec)
error("Cell shape must be a 3×3 matrix of integers. Received $shape.")
end
else
shape_in_prim = prim_cell \ shape
if !all_integer(shape_in_prim; cryst.symprec)
error("Cell shape must be integer multiples of primitive lattice vectors. Calculated $shape_in_prim.")
prim_cell = @something primitive_cell(cryst) Mat3(I)
shape_in_prim = prim_cell \ shape
if !all_integer(shape_in_prim; cryst.symprec)
if prim_cell I
error("Elements of `shape` must be integer. Received $shape.")
else
error("Elements of `primitive_cell(cryst) \\ shape` must be integer. Calculated $shape_in_prim.")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,4 @@ end
# * Missing μ0_μB² in enable_dipole_dipole! and
# modify_exchange_with_truncated_dipole_dipole!
# * energy_ϵ argument in SpinWaveTheory
# * setting argument in Crystal
# * setting argument in Crystal
25 changes: 25 additions & 0 deletions test/test_resize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@
@test energy_per_site(newsys) 2.55
end


@testitem "Validate reshaping" begin
using LinearAlgebra

latvecs = lattice_vectors(1, 1, 1, 90, 90, 90)
positions = [[0, 0, 0]/2, [1, 1, 1]/2]
cryst = Crystal(latvecs, positions)
sys = System(cryst, [1 => Moment(s=1, g=2)], :dipole)

primcell = primitive_cell(cryst)
reshape_supercell(sys, Diagonal([2, 3, 4])) # Fine
reshape_supercell(sys, primcell) # Fine
reshape_supercell(sys, primcell * Diagonal([2, 3, 4])) # Fine
msg = "Elements of `primitive_cell(cryst) \\ shape` must be integer. Calculated [3.5 0.5 -0.5; 1.0 3.0 -1.0; 0.5 -0.5 2.5]."
@test_throws msg reshape_supercell(sys, Diagonal([2, 3, 4]) * primcell)

positions = [[0, 0, 0]]
cryst = Crystal(latvecs, positions)
sys = System(cryst, [1 => Moment(s=1, g=2)], :dipole)
reshape_supercell(sys, Diagonal([2.0, 3, 4]))
msg = "Elements of `shape` must be integer. Received [2.5 0.0 0.0; 0.0 3.0 0.0; 0.0 0.0 4.0]."
@test_throws msg reshape_supercell(sys, Diagonal([2.5, 3, 4]))
end


@testitem "Equivalent reshaping" begin
using LinearAlgebra

Expand Down

0 comments on commit 434b9d9

Please sign in to comment.