From ee04f09f2ca1f3036490bc1d53bdc91fe56e36e5 Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Sat, 12 Aug 2023 17:41:52 -0400 Subject: [PATCH] fixed bug for issue #47; renamed some test files --- src/grid/simple.jl | 1 + test/{chebyshev.jl => BaryChebTools.jl} | 0 test/CompositeG.jl | 127 ++++++++++++++++++++++++ test/{interpolate.jl => Interp.jl} | 0 test/{grid.jl => SimpleG.jl} | 97 ------------------ test/runtests.jl | 7 +- 6 files changed, 132 insertions(+), 100 deletions(-) rename test/{chebyshev.jl => BaryChebTools.jl} (100%) create mode 100644 test/CompositeG.jl rename test/{interpolate.jl => Interp.jl} (100%) rename test/{grid.jl => SimpleG.jl} (62%) diff --git a/src/grid/simple.jl b/src/grid/simple.jl index c46a19b..cffaf63 100644 --- a/src/grid/simple.jl +++ b/src/grid/simple.jl @@ -248,6 +248,7 @@ function Uniform{T,BTIN}(bound, N; Ntot = N - 1 interval = (gpbound[2] - gpbound[1]) / Ntot grid = gpbound[1] .+ Vector(1:N) .* interval .- (interval) + grid[1], grid[end] = gpbound[1], gpbound[end] # ensure bounds of grid points are exactly gpbound weight = similar(grid) for i in 1:N if i == 1 diff --git a/test/chebyshev.jl b/test/BaryChebTools.jl similarity index 100% rename from test/chebyshev.jl rename to test/BaryChebTools.jl diff --git a/test/CompositeG.jl b/test/CompositeG.jl new file mode 100644 index 0000000..c2a2034 --- /dev/null +++ b/test/CompositeG.jl @@ -0,0 +1,127 @@ + +@testset "CompositeGrids" begin + + # with a shift to the grid element, check if produce the correct floored index + function check(grid, range, shift, idx_shift) + for i in range + @test(floor(grid, grid[i] + shift) == i + idx_shift) + # if floor(grid, grid[i] + shift) != i + idx_shift + # return false + # end + end + return true + end + + @testset "Composite" begin + uniform = SimpleGrid.Uniform{Float64}([0.0, 1.0], 3) + gauss1 = SimpleGrid.GaussLegendre{Float64}([0.0, 0.5], 4) + gauss2 = SimpleGrid.GaussLegendre{Float64}([0.5, 1.0], 4) + comp = CompositeGrid.Composite{ + Float64, + typeof(uniform), + SimpleGrid.GaussLegendre{Float64} + }(uniform, [gauss1, gauss2]) + + println(comp) + # println(comp.inits) + + @test floor(comp, 0.0) == 1 + @test floor(comp, comp[1]) == 1 + + δ = 1.0e-12 + check(comp, 2:comp.size-1, δ, 0) + check(comp, 2:comp.size-1, -δ, -1) + + @test floor(comp, comp[end]) == comp.size - 1 + @test floor(comp, 1.0) == comp.size - 1 + + end + + @testset "CompositeLog" begin + + comp = CompositeGrid.CompositeLogGrid(:cheb, [0.0, 1.0], 4, 0.001, true, 4) + println(comp) + # println(comp.inits) + + @test floor(comp, 0.0) == 1 + @test floor(comp, comp[1]) == 1 + + δ = 1.0e-12 + check(comp, 2:comp.size-1, δ, 0) + check(comp, 2:comp.size-1, -δ, -1) + + @test floor(comp, comp[end]) == comp.size - 1 + @test floor(comp, 1.0) == comp.size - 1 + + # comp = CompositeGrid.LogDensedGrid(:uniform, [0.0, 10.0], [0.0, 1.0, 1.0, 2.0, 2.000001], 4, 0.001, 4) + comp = CompositeGrid.LogDensedGrid(type=:uniform, + bound=[0.0, 10.0], + dense_at=[0.0, 1.0, 1.0, 2.0, 2.000001], + N=4, + minterval=0.001, + order=4) + # println(comp.grid) + # println(comp.inits) + # println(CompositeGrid.denseindex(comp)) + # println([comp[i] for i in CompositeGrid.denseindex(comp)]) + + @test floor(comp, 0.0) == 1 + @test floor(comp, comp[1]) == 1 + + δ = 1.0e-12 + check(comp, 2:comp.size-1, δ, 0) + check(comp, 2:comp.size-1, -δ, -1) + + @test floor(comp, comp[end]) == comp.size - 1 + @test floor(comp, 10.0) == comp.size - 1 + + # test grid generation + comp = CompositeGrid.LogDensedGrid(:uniform, [0.0, 10.0], [0.0,], 4, 0.001, 4) + # println(comp.grid) + comp = CompositeGrid.LogDensedGrid(:uniform, [0.0, 10.0], [0.0, 1.0], 4, 0.001, 4) + # println(comp.grid) + comp = CompositeGrid.LogDensedGrid(:uniform, [0.0, 10.0], [0.5, 1.0], 4, 0.001, 4) + # println(comp.grid) + comp = CompositeGrid.LogDensedGrid(:uniform, [0.0, 10.0], [0.5, 10.0], 4, 0.001, 4) + # println(comp.grid) + + # test edge case when two panel points are exactly minterval away + # raw panel will be [0.0, 5.0,5.001,5.002,10.0], + # after construction it will be [0.0, 5.001, 10.0] + comp = CompositeGrid.LogDensedGrid(:uniform, [0.0, 10.0], [5.0, 5.002], 4, 0.001, 4) + @test length(comp.panel) == 3 + @test isapprox(comp.panel[2], 5.001, atol=0.001) + end + @testset "Issue #47" begin + function no_non_increasing(vec::Vector{T}) where {T} + for i in 2:length(vec) + if vec[i] <= vec[i-1] + # Print the neighbors + if i == 2 + println("Non-increasing point: ", vec[i], " with left neighbor: ", vec[i-1]) + return false + elseif i == length(vec) + println("Non-increasing point: ", vec[i-1], " with right neighbor: ", vec[i]) + return false + else + println("Non-increasing point: ", vec[i-1], " with neighbors: ", vec[i-2], " and ", vec[i]) + return false + end + end + end + return true + end + kF = 1.9191582926775128 + kgrid = CompositeGrid.LogDensedGrid(:uniform, [1.0 * kF, 100 * kF], [kF,], 8, 0.1 * kF, 8) + @test no_non_increasing(kgrid.grid) + # println(kgrid.panel.grid) + # loggrid = kgrid.subgrids[1].panel + # println(loggrid.grid) + # ugrid1, ugrid2 = kgrid.subgrids[1].subgrids[6], kgrid.subgrids[1].subgrids[7] + # println(ugrid1.grid) + # println(ugrid2.grid) + # println(ugrid1.bound) + # println(ugrid2.bound) + end +end + diff --git a/test/interpolate.jl b/test/Interp.jl similarity index 100% rename from test/interpolate.jl rename to test/Interp.jl diff --git a/test/grid.jl b/test/SimpleG.jl similarity index 62% rename from test/grid.jl rename to test/SimpleG.jl index faa409c..5855071 100644 --- a/test/grid.jl +++ b/test/SimpleG.jl @@ -156,100 +156,3 @@ end end - -@testset "CompositeGrids" begin - - # with a shift to the grid element, check if produce the correct floored index - function check(grid, range, shift, idx_shift) - for i in range - @test(floor(grid, grid[i] + shift) == i + idx_shift) - # if floor(grid, grid[i] + shift) != i + idx_shift - # return false - # end - end - return true - end - - @testset "Composite" begin - uniform = SimpleGrid.Uniform{Float64}([0.0, 1.0], 3) - gauss1 = SimpleGrid.GaussLegendre{Float64}([0.0, 0.5], 4) - gauss2 = SimpleGrid.GaussLegendre{Float64}([0.5, 1.0], 4) - comp = CompositeGrid.Composite{ - Float64, - typeof(uniform), - SimpleGrid.GaussLegendre{Float64} - }(uniform, [gauss1, gauss2]) - - println(comp) - # println(comp.inits) - - @test floor(comp, 0.0) == 1 - @test floor(comp, comp[1]) == 1 - - δ = 1.0e-12 - check(comp, 2:comp.size-1, δ, 0) - check(comp, 2:comp.size-1, -δ, -1) - - @test floor(comp, comp[end]) == comp.size - 1 - @test floor(comp, 1.0) == comp.size - 1 - - end - - @testset "CompositeLog" begin - - comp = CompositeGrid.CompositeLogGrid(:cheb, [0.0, 1.0], 4, 0.001, true, 4) - println(comp) - # println(comp.inits) - - @test floor(comp, 0.0) == 1 - @test floor(comp, comp[1]) == 1 - - δ = 1.0e-12 - check(comp, 2:comp.size-1, δ, 0) - check(comp, 2:comp.size-1, -δ, -1) - - @test floor(comp, comp[end]) == comp.size - 1 - @test floor(comp, 1.0) == comp.size - 1 - - # comp = CompositeGrid.LogDensedGrid(:uniform, [0.0, 10.0], [0.0, 1.0, 1.0, 2.0, 2.000001], 4, 0.001, 4) - comp = CompositeGrid.LogDensedGrid(type=:uniform, - bound=[0.0, 10.0], - dense_at=[0.0, 1.0, 1.0, 2.0, 2.000001], - N=4, - minterval=0.001, - order=4) - # println(comp.grid) - # println(comp.inits) - # println(CompositeGrid.denseindex(comp)) - # println([comp[i] for i in CompositeGrid.denseindex(comp)]) - - @test floor(comp, 0.0) == 1 - @test floor(comp, comp[1]) == 1 - - δ = 1.0e-12 - check(comp, 2:comp.size-1, δ, 0) - check(comp, 2:comp.size-1, -δ, -1) - - @test floor(comp, comp[end]) == comp.size - 1 - @test floor(comp, 10.0) == comp.size - 1 - - # test grid generation - comp = CompositeGrid.LogDensedGrid(:uniform, [0.0, 10.0], [0.0,], 4, 0.001, 4) - # println(comp.grid) - comp = CompositeGrid.LogDensedGrid(:uniform, [0.0, 10.0], [0.0, 1.0], 4, 0.001, 4) - # println(comp.grid) - comp = CompositeGrid.LogDensedGrid(:uniform, [0.0, 10.0], [0.5, 1.0], 4, 0.001, 4) - # println(comp.grid) - comp = CompositeGrid.LogDensedGrid(:uniform, [0.0, 10.0], [0.5, 10.0], 4, 0.001, 4) - # println(comp.grid) - - # test edge case when two panel points are exactly minterval away - # raw panel will be [0.0, 5.0,5.001,5.002,10.0], - # after construction it will be [0.0, 5.001, 10.0] - comp = CompositeGrid.LogDensedGrid(:uniform, [0.0, 10.0], [5.0, 5.002], 4, 0.001, 4) - @test length(comp.panel) == 3 - @test isapprox(comp.panel[2], 5.001, atol=0.001) - end - -end - diff --git a/test/runtests.jl b/test/runtests.jl index 045bf26..ee1c429 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,9 +2,10 @@ using CompositeGrids, Test, StaticArrays, LinearAlgebra, Printf, Random, Statist # import Test: @test, @testset if isempty(ARGS) - include("chebyshev.jl") - include("grid.jl") - include("interpolate.jl") + include("BaryChebTools.jl") + include("SimpleG.jl") + include("CompositeG.jl") + include("Interp.jl") # include("io.jl") include("mc.jl") include("periodic.jl")