From 37672e65fdbabf02ffb3f2848afceafc28a5da50 Mon Sep 17 00:00:00 2001 From: singularitti Date: Mon, 14 Aug 2023 17:50:09 -0400 Subject: [PATCH 1/8] Fix `niggli_reduce` Remember to put `transpose` in `Base.cconvert` --- src/reduce.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/reduce.jl b/src/reduce.jl index 905ee6f..05c656f 100644 --- a/src/reduce.jl +++ b/src/reduce.jl @@ -23,10 +23,10 @@ and the matrix elements have to be almost integers. See also [Computing rigid rotation introduced by idealization](@ref). """ function niggli_reduce(lattice::Lattice, symprec=1e-5) - clattice = transpose(convert(Matrix{Cdouble}, lattice)) - @ccall libsymspg.spg_niggli_reduce(clattice::Ptr{Cdouble}, symprec::Cdouble)::Cint + niggli_lattice = Base.cconvert(Matrix{Cdouble}, transpose(lattice)) # `transpose` must before `cconvert`! + @ccall libsymspg.spg_niggli_reduce(niggli_lattice::Ptr{Cdouble}, symprec::Cdouble)::Cint check_error() - return Lattice(transpose(clattice)) + return Lattice(transpose(niggli_lattice)) end function niggli_reduce(cell::Cell, symprec=1e-5) lattice = Lattice(cell) From 9b481b8da9650c7b00401332ee1dfd2a5de0c173 Mon Sep 17 00:00:00 2001 From: singularitti Date: Mon, 14 Aug 2023 17:50:35 -0400 Subject: [PATCH 2/8] Add "Test Niggli reduction from Python example" --- test/reduce.jl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/reduce.jl b/test/reduce.jl index 1cf3d9c..79dfb0a 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -1,3 +1,16 @@ +# From https://github.com/spglib/spglib/blob/d8c39f6/example/python_api/example_full.py#L119-L122 +@testset "Test Niggli reduction from Python example" begin + 𝐚 = [3, 0, 0] + 𝐛 = [-3.66666667, 3.68178701, 0] + 𝐜 = [-0.66666667, -1.3429469, 1.32364995] + lattice = Lattice(𝐚, 𝐛, 𝐜) + @test niggli_reduce(lattice, 1e-5) ≈ Lattice([ + [-0.66666667, -1.3429469, 1.32364995], + [2.33333333, -1.3429469, 1.32364995], + [0.99999999, 0.99589321, 2.6472999], + ]) +end + # From https://github.com/unkcpz/LibSymspg.jl/blob/f342e72/test/runtests.jl#L83-L85 @testset "Niggli reduce" begin lattice = Lattice([ From a93a590a39cbd6be7bcdfaf23791139ca7704d07 Mon Sep 17 00:00:00 2001 From: singularitti Date: Mon, 14 Aug 2023 17:51:07 -0400 Subject: [PATCH 3/8] Fix "Test Niggli reduction" --- test/reduce.jl | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/test/reduce.jl b/test/reduce.jl index 79dfb0a..8911192 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -12,17 +12,13 @@ end # From https://github.com/unkcpz/LibSymspg.jl/blob/f342e72/test/runtests.jl#L83-L85 -@testset "Niggli reduce" begin +@testset "Test Niggli reduction" begin lattice = Lattice([ - 4.0 0.0 0.0 - 20.0 2.0 0.0 - 0.0 0.0 12.0 - ]) - @test niggli_reduce(lattice, 1e-3) ≈ [ - 0.0 4.0 0.0 - -2.0 0.0 0.0 - 0.0 0.0 12.0 - ] + 4 0 0 + 20 2 0 + 0 0 12 + ]) # Note in `LibSymspg.jl`, the lattice is transposed + @test niggli_reduce(lattice, 1e-3) == Lattice([[0, -2, 0], [4, 0, 0], [0, 0, 12]]) # Compared also with Python results cell = Cell(lattice, [[0.0, 0.0, 0.0], [0.05, 0.05, 0.05]], [1, 1]) rcell = niggli_reduce(cell) c1 = Ref(cell.lattice) .* cell.positions @@ -30,6 +26,7 @@ end # Cartesian coordinates should remain the same @test c1 == c2 end + # From https://github.com/unkcpz/LibSymspg.jl/blob/f342e72/test/runtests.jl#L87-89 @testset "Delaunay reduce" begin lattice = Lattice([ From cbd3032474667bbc69911fa607e8eb92ca486b08 Mon Sep 17 00:00:00 2001 From: singularitti Date: Mon, 14 Aug 2023 17:57:14 -0400 Subject: [PATCH 4/8] Fix `delaunay_reduce` Remember to put `transpose` in `Base.cconvert` --- src/reduce.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/reduce.jl b/src/reduce.jl index 05c656f..fb4c7dc 100644 --- a/src/reduce.jl +++ b/src/reduce.jl @@ -62,10 +62,12 @@ and the matrix elements have to be almost integers. See also [Computing rigid rotation introduced by idealization](@ref). """ function delaunay_reduce(lattice::Lattice, symprec=1e-5) - clattice = transpose(convert(Matrix{Cdouble}, lattice)) - @ccall libsymspg.spg_delaunay_reduce(clattice::Ptr{Cdouble}, symprec::Cdouble)::Cint + delaunay_lattice = Base.cconvert(Matrix{Cdouble}, transpose(lattice)) # `transpose` must before `cconvert`! + @ccall libsymspg.spg_delaunay_reduce( + delaunay_lattice::Ptr{Cdouble}, symprec::Cdouble + )::Cint check_error() - return Lattice(transpose(clattice)) + return Lattice(transpose(delaunay_lattice)) end function delaunay_reduce(cell::Cell, symprec=1e-5) lattice = Lattice(cell) From ba402cdbcaeb7dd01c9a4274e25637dc3f75612b Mon Sep 17 00:00:00 2001 From: singularitti Date: Mon, 14 Aug 2023 17:57:56 -0400 Subject: [PATCH 5/8] Fix "Test Delaunay reduction" --- test/reduce.jl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/reduce.jl b/test/reduce.jl index 8911192..c24445c 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -28,19 +28,19 @@ end end # From https://github.com/unkcpz/LibSymspg.jl/blob/f342e72/test/runtests.jl#L87-89 -@testset "Delaunay reduce" begin +@testset "Test Delaunay reduction" begin lattice = Lattice([ - 4.0 0.0 0.0 - 20.0 2.0 0.0 - 0.0 0.0 12.0 + 4 0 0 + 20 2 0 + 0 0 12 + ]) # Note in `LibSymspg.jl`, the lattice is transposed + @test delaunay_reduce(lattice, 1e-3) == Lattice([ + 0 -4 0 + 2 0 0 + 0 0 12 ]) - @test delaunay_reduce(lattice, 1e-3) ≈ [ - 0.0 -4.0 0.0 - 2.0 0.0 0.0 - 0.0 0.0 12.0 - ] cell = Cell(lattice, [[0.0, 0.0, 0.0], [0.05, 0.05, 0.05]], [1, 1]) - rcell = niggli_reduce(cell) + rcell = delaunay_reduce(cell) c1 = Ref(cell.lattice) .* cell.positions c2 = Ref(rcell.lattice) .* rcell.positions # Cartesian coordinates should remain the same From 89d47c6d96085720647eafb7400cb716a7801580 Mon Sep 17 00:00:00 2001 From: singularitti Date: Mon, 14 Aug 2023 18:29:07 -0400 Subject: [PATCH 6/8] Update "Test Niggli reduction from Python example" to "Test examples from Python" --- test/reduce.jl | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/test/reduce.jl b/test/reduce.jl index c24445c..c57b4cd 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -1,14 +1,39 @@ -# From https://github.com/spglib/spglib/blob/d8c39f6/example/python_api/example_full.py#L119-L122 -@testset "Test Niggli reduction from Python example" begin - 𝐚 = [3, 0, 0] - 𝐛 = [-3.66666667, 3.68178701, 0] - 𝐜 = [-0.66666667, -1.3429469, 1.32364995] - lattice = Lattice(𝐚, 𝐛, 𝐜) - @test niggli_reduce(lattice, 1e-5) ≈ Lattice([ - [-0.66666667, -1.3429469, 1.32364995], - [2.33333333, -1.3429469, 1.32364995], - [0.99999999, 0.99589321, 2.6472999], - ]) +@testset "Test examples from Python" begin + # From https://github.com/spglib/spglib/blob/d8c39f6/example/python_api/example_full.py#L119-L122 + @testset "Unknown lattice" begin + 𝐚 = [3, 0, 0] + 𝐛 = [-3.66666667, 3.68178701, 0] + 𝐜 = [-0.66666667, -1.3429469, 1.32364995] + lattice = Lattice(𝐚, 𝐛, 𝐜) + @test niggli_reduce(lattice, 1e-5) ≈ Lattice([ + [-0.66666667, -1.3429469, 1.32364995], + [2.33333333, -1.3429469, 1.32364995], + [0.99999999, 0.99589321, 2.6472999], + ]) # Compared with Python result + @test delaunay_reduce(lattice, 1e-5) ≈ Lattice([ + [-0.66666667, -1.3429469, 1.32364995], + [-2.33333333, 1.3429469, -1.32364995], + [-0.99999999, -0.99589321, -2.6472999], + ]) # Compared with Python result + end + # From https://spglib.github.io/spglib/definition.html#computing-rigid-rotation-introduced-by-idealization + @testset "Test another lattice" begin + lattice = Lattice([ + [5.0759761474456697, 5.0759761474456697, 0], + [-2.8280307701821314, 2.8280307701821314, 0], + [0, 0, 8.57154746], + ]) + @test niggli_reduce(lattice, 1e-5) ≈ Lattice([ + [2.82803077, -2.82803077, 0.0], + [-5.07597615, -5.07597615, 0.0], + [0.0, 0.0, -8.57154746], + ]) # Compared with Python result + @test delaunay_reduce(lattice, 1e-5) ≈ Lattice([ + [2.82803077, -2.82803077, 0.0], + [-5.07597615, -5.07597615, 0.0], + [0.0, 0.0, -8.57154746], + ]) # Compared with Python result + end end # From https://github.com/unkcpz/LibSymspg.jl/blob/f342e72/test/runtests.jl#L83-L85 From 3c5a4588ce94cf3b1c1598a148f67bb3c1ede23c Mon Sep 17 00:00:00 2001 From: singularitti Date: Mon, 14 Aug 2023 18:29:13 -0400 Subject: [PATCH 7/8] Add "Test an example from C" --- test/reduce.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/reduce.jl b/test/reduce.jl index c57b4cd..2eb62e9 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -71,3 +71,14 @@ end # Cartesian coordinates should remain the same @test c1 == c2 end + +# From https://github.com/spglib/spglib/blob/4aa0806/test/functional/c/test_delaunay.cpp +@testset "Test an example from C" begin + lattice = Lattice([ + 1 0 0 + 0 37 10 + 0 11 3 + ]) + @test niggli_reduce(lattice, 1e-5) == Lattice([[0, 1, 0], [1, 0, 0], [0, 0, -1]]) # Compared with Python result + @test delaunay_reduce(lattice, 1e-5) ≈ Lattice([[1, 0, 0], [0, -1, 0], [0, 0, -1]]) # Compared with Python result +end From 358b203e5cffb921d69a491c33dd61d734e07dad Mon Sep 17 00:00:00 2001 From: singularitti Date: Mon, 14 Aug 2023 18:30:21 -0400 Subject: [PATCH 8/8] Move tests under "Test examples from `LibSymspg.jl`" --- test/reduce.jl | 54 +++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/test/reduce.jl b/test/reduce.jl index 2eb62e9..2df41c4 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -36,40 +36,36 @@ end end -# From https://github.com/unkcpz/LibSymspg.jl/blob/f342e72/test/runtests.jl#L83-L85 -@testset "Test Niggli reduction" begin +@testset "Test examples from `LibSymspg.jl`" begin lattice = Lattice([ 4 0 0 20 2 0 0 0 12 ]) # Note in `LibSymspg.jl`, the lattice is transposed - @test niggli_reduce(lattice, 1e-3) == Lattice([[0, -2, 0], [4, 0, 0], [0, 0, 12]]) # Compared also with Python results - cell = Cell(lattice, [[0.0, 0.0, 0.0], [0.05, 0.05, 0.05]], [1, 1]) - rcell = niggli_reduce(cell) - c1 = Ref(cell.lattice) .* cell.positions - c2 = Ref(rcell.lattice) .* rcell.positions - # Cartesian coordinates should remain the same - @test c1 == c2 -end - -# From https://github.com/unkcpz/LibSymspg.jl/blob/f342e72/test/runtests.jl#L87-89 -@testset "Test Delaunay reduction" begin - lattice = Lattice([ - 4 0 0 - 20 2 0 - 0 0 12 - ]) # Note in `LibSymspg.jl`, the lattice is transposed - @test delaunay_reduce(lattice, 1e-3) == Lattice([ - 0 -4 0 - 2 0 0 - 0 0 12 - ]) - cell = Cell(lattice, [[0.0, 0.0, 0.0], [0.05, 0.05, 0.05]], [1, 1]) - rcell = delaunay_reduce(cell) - c1 = Ref(cell.lattice) .* cell.positions - c2 = Ref(rcell.lattice) .* rcell.positions - # Cartesian coordinates should remain the same - @test c1 == c2 + # From https://github.com/unkcpz/LibSymspg.jl/blob/f342e72/test/runtests.jl#L83-L85 + @testset "Test Niggli reduction" begin + @test niggli_reduce(lattice, 1e-3) == Lattice([[0, -2, 0], [4, 0, 0], [0, 0, 12]]) # Compared also with Python results + cell = Cell(lattice, [[0.0, 0.0, 0.0], [0.05, 0.05, 0.05]], [1, 1]) + rcell = niggli_reduce(cell) + c1 = Ref(cell.lattice) .* cell.positions + c2 = Ref(rcell.lattice) .* rcell.positions + # Cartesian coordinates should remain the same + @test c1 == c2 + end + # From https://github.com/unkcpz/LibSymspg.jl/blob/f342e72/test/runtests.jl#L87-89 + @testset "Test Delaunay reduction" begin + @test delaunay_reduce(lattice, 1e-3) == Lattice([ + 0 -4 0 + 2 0 0 + 0 0 12 + ]) + cell = Cell(lattice, [[0.0, 0.0, 0.0], [0.05, 0.05, 0.05]], [1, 1]) + rcell = delaunay_reduce(cell) + c1 = Ref(cell.lattice) .* cell.positions + c2 = Ref(rcell.lattice) .* rcell.positions + # Cartesian coordinates should remain the same + @test c1 == c2 + end end # From https://github.com/spglib/spglib/blob/4aa0806/test/functional/c/test_delaunay.cpp