Mercurial > repos > public > sbplib_julia
changeset 1917:27a2d37ff3b4 feature/grids/manifolds
Merge feature/grids/multiblock_boundaries
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 04 Feb 2025 21:45:06 +0100 |
parents | e7f8d11c4670 (diff) e68669552ed8 (current diff) |
children | b1560da986f3 |
files | src/Grids/Grids.jl |
diffstat | 13 files changed, 503 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/Manifest.toml Mon Feb 03 23:10:12 2025 +0100 +++ b/Manifest.toml Tue Feb 04 21:45:06 2025 +0100 @@ -2,7 +2,7 @@ julia_version = "1.11.3" manifest_format = "2.0" -project_hash = "383b10bc09a08d519764abf167e40dae29b16b2e" +project_hash = "51374910f6b7831392791bcca0079860f1023f47" [[deps.Artifacts]] uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
--- a/Project.toml Mon Feb 03 23:10:12 2025 +0100 +++ b/Project.toml Tue Feb 04 21:45:06 2025 +0100 @@ -9,6 +9,7 @@ TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" [weakdeps] +Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" SparseArrayKit = "a9a3c162-d163-4c15-8926-b8794fbefed2" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" @@ -16,6 +17,7 @@ [extensions] DiffinitiveMakieExt = "Makie" +DiffinitivePlotsExt = "Plots" DiffinitiveSparseArrayKitExt = ["SparseArrayKit", "Tokens"] DiffinitiveSparseArraysExt = ["SparseArrays", "Tokens"] @@ -24,6 +26,7 @@ StaticArrays = "1.0" TOML = "1.0" Makie = "0.21" +Plots = "1.40" SparseArrayKit = "0.4" Tokens = "0.1.1" SparseArrays = "1.10"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ext/DiffinitivePlotsExt.jl Tue Feb 04 21:45:06 2025 +0100 @@ -0,0 +1,72 @@ +module DiffinitivePlotsExt + +using Diffinitive.Grids +using Plots + +@recipe f(::Type{<:Grid}, g::Grid) = map(Tuple,g)[:] + +@recipe function f(c::Chart{2,<:Rectangle}, n=5, m=n; draw_border=true, bordercolor=1) + Ξ = parameterspace(c) + ξs = range(limits(Ξ,1)..., n) + ηs = range(limits(Ξ,2)..., m) + + label := false + seriescolor --> 2 + for ξ ∈ ξs + @series adapted_curve_grid(η->c((ξ,η)),limits(Ξ,1)) + end + + for η ∈ ηs + @series adapted_curve_grid(ξ->c((ξ,η)),limits(Ξ,2)) + end + + if ~draw_border + return + end + + for ξ ∈ limits(Ξ,1) + @series begin + linewidth --> 3 + seriescolor := bordercolor + adapted_curve_grid(η->c((ξ,η)),limits(Ξ,1)) + end + end + + for η ∈ limits(Ξ,2) + @series begin + linewidth --> 3 + seriescolor := bordercolor + adapted_curve_grid(ξ->c((ξ,η)),limits(Ξ,2)) + end + end +end + +function adapted_curve_grid(g, minmax) + t1, _ = PlotUtils.adapted_grid(t->g(t)[1], minmax) + t2, _ = PlotUtils.adapted_grid(t->g(t)[2], minmax) + + ts = sort(vcat(t1,t2)) + + x = map(ts) do t + g(t)[1] + end + y = map(ts) do t + g(t)[2] + end + + return x, y +end + +# get_axis_limits(plt, :x) + + +# ReicpesPipline/src/user_recipe.jl +# @recipe function f(f::FuncOrFuncs{F}) where {F<:Function} + +# @recipe function f(f::Function, xmin::Number, xmax::Number) + +# _scaled_adapted_grid(f, xscale, yscale, xmin, xmax) + +end + +
--- a/src/Grids/Grids.jl Mon Feb 03 23:10:12 2025 +0100 +++ b/src/Grids/Grids.jl Tue Feb 04 21:45:06 2025 +0100 @@ -4,6 +4,35 @@ using StaticArrays using LinearAlgebra +export ParameterSpace +export HyperBox +export Simplex +export Interval +export Rectangle +export Box +export Triangle +export Tetrahedron + +export limits +export unitinterval +export unitsquare +export unitcube +export unithyperbox + +export verticies +export unittriangle +export unittetrahedron +export unitsimplex + +export Chart + +export Atlas +export charts +export connections +export CartesianAtlas + +export parameterspace + # Grid export Grid export coordinate_size @@ -45,6 +74,8 @@ export mapped_grid export metric_tensor +include("parameter_space.jl") +include("manifolds.jl") include("grid.jl") include("tensor_grid.jl") include("equidistant_grid.jl")
--- a/src/Grids/equidistant_grid.jl Mon Feb 03 23:10:12 2025 +0100 +++ b/src/Grids/equidistant_grid.jl Tue Feb 04 21:45:06 2025 +0100 @@ -135,21 +135,30 @@ end """ - equidistant_grid(limit_lower::T, limit_upper::T, size::Int) + equidistant_grid(limit_lower::Number, limit_upper::Number, size::Int) Constructs a 1D equidistant grid. """ function equidistant_grid(limit_lower::Number, limit_upper::Number, size::Int) - if any(size .<= 0) + if size <= 0 throw(DomainError("size must be postive")) end - if any(limit_upper.-limit_lower .<= 0) + if limit_upper-limit_lower <= 0 throw(DomainError("side length must be postive")) end + return EquidistantGrid(range(limit_lower, limit_upper, length=size)) # TBD: Should it use LinRange instead? end +equidistant_grid(d::Interval, size::Int) = equidistant_grid(limits(d)..., size) +equidistant_grid(hb::HyperBox, dims::Vararg{Int}) = equidistant_grid(limits(hb)..., dims...) + +function equidistant_grid(c::Chart, dims::Vararg{Int}) + mapped_grid(c, ξ->jacobian(c,ξ), parameterspace(c), dims...) +end + + CartesianBoundary{D,BID} = TensorGridBoundary{D,BID} # TBD: What should we do about the naming of this boundary?
--- a/src/Grids/grid.jl Mon Feb 03 23:10:12 2025 +0100 +++ b/src/Grids/grid.jl Tue Feb 04 21:45:06 2025 +0100 @@ -122,6 +122,8 @@ """ function boundary_identifiers end +# TBD: Boundary identifiers for charts and atlases? + """ boundary_grid(g::Grid, id::BoundaryIdentifier)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Grids/manifolds.jl Tue Feb 04 21:45:06 2025 +0100 @@ -0,0 +1,74 @@ +""" + Chart{D} + +A parametrized description of a manifold or part of a manifold. +""" +struct Chart{D, PST<:ParameterSpace{D}, MT} + mapping::MT + parameterspace::PST +end + +Base.ndims(::Chart{D}) where D = D +(c::Chart)(ξ) = c.mapping(ξ) +parameterspace(c::Chart) = c.parameterspace + +""" + jacobian(c::Chart, ξ) + +The jacobian of the mapping evaluated at `ξ`. This defers to the +implementation of `jacobian` for the mapping itself. If no implementation is +available one can easily be specified for either the mapping function or the +chart itself. +```julia +c = Chart(f, ps) +jacobian(f::typeof(f), ξ) = f′(ξ) +``` +or +```julia +c = Chart(f, ps) +jacobian(c::typeof(c),ξ) = f′(ξ) +``` +which will both allow calling `jacobian(c,ξ)`. +""" +jacobian(c::Chart, ξ) = jacobian(c.mapping, ξ) +# TBD: Can we register a error hint for when jacobian is called with a function that doesn't have a registered jacobian? + + +# TBD: Should Charts, parameterspaces, Atlases, have boundary names? + +""" + Atlas + +A collection of charts and their connections. +Should implement methods for `charts` and `connections`. +""" +abstract type Atlas end + +""" + charts(::Atlas) + +The colloction of charts in the atlas. +""" +function charts end + +""" + connections(::Atlas) + +TBD: What exactly should this return? +""" +function connections end + +struct CartesianAtlas <: Atlas + charts::Matrix{Chart} +end + +charts(a::CartesianAtlas) = a.charts +connections(a::CartesianAtlas) = nothing + +struct UnstructuredAtlas <: Atlas + charts::Vector{Chart} + connections +end + +charts(a::UnstructuredAtlas) = a.charts +connections(a::UnstructuredAtlas) = nothing
--- a/src/Grids/mapped_grid.jl Mon Feb 03 23:10:12 2025 +0100 +++ b/src/Grids/mapped_grid.jl Tue Feb 04 21:45:06 2025 +0100 @@ -92,9 +92,8 @@ ) end -# 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 @@ -102,8 +101,7 @@ """ function mapped_grid(x, J, size::Vararg{Int}) D = length(size) - lg = equidistant_grid(ntuple(i->0., D), ntuple(i->1., D), size...) # TODO: Clean this up with ParamaterSpace once feature/grids/manifolds is merged - return mapped_grid(x, J, lg) + return mapped_grid(x, J, unithyperbox(D), size...) end """ @@ -121,13 +119,13 @@ 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`. """ -function mapped_grid(x, J, parameterspace, size::Vararg{Int}) - lg = equidistant_grid(parameterspace, size...) +function mapped_grid(x, J, ps::ParameterSpace, size::Vararg{Int}) + lg = equidistant_grid(ps, size...) return mapped_grid(x, J, lg) end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Grids/parameter_space.jl Tue Feb 04 21:45:06 2025 +0100 @@ -0,0 +1,176 @@ +""" + ParameterSpace{D} + +A space of parameters of dimension `D`. + +Common parameter spaces are created using functions for unit sized spaces +* [`unitinterval`](@ref) +* [`unitsquare`](@ref) +* [`unitcube`](@ref) +* [`unithyperbox`](@ref) +* [`unittriangle`](@ref) +* [`unittetrahedron`](@ref) +* [`unitsimplex`](@ref) + +See also: [`Interval`](@ref), [`HyperBox`](@ref), +[`Simplex`](@ref). +""" +abstract type ParameterSpace{D} end +Base.ndims(::ParameterSpace{D}) where D = D + +""" + Interval{T} <: ParameterSpace{1} + +A `ParameterSpace` representing an interval. +""" +struct Interval{T} <: ParameterSpace{1} + a::T + b::T +end + +""" + Interval(a,b) + +An interval with limits `a` and `b`. +""" +function Interval(a,b) + a, b = promote(a, b) + Interval{typeof(a)}(a,b) +end + +""" + limits(i::Interval) + +The limits of the interval. +""" +limits(i::Interval) = (i.a, i.b) + +""" + unitinterval(T=Float64) + +The interval ``(0,1)``. +""" +unitinterval(T=Float64) = Interval(zero(T), one(T)) + + +""" + HyperBox{T,D} <: ParameterSpace{D} + +A `ParameterSpace` representing a hyper box. +""" +struct HyperBox{T,D} <: ParameterSpace{D} + a::SVector{D,T} + b::SVector{D,T} +end + +""" + HyperBox(a,b) + +A `HyperBox` with lower limits `a` and upper limits `b` for each dimension. +""" +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) + +Limits of `box` along dimension `d`. +""" +limits(box::HyperBox, d) = (box.a[d], box.b[d]) + +""" + limits(box::HyperBox) + +The lower and upper limits of `box` as tuples. +""" +limits(box::HyperBox) = (box.a, box.b) + +""" + unitsquare(T=Float64) + +The square starting at ``(0,0)`` with side length 1. +""" +unitsquare(T=Float64) = unithyperbox(T,2) + +""" + unitcube(T=Float64) + +The cube starting at ``(0,0,0)`` with side length 1. +""" +unitcube(T=Float64) = unithyperbox(T,3) + +""" + unithyperbox(T=Float64, D) + +The hypercube in dimension `D` starting at ``(0,0,0,...)`` with side length 1. +""" +unithyperbox(T, D) = HyperBox((@SVector zeros(T,D)), (@SVector ones(T,D))) +unithyperbox(D) = unithyperbox(Float64,D) + + +""" + Simplex{T,D,NV} <: ParameterSpace{D} + +A `ParameterSpace` representing a simplex. +""" +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 + +""" + Simplex(verticies...) + +A simplex with the given vierticies. +""" +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) + +Verticies of `s`. +""" +verticies(s::Simplex) = s.verticies + +Triangle{T} = Simplex{T,2} +Tetrahedron{T} = Simplex{T,3} + +""" + unittriangle(T=Float64) + +The simplex with verticies ``(0,0)``, ``(1,0)``, and ``(0,1)``. +""" +unittriangle(T=Float64) = unitsimplex(T,2) + +""" + unittetrahedron(T=Float64) + +The simplex with verticies ``(0,0,0)``, ``(1,0,0)``, ``(0,1,0)``, and ``(0,0,1)``. +""" +unittetrahedron(T=Float64) = unitsimplex(T,3) + +""" + unitsimplex(T=Float64,D) + +The unit simplex in dimension `D` with verticies ``(0,0,0,...)``, ``(1,0,0,...)``, ``(0,1,0,...)``, ``(0,0,1,...)``... +""" +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 Mon Feb 03 23:10:12 2025 +0100 +++ b/test/Grids/equidistant_grid_test.jl Tue Feb 04 21:45:06 2025 +0100 @@ -1,6 +1,7 @@ using Diffinitive.Grids using Test using Diffinitive.LazyTensors +using StaticArrays @testset "EquidistantGrid" begin @@ -153,6 +154,25 @@ @test [gp[i]...] ≈ [p[i]...] atol=5e-13 end end + + @testset "equidistant_grid(::ParameterSpace)" begin + ps = HyperBox((0,0),(2,1)) + + @test equidistant_grid(ps, 3,4) == equidistant_grid((0,0), (2,1), 3,4) + + @test equidistant_grid(unitinterval(),3) == equidistant_grid(0,1,3) + @test equidistant_grid(HyperBox((0,),(2,)),4) == equidistant_grid(@SVector[0], @SVector[2], 4) + end + + + @testset "equidistant_grid(::Chart)" begin + c = Chart(unitsquare()) do (ξ,η) + @SVector[2ξ, 3η] + end + Grids.jacobian(c::typeof(c), ξ̄) = @SMatrix[2 0; 0 3] + + @test equidistant_grid(c, 5, 4) isa Grid + end end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/Grids/manifolds_test.jl Tue Feb 04 21:45:06 2025 +0100 @@ -0,0 +1,45 @@ +using Test + +using Diffinitive.Grids +using Diffinitive.RegionIndices +using Diffinitive.LazyTensors + +@testset "Chart" begin + c = Chart(x->2x, unitsquare()) + @test c isa Chart{2} + @test c([3,2]) == [6,4] + @test parameterspace(c) == unitsquare() + @test ndims(c) == 2 + + @test_broken jacobian(c, [3,2]) +end + +@testset "CartesianAtlas" begin + c = Chart(identity, unitsquare()) + + a = CartesianAtlas([c c; c c]) + @test a isa Atlas + @test charts(a) == [c c; c c] + @test_broken connections(a) == [ + ( + ((1,1), CartesianBoundary{1,UpperBoundary}()), + ((1,2), CartesianBoundary{1,LowerBoundary}()), + ), + ( + ((1,1), CartesianBoundary{2,LowerBoundary}()), + ((2,1), CartesianBoundary{2,UpperBoundary}()), + ), + ( + ((2,2), CartesianBoundary{1,LowerBoundary}()), + ((2,1), CartesianBoundary{1,UpperBoundary}()), + ), + ( + ((2,2), CartesianBoundary{2,UpperBoundary}()), + ((1,2), CartesianBoundary{2,LowerBoundary}()), + ) + ] +end + +@testset "UnstructuredAtlas" begin + @test_broken false +end
--- a/test/Grids/mapped_grid_test.jl Mon Feb 03 23:10:12 2025 +0100 +++ b/test/Grids/mapped_grid_test.jl Tue Feb 04 21:45:06 2025 +0100 @@ -278,11 +278,13 @@ mg = mapped_grid(x̄, J, 10, 11) @test mg isa MappedGrid{SVector{2,Float64}, 2} - lg = equidistant_grid((0,0), (1,1), 10, 11) + lg = equidistant_grid(unitsquare(), 10, 11) @test logical_grid(mg) == lg @test collect(mg) == map(x̄, lg) @test mapped_grid(x̄, J, lg) == mg + + @test mapped_grid(x̄, J, unitsquare(), 10, 11) == mg end @testset "metric_tensor" begin
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/Grids/parameter_space_test.jl Tue Feb 04 21:45:06 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