From 083ad8e79e202da488fedde4ad682d91c8c7b3b2 Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Mon, 20 Mar 2023 23:06:50 -0400 Subject: [PATCH 1/8] reorganizing tests --- test/AbstractMeshes.jl | 22 +++++++++++ test/BZMeshes.jl | 88 ++++++++++++++++++++++++++++++++++++++++++ test/BaseMesh.jl | 88 ------------------------------------------ test/runtests.jl | 7 ++-- 4 files changed, 114 insertions(+), 91 deletions(-) create mode 100644 test/AbstractMeshes.jl diff --git a/test/AbstractMeshes.jl b/test/AbstractMeshes.jl new file mode 100644 index 0000000..9f63892 --- /dev/null +++ b/test/AbstractMeshes.jl @@ -0,0 +1,22 @@ +@testset "AbstractMeshes" begin + + # create a random concrete mesh + DIM = 2 + N1, N2 = 3, 5 + lattice = Matrix([1/N1/2 0; 0 1.0/N2/2]') .* 2π + cell = BZMeshes.Cell(lattice=lattice) + mesh = BaseMesh.UMesh(br=cell, origin=ones(DIM) ./ 2, size=(N1, N2), shift=zeros(DIM)) + + # type + @test eltype(mesh) == BrillouinZoneMeshes.SVector{Float64,DIM} + + # size + @test length(mesh) == N1 * N2 + @test size(mesh) == (N1, N2) + @test size(mesh, 1) == N1 + @test ndims(mesh) == DIM + + # tools + @test AbstractMeshes._inds2ind(size(mesh), (2, 2)) == 5 + @test AbstractMeshes._ind2inds(size(mesh), 5) == [2, 2] +end \ No newline at end of file diff --git a/test/BZMeshes.jl b/test/BZMeshes.jl index 36e5580..7ec50e5 100644 --- a/test/BZMeshes.jl +++ b/test/BZMeshes.jl @@ -10,6 +10,94 @@ using BrillouinZoneMeshes.BZMeshes: find_kFermi, find_kFermi, radial_rescale, kF_densed_kgrids rng = MersenneTwister(1234) + @testset "UniformBZMesh" begin + @testset "Indexing" begin + size = (3, 4, 5) + for i in 1:prod(size) + @test i == AbstractMeshes._inds2ind(size, AbstractMeshes._ind2inds(size, i)) + end + end + + @testset "Array Interface" begin + 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) + br = BZMeshes.Cell(lattice=lattice) + bzmesh = BZMeshes.UniformBZMesh(cell=br, size=(N1, N2), origin=0) + + println(bzmesh) + display(bzmesh) + + for (pi, p) in enumerate(bzmesh) + @test bzmesh[pi] ≈ p # linear index + inds = AbstractMeshes._ind2inds(size(bzmesh), pi) + @test p ≈ inds .* 2.0 .- 1.0 + @test bzmesh[inds...] ≈ p # cartesian index + end + end + + @testset "lattice vector and inverse lattice vector" begin + lattice = Matrix([2.0 0 0; 1 sqrt(3) 0; 7 11 19]') + msize = (3, 5, 7) + br = BZMeshes.Cell(lattice=lattice) + bzmesh = BZMeshes.UniformBZMesh(cell=br, size=msize) + + @test lattice_vector(bzmesh, 1) ≈ br.recip_lattice[:, 1] + @test lattice_vector(bzmesh, 2) ≈ br.recip_lattice[:, 2] + @test lattice_vector(bzmesh, 3) ≈ br.recip_lattice[:, 3] + + @test inv_lattice_vector(bzmesh, 1) ≈ br.inv_recip_lattice[:, 1] + @test inv_lattice_vector(bzmesh, 2) ≈ br.inv_recip_lattice[:, 2] + @test inv_lattice_vector(bzmesh, 3) ≈ br.inv_recip_lattice[:, 3] + + @test lattice_vector(bzmesh) * inv_lattice_vector(bzmesh) ≈ Matrix(I, 3, 3) + end + + @testset "locate and volume" begin + msize = (3, 5, 7) + lattice = Matrix([2.0 0 0; 1 sqrt(3) 0; 7 11 19]') + # size = (3, 5) + # lattice = Matrix([2.3 0; 0 7.0]') + br = BZMeshes.Cell(lattice=lattice) + bzmesh = BZMeshes.UniformBZMesh(cell=br, size=msize) + vol = 0.0 + for (i, p) in enumerate(bzmesh) + @test bzmesh[i] ≈ p # linear index + inds = AbstractMeshes._ind2inds(size(bzmesh), i) + @test bzmesh[inds...] ≈ p # cartesian index + + @test AbstractMeshes.locate(bzmesh, p) == i + vol += AbstractMeshes.volume(bzmesh, i) + end + @test vol ≈ AbstractMeshes.volume(bzmesh) + end + + @testset "origin and shift convention" begin + DIM = 2 + lattice = Matrix([1.0 0; 0 1]') + br = BZMeshes.Cell(lattice=lattice) + + # even numbers + size = (4, 4) + # Gamma-centered, no shift + bzmesh = BZMeshes.UniformBZMesh(cell=br, size=size, origin=0, shift=[false, false]) + @test bzmesh[1, 1] ≈ BZMeshes.SVector{DIM,eltype(lattice)}([0.0, 0.0]) + # M-P, no shift + bzmesh = BZMeshes.UniformBZMesh(cell=br, size=size, origin=-1 / 2, shift=[false, false]) + @test bzmesh[3, 3] ≈ BZMeshes.SVector{DIM,eltype(lattice)}([0.0, 0.0]) + + # odd numbers + size = (5, 5) + # Gamma-centered, no shift + bzmesh = BZMeshes.UniformBZMesh(cell=br, size=size, origin=0, shift=[false, false]) + @test bzmesh[1, 1] ≈ BZMeshes.SVector{DIM,eltype(lattice)}([0.0, 0.0]) + # M-P, 1/2 shift + bzmesh = BZMeshes.UniformBZMesh(cell=br, size=size, origin=-1 / 2, shift=[true, true]) + @test bzmesh[3, 3] ≈ BZMeshes.SVector{DIM,eltype(lattice)}([0.0, 0.0]) + + end + end + @testset "UniformBZMesh" begin DIM = 2 N1, N2 = 4, 5 diff --git a/test/BaseMesh.jl b/test/BaseMesh.jl index 3a00b4f..72f4cfc 100644 --- a/test/BaseMesh.jl +++ b/test/BaseMesh.jl @@ -23,94 +23,6 @@ @test AbstractMeshes.volume(mesh) ≈ vol end - @testset "UniformBZMesh" begin - @testset "Indexing" begin - size = (3, 4, 5) - for i in 1:prod(size) - @test i == AbstractMeshes._inds2ind(size, AbstractMeshes._ind2inds(size, i)) - end - end - - @testset "Array Interface" begin - 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) - br = BZMeshes.Cell(lattice=lattice) - bzmesh = BZMeshes.UniformBZMesh(cell=br, size=(N1, N2), origin=0) - - println(bzmesh) - display(bzmesh) - - for (pi, p) in enumerate(bzmesh) - @test bzmesh[pi] ≈ p # linear index - inds = AbstractMeshes._ind2inds(size(bzmesh), pi) - @test p ≈ inds .* 2.0 .- 1.0 - @test bzmesh[inds...] ≈ p # cartesian index - end - end - - @testset "lattice vector and inverse lattice vector" begin - lattice = Matrix([2.0 0 0; 1 sqrt(3) 0; 7 11 19]') - msize = (3, 5, 7) - br = BZMeshes.Cell(lattice=lattice) - bzmesh = BZMeshes.UniformBZMesh(cell=br, size=msize) - - @test lattice_vector(bzmesh, 1) ≈ br.recip_lattice[:, 1] - @test lattice_vector(bzmesh, 2) ≈ br.recip_lattice[:, 2] - @test lattice_vector(bzmesh, 3) ≈ br.recip_lattice[:, 3] - - @test inv_lattice_vector(bzmesh, 1) ≈ br.inv_recip_lattice[:, 1] - @test inv_lattice_vector(bzmesh, 2) ≈ br.inv_recip_lattice[:, 2] - @test inv_lattice_vector(bzmesh, 3) ≈ br.inv_recip_lattice[:, 3] - - @test lattice_vector(bzmesh) * inv_lattice_vector(bzmesh) ≈ Matrix(I, 3, 3) - end - - @testset "locate and volume" begin - msize = (3, 5, 7) - lattice = Matrix([2.0 0 0; 1 sqrt(3) 0; 7 11 19]') - # size = (3, 5) - # lattice = Matrix([2.3 0; 0 7.0]') - br = BZMeshes.Cell(lattice=lattice) - bzmesh = BZMeshes.UniformBZMesh(cell=br, size=msize) - vol = 0.0 - for (i, p) in enumerate(bzmesh) - @test bzmesh[i] ≈ p # linear index - inds = AbstractMeshes._ind2inds(size(bzmesh), i) - @test bzmesh[inds...] ≈ p # cartesian index - - @test AbstractMeshes.locate(bzmesh, p) == i - vol += AbstractMeshes.volume(bzmesh, i) - end - @test vol ≈ AbstractMeshes.volume(bzmesh) - end - - @testset "origin and shift convention" begin - DIM = 2 - lattice = Matrix([1.0 0; 0 1]') - br = BZMeshes.Cell(lattice=lattice) - - # even numbers - size = (4, 4) - # Gamma-centered, no shift - bzmesh = BZMeshes.UniformBZMesh(cell=br, size=size, origin=0, shift=[false, false]) - @test bzmesh[1, 1] ≈ BZMeshes.SVector{DIM,eltype(lattice)}([0.0, 0.0]) - # M-P, no shift - bzmesh = BZMeshes.UniformBZMesh(cell=br, size=size, origin=-1 / 2, shift=[false, false]) - @test bzmesh[3, 3] ≈ BZMeshes.SVector{DIM,eltype(lattice)}([0.0, 0.0]) - - # odd numbers - size = (5, 5) - # Gamma-centered, no shift - bzmesh = BZMeshes.UniformBZMesh(cell=br, size=size, origin=0, shift=[false, false]) - @test bzmesh[1, 1] ≈ BZMeshes.SVector{DIM,eltype(lattice)}([0.0, 0.0]) - # M-P, 1/2 shift - bzmesh = BZMeshes.UniformBZMesh(cell=br, size=size, origin=-1 / 2, shift=[true, true]) - @test bzmesh[3, 3] ≈ BZMeshes.SVector{DIM,eltype(lattice)}([0.0, 0.0]) - - end - end - @testset "ProdMesh" begin using BrillouinZoneMeshes.CompositeGrids using BrillouinZoneMeshes.BaseMesh diff --git a/test/runtests.jl b/test/runtests.jl index de9800c..4f68595 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -15,16 +15,17 @@ include("testcase.jl") @testset "BrillouinZoneMeshes.jl" begin if isempty(ARGS) + include("AbstractMeshes.jl") # include("barycheb.jl") + include("Cells.jl") include("BaseMesh.jl") include("CompositeMeshes.jl") + include("MeshMap.jl") + include("BZMeshes.jl") # include("TreeMeshes.jl") # include("mc.jl") include("PointSymmetry.jl") include("UniformMeshMap.jl") - include("PolarMeshes.jl") - include("MeshMap.jl") - include("Cells.jl") else include(ARGS[1]) end From 1ca6fadd13ffd43c0de7c3395f019a23d1dc8e49 Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Tue, 21 Mar 2023 15:34:53 -0400 Subject: [PATCH 2/8] hide wip codes --- src/BZMeshes.jl | 3 ++- src/BrillouinZoneMeshes.jl | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/BZMeshes.jl b/src/BZMeshes.jl index 1d9d9fe..5bf7ea9 100644 --- a/src/BZMeshes.jl +++ b/src/BZMeshes.jl @@ -329,6 +329,7 @@ function MeshMaps.MeshMap(mesh::UniformBZMesh{T,DIM}, return MeshMaps.MeshMap(new_map) end -include("PolarMeshes.jl") +# temporarily hide wip codes +# include("PolarMeshes.jl") end \ No newline at end of file diff --git a/src/BrillouinZoneMeshes.jl b/src/BrillouinZoneMeshes.jl index b5b56e8..7be921b 100644 --- a/src/BrillouinZoneMeshes.jl +++ b/src/BrillouinZoneMeshes.jl @@ -34,9 +34,9 @@ export UniformMesh, BaryChebMesh, CenteredMesh, EdgedMesh, AbstractMesh# , locat export inv_lattice_vector, lattice_vector, cell_volume export AbstractUniformMesh -include("CompositeMeshes.jl") -using .CompositeMeshes -export CompositeMeshes +# include("CompositeMeshes.jl") +# using .CompositeMeshes +# export CompositeMeshes # include("TreeMeshes.jl") # using .TreeMeshes From 80ece06a06a89c3fe4539b69c26c787936167dab Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Tue, 21 Mar 2023 15:41:59 -0400 Subject: [PATCH 3/8] wip --- test/BZMeshes.jl | 270 +------------------------------------------- test/PolarMeshes.jl | 266 +++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 3 +- 3 files changed, 270 insertions(+), 269 deletions(-) create mode 100644 test/PolarMeshes.jl diff --git a/test/BZMeshes.jl b/test/BZMeshes.jl index 7ec50e5..6f472bb 100644 --- a/test/BZMeshes.jl +++ b/test/BZMeshes.jl @@ -1,13 +1,13 @@ @testset "BZMeshes.jl" begin using BrillouinZoneMeshes.BZMeshes - using BrillouinZoneMeshes.BZMeshes.Coordinates + # using BrillouinZoneMeshes.BZMeshes.Coordinates using BrillouinZoneMeshes.CompositeGrids using BrillouinZoneMeshes.BaseMesh using BrillouinZoneMeshes.AbstractMeshes using BrillouinZoneMeshes.LinearAlgebra using BrillouinZoneMeshes.StaticArrays using BrillouinZoneMeshes.Roots - using BrillouinZoneMeshes.BZMeshes: find_kFermi, find_kFermi, radial_rescale, kF_densed_kgrids + # using BrillouinZoneMeshes.BZMeshes: find_kFermi, find_kFermi, radial_rescale, kF_densed_kgrids rng = MersenneTwister(1234) @testset "UniformBZMesh" begin @@ -118,271 +118,5 @@ @test AbstractMeshes.volume(mesh) ≈ vol end - @testset "PolarMeshes" begin - @testset "CoordinateTransformations" begin - - # special cases - r = Polar(1, 0) - @test BZMeshes._polar2cart(r) ≈ [1.0, 0] - r = Polar(1, π / 2) - @test BZMeshes._polar2cart(r) ≈ [0, 1.0] - r = Polar(1, π) - @test BZMeshes._polar2cart(r) ≈ [-1.0, 0] - r = Polar(1, 3π / 2) - @test BZMeshes._polar2cart(r) ≈ [0, -1.0] - - # random test - Ntest = 16 - for i in 1:Ntest - x = rand(rng, 2) - @test BZMeshes._polar2cart(BZMeshes._cart2polar(x)) ≈ x - end - - Ntest = 16 - for i in 1:Ntest - x = rand(rng, 3) - @test BZMeshes._spherical2cart(BZMeshes._cart2spherical(x)) ≈ x - end - - end - - @testset "PolarMeshes" begin - @testset "2D PolarMesh" begin - - a, b = 0.8, 1.2 - N, M = 3, 2 - # theta grid dense around 0 and π - theta = CompositeGrid.LogDensedGrid( - :cheb, - [-π, π], - [-π, 0.0, π], - N, - 0.1, - M - ) - println(theta) - grids = [CompositeGrid.LogDensedGrid(:cheb, [0.0, 2.0], [sqrt(a * cos(θ)^2 + b * sin(θ)^2),], N, 0.1, M) for θ in theta] - cm = ProdMesh(grids, theta) - - DIM = 2 - lattice = Matrix([1.0 0; 0 1]') - br = BZMeshes.Cell(lattice=lattice) - - pm = PolarMesh(br, cm) - vol = 0.0 - for (i, p) in enumerate(pm) - @test p == BZMeshes._polar2cart(pm[AngularCoords, i]) - @test AbstractMeshes.locate(pm, p) == i - vol += AbstractMeshes.volume(pm, i) - end - # grid is on a circle with r=2.0 - @test vol ≈ 4π - end - - @testset "3D PolarMesh" begin - N, M = 2, 2 - # theta grid dense around 0 and π - phi = CompositeGrid.LogDensedGrid( - :cheb, - [-π, π], - [-π, 0.0, π], - N, - 0.1, - M - ) - theta = CompositeGrid.LogDensedGrid( - :cheb, - [-π / 2, π / 2], - [0.0,], - N, - 0.1, - M - ) - rg = CompositeGrid.LogDensedGrid(:cheb, [0.0, 2.0], [1.0,], N, 0.1, M) - am = ProdMesh([theta for i in 1:length(phi)], phi) - println(typeof(size(am))) - cm = ProdMesh([rg for i in 1:length(am)], am) - println(typeof(size(cm))) - println(typeof(size(cm.mesh))) - - DIM = 3 - lattice = Matrix([1.0 0 0; 0 1 0; 0 0 1]') - br = BZMeshes.Cell(lattice=lattice) - - pm = PolarMesh(br, cm) - println(typeof(size(pm))) - - vol = 0.0 - for (i, p) in enumerate(pm) - @test p == BZMeshes._spherical2cart(pm[AngularCoords, i]) - @test AbstractMeshes.locate(pm, p) == i - vol += AbstractMeshes.volume(pm, i) - end - # a ball with r=2.0 - @test vol ≈ 32π / 3 - end - end - @testset "PolarMesh Generator" begin - @testset "2D" begin - # given dispersion function accept a k in cartesian - # goal is to find k_F at direction specified by angle - dispersion(k) = dot(k, k) - 1.0 - - # 2d - N = 10 - bound = [-π, π] - theta = SimpleGrid.Uniform(bound, N; isperiodic=true) - - k_F_previous = 0.0 - for θ in theta - f(k) = dispersion(BZMeshes._polar2cart(Polar(k, θ))) - k_F = find_zero(f, k_F_previous) - @test k_F ≈ 1.0 - @test find_kFermi(dispersion, θ; kinit=k_F_previous) ≈ 1.0 - k_F_previous = k_F - end - - # grids = kF_densed_kgrids(dispersion=dispersion, anglemesh=theta, - # bound=[0.0, 2.0]) - # cm = ProdMesh(theta, grids) - DIM = 2 - lattice = Matrix([1.0 0; 0 1]') - br = BZMeshes.Cell(lattice=lattice) - - pm = PolarMesh(dispersion=dispersion, anglemesh=theta, cell=br, kmax=2.0) - @test AbstractMeshes.volume(pm) ≈ 4π - - end - - @testset "2D CompositePolarMesh" begin - # given dispersion function accept a k in cartesian - # goal is to find k_F at direction specified by angle - dispersion(k) = dot(k, k) - 1.0 - - # 2d - N = 10 - bound = [-π, π] - theta = SimpleGrid.Uniform(bound, N; isperiodic=true) - - DIM = 2 - lattice = Matrix([1.0 0; 0 1]') - br = BZMeshes.Cell(lattice=lattice) - - pm = CompositePolarMesh(dispersion=dispersion, anglemesh=theta, cell=br, kmax=2.0, N=3) - @test AbstractMeshes.volume(pm) ≈ 4π - - data = zeros(size(pm)) - for (pi, p) in enumerate(pm) - data[pi] = dispersion(p) - end - - testN = 10 - for i in 1:testN - r, θ = rand(rng) * 2.0, (rand(rng) * 2 - 1) * π - p = Polar(r, θ) - x = BZMeshes._cartesianize(p) - @test isapprox(AbstractMeshes.interp(data, pm, x), dispersion(x), rtol=1e-4) - @test isapprox(AbstractMeshes.interp(data, pm, p), dispersion(x), rtol=1e-4) - end - - @test isapprox(AbstractMeshes.integrate(data, pm), 4π, rtol=1e-4) - end - - @testset "3D" begin - dispersion(k) = dot(k, k) - 1.0 - - N = 6 - bound = [-π, π] - phi = SimpleGrid.Uniform(bound, N; isperiodic=true) - - N = 4 - bound = [-π / 2, π / 2] - theta = SimpleGrid.Uniform(bound, N; isperiodic=true) - - am = ProdMesh([theta for i in 1:length(phi)], phi) - - k_F_previous = 0.0 - for ap in am - f(k) = dispersion(BZMeshes._spherical2cart(Spherical(k, ap...))) - k_F = find_zero(f, k_F_previous) - @test k_F ≈ 1.0 - @test find_kFermi(dispersion, ap; kinit=k_F_previous) ≈ 1.0 - k_F_previous = k_F - end - - DIM = 3 - lattice = Matrix([1.0 1.0 0; 1 0 1; 0 1 1]') - br = BZMeshes.Cell(lattice=lattice) - - pm = PolarMesh(dispersion=dispersion, anglemesh=am, cell=br, kmax=2.0) - @test AbstractMeshes.volume(pm) ≈ 32π / 3 - - end - - @testset "3D CompositePolarMesh" begin - dispersion(k) = dot(k, k) - 1.0 - - N = 6 - bound = [-π, π] - phi = SimpleGrid.Uniform(bound, N) - - N = 4 - bound = [-π / 2, π / 2] - theta = SimpleGrid.Uniform(bound, N) - - am = ProdMesh([theta for i in 1:length(phi)], phi) - - DIM = 3 - lattice = Matrix([1.0 1.0 0; 1 0 1; 0 1 1]') - br = BZMeshes.Cell(lattice=lattice) - - pm = CompositePolarMesh(dispersion=dispersion, anglemesh=am, cell=br, kmax=2.0, N=4) - @test AbstractMeshes.volume(pm) ≈ 32π / 3 - - data = zeros(size(pm)) - for (pi, p) in enumerate(pm) - data[pi] = dispersion(p) - end - - testN = 10 - for i in 1:testN - r, ϕ, θ = rand(rng) * 2.0, (rand(rng) * 2 - 1) * π, (rand(rng) * 2 - 1) * π / 2 - p = Spherical(r, θ, ϕ) - x = BZMeshes._cartesianize(p) - @test isapprox(AbstractMeshes.interp(data, pm, x), dispersion(x), rtol=1e-4) - @test isapprox(AbstractMeshes.interp(data, pm, p), dispersion(x), rtol=1e-4) - end - - @test isapprox(AbstractMeshes.integrate(data, pm), 4π * 56 / 15, rtol=1e-4) - - end - - @testset "Radial rescale" begin - @testset "RescaledGrid" begin - rmax = 2.0 - N = 10 - rgrid = SimpleG.Uniform([0.0, rmax^2], N; gpbound=[rmax^2 / 2N, rmax^2 * (1 - 1 / 2N)]) - rrg = radial_rescale(grid=rgrid, DIM=2) - println(rrg.grid) - bound = [-π, π] - theta = SimpleGrid.Uniform(bound, N; gpbound=[π * (1 / N - 1), π * (1 - 1 / N)], isperiodic=true) - - # for (i, p) in enumerate(theta) - # println(volume(theta, i)) - # end - - DIM = 2 - lattice = Matrix([1.0 0; 0 1]') - br = BZMeshes.Cell(lattice=lattice) - bzmesh = PolarMesh(br, ProdMesh([rrg for i in 1:length(theta)], theta)) - for (i, p) in enumerate(bzmesh) - # println(p, AbstractMeshes.volume(bzmesh, i)) - end - end - - end - end - - end end diff --git a/test/PolarMeshes.jl b/test/PolarMeshes.jl new file mode 100644 index 0000000..0aed47a --- /dev/null +++ b/test/PolarMeshes.jl @@ -0,0 +1,266 @@ +@testset "PolarMeshes" begin + @testset "CoordinateTransformations" begin + + # special cases + r = Polar(1, 0) + @test BZMeshes._polar2cart(r) ≈ [1.0, 0] + r = Polar(1, π / 2) + @test BZMeshes._polar2cart(r) ≈ [0, 1.0] + r = Polar(1, π) + @test BZMeshes._polar2cart(r) ≈ [-1.0, 0] + r = Polar(1, 3π / 2) + @test BZMeshes._polar2cart(r) ≈ [0, -1.0] + + # random test + Ntest = 16 + for i in 1:Ntest + x = rand(rng, 2) + @test BZMeshes._polar2cart(BZMeshes._cart2polar(x)) ≈ x + end + + Ntest = 16 + for i in 1:Ntest + x = rand(rng, 3) + @test BZMeshes._spherical2cart(BZMeshes._cart2spherical(x)) ≈ x + end + + end + + @testset "PolarMeshes" begin + @testset "2D PolarMesh" begin + + a, b = 0.8, 1.2 + N, M = 3, 2 + # theta grid dense around 0 and π + theta = CompositeGrid.LogDensedGrid( + :cheb, + [-π, π], + [-π, 0.0, π], + N, + 0.1, + M + ) + println(theta) + grids = [CompositeGrid.LogDensedGrid(:cheb, [0.0, 2.0], [sqrt(a * cos(θ)^2 + b * sin(θ)^2),], N, 0.1, M) for θ in theta] + cm = ProdMesh(grids, theta) + + DIM = 2 + lattice = Matrix([1.0 0; 0 1]') + br = BZMeshes.Cell(lattice=lattice) + + pm = PolarMesh(br, cm) + vol = 0.0 + for (i, p) in enumerate(pm) + @test p == BZMeshes._polar2cart(pm[AngularCoords, i]) + @test AbstractMeshes.locate(pm, p) == i + vol += AbstractMeshes.volume(pm, i) + end + # grid is on a circle with r=2.0 + @test vol ≈ 4π + end + + @testset "3D PolarMesh" begin + N, M = 2, 2 + # theta grid dense around 0 and π + phi = CompositeGrid.LogDensedGrid( + :cheb, + [-π, π], + [-π, 0.0, π], + N, + 0.1, + M + ) + theta = CompositeGrid.LogDensedGrid( + :cheb, + [-π / 2, π / 2], + [0.0,], + N, + 0.1, + M + ) + rg = CompositeGrid.LogDensedGrid(:cheb, [0.0, 2.0], [1.0,], N, 0.1, M) + am = ProdMesh([theta for i in 1:length(phi)], phi) + println(typeof(size(am))) + cm = ProdMesh([rg for i in 1:length(am)], am) + println(typeof(size(cm))) + println(typeof(size(cm.mesh))) + + DIM = 3 + lattice = Matrix([1.0 0 0; 0 1 0; 0 0 1]') + br = BZMeshes.Cell(lattice=lattice) + + pm = PolarMesh(br, cm) + println(typeof(size(pm))) + + vol = 0.0 + for (i, p) in enumerate(pm) + @test p == BZMeshes._spherical2cart(pm[AngularCoords, i]) + @test AbstractMeshes.locate(pm, p) == i + vol += AbstractMeshes.volume(pm, i) + end + # a ball with r=2.0 + @test vol ≈ 32π / 3 + end + end + @testset "PolarMesh Generator" begin + @testset "2D" begin + # given dispersion function accept a k in cartesian + # goal is to find k_F at direction specified by angle + dispersion(k) = dot(k, k) - 1.0 + + # 2d + N = 10 + bound = [-π, π] + theta = SimpleGrid.Uniform(bound, N; isperiodic=true) + + k_F_previous = 0.0 + for θ in theta + f(k) = dispersion(BZMeshes._polar2cart(Polar(k, θ))) + k_F = find_zero(f, k_F_previous) + @test k_F ≈ 1.0 + @test find_kFermi(dispersion, θ; kinit=k_F_previous) ≈ 1.0 + k_F_previous = k_F + end + + # grids = kF_densed_kgrids(dispersion=dispersion, anglemesh=theta, + # bound=[0.0, 2.0]) + # cm = ProdMesh(theta, grids) + DIM = 2 + lattice = Matrix([1.0 0; 0 1]') + br = BZMeshes.Cell(lattice=lattice) + + pm = PolarMesh(dispersion=dispersion, anglemesh=theta, cell=br, kmax=2.0) + @test AbstractMeshes.volume(pm) ≈ 4π + + end + + @testset "2D CompositePolarMesh" begin + # given dispersion function accept a k in cartesian + # goal is to find k_F at direction specified by angle + dispersion(k) = dot(k, k) - 1.0 + + # 2d + N = 10 + bound = [-π, π] + theta = SimpleGrid.Uniform(bound, N; isperiodic=true) + + DIM = 2 + lattice = Matrix([1.0 0; 0 1]') + br = BZMeshes.Cell(lattice=lattice) + + pm = CompositePolarMesh(dispersion=dispersion, anglemesh=theta, cell=br, kmax=2.0, N=3) + @test AbstractMeshes.volume(pm) ≈ 4π + + data = zeros(size(pm)) + for (pi, p) in enumerate(pm) + data[pi] = dispersion(p) + end + + testN = 10 + for i in 1:testN + r, θ = rand(rng) * 2.0, (rand(rng) * 2 - 1) * π + p = Polar(r, θ) + x = BZMeshes._cartesianize(p) + @test isapprox(AbstractMeshes.interp(data, pm, x), dispersion(x), rtol=1e-4) + @test isapprox(AbstractMeshes.interp(data, pm, p), dispersion(x), rtol=1e-4) + end + + @test isapprox(AbstractMeshes.integrate(data, pm), 4π, rtol=1e-4) + end + + @testset "3D" begin + dispersion(k) = dot(k, k) - 1.0 + + N = 6 + bound = [-π, π] + phi = SimpleGrid.Uniform(bound, N; isperiodic=true) + + N = 4 + bound = [-π / 2, π / 2] + theta = SimpleGrid.Uniform(bound, N; isperiodic=true) + + am = ProdMesh([theta for i in 1:length(phi)], phi) + + k_F_previous = 0.0 + for ap in am + f(k) = dispersion(BZMeshes._spherical2cart(Spherical(k, ap...))) + k_F = find_zero(f, k_F_previous) + @test k_F ≈ 1.0 + @test find_kFermi(dispersion, ap; kinit=k_F_previous) ≈ 1.0 + k_F_previous = k_F + end + + DIM = 3 + lattice = Matrix([1.0 1.0 0; 1 0 1; 0 1 1]') + br = BZMeshes.Cell(lattice=lattice) + + pm = PolarMesh(dispersion=dispersion, anglemesh=am, cell=br, kmax=2.0) + @test AbstractMeshes.volume(pm) ≈ 32π / 3 + + end + + @testset "3D CompositePolarMesh" begin + dispersion(k) = dot(k, k) - 1.0 + + N = 6 + bound = [-π, π] + phi = SimpleGrid.Uniform(bound, N) + + N = 4 + bound = [-π / 2, π / 2] + theta = SimpleGrid.Uniform(bound, N) + + am = ProdMesh([theta for i in 1:length(phi)], phi) + + DIM = 3 + lattice = Matrix([1.0 1.0 0; 1 0 1; 0 1 1]') + br = BZMeshes.Cell(lattice=lattice) + + pm = CompositePolarMesh(dispersion=dispersion, anglemesh=am, cell=br, kmax=2.0, N=4) + @test AbstractMeshes.volume(pm) ≈ 32π / 3 + + data = zeros(size(pm)) + for (pi, p) in enumerate(pm) + data[pi] = dispersion(p) + end + + testN = 10 + for i in 1:testN + r, ϕ, θ = rand(rng) * 2.0, (rand(rng) * 2 - 1) * π, (rand(rng) * 2 - 1) * π / 2 + p = Spherical(r, θ, ϕ) + x = BZMeshes._cartesianize(p) + @test isapprox(AbstractMeshes.interp(data, pm, x), dispersion(x), rtol=1e-4) + @test isapprox(AbstractMeshes.interp(data, pm, p), dispersion(x), rtol=1e-4) + end + + @test isapprox(AbstractMeshes.integrate(data, pm), 4π * 56 / 15, rtol=1e-4) + + end + + @testset "Radial rescale" begin + @testset "RescaledGrid" begin + rmax = 2.0 + N = 10 + rgrid = SimpleG.Uniform([0.0, rmax^2], N; gpbound=[rmax^2 / 2N, rmax^2 * (1 - 1 / 2N)]) + rrg = radial_rescale(grid=rgrid, DIM=2) + println(rrg.grid) + bound = [-π, π] + theta = SimpleGrid.Uniform(bound, N; gpbound=[π * (1 / N - 1), π * (1 - 1 / N)], isperiodic=true) + + # for (i, p) in enumerate(theta) + # println(volume(theta, i)) + # end + + DIM = 2 + lattice = Matrix([1.0 0; 0 1]') + br = BZMeshes.Cell(lattice=lattice) + bzmesh = PolarMesh(br, ProdMesh([rrg for i in 1:length(theta)], theta)) + for (i, p) in enumerate(bzmesh) + # println(p, AbstractMeshes.volume(bzmesh, i)) + end + end + + end + end + +end diff --git a/test/runtests.jl b/test/runtests.jl index 4f68595..4eb2182 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,9 +19,10 @@ include("testcase.jl") # include("barycheb.jl") include("Cells.jl") include("BaseMesh.jl") - include("CompositeMeshes.jl") + # include("CompositeMeshes.jl") include("MeshMap.jl") include("BZMeshes.jl") + # include("PolarMeshes.jl") # include("TreeMeshes.jl") # include("mc.jl") include("PointSymmetry.jl") From 3931b3aea31be59146b66518456ff9354872083c Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Tue, 21 Mar 2023 16:36:59 -0400 Subject: [PATCH 4/8] add more tests for AbstractMeshes.jl --- .gitignore | 1 + test/AbstractMeshes.jl | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/.gitignore b/.gitignore index a4ee139..70aaa34 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ deps/src/ docs/build/ docs/site/ /run/ +/coverage/ # File generated by Pkg, the package manager, based on a corresponding Project.toml # It records a fixed state of all packages used by the project. As such, it should not be diff --git a/test/AbstractMeshes.jl b/test/AbstractMeshes.jl index 9f63892..68a1814 100644 --- a/test/AbstractMeshes.jl +++ b/test/AbstractMeshes.jl @@ -1,4 +1,15 @@ @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 @@ -16,7 +27,35 @@ @test size(mesh, 1) == N1 @test ndims(mesh) == DIM + # indexing + @test mesh[end] == mesh[length(mesh)] + @test mesh[begin] == mesh[1] + # tools @test AbstractMeshes._inds2ind(size(mesh), (2, 2)) == 5 @test AbstractMeshes._ind2inds(size(mesh), 5) == [2, 2] + + # 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_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_func_not_implemented(x -> locate(x, 1), notamesh) + test_func_not_implemented(x -> volume(x, 1), notamesh) + test_func_not_implemented(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_func_not_implemented(x -> integrate([1,], x), notamesh) + test_func_not_implemented(x -> interp([1,], x, 1), notamesh) + + test_func_not_implemented(x -> interval(x, 1), notamesh) + end \ No newline at end of file From 7f70a95fe46871abcae6128ee559da3278ada031 Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Thu, 23 Mar 2023 21:27:49 -0400 Subject: [PATCH 5/8] recover wip codes and tests --- src/BZMeshes.jl | 2 +- src/BrillouinZoneMeshes.jl | 4 ++-- test/PolarMeshes.jl | 7 ++++++- test/runtests.jl | 8 ++++---- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/BZMeshes.jl b/src/BZMeshes.jl index 5bf7ea9..e317745 100644 --- a/src/BZMeshes.jl +++ b/src/BZMeshes.jl @@ -330,6 +330,6 @@ function MeshMaps.MeshMap(mesh::UniformBZMesh{T,DIM}, end # temporarily hide wip codes -# include("PolarMeshes.jl") +include("PolarMeshes.jl") end \ No newline at end of file diff --git a/src/BrillouinZoneMeshes.jl b/src/BrillouinZoneMeshes.jl index 7be921b..3347291 100644 --- a/src/BrillouinZoneMeshes.jl +++ b/src/BrillouinZoneMeshes.jl @@ -34,8 +34,8 @@ export UniformMesh, BaryChebMesh, CenteredMesh, EdgedMesh, AbstractMesh# , locat export inv_lattice_vector, lattice_vector, cell_volume export AbstractUniformMesh -# include("CompositeMeshes.jl") -# using .CompositeMeshes +include("CompositeMeshes.jl") +using .CompositeMeshes # export CompositeMeshes # include("TreeMeshes.jl") diff --git a/test/PolarMeshes.jl b/test/PolarMeshes.jl index 0aed47a..9fc9e19 100644 --- a/test/PolarMeshes.jl +++ b/test/PolarMeshes.jl @@ -1,6 +1,11 @@ @testset "PolarMeshes" begin - @testset "CoordinateTransformations" begin + using BrillouinZoneMeshes.CompositeGrids + using BrillouinZoneMeshes.BZMeshes + using BrillouinZoneMeshes.BZMeshes.Coordinates + using BrillouinZoneMeshes.BZMeshes: radial_rescale, find_kFermi, find_zero + rng = MersenneTwister(1234) + @testset "CoordinateTransformations" begin # special cases r = Polar(1, 0) @test BZMeshes._polar2cart(r) ≈ [1.0, 0] diff --git a/test/runtests.jl b/test/runtests.jl index 4eb2182..ead4296 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,8 +3,8 @@ module _Test_BrillouinZoneMeshes using BrillouinZoneMeshes using BrillouinZoneMeshes.AbstractTrees # using BrillouinZoneMeshes.TreeMeshes -# using BrillouinZoneMeshes.BaseMesh -# using BrillouinZoneMeshes.BaryCheb +using BrillouinZoneMeshes.BaseMesh +using BrillouinZoneMeshes.BaryCheb # using BrillouinZoneMeshes.SymMaps using LinearAlgebra, Random @@ -19,10 +19,10 @@ include("testcase.jl") # include("barycheb.jl") include("Cells.jl") include("BaseMesh.jl") - # include("CompositeMeshes.jl") + include("CompositeMeshes.jl") include("MeshMap.jl") include("BZMeshes.jl") - # include("PolarMeshes.jl") + include("PolarMeshes.jl") # include("TreeMeshes.jl") # include("mc.jl") include("PointSymmetry.jl") From ea33bcc79f388b70a719a6f60e5ecf5fc9933399 Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Thu, 23 Mar 2023 21:41:00 -0400 Subject: [PATCH 6/8] more tests for Cells.jl --- {src => legacy}/TreeMeshes.jl | 0 test/Cells.jl | 130 +++++++++++++++++++--------------- test/runtests.jl | 1 - 3 files changed, 72 insertions(+), 59 deletions(-) rename {src => legacy}/TreeMeshes.jl (100%) diff --git a/src/TreeMeshes.jl b/legacy/TreeMeshes.jl similarity index 100% rename from src/TreeMeshes.jl rename to legacy/TreeMeshes.jl diff --git a/test/Cells.jl b/test/Cells.jl index 04a30dd..8d9326e 100644 --- a/test/Cells.jl +++ b/test/Cells.jl @@ -1,67 +1,81 @@ -using BrillouinZoneMeshes.PointSymmetry: spglib_spacegroup_number, spglib_standardize_cell +@testset "Cells" begin + using BrillouinZoneMeshes.PointSymmetry: spglib_spacegroup_number, spglib_standardize_cell -@testset "Brillouin" begin - Brillouin = BrillouinZoneMeshes.Cells.Cell - get_latvec = BrillouinZoneMeshes.Cells.get_latvec - # square lattice - DIM = 2 - lattice = Matrix([1.0 0; 0 1]') - br = Brillouin(lattice=lattice) - @test br.inv_lattice .* 2π ≈ br.recip_lattice' - @test br.cell_volume ≈ abs(det(lattice)) - @test br.recip_cell_volume ≈ 1 / abs(det(lattice)) * (2π)^DIM + @testset "Brillouin" begin + Brillouin = BrillouinZoneMeshes.Cells.Cell + get_latvec = BrillouinZoneMeshes.Cells.get_latvec + # square lattice + DIM = 2 + lattice = Matrix([1.0 0; 0 1]') + br = Brillouin(lattice=lattice) + @test get_latvec(br, 1; isrecip=false) ≈ [1.0, 0] + @test get_latvec(br, 1) ≈ [2π, 0] + @test br.inv_lattice .* 2π ≈ br.recip_lattice' + @test br.cell_volume ≈ abs(det(lattice)) + @test br.recip_cell_volume ≈ 1 / abs(det(lattice)) * (2π)^DIM - # triagular lattice - DIM = 2 - lattice = Matrix([2.0 0; 1 sqrt(3)]') - br = Brillouin(lattice=lattice) - @test br.inv_lattice .* 2π ≈ br.recip_lattice' - @test br.cell_volume ≈ abs(det(lattice)) - @test br.recip_cell_volume ≈ 1 / abs(det(lattice)) * (2π)^DIM - for i in 1:DIM - @test dot(get_latvec(br.recip_lattice,i),get_latvec(br.lattice,i)) ≈ 2π - end - # 3d testing lattice - DIM = 3 - lattice = Matrix([2.0 0 0; 1 sqrt(3) 0; 7 11 19]') - br = Brillouin(lattice=lattice) - @test br.inv_lattice .* 2π ≈ br.recip_lattice' - @test br.cell_volume ≈ abs(det(lattice)) - @test br.recip_cell_volume ≈ 1 / abs(det(lattice)) * (2π)^DIM - for i in 1:DIM - @test dot(get_latvec(br.recip_lattice,i),get_latvec(br.lattice,i)) ≈ 2π - end + # helper functions + x = [1.0, 0.5] + @test BrillouinZoneMeshes.Cells.vector_frac_to_cart(br, x) == br.lattice * x + @test BrillouinZoneMeshes.Cells.vector_cart_to_frac(br, x) == br.inv_lattice * x + @test BrillouinZoneMeshes.Cells.covector_frac_to_cart(br, x) == br.inv_lattice' * x + @test BrillouinZoneMeshes.Cells.covector_cart_to_frac(br, x) == br.lattice' * x + @test BrillouinZoneMeshes.Cells.recip_vector_frac_to_cart(br, x) == br.recip_lattice * x + @test BrillouinZoneMeshes.Cells.recip_vector_cart_to_frac(br, x) == br.inv_recip_lattice * x -end -@testset "Standard Brillouin" begin - a = 10.3 - Si = 1 - Ge = 2 + # triagular lattice + DIM = 2 + lattice = Matrix([2.0 0; 1 sqrt(3)]') + br = Brillouin(lattice=lattice) + @test br.inv_lattice .* 2π ≈ br.recip_lattice' + @test br.cell_volume ≈ abs(det(lattice)) + @test br.recip_cell_volume ≈ 1 / abs(det(lattice)) * (2π)^DIM + for i in 1:DIM + @test dot(get_latvec(br.recip_lattice, i), get_latvec(br.lattice, i)) ≈ 2π + end + # 3d testing lattice + DIM = 3 + lattice = Matrix([2.0 0 0; 1 sqrt(3) 0; 7 11 19]') + br = Brillouin(lattice=lattice) + @test br.inv_lattice .* 2π ≈ br.recip_lattice' + @test br.cell_volume ≈ abs(det(lattice)) + @test br.recip_cell_volume ≈ 1 / abs(det(lattice)) * (2π)^DIM + for i in 1:DIM + @test dot(get_latvec(br.recip_lattice, i), get_latvec(br.lattice, i)) ≈ 2π + end - # silicon with Cartesian x coordinates flipped - lattice = a / 2 * [[0 -1 -1.0]; [1 0 1.0]; [1 1 0.0]] - atoms = [Si, Si] - positions = [ones(3) / 8, -ones(3) / 8] - model = BrillouinZoneMeshes.Cells.standard_cell(lattice=lattice, atoms=atoms, positions=positions, primitive=false) - println(model) - display(model) + end - @test spglib_spacegroup_number(model) == 227 - @test model.lattice ≈ a * I(3) + @testset "Standard Brillouin" begin + a = 10.3 + Si = 1 + Ge = 2 - # Zincblende structure with different lattice vectors - lattice = a / 2 * [[0 1 1.0]; [-1 0 1.0]; [-1 1 0.0]] - atoms = [Si, Ge] - positions = [[-1, 1, 1] / 8, -[-1, 1, 1] / 8] - model = BrillouinZoneMeshes.Cells.standard_cell(lattice=lattice, atoms=atoms, positions=positions, primitive=false) - @test spglib_spacegroup_number(model) == 216 - @test model.lattice ≈ a * I(3) + # silicon with Cartesian x coordinates flipped + lattice = a / 2 * [[0 -1 -1.0]; [1 0 1.0]; [1 1 0.0]] + atoms = [Si, Si] + positions = [ones(3) / 8, -ones(3) / 8] + model = BrillouinZoneMeshes.Cells.standard_cell(lattice=lattice, atoms=atoms, positions=positions, primitive=false) + println(model) + display(model) - # Two-dimensional example - lattice = [[1.0 0.0]; [0.0 1.0]] - atoms = [Si,] - positions = [[0.0, 0.0],] - model = BrillouinZoneMeshes.Cells.standard_cell(lattice=lattice, atoms=atoms, positions=positions, primitive=true) - @test model.lattice ≈ lattice + @test spglib_spacegroup_number(model) == 227 + @test model.lattice ≈ a * I(3) + + # Zincblende structure with different lattice vectors + lattice = a / 2 * [[0 1 1.0]; [-1 0 1.0]; [-1 1 0.0]] + atoms = [Si, Ge] + positions = [[-1, 1, 1] / 8, -[-1, 1, 1] / 8] + model = BrillouinZoneMeshes.Cells.standard_cell(lattice=lattice, atoms=atoms, positions=positions, primitive=false) + @test spglib_spacegroup_number(model) == 216 + @test model.lattice ≈ a * I(3) + + # Two-dimensional example + lattice = [[1.0 0.0]; [0.0 1.0]] + atoms = [Si,] + positions = [[0.0, 0.0],] + model = BrillouinZoneMeshes.Cells.standard_cell(lattice=lattice, atoms=atoms, positions=positions, primitive=true) + @test model.lattice ≈ lattice + end end diff --git a/test/runtests.jl b/test/runtests.jl index ead4296..f9a9758 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -16,7 +16,6 @@ include("testcase.jl") if isempty(ARGS) include("AbstractMeshes.jl") - # include("barycheb.jl") include("Cells.jl") include("BaseMesh.jl") include("CompositeMeshes.jl") From 73820b02a9b590a40ec20b5e2f5edd543d5c40ef Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Thu, 23 Mar 2023 22:05:20 -0400 Subject: [PATCH 7/8] add more tests for BaseMesh.jl --- test/BaseMesh.jl | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/test/BaseMesh.jl b/test/BaseMesh.jl index 72f4cfc..4a63f31 100644 --- a/test/BaseMesh.jl +++ b/test/BaseMesh.jl @@ -1,6 +1,15 @@ @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 @@ -16,17 +25,28 @@ vol = 0.0 for (i, x) in enumerate(mesh) fracx = mesh[AbstractMeshes.FracCoords, i] + @test mesh[i] ≈ x @test fracx ≈ AbstractMeshes.cart_to_frac(mesh, x) @test AbstractMeshes.locate(mesh, x) == i vol += AbstractMeshes.volume(mesh, i) end @test AbstractMeshes.volume(mesh) ≈ vol + data = ones(Float64, size(mesh)) + @test AbstractMeshes.interp(data, mesh, [0.0, 0.0]) ≈ 1.0 + @test AbstractMeshes.integrate(data, mesh) ≈ vol + @test AbstractMeshes.lattice_vector(mesh, 1) ≈ [2 * N1, 0] + @test AbstractMeshes.inv_lattice_vector(mesh, 1) ≈ [1 / 2 / N1, 0] end @testset "ProdMesh" begin using BrillouinZoneMeshes.CompositeGrids using BrillouinZoneMeshes.BaseMesh using BrillouinZoneMeshes.AbstractMeshes + + # basics + struct NotAPM{T,DIM} <: BaseMesh.AbstractProdMesh{T,DIM} end + test_func_not_implemented(x -> BaseMesh._getgrid(x, 1), NotAPM{Int,3}()) + @testset "DirectProdMesh" begin N, M = 3, 2 r = CompositeGrid.LogDensedGrid( @@ -45,7 +65,7 @@ vol = 0.0 for (pi, p) in enumerate(dpm) i, j, k = AbstractMeshes._ind2inds(size(dpm), pi) - @test p ≈ [r[i], theta[j], phi[k]] + @test dpm[pi] ≈ [r[i], theta[j], phi[k]] @test pi == AbstractMeshes.locate(dpm, p) vol += AbstractMeshes.volume(dpm, pi) end @@ -95,6 +115,15 @@ latvec = [π 0; 0 π]' cm = ChebMesh(origin, latvec, DIM, N) + cm2 = ChebMesh(origin, latvec, cm) + + vol = 0.0 + for (i, x) in enumerate(cm2) + @test cm2[i] ≈ x + @test AbstractMeshes.locate(cm2, x) == i + vol += AbstractMeshes.volume(cm2, i) + end + @test AbstractMeshes.volume(cm2) ≈ vol # f(x) = x[1] + 2 * x[2] + x[1] * x[2] f(x) = sin(x[1]) + cos(x[2]) From 81d83896786b0a65dbf6237e4433758910e0b514 Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Thu, 23 Mar 2023 22:11:28 -0400 Subject: [PATCH 8/8] add tests for MeshMaps.jl --- test/MeshMap.jl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/MeshMap.jl b/test/MeshMap.jl index 9c89643..40d2046 100644 --- a/test/MeshMap.jl +++ b/test/MeshMap.jl @@ -1,8 +1,23 @@ @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 MeshMap constructor map = [1, 2, 2, 1, 2, 6, 6, 2, 2, 6, 6, 2, 1, 2, 2, 1] mm = MeshMap(map) @@ -30,6 +45,11 @@ mm = MeshMap(map) rmesh = ReducedBZMesh(umesh, mm) + @test size(rmesh) == size(mm) + @test rmesh[1, 2] ≈ umesh[1, 2] + + println(rmesh) + vol = 0.0 for (i, p) in enumerate(rmesh) vol += AbstractMeshes.volume(rmesh, i)