changeset 1678:13a7a4ff49e3 feature/grids/manifolds

Merge feature/grids/curvilinear
author Jonatan Werpers <jonatan@werpers.com>
date Sun, 30 Jun 2024 10:50:44 +0200
parents 8250cf5a3ce9 (diff) 51f0c5f895fb (current diff)
children 529b533a1dbb 4aa0973bffb0
files Project.toml src/Grids/Grids.jl src/Grids/equidistant_grid.jl src/Grids/grid.jl src/Grids/mapped_grid.jl test/Grids/equidistant_grid_test.jl test/Grids/mapped_grid_test.jl
diffstat 9 files changed, 404 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Project.toml	Sun Jun 30 10:43:02 2024 +0200
+++ b/Project.toml	Sun Jun 30 10:50:44 2024 +0200
@@ -9,9 +9,11 @@
 TiledIteration = "06e1c1a7-607b-532d-9fad-de7d9aa2abac"
 
 [weakdeps]
+Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
 Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
 
 [extensions]
+SbplibPlotsExt = "Plots"
 SbplibMakieExt = "Makie"
 
 [compat]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ext/SbplibPlotsExt.jl	Sun Jun 30 10:50:44 2024 +0200
@@ -0,0 +1,72 @@
+module SbplibPlotsExt
+
+using Sbplib.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	Sun Jun 30 10:43:02 2024 +0200
+++ b/src/Grids/Grids.jl	Sun Jun 30 10:50:44 2024 +0200
@@ -5,6 +5,30 @@
 using Sbplib.LazyTensors
 using StaticArrays
 
+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 ConcreteChart
+export parameterspace
+
 # Grid
 export Grid
 export coordinate_size
@@ -46,6 +70,7 @@
 
 abstract type BoundaryIdentifier end
 
+include("manifolds.jl")
 include("grid.jl")
 include("tensor_grid.jl")
 include("equidistant_grid.jl")
--- a/src/Grids/equidistant_grid.jl	Sun Jun 30 10:43:02 2024 +0200
+++ b/src/Grids/equidistant_grid.jl	Sun Jun 30 10:50:44 2024 +0200
@@ -132,6 +132,20 @@
 	return EquidistantGrid(range(limit_lower, limit_upper, length=size)) # TBD: Should it use LinRange instead?
 end
 
+
+equidistant_grid(hb::HyperBox, dims::Vararg{Int}) = equidistant_grid(hb.a, hb.b, dims...)
+# TODO: One dimensional grids shouldn't have vector eltype right?, Change here or in HyperBox?
+
+function equidistant_grid(c::Chart, dims::Vararg{Int})
+    lg = equidistant_grid(parameterspace(c), dims...)
+    return MappedGrid(
+        lg,
+        map(c,lg),
+        map(ξ->jacobian(c, ξ), lg),
+    )
+end
+
+
 CartesianBoundary{D,BID} = TensorGridBoundary{D,BID} # TBD: What should we do about the naming of this boundary?
 
 
--- a/src/Grids/grid.jl	Sun Jun 30 10:43:02 2024 +0200
+++ b/src/Grids/grid.jl	Sun Jun 30 10:50:44 2024 +0200
@@ -115,6 +115,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	Sun Jun 30 10:50:44 2024 +0200
@@ -0,0 +1,207 @@
+"""
+    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
+# TBD:  Should implement domain_dim?
+
+struct HyperBox{T,D} <: ParameterSpace{D}
+    a::SVector{D,T}
+    b::SVector{D,T}
+end
+
+function HyperBox(a,b)
+    T = SVector{length(a)}
+    HyperBox(convert(T,a), convert(T,b))
+end
+
+Interval{T} = HyperBox{T,1}
+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)
+
+unitinterval(T=Float64) = unithyperbox(T,1)
+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}}
+end
+
+Simplex(verticies::Vararg{AbstractArray}) = Simplex(Tuple(SVector(v...) for v ∈ verticies))
+
+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 parametrized description of a manifold or part of a manifold.
+"""
+struct Chart{D, PST<:ParameterSpace{D}, MT}
+    mapping::MT
+    parameterspace::PST
+end
+
+domain_dim(::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 have boundary names?
+
+"""
+    Atlas
+
+A collection of charts and their connections.
+Should implement methods for `charts` and
+"""
+abstract type Atlas end
+
+"""
+    charts(::Atlas)
+
+The colloction of charts in the atlas.
+"""
+function charts end
+
+"""
+    connections
+
+TBD: What exactly should this return?
+
+"""
+
+struct CartesianAtlas <: Atlas
+    charts::Matrix{Chart}
+end
+
+charts(a::CartesianAtlas) = a.charts
+
+struct UnstructuredAtlas <: Atlas
+    charts::Vector{Chart}
+    connections
+end
+
+charts(a::UnstructuredAtlas) = a.charts
+
+
+###
+# Geometry
+###
+
+abstract type Curve end
+abstract type Surface end
+
+
+struct Line{PT} <: Curve
+    p::PT
+    tangent::PT
+end
+
+(c::Line)(s) = c.p + s*c.tangent
+
+
+struct LineSegment{PT} <: Curve
+    a::PT
+    b::PT
+end
+
+(c::LineSegment)(s) = (1-s)*c.a + s*c.b
+
+
+function linesegments(ps...)
+    return [LineSegment(ps[i], ps[i+1]) for i ∈ 1:length(ps)-1]
+end
+
+
+function polygon_edges(ps...)
+    n = length(ps)
+    return [LineSegment(ps[i], ps[mod1(i+1,n)]) for i ∈ eachindex(Ps)]
+end
+
+struct Circle{T,PT} <: Curve
+    c::PT
+    r::T
+end
+
+(c::Circle)(θ) = c.c + r*@SVector[cos(Θ), sin(Θ)]
+
+struct TransfiniteInterpolationSurface{T1,T2,T3,T4} <: Surface
+    c₁::T1
+    c₂::T2
+    c₃::T3
+    c₄::T4
+end
+
+function (s::TransfiniteInterpolationSurface)(u,v)
+    c₁, c₂, c₃, c₄ = s.c₁, s.c₂, s.c₃, s.c₄
+    P₀₀ = c₁(0)
+    P₁₀ = c₂(0)
+    P₁₁ = c₃(0)
+    P₀₁ = c₄(0)
+    return (1-v)*c₁(u) + u*c₂(v) + v*c₃(1-u) + (1-u)*c₄(1-v) - (
+        (1-u)*(1-v)*P₀₀ + u*(1-v)*P₁₀ + u*v*P₁₁ + (1-u)*v*P₀₁
+    )
+end
+
+function (s::TransfiniteInterpolationSurface)(ξ̄::AbstractArray)
+    s(ξ̄...)
+end
+
+# TODO: Implement jacobian() for the different mapping helpers
+
--- a/src/Grids/mapped_grid.jl	Sun Jun 30 10:43:02 2024 +0200
+++ b/src/Grids/mapped_grid.jl	Sun Jun 30 10:50:44 2024 +0200
@@ -62,6 +62,7 @@
     )
 end
 
+
 function jacobian_determinant(g::MappedGrid)
     return map(jacobian(g)) do ∂x∂ξ
         det(∂x∂ξ)
--- a/test/Grids/equidistant_grid_test.jl	Sun Jun 30 10:43:02 2024 +0200
+++ b/test/Grids/equidistant_grid_test.jl	Sun Jun 30 10:50:44 2024 +0200
@@ -2,6 +2,7 @@
 using Test
 using Sbplib.RegionIndices
 using Sbplib.LazyTensors
+using StaticArrays
 
 
 @testset "EquidistantGrid" begin
@@ -150,6 +151,23 @@
             @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)
+    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	Sun Jun 30 10:50:44 2024 +0200
@@ -0,0 +1,63 @@
+using Test
+
+using Sbplib.Grids
+using Sbplib.RegionIndices
+using Sbplib.LazyTensors
+
+# using StaticArrays
+
+@testset "ParameterSpace" begin
+    @test ndims(HyperBox([1,1], [2,2])) == 2
+    @test ndims(unittetrahedron()) == 3
+end
+
+@testset "HyperBox" begin
+    @test HyperBox{<:Any, 2} <: ParameterSpace{2}
+    @test HyperBox([1,1], [2,2]) isa HyperBox{Int, 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 unitinterval() isa HyperBox{Float64,1}
+    @test limits(unitinterval()) == ([0], [1])
+
+    @test unitinterval(Int) isa HyperBox{Int,1}
+    @test limits(unitinterval(Int)) == ([0], [1])
+
+    @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 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}
+    @test c([3,2]) == [6,4]
+    @test parameterspace(c) == unitsquare()
+end
+
+@testset "Atlas" begin
+
+end