From 0178ca95ee231f0310909a10083148562901dd12 Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Sun, 26 Mar 2023 23:41:49 -0400 Subject: [PATCH 1/8] Change version number to 0.2.0 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 27230a0..6175e5c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BrillouinZoneMeshes" uuid = "9f696214-9961-49f3-89a2-cb1ea204eb6e" authors = ["Xiansheng Cai", "Tao Wang", "Kun Chen"] -version = "0.1.0" +version = "0.2.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From d4dc905c4721f5d2c3587b07d25e8439494d89c6 Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Mon, 27 Mar 2023 00:11:57 -0400 Subject: [PATCH 2/8] move some packages used in examples to extras --- Project.toml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 6175e5c..7c61a09 100644 --- a/Project.toml +++ b/Project.toml @@ -6,13 +6,10 @@ version = "0.2.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" CompositeGrids = "b5136c89-beeb-4521-9139-60d2cac8be56" -FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" -QHull = "a8468747-bd6f-53ef-9e5c-744dbc5c59e7" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665" -Spglib = "f761d5c5-86db-4880-b97f-9680a7cccfb5" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" spglib_jll = "ac4a9f1e-bdb2-5204-990c-47c8b2f70d4e" @@ -21,10 +18,14 @@ spglib_jll = "ac4a9f1e-bdb2-5204-990c-47c8b2f70d4e" AbstractTrees = "0.3, 0.4" CompositeGrids = "0.0.6, 0.1" StaticArrays = "1" +Roots = "2.0" julia = "1.6" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" +QHull = "a8468747-bd6f-53ef-9e5c-744dbc5c59e7" +Spglib = "f761d5c5-86db-4880-b97f-9680a7cccfb5" [targets] test = ["Test"] From 3227e9a1184b2fb1eac448bce06d10b3183a178b Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Mon, 27 Mar 2023 00:16:05 -0400 Subject: [PATCH 3/8] add Spglib to compat --- Project.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 7c61a09..cdf14da 100644 --- a/Project.toml +++ b/Project.toml @@ -13,19 +13,21 @@ Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" spglib_jll = "ac4a9f1e-bdb2-5204-990c-47c8b2f70d4e" +Spglib = "f761d5c5-86db-4880-b97f-9680a7cccfb5" [compat] AbstractTrees = "0.3, 0.4" CompositeGrids = "0.0.6, 0.1" StaticArrays = "1" Roots = "2.0" +Spglib = "0.6.0" julia = "1.6" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" QHull = "a8468747-bd6f-53ef-9e5c-744dbc5c59e7" -Spglib = "f761d5c5-86db-4880-b97f-9680a7cccfb5" + [targets] test = ["Test"] From c34a6662fceb6367dd85be5514313d33c0a34497 Mon Sep 17 00:00:00 2001 From: Kun Chen Date: Mon, 27 Mar 2023 07:52:32 -0400 Subject: [PATCH 4/8] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d0b7618..0f42024 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Setup with: and here are some examples of usage: -```jldoctest +```julia julia> using BrillouinZoneMeshes; DIM = 2; lattice = Matrix([1.0 0; 0 1]'); br = BZMeshes.Cell(lattice=lattice); umesh = BZMeshes.UniformBZMesh(cell=br, size=(4, 4)); mm = MeshMaps.MeshMap(umesh); rmesh = ReducedBZMesh(umesh, mm); julia> length(umesh), length(rmesh) @@ -102,4 +102,4 @@ Monkhorst-Pack meshes, could be generated via `BZMeshes.UniformBZMesh` with different parameters. The default parameter of the constructor of `BZMeshes.UniformBZMesh` generates Gamma-centered mesh, while two constructors for M-P mesh, `Monkhorst_Pack` and `DFTK_Monkhorst_Pack`, follow conventions -from VASP and DFTK respectively. \ No newline at end of file +from VASP and DFTK respectively. From 76b98f026141a9b76cedda696e196d98acd3fc08 Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Wed, 5 Apr 2023 22:53:46 -0400 Subject: [PATCH 5/8] wip --- src/BaseMesh.jl | 7 ++++++- test/test_typestable.jl | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/test_typestable.jl diff --git a/src/BaseMesh.jl b/src/BaseMesh.jl index 087a1c4..16b01d1 100644 --- a/src/BaseMesh.jl +++ b/src/BaseMesh.jl @@ -41,7 +41,12 @@ export fractional_coordinates, cartesian_coordinates function Base.getindex(mesh::AbstractUniformMesh{T,DIM}, inds...) where {T,DIM} n = SVector{DIM,Int}(inds) - return mesh.origin + lattice_vector(mesh) * ((n .- 1 .+ mesh.shift) ./ mesh.size) + # fracx = SVector{DIM,Float64}((n .- 1 .+ mesh.shift) ./ mesh.size) + fracx = SVector{DIM,Float64}((n[i] - 1 + mesh.shift[i]) / mesh.size[i] for i in 1:DIM) + # displace = lattice_vector(mesh) * fracx + latvec = lattice_vector(mesh) + displace = SVector{DIM,Float64}(sum(latvec[i, j] * fracx[j] for j in 1:DIM) for i in 1:DIM) + return mesh.origin + displace end function Base.getindex(mesh::AbstractUniformMesh, I::Int) diff --git a/test/test_typestable.jl b/test/test_typestable.jl new file mode 100644 index 0000000..3254241 --- /dev/null +++ b/test/test_typestable.jl @@ -0,0 +1,14 @@ + +using BrillouinZoneMeshes +using Test + +@testset "Type Stable?" begin + DIM = 2 + N1, N2 = 3, 5 + lattice = Matrix([1/N1/2 0; 0 1.0/N2/2]') .* 2π + # so that bzmesh[i,j] = (2i-1,2j-1) + cell = BZMeshes.Cell(lattice=lattice) + mesh = BaseMesh.UMesh(br=cell, origin=ones(DIM) ./ 2, size=(N1, N2), shift=zeros(DIM)) + + @inferred mesh[1] +end \ No newline at end of file From dfd96a1c7d2f7304262ad9c64f7dbb7c6b27e301 Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Thu, 6 Apr 2023 13:31:27 -0400 Subject: [PATCH 6/8] fixed type stable problem caused by mesh.shift --- src/BaseMesh.jl | 11 ++++------- test/BZMeshes.jl | 4 ++++ test/BaseMesh.jl | 11 +++++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/BaseMesh.jl b/src/BaseMesh.jl index 16b01d1..da6fd2f 100644 --- a/src/BaseMesh.jl +++ b/src/BaseMesh.jl @@ -41,12 +41,8 @@ export fractional_coordinates, cartesian_coordinates function Base.getindex(mesh::AbstractUniformMesh{T,DIM}, inds...) where {T,DIM} n = SVector{DIM,Int}(inds) - # fracx = SVector{DIM,Float64}((n .- 1 .+ mesh.shift) ./ mesh.size) - fracx = SVector{DIM,Float64}((n[i] - 1 + mesh.shift[i]) / mesh.size[i] for i in 1:DIM) - # displace = lattice_vector(mesh) * fracx - latvec = lattice_vector(mesh) - displace = SVector{DIM,Float64}(sum(latvec[i, j] * fracx[j] for j in 1:DIM) for i in 1:DIM) - return mesh.origin + displace + mshift = SVector{DIM,Float64}(mesh.shift) + return mesh.origin + lattice_vector(mesh) * ((n .- 1 .+ mshift) ./ mesh.size) end function Base.getindex(mesh::AbstractUniformMesh, I::Int) @@ -55,7 +51,8 @@ end function Base.getindex(mesh::AbstractUniformMesh{T,DIM}, ::Type{<:FracCoords}, I::Int) where {T,DIM} n = SVector{DIM,Int}(AbstractMeshes._ind2inds(mesh.size, I)) - return inv_lattice_vector(mesh) * mesh.origin + (n .- 1 .+ mesh.shift) ./ mesh.size + mshift = SVector{DIM,Float64}(mesh.shift) + return inv_lattice_vector(mesh) * mesh.origin + (n .- 1 .+ mshift) ./ mesh.size end """ diff --git a/test/BZMeshes.jl b/test/BZMeshes.jl index 6f472bb..bac77d4 100644 --- a/test/BZMeshes.jl +++ b/test/BZMeshes.jl @@ -28,6 +28,10 @@ println(bzmesh) display(bzmesh) + @inferred bzmesh[1] + @inferred bzmesh[1, 1] + @inferred bzmesh[AbstractMeshes.FracCoords, 1] + for (pi, p) in enumerate(bzmesh) @test bzmesh[pi] ≈ p # linear index inds = AbstractMeshes._ind2inds(size(bzmesh), pi) diff --git a/test/BaseMesh.jl b/test/BaseMesh.jl index 4a63f31..51eb21e 100644 --- a/test/BaseMesh.jl +++ b/test/BaseMesh.jl @@ -19,6 +19,10 @@ cell = BZMeshes.Cell(lattice=lattice) mesh = BaseMesh.UMesh(br=cell, origin=ones(DIM) ./ 2, size=(N1, N2), shift=zeros(DIM)) + @inferred mesh[1] + @inferred mesh[1, 1] + @inferred mesh[AbstractMeshes.FracCoords, 1] + @test length(mesh) == N1 * N2 @test size(mesh) == (N1, N2) @@ -62,6 +66,8 @@ dpm = DirectProdMesh(r, theta, phi) println(size(dpm)) + @inferred dpm[1] + vol = 0.0 for (pi, p) in enumerate(dpm) i, j, k = AbstractMeshes._ind2inds(size(dpm), pi) @@ -91,6 +97,9 @@ cm = ProdMesh(grids, theta) println([cm.grids[i].panel[2] for i in 1:length(theta)]) println(size(cm)) + + @inferred cm[1] + for j in 1:length(cm.mesh) for i in 1:length(cm.grids[j]) p = cm[i, j] @@ -117,6 +126,8 @@ cm2 = ChebMesh(origin, latvec, cm) + @inferred cm[1] + vol = 0.0 for (i, x) in enumerate(cm2) @test cm2[i] ≈ x From 6d9366b028423869d516f108e5c5b581953fb687 Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Thu, 6 Apr 2023 13:34:32 -0400 Subject: [PATCH 7/8] Float64 -> T --- src/BaseMesh.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BaseMesh.jl b/src/BaseMesh.jl index da6fd2f..7840af2 100644 --- a/src/BaseMesh.jl +++ b/src/BaseMesh.jl @@ -41,7 +41,7 @@ export fractional_coordinates, cartesian_coordinates function Base.getindex(mesh::AbstractUniformMesh{T,DIM}, inds...) where {T,DIM} n = SVector{DIM,Int}(inds) - mshift = SVector{DIM,Float64}(mesh.shift) + mshift = SVector{DIM,T}(mesh.shift) return mesh.origin + lattice_vector(mesh) * ((n .- 1 .+ mshift) ./ mesh.size) end @@ -51,7 +51,7 @@ end function Base.getindex(mesh::AbstractUniformMesh{T,DIM}, ::Type{<:FracCoords}, I::Int) where {T,DIM} n = SVector{DIM,Int}(AbstractMeshes._ind2inds(mesh.size, I)) - mshift = SVector{DIM,Float64}(mesh.shift) + mshift = SVector{DIM,T}(mesh.shift) return inv_lattice_vector(mesh) * mesh.origin + (n .- 1 .+ mshift) ./ mesh.size end From 6d69025d2b3e341fd131c015e6a02e53ef980850 Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Thu, 6 Apr 2023 15:59:23 -0400 Subject: [PATCH 8/8] replace hand made tests with @test_throws --- test/AbstractMeshes.jl | 38 ++++++++++++++------------------------ test/BaseMesh.jl | 12 +----------- test/MeshMap.jl | 12 +----------- 3 files changed, 16 insertions(+), 46 deletions(-) diff --git a/test/AbstractMeshes.jl b/test/AbstractMeshes.jl index 68a1814..10104b0 100644 --- a/test/AbstractMeshes.jl +++ b/test/AbstractMeshes.jl @@ -1,16 +1,6 @@ @testset "AbstractMeshes" begin using BrillouinZoneMeshes.AbstractMeshes - function test_func_not_implemented(func, obj) - # if a func required is not implemented for obj - # an error occur - try - func(obj) - catch e - @test e isa ErrorException - end - end - # create a random concrete mesh DIM = 2 N1, N2 = 3, 5 @@ -38,24 +28,24 @@ # test error thrown from funcs not implemented struct NotAMesh{T,DIM} <: AbstractMesh{T,DIM} end notamesh = NotAMesh{Float64,3}() - test_func_not_implemented(println, notamesh) + @test_throws ErrorException println(notamesh) - test_func_not_implemented(x -> getindex(x, 1), notamesh) - test_func_not_implemented(x -> getindex(x, 1, 2, 3), notamesh) - test_func_not_implemented(x -> getindex(x, FracCoords, 1), notamesh) - test_func_not_implemented(x -> getindex(x, FracCoords, 1, 2, 3), notamesh) + @test_throws ErrorException notamesh[1] + @test_throws ErrorException notamesh[1, 2, 3] + @test_throws ErrorException notamesh[FracCoords, 1] + @test_throws ErrorException notamesh[FracCoords, 1, 2, 3] - test_func_not_implemented(x -> locate(x, 1), notamesh) - test_func_not_implemented(x -> volume(x, 1), notamesh) - test_func_not_implemented(volume, notamesh) + @test_throws ErrorException locate(notamesh, 1) + @test_throws ErrorException volume(notamesh, 1) + @test_throws ErrorException volume(notamesh) - test_func_not_implemented(lattice_vector, notamesh) - test_func_not_implemented(inv_lattice_vector, notamesh) - test_func_not_implemented(cell_volume, notamesh) + @test_throws ErrorException lattice_vector(notamesh) + @test_throws ErrorException inv_lattice_vector(notamesh) + @test_throws ErrorException cell_volume(notamesh) - test_func_not_implemented(x -> integrate([1,], x), notamesh) - test_func_not_implemented(x -> interp([1,], x, 1), notamesh) + @test_throws ErrorException integrate([1,], notamesh) + @test_throws ErrorException interp([1,], notamesh, 1) - test_func_not_implemented(x -> interval(x, 1), notamesh) + @test_throws ErrorException interval(notamesh, 1) end \ No newline at end of file diff --git a/test/BaseMesh.jl b/test/BaseMesh.jl index 51eb21e..1d399fc 100644 --- a/test/BaseMesh.jl +++ b/test/BaseMesh.jl @@ -1,16 +1,6 @@ @testset "Base Mesh" begin rng = MersenneTwister(1234) - function test_func_not_implemented(func, obj) - # if a func required is not implemented for obj - # an error occur - try - func(obj) - catch e - @test e isa ErrorException - end - end - @testset "UMesh" begin DIM = 2 N1, N2 = 3, 5 @@ -49,7 +39,7 @@ # basics struct NotAPM{T,DIM} <: BaseMesh.AbstractProdMesh{T,DIM} end - test_func_not_implemented(x -> BaseMesh._getgrid(x, 1), NotAPM{Int,3}()) + @test_throws ErrorException BaseMesh._getgrid(NotAPM{Int,3}(), 1) @testset "DirectProdMesh" begin N, M = 3, 2 diff --git a/test/MeshMap.jl b/test/MeshMap.jl index 40d2046..288c54a 100644 --- a/test/MeshMap.jl +++ b/test/MeshMap.jl @@ -1,22 +1,12 @@ @testset "MeshMaps" begin - function test_func_not_implemented(func, obj) - # if a func required is not implemented for obj - # an error occur - try - func(obj) - catch e - @test e isa ErrorException - end - end - locate, volume = MeshMaps.locate, MeshMaps.volume @testset "MeshMap" begin struct NotAMesh{T,DIM} <: AbstractMesh{T,DIM} end notamesh = NotAMesh{Float64,3}() - test_func_not_implemented(MeshMap, notamesh) + @test_throws ErrorException MeshMap(notamesh) # test MeshMap constructor map = [1, 2, 2, 1, 2, 6, 6, 2, 2, 6, 6, 2, 1, 2, 2, 1]