Mercurial > repos > public > sbplib_julia
comparison test/Grids/equidistant_grid_test.jl @ 2057:8a2a0d678d6f feature/lazy_tensors/pretty_printing
Merge default
| author | Jonatan Werpers <jonatan@werpers.com> |
|---|---|
| date | Tue, 10 Feb 2026 22:41:19 +0100 |
| parents | 624e19c20c19 |
| children |
comparison
equal
deleted
inserted
replaced
| 1110:c0bff9f6e0fb | 2057:8a2a0d678d6f |
|---|---|
| 1 using Diffinitive.Grids | |
| 2 using Test | |
| 3 using Diffinitive.LazyTensors | |
| 4 using StaticArrays | |
| 5 | |
| 6 | |
| 7 @testset "EquidistantGrid" begin | |
| 8 @test EquidistantGrid(0:0.1:10) isa EquidistantGrid | |
| 9 @test EquidistantGrid(range(0,1,length=10)) isa EquidistantGrid | |
| 10 @test EquidistantGrid(LinRange(0,1,11)) isa EquidistantGrid | |
| 11 | |
| 12 @testset "Indexing Interface" begin | |
| 13 g = EquidistantGrid(0:0.1:10) | |
| 14 @test g[1] == 0.0 | |
| 15 @test g[5] == 0.4 | |
| 16 @test g[101] == 10.0 | |
| 17 | |
| 18 @test g[begin] == 0.0 | |
| 19 @test g[end] == 10.0 | |
| 20 | |
| 21 @test all(eachindex(g) .== 1:101) | |
| 22 | |
| 23 @test firstindex(g) == 1 | |
| 24 @test lastindex(g) == 101 | |
| 25 end | |
| 26 | |
| 27 @testset "Iterator interface" begin | |
| 28 @test eltype(EquidistantGrid(0:10)) == Int | |
| 29 @test eltype(EquidistantGrid(0:2:10)) == Int | |
| 30 @test eltype(EquidistantGrid(0:0.1:10)) == Float64 | |
| 31 @test size(EquidistantGrid(0:10)) == (11,) | |
| 32 @test size(EquidistantGrid(0:0.1:10)) == (101,) | |
| 33 | |
| 34 @test size(EquidistantGrid(0:0.1:10),1) == 101 | |
| 35 | |
| 36 @test collect(EquidistantGrid(0:0.1:0.5)) == [0.0, 0.1, 0.2, 0.3, 0.4, 0.5] | |
| 37 | |
| 38 @test Base.IteratorSize(EquidistantGrid{Float64, StepRange{Float64}}) == Base.HasShape{1}() | |
| 39 end | |
| 40 | |
| 41 @testset "Base" begin | |
| 42 @test ndims(EquidistantGrid(0:10)) == 1 | |
| 43 | |
| 44 g = EquidistantGrid(0:0.1:10) | |
| 45 @test axes(g,1) == 1:101 | |
| 46 @test axes(g) == (1:101,) | |
| 47 end | |
| 48 | |
| 49 @testset "spacing" begin | |
| 50 @test spacing(EquidistantGrid(0:10)) == 1 | |
| 51 @test spacing(EquidistantGrid(0:0.1:10)) == 0.1 | |
| 52 end | |
| 53 | |
| 54 @testset "inverse_spacing" begin | |
| 55 @test inverse_spacing(EquidistantGrid(0:10)) == 1 | |
| 56 @test inverse_spacing(EquidistantGrid(0:0.1:10)) == 10 | |
| 57 end | |
| 58 | |
| 59 @testset "min_spacing" begin | |
| 60 @test min_spacing(EquidistantGrid(0:10)) == 1 | |
| 61 @test min_spacing(EquidistantGrid(0:0.1:10)) == 0.1 | |
| 62 end | |
| 63 | |
| 64 @testset "boundary_identifiers" begin | |
| 65 g = EquidistantGrid(0:0.1:10) | |
| 66 @test boundary_identifiers(g) == (LowerBoundary(), UpperBoundary()) | |
| 67 @inferred boundary_identifiers(g) | |
| 68 end | |
| 69 | |
| 70 @testset "boundary_grid" begin | |
| 71 g = EquidistantGrid(0:0.1:1) | |
| 72 @test boundary_grid(g, LowerBoundary()) == ZeroDimGrid(0.0) | |
| 73 @test boundary_grid(g, UpperBoundary()) == ZeroDimGrid(1.0) | |
| 74 end | |
| 75 | |
| 76 @testset "boundary_indices" begin | |
| 77 g = EquidistantGrid(0:0.1:1) | |
| 78 @test boundary_indices(g, LowerBoundary()) == 1 | |
| 79 @test boundary_indices(g, UpperBoundary()) == 11 | |
| 80 | |
| 81 gf = collect(g) | |
| 82 @test gf[boundary_indices(g, LowerBoundary())] == gf[1] | |
| 83 @test gf[boundary_indices(g, UpperBoundary())] == gf[11] | |
| 84 | |
| 85 g = EquidistantGrid(2:0.1:10) | |
| 86 @test boundary_indices(g, LowerBoundary()) == 1 | |
| 87 @test boundary_indices(g, UpperBoundary()) == 81 | |
| 88 end | |
| 89 | |
| 90 @testset "refine" begin | |
| 91 g = EquidistantGrid(0:0.1:1) | |
| 92 @test refine(g, 1) == g | |
| 93 @test refine(g, 2) == EquidistantGrid(0:0.05:1) | |
| 94 @test refine(g, 3) == EquidistantGrid(0:(0.1/3):1) | |
| 95 end | |
| 96 | |
| 97 @testset "coarsen" begin | |
| 98 g = EquidistantGrid(0:1:10) | |
| 99 @test coarsen(g, 1) == g | |
| 100 @test coarsen(g, 2) == EquidistantGrid(0:2:10) | |
| 101 | |
| 102 g = EquidistantGrid(0:0.1:1) | |
| 103 @test coarsen(g, 1) == g | |
| 104 @test coarsen(g, 2) == EquidistantGrid(0:0.2:1) | |
| 105 | |
| 106 g = EquidistantGrid(0:10) | |
| 107 @test coarsen(g, 1) == EquidistantGrid(0:1:10) | |
| 108 @test coarsen(g, 2) == EquidistantGrid(0:2:10) | |
| 109 | |
| 110 @test_throws DomainError(3, "Size minus 1 must be divisible by the ratio.") coarsen(g, 3) | |
| 111 end | |
| 112 end | |
| 113 | |
| 114 | |
| 115 @testset "equidistant_grid" begin | |
| 116 @test equidistant_grid(0.0,1.0, 4) isa EquidistantGrid | |
| 117 @test equidistant_grid((0.0,0.0),(8.0,5.0), 4, 3) isa TensorGrid | |
| 118 @test equidistant_grid((0.0,),(8.0,), 4) isa TensorGrid | |
| 119 | |
| 120 # constuctor | |
| 121 @test_throws DomainError equidistant_grid(0.0, 1.0, 0) | |
| 122 @test_throws DomainError equidistant_grid(1.0, 1.0, 1) | |
| 123 @test_throws DomainError equidistant_grid(1.0, -1.0, 1) | |
| 124 | |
| 125 @test_throws DomainError equidistant_grid((0.0,0.0),(1.0,1.0), 0, 0) | |
| 126 @test_throws DomainError equidistant_grid((1.0,1.0),(1.0,1.0), 1, 1) | |
| 127 @test_throws DomainError equidistant_grid((1.0,1.0),(-1.0,-1.0), 1, 1) | |
| 128 | |
| 129 @test_throws ArgumentError equidistant_grid((0.0,),(8.0,5.0), 4, 3, 4) | |
| 130 | |
| 131 @testset "Base" begin | |
| 132 @test eltype(equidistant_grid(0.0, 1.0, 4)) == Float64 | |
| 133 @test eltype(equidistant_grid((0,0),(1,3), 4, 3)) <: AbstractVector{Float64} | |
| 134 | |
| 135 @test size(equidistant_grid(0.0, 1.0, 4)) == (4,) | |
| 136 @test size(equidistant_grid((0.0,0.0), (2.0,1.0), 5, 3)) == (5,3) | |
| 137 | |
| 138 @test size(equidistant_grid((0.0,0.0), (2.0,1.0), 5, 3), 1) == 5 | |
| 139 @test size(equidistant_grid((0.0,0.0), (2.0,1.0), 5, 3), 2) == 3 | |
| 140 | |
| 141 @test ndims(equidistant_grid(0.0, 1.0, 4)) == 1 | |
| 142 @test ndims(equidistant_grid((0.0,0.0), (2.0,1.0), 5, 3)) == 2 | |
| 143 end | |
| 144 | |
| 145 @testset "getindex" begin | |
| 146 g = equidistant_grid((-1.0,0.0), (0.0,7.11), 5, 3) | |
| 147 gp = collect(g); | |
| 148 p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11); | |
| 149 (-0.75,0.) (-0.75,7.11/2) (-0.75,7.11); | |
| 150 (-0.5,0.) (-0.5,7.11/2) (-0.5,7.11); | |
| 151 (-0.25,0.) (-0.25,7.11/2) (-0.25,7.11); | |
| 152 (0.,0.) (0.,7.11/2) (0.,7.11)] | |
| 153 for i ∈ eachindex(gp) | |
| 154 @test [gp[i]...] ≈ [p[i]...] atol=5e-13 | |
| 155 end | |
| 156 end | |
| 157 | |
| 158 @testset "equidistant_grid(::ParameterSpace)" begin | |
| 159 ps = HyperBox((0,0),(2,1)) | |
| 160 | |
| 161 @test equidistant_grid(ps, 3,4) == equidistant_grid((0,0), (2,1), 3,4) | |
| 162 | |
| 163 @test equidistant_grid(unitinterval(),3) == equidistant_grid(0,1,3) | |
| 164 @test equidistant_grid(HyperBox((0,),(2,)),4) == equidistant_grid(@SVector[0], @SVector[2], 4) | |
| 165 end | |
| 166 | |
| 167 @testset "equidistant_grid(::Chart)" begin | |
| 168 c = Chart(unitsquare()) do (ξ,η) | |
| 169 @SVector[2ξ, 3η] | |
| 170 end | |
| 171 Grids.jacobian(c::typeof(c), ξ̄) = @SMatrix[2 0; 0 3] | |
| 172 | |
| 173 @test equidistant_grid(c, 5, 4) isa Grid | |
| 174 end | |
| 175 end | |
| 176 | |
| 177 | |
| 178 @testset "change_length" begin | |
| 179 @test Grids.change_length(0:20, 21) == 0:20 | |
| 180 @test Grids.change_length(0:20, 11) == 0:2:20 | |
| 181 @test Grids.change_length(0:2:20, 21) == 0:20 | |
| 182 | |
| 183 @test Grids.change_length(range(0,1,length=10), 10) == range(0,1,length=10) | |
| 184 @test Grids.change_length(range(0,1,length=10), 5) == range(0,1,length=5) | |
| 185 @test Grids.change_length(range(0,1,length=10), 20) == range(0,1,length=20) | |
| 186 | |
| 187 @test Grids.change_length(LinRange(1,2,10),10) == LinRange(1,2,10) | |
| 188 @test Grids.change_length(LinRange(1,2,10),15) == LinRange(1,2,15) | |
| 189 end |
