Mercurial > repos > public > sbplib_julia
changeset 1903:04c251bccbd4 feature/grids/manifolds
Merge feature/grids/parameter_spaces
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Sat, 01 Feb 2025 22:17:39 +0100 |
parents | edee7d677efb (current diff) f93ba5832146 (diff) |
children | e97f4352b8d0 |
files | src/Grids/Grids.jl src/Grids/equidistant_grid.jl src/Grids/manifolds.jl src/Grids/mapped_grid.jl test/Grids/equidistant_grid_test.jl test/Grids/manifolds_test.jl test/Grids/mapped_grid_test.jl |
diffstat | 7 files changed, 150 insertions(+), 152 deletions(-) [+] |
line wrap: on
line diff
--- a/src/Grids/Grids.jl Sat Feb 01 21:38:49 2025 +0100 +++ b/src/Grids/Grids.jl Sat Feb 01 22:17:39 2025 +0100 @@ -72,6 +72,7 @@ export mapped_grid export metric_tensor +include("parameter_space.jl") include("manifolds.jl") include("grid.jl") include("tensor_grid.jl")
--- a/src/Grids/manifolds.jl Sat Feb 01 21:38:49 2025 +0100 +++ b/src/Grids/manifolds.jl Sat Feb 01 22:17:39 2025 +0100 @@ -1,92 +1,3 @@ -""" - ParameterSpace{D} - -A space of parameters of dimension `D`. Used with `Chart` to indicate which -parameters are valid for that chart. - -Common parameter spaces are created using the functions unit sized spaces -* `unitinterval` -* `unitrectangle` -* `unitbox` -* `unittriangle` -* `unittetrahedron` -* `unithyperbox` -* `unitsimplex` - -See also: [`Interval`](@ref), [`Rectangle`](@ref), [`Box`](@ref), -[`Triangle`](@ref), [`Tetrahedron`](@ref), [`HyperBox`](@ref), -[`Simplex`](@ref), -""" -abstract type ParameterSpace{D} end -Base.ndims(::ParameterSpace{D}) where D = D - -struct Interval{T} <: ParameterSpace{1} - a::T - b::T - - function Interval(a,b) - a, b = promote(a, b) - new{typeof(a)}(a,b) - end -end - -limits(i::Interval) = (i.a, i.b) - -unitinterval(T=Float64) = Interval(zero(T), one(T)) - - -struct HyperBox{T,D} <: ParameterSpace{D} - a::SVector{D,T} - b::SVector{D,T} -end - -function HyperBox(a,b) - ET = promote_type(eltype(a),eltype(b)) - T = SVector{length(a),ET} - HyperBox(convert(T,a), convert(T,b)) -end - -Rectangle{T} = HyperBox{T,2} -Box{T} = HyperBox{T,3} - -limits(box::HyperBox, d) = (box.a[d], box.b[d]) -limits(box::HyperBox) = (box.a, box.b) - -unitsquare(T=Float64) = unithyperbox(T,2) -unitcube(T=Float64) = unithyperbox(T,3) -unithyperbox(T, D) = HyperBox((@SVector zeros(T,D)), (@SVector ones(T,D))) -unithyperbox(D) = unithyperbox(Float64,D) - - -struct Simplex{T,D,NV} <: ParameterSpace{D} - verticies::NTuple{NV,SVector{D,T}} - - Simplex(verticies::Tuple{SVector{D,T}, Vararg{SVector{D,T},N}}) where {T,D,N} = new{T,D,N+1}(verticies) - Simplex(::Tuple{}) = throw(ArgumentError("Must provide at least one vertex.")) -end - -function Simplex(verticies::Vararg{AbstractArray}) - ET = mapreduce(eltype,promote_type,verticies) - T = SVector{length(verticies[1]),ET} - - return Simplex(Tuple(convert(T,v) for v ∈ verticies)) -end - -verticies(s::Simplex) = s.verticies - -Triangle{T} = Simplex{T,2} -Tetrahedron{T} = Simplex{T,3} - -unittriangle(T=Float64) = unitsimplex(T,2) -unittetrahedron(T=Float64) = unitsimplex(T,3) -function unitsimplex(T,D) - z = @SVector zeros(T,D) - unitelement = one(eltype(z)) - verticies = ntuple(i->setindex(z, unitelement, i), D) - return Simplex((z,verticies...)) -end -unitsimplex(D) = unitsimplex(Float64, D) - """ Chart{D}
--- a/src/Grids/mapped_grid.jl Sat Feb 01 21:38:49 2025 +0100 +++ b/src/Grids/mapped_grid.jl Sat Feb 01 22:17:39 2025 +0100 @@ -94,7 +94,7 @@ # TODO: Make sure all methods of `mapped_grid` are implemented correctly and tested. """ - mapped_grid(x, J, size::Vararg{Int}) + mapped_grid(x, J, size...) A `MappedGrid` with a default logical grid on the D-dimensional unit hyper box [0,1]ᴰ. `x` and `J` are functions to be evaluated on the logical grid @@ -121,7 +121,7 @@ end """ - mapped_grid(x, J, parameterspace, size) + mapped_grid(x, J, ps::ParameterSpace, size...) A `MappedGrid` with logical grid `lg`. Physical coordinates and Jacobian are determined by the functions `x` and `J`.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Grids/parameter_space.jl Sat Feb 01 22:17:39 2025 +0100 @@ -0,0 +1,88 @@ +""" + ParameterSpace{D} + +A space of parameters of dimension `D`. Used with `Chart` to indicate which +parameters are valid for that chart. + +Common parameter spaces are created using the functions unit sized spaces +* `unitinterval` +* `unitrectangle` +* `unitbox` +* `unittriangle` +* `unittetrahedron` +* `unithyperbox` +* `unitsimplex` + +See also: [`Interval`](@ref), [`Rectangle`](@ref), [`Box`](@ref), +[`Triangle`](@ref), [`Tetrahedron`](@ref), [`HyperBox`](@ref), +[`Simplex`](@ref), +""" +abstract type ParameterSpace{D} end +Base.ndims(::ParameterSpace{D}) where D = D + +struct Interval{T} <: ParameterSpace{1} + a::T + b::T + + function Interval(a,b) + a, b = promote(a, b) + new{typeof(a)}(a,b) + end +end + +limits(i::Interval) = (i.a, i.b) + +unitinterval(T=Float64) = Interval(zero(T), one(T)) + + +struct HyperBox{T,D} <: ParameterSpace{D} + a::SVector{D,T} + b::SVector{D,T} +end + +function HyperBox(a,b) + ET = promote_type(eltype(a),eltype(b)) + T = SVector{length(a),ET} + HyperBox(convert(T,a), convert(T,b)) +end + +Rectangle{T} = HyperBox{T,2} +Box{T} = HyperBox{T,3} + +limits(box::HyperBox, d) = (box.a[d], box.b[d]) +limits(box::HyperBox) = (box.a, box.b) + +unitsquare(T=Float64) = unithyperbox(T,2) +unitcube(T=Float64) = unithyperbox(T,3) +unithyperbox(T, D) = HyperBox((@SVector zeros(T,D)), (@SVector ones(T,D))) +unithyperbox(D) = unithyperbox(Float64,D) + + +struct Simplex{T,D,NV} <: ParameterSpace{D} + verticies::NTuple{NV,SVector{D,T}} + + Simplex(verticies::Tuple{SVector{D,T}, Vararg{SVector{D,T},N}}) where {T,D,N} = new{T,D,N+1}(verticies) + Simplex(::Tuple{}) = throw(ArgumentError("Must provide at least one vertex.")) +end + +function Simplex(verticies::Vararg{AbstractArray}) + ET = mapreduce(eltype,promote_type,verticies) + T = SVector{length(verticies[1]),ET} + + return Simplex(Tuple(convert(T,v) for v ∈ verticies)) +end + +verticies(s::Simplex) = s.verticies + +Triangle{T} = Simplex{T,2} +Tetrahedron{T} = Simplex{T,3} + +unittriangle(T=Float64) = unitsimplex(T,2) +unittetrahedron(T=Float64) = unitsimplex(T,3) +function unitsimplex(T,D) + z = @SVector zeros(T,D) + unitelement = one(eltype(z)) + verticies = ntuple(i->setindex(z, unitelement, i), D) + return Simplex((z,verticies...)) +end +unitsimplex(D) = unitsimplex(Float64, D)
--- a/test/Grids/equidistant_grid_test.jl Sat Feb 01 21:38:49 2025 +0100 +++ b/test/Grids/equidistant_grid_test.jl Sat Feb 01 22:17:39 2025 +0100 @@ -155,7 +155,6 @@ end end - @testset "equidistant_grid(::ParameterSpace)" begin ps = HyperBox((0,0),(2,1))
--- a/test/Grids/manifolds_test.jl Sat Feb 01 21:38:49 2025 +0100 +++ b/test/Grids/manifolds_test.jl Sat Feb 01 22:17:39 2025 +0100 @@ -4,66 +4,6 @@ using Diffinitive.RegionIndices using Diffinitive.LazyTensors -# using StaticArrays - -@testset "ParameterSpace" begin - @test ndims(HyperBox([1,1], [2,2])) == 2 - @test ndims(unittetrahedron()) == 3 -end - -@testset "Interval" begin - @test Interval <: ParameterSpace{1} - - @test Interval(0,1) isa Interval{Int} - @test Interval(0,1.) isa Interval{Float64} - - @test unitinterval() isa Interval{Float64} - @test unitinterval() == Interval(0.,1.) - @test limits(unitinterval()) == (0.,1.) - - @test unitinterval(Int) isa Interval{Int} - @test unitinterval(Int) == Interval(0,1) - @test limits(unitinterval(Int)) == (0,1) -end - -@testset "HyperBox" begin - @test HyperBox{<:Any, 2} <: ParameterSpace{2} - @test HyperBox([1,1], [2,2]) isa HyperBox{Int, 2} - - @test HyperBox([1,2], [1.,2.]) isa HyperBox{Float64,2} - - @test limits(HyperBox([1,2], [3,4])) == ([1,2], [3,4]) - @test limits(HyperBox([1,2], [3,4]), 1) == (1,3) - @test limits(HyperBox([1,2], [3,4]), 2) == (2,4) - - @test unitsquare() isa HyperBox{Float64,2} - @test limits(unitsquare()) == ([0,0],[1,1]) - - @test unitcube() isa HyperBox{Float64,3} - @test limits(unitcube()) == ([0,0,0],[1,1,1]) - - @test unithyperbox(4) isa HyperBox{Float64,4} - @test limits(unithyperbox(4)) == ([0,0,0,0],[1,1,1,1]) -end - -@testset "Simplex" begin - @test Simplex{<:Any, 3} <: ParameterSpace{3} - @test Simplex([1,2], [3,4]) isa Simplex{Int, 2} - @test Simplex([1,2,3], [4,5,6],[1,1,1]) isa Simplex{Int, 3} - - @test Simplex([1,2], [3.,4.]) isa Simplex{Float64, 2} - - @test verticies(Simplex([1,2], [3,4])) == ([1,2], [3,4]) - - @test unittriangle() isa Simplex{Float64,2} - @test verticies(unittriangle()) == ([0,0], [1,0], [0,1]) - - @test unittetrahedron() isa Simplex{Float64,3} - @test verticies(unittetrahedron()) == ([0,0,0], [1,0,0], [0,1,0],[0,0,1]) - - @test unitsimplex(4) isa Simplex{Float64,4} -end - @testset "Chart" begin c = Chart(x->2x, unitsquare()) @test c isa Chart{2}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/Grids/parameter_space_test.jl Sat Feb 01 22:17:39 2025 +0100 @@ -0,0 +1,59 @@ +using Test + +@testset "ParameterSpace" begin + @test ndims(HyperBox([1,1], [2,2])) == 2 + @test ndims(unittetrahedron()) == 3 +end + +@testset "Interval" begin + @test Interval <: ParameterSpace{1} + + @test Interval(0,1) isa Interval{Int} + @test Interval(0,1.) isa Interval{Float64} + + @test unitinterval() isa Interval{Float64} + @test unitinterval() == Interval(0.,1.) + @test limits(unitinterval()) == (0.,1.) + + @test unitinterval(Int) isa Interval{Int} + @test unitinterval(Int) == Interval(0,1) + @test limits(unitinterval(Int)) == (0,1) +end + +@testset "HyperBox" begin + @test HyperBox{<:Any, 2} <: ParameterSpace{2} + @test HyperBox([1,1], [2,2]) isa HyperBox{Int, 2} + + @test HyperBox([1,2], [1.,2.]) isa HyperBox{Float64,2} + + @test limits(HyperBox([1,2], [3,4])) == ([1,2], [3,4]) + @test limits(HyperBox([1,2], [3,4]), 1) == (1,3) + @test limits(HyperBox([1,2], [3,4]), 2) == (2,4) + + @test unitsquare() isa HyperBox{Float64,2} + @test limits(unitsquare()) == ([0,0],[1,1]) + + @test unitcube() isa HyperBox{Float64,3} + @test limits(unitcube()) == ([0,0,0],[1,1,1]) + + @test unithyperbox(4) isa HyperBox{Float64,4} + @test limits(unithyperbox(4)) == ([0,0,0,0],[1,1,1,1]) +end + +@testset "Simplex" begin + @test Simplex{<:Any, 3} <: ParameterSpace{3} + @test Simplex([1,2], [3,4]) isa Simplex{Int, 2} + @test Simplex([1,2,3], [4,5,6],[1,1,1]) isa Simplex{Int, 3} + + @test Simplex([1,2], [3.,4.]) isa Simplex{Float64, 2} + + @test verticies(Simplex([1,2], [3,4])) == ([1,2], [3,4]) + + @test unittriangle() isa Simplex{Float64,2} + @test verticies(unittriangle()) == ([0,0], [1,0], [0,1]) + + @test unittetrahedron() isa Simplex{Float64,3} + @test verticies(unittetrahedron()) == ([0,0,0], [1,0,0], [0,1,0],[0,0,1]) + + @test unitsimplex(4) isa Simplex{Float64,4} +end