changeset 1810:2abde39a8285

Merge feature/grids/curvilinear
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 03 Oct 2024 18:30:08 +0200
parents 79053d93dd6b (current diff) 8a98deb867fd (diff)
children 89d7f7b02824
files
diffstat 8 files changed, 607 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/Manifest.toml	Thu Oct 03 15:39:27 2024 +0200
+++ b/Manifest.toml	Thu Oct 03 18:30:08 2024 +0200
@@ -2,7 +2,7 @@
 
 julia_version = "1.10.5"
 manifest_format = "2.0"
-project_hash = "28d35bafed672f63e2364cf8258fc24801cb971b"
+project_hash = "07350208c6e9bd0ec3979df9ac99bb401ac56208"
 
 [[deps.Artifacts]]
 uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
--- a/Project.toml	Thu Oct 03 15:39:27 2024 +0200
+++ b/Project.toml	Thu Oct 03 18:30:08 2024 +0200
@@ -4,6 +4,7 @@
 version = "0.1.2"
 
 [deps]
+LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
 StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
 TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
 
--- a/benchmark/Manifest.toml	Thu Oct 03 15:39:27 2024 +0200
+++ b/benchmark/Manifest.toml	Thu Oct 03 18:30:08 2024 +0200
@@ -45,7 +45,7 @@
 uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
 
 [[deps.Diffinitive]]
-deps = ["StaticArrays", "TOML"]
+deps = ["LinearAlgebra", "StaticArrays", "TOML"]
 path = ".."
 uuid = "5a373a26-915f-4769-bcab-bf03835de17b"
 version = "0.1.2"
--- a/docs/Manifest.toml	Thu Oct 03 15:39:27 2024 +0200
+++ b/docs/Manifest.toml	Thu Oct 03 18:30:08 2024 +0200
@@ -40,7 +40,7 @@
 uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
 
 [[deps.Diffinitive]]
-deps = ["StaticArrays", "TOML"]
+deps = ["LinearAlgebra", "StaticArrays", "TOML"]
 path = ".."
 uuid = "5a373a26-915f-4769-bcab-bf03835de17b"
 version = "0.1.2"
--- a/ext/DiffinitiveMakieExt.jl	Thu Oct 03 15:39:27 2024 +0200
+++ b/ext/DiffinitiveMakieExt.jl	Thu Oct 03 18:30:08 2024 +0200
@@ -33,12 +33,12 @@
 
 ## Grids
 
-Makie.convert_arguments(::Type{<:Scatter}, g::Grid) = (reshape(map(Point,g),:),) # (map(Point,collect(g)[:]),)
-function Makie.convert_arguments(::Type{<:Lines}, g::Grid{<:Any,2})
+Makie.convert_arguments(::Type{<:Scatter}, g::Grid) = (reshape(map(Point,g),:),)
+function Makie.convert_arguments(::Type{<:Lines}, g::Grid{<:AbstractVector})
     M = collect(g)
 
     function cat_with_NaN(a,b)
-        vcat(a,[@SVector[NaN,NaN]],b)
+        vcat(a,[@SVector fill(NaN, coordinate_size(g))],b)
     end
 
     xlines = reduce(cat_with_NaN, eachrow(M))
@@ -47,7 +47,7 @@
     return (cat_with_NaN(xlines,ylines),)
 end
 
-Makie.plot!(plot::Plot(Grid{<:Any,2})) = lines!(plot, plot.attributes, plot[1])
+Makie.plot!(plot::Plot(Grid)) = lines!(plot, plot.attributes, plot[1])
 
 
 ## Grid functions
--- a/src/Grids/Grids.jl	Thu Oct 03 15:39:27 2024 +0200
+++ b/src/Grids/Grids.jl	Thu Oct 03 18:30:08 2024 +0200
@@ -2,6 +2,7 @@
 
 using Diffinitive.LazyTensors
 using StaticArrays
+using LinearAlgebra
 
 # Grid
 export Grid
@@ -18,6 +19,7 @@
 export eval_on
 export componentview
 export ArrayComponentView
+export normal
 
 export BoundaryIdentifier
 export TensorGridBoundary
@@ -33,9 +35,18 @@
 export spacing
 export equidistant_grid
 
+
+# MappedGrid
+export MappedGrid
+export jacobian
+export logical_grid
+export mapped_grid
+export metric_tensor
+
 include("grid.jl")
 include("tensor_grid.jl")
 include("equidistant_grid.jl")
 include("zero_dim_grid.jl")
+include("mapped_grid.jl")
 
 end # module
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Grids/mapped_grid.jl	Thu Oct 03 18:30:08 2024 +0200
@@ -0,0 +1,207 @@
+"""
+    MappedGrid{T,D} <: Grid{T,D}
+
+A grid defined by a coordinate mapping from a logical grid to some physical
+coordinates. The physical coordinates and the Jacobian are stored as grid
+functions corresponding to the logical grid.
+
+See also: [`logical_grid`](@ref), [`jacobian`](@ref), [`metric_tensor`](@ref).
+"""
+struct MappedGrid{T,D, GT<:Grid{<:Any,D}, CT<:AbstractArray{T,D}, JT<:AbstractArray{<:AbstractMatrix{<:Any}, D}} <: Grid{T,D}
+    logical_grid::GT
+    physicalcoordinates::CT
+    jacobian::JT
+
+    """
+        MappedGrid(logical_grid, physicalcoordinates, jacobian)
+
+    A MappedGrid with the given physical coordinates and jacobian.
+    """
+    function MappedGrid(logical_grid::GT, physicalcoordinates::CT, jacobian::JT) where {T,D, GT<:Grid{<:Any,D}, CT<:AbstractArray{T,D}, JT<:AbstractArray{<:AbstractMatrix{<:Any}, D}}
+        if !(size(logical_grid) == size(physicalcoordinates) == size(jacobian))
+            throw(ArgumentError("Sizes must match"))
+        end
+
+        if size(first(jacobian)) != (length(first(physicalcoordinates)),D)
+            throw(ArgumentError("The size of the jacobian must match the dimensions of the grid and coordinates"))
+        end
+
+        return new{T,D,GT,CT,JT}(logical_grid, physicalcoordinates, jacobian)
+    end
+end
+
+function Base.:(==)(a::MappedGrid, b::MappedGrid)
+    same_logical_grid = logical_grid(a) == logical_grid(b)
+    same_coordinates = collect(a) == collect(b)
+    same_jacobian = jacobian(a) == jacobian(b)
+
+    return same_logical_grid && same_coordinates && same_jacobian
+end
+
+"""
+    logical_grid(g::MappedGrid)
+
+The logical grid of `g`.
+"""
+logical_grid(g::MappedGrid) = g.logical_grid
+
+"""
+    jacobian(g::MappedGrid)
+
+The Jacobian matrix of `g` as a grid function.
+"""
+jacobian(g::MappedGrid) = g.jacobian
+
+
+# Indexing interface
+Base.getindex(g::MappedGrid, I::Vararg{Int}) = g.physicalcoordinates[I...]
+Base.eachindex(g::MappedGrid) = eachindex(g.logical_grid)
+
+Base.firstindex(g::MappedGrid, d) = firstindex(g.logical_grid, d)
+Base.lastindex(g::MappedGrid, d) = lastindex(g.logical_grid, d)
+
+# Iteration interface
+Base.iterate(g::MappedGrid) = iterate(g.physicalcoordinates)
+Base.iterate(g::MappedGrid, state) = iterate(g.physicalcoordinates, state)
+
+Base.IteratorSize(::Type{<:MappedGrid{<:Any, D}}) where D = Base.HasShape{D}()
+Base.length(g::MappedGrid) = length(g.logical_grid)
+Base.size(g::MappedGrid) = size(g.logical_grid)
+Base.size(g::MappedGrid, d) = size(g.logical_grid, d)
+
+boundary_identifiers(g::MappedGrid) = boundary_identifiers(g.logical_grid)
+boundary_indices(g::MappedGrid, id::TensorGridBoundary) = boundary_indices(g.logical_grid, id)
+
+function boundary_grid(g::MappedGrid, id::TensorGridBoundary)
+    b_indices = boundary_indices(g.logical_grid, id)
+
+    # Calculate indices of needed jacobian components
+    D = ndims(g)
+    all_indices = SVector{D}(1:D)
+    free_variable_indices = deleteat(all_indices, grid_id(id))
+    jacobian_components = (:, free_variable_indices)
+
+    # Create grid function for boundary grid jacobian
+    boundary_jacobian = componentview((@view g.jacobian[b_indices...])  , jacobian_components...)
+    boundary_physicalcoordinates = @view g.physicalcoordinates[b_indices...]
+
+    return MappedGrid(
+        boundary_grid(g.logical_grid, id),
+        boundary_physicalcoordinates,
+        boundary_jacobian,
+    )
+end
+
+
+"""
+    mapped_grid(x, J, size::Vararg{Int})
+
+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
+and `size` determines the size of the logical grid.
+"""
+function mapped_grid(x, J, size::Vararg{Int})
+    D = length(size)
+    lg = equidistant_grid(ntuple(i->0., D), ntuple(i->1., D), size...)
+    return mapped_grid(lg, x, J)
+end
+
+"""
+    mapped_grid(lg::Grid, x, J)
+
+A `MappedGrid` with logical grid `lg`. Physical coordinates and Jacobian are
+determined by the functions `x` and `J`.
+"""
+function mapped_grid(lg::Grid, x, J)
+    return MappedGrid(
+        lg,
+        map(x,lg),
+        map(J,lg),
+    )
+end
+
+"""
+    metric_tensor(g::MappedGrid)
+
+The metric tensor of `g` as a grid function.
+"""
+function metric_tensor(g::MappedGrid)
+    return map(jacobian(g)) do ∂x∂ξ
+        ∂x∂ξ'*∂x∂ξ
+    end
+end
+
+function min_spacing(g::MappedGrid{T,1} where T)
+    n, = size(g)
+
+    ms = Inf
+    for i ∈ 1:n-1
+        ms = min(ms, norm(g[i+1]-g[i]))
+    end
+
+    return ms
+end
+
+function min_spacing(g::MappedGrid{T,2} where T)
+    n, m = size(g)
+
+    ms = Inf
+    for i ∈ 1:n-1, j ∈ 1:m-1 # loop over each cell of the grid
+
+        ms = min(
+            ms,
+            norm(g[i+1,j]-g[i,j]),
+            norm(g[i,j+1]-g[i,j]),
+
+            norm(g[i+1,j]-g[i+1,j+1]),
+            norm(g[i,j+1]-g[i+1,j+1]),
+
+            norm(g[i+1,j+1]-g[i,j]),
+            norm(g[i+1,j]-g[i,j+1]),
+        )
+        # NOTE: This could be optimized to avoid checking all interior edges twice.
+    end
+
+    return ms
+end
+
+"""
+    normal(g::MappedGrid, boundary)
+
+The outward pointing normal as a grid function on the corresponding boundary grid.
+"""
+function normal(g::MappedGrid, boundary)
+    b_indices = boundary_indices(g, boundary)
+    σ = _boundary_sign(component_type(g), boundary)
+
+    # TODO: Refactor this when `boundary_indices(g, ...)` has been made iterable.
+    return map(jacobian(g)[b_indices...]) do ∂x∂ξ
+        ∂ξ∂x = inv(∂x∂ξ)
+        k = grid_id(boundary)
+        σ*∂ξ∂x[k,:]/norm(∂ξ∂x[k,:])
+    end
+end
+
+"""
+    normal(g::MappedGrid, boundary, i...)
+
+The outward pointing normal to the specified boundary in grid point `i`.
+"""
+function normal(g::MappedGrid, boundary, i...)
+    σ = _boundary_sign(component_type(g), boundary)
+    ∂ξ∂x = inv(jacobian(g)[i...])
+
+    k = grid_id(boundary)
+    return σ*∂ξ∂x[k,:]/norm(∂ξ∂x[k,:])
+end
+
+
+function _boundary_sign(T, boundary)
+    if boundary_id(boundary) == UpperBoundary()
+        return one(T)
+    elseif boundary_id(boundary) == LowerBoundary()
+        return -one(T)
+    else
+        throw(ArgumentError("The boundary identifier must be either `LowerBoundary()` or `UpperBoundary()`"))
+    end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/Grids/mapped_grid_test.jl	Thu Oct 03 18:30:08 2024 +0200
@@ -0,0 +1,381 @@
+using Diffinitive.Grids
+using Diffinitive.RegionIndices
+using Test
+using StaticArrays
+using LinearAlgebra
+
+
+_skew_mapping(a,b) = (ξ̄->ξ̄[1]*a + ξ̄[2]*b, ξ̄->[a  b])
+
+function _partially_curved_mapping()
+    x̄((ξ, η)) = @SVector[ξ, η*(1+ξ*(ξ-1))]
+    J((ξ, η)) = @SMatrix[
+        1         0;
+        η*(2ξ-1)  1+ξ*(ξ-1);
+    ]
+
+    return x̄, J
+end
+
+function _fully_curved_mapping()
+    x̄((ξ, η)) = @SVector[2ξ + η*(1-η), 3η+(1+η/2)*ξ^2]
+    J((ξ, η)) = @SMatrix[
+        2       1-2η;
+        (2+η)*ξ 3+1/2*ξ^2;
+    ]
+
+    return x̄, J
+end
+
+@testset "MappedGrid" begin
+    @testset "Constructor" begin
+        lg = equidistant_grid((0,0), (1,1), 11, 21)
+
+        x̄ = map(ξ̄ -> 2ξ̄, lg)
+        J = map(ξ̄ -> @SArray(fill(2., 2, 2)), lg)
+        mg = MappedGrid(lg, x̄, J)
+
+        @test mg isa Grid{SVector{2, Float64},2}
+        @test jacobian(mg) isa Array{<:AbstractMatrix}
+        @test logical_grid(mg) isa Grid
+
+        @test collect(mg) == x̄
+        @test jacobian(mg) == J
+        @test logical_grid(mg) == lg
+
+
+        x̄ = map(ξ̄ -> @SVector[ξ̄[1],ξ̄[2], ξ̄[1] + ξ̄[2]], lg)
+        J = map(ξ̄ -> @SMatrix[1 0; 0 1; 1 1], lg)
+        mg = MappedGrid(lg, x̄, J)
+
+        @test mg isa Grid{SVector{3, Float64},2}
+        @test jacobian(mg) isa Array{<:AbstractMatrix}
+        @test logical_grid(mg) isa Grid
+
+        @test collect(mg) == x̄
+        @test jacobian(mg) == J
+        @test logical_grid(mg) == lg
+
+        sz1 = (10,11)
+        sz2 = (10,12)
+        @test_throws ArgumentError("Sizes must match") MappedGrid(
+            equidistant_grid((0,0), (1,1), sz2...),
+            rand(SVector{2},sz1...),
+            rand(SMatrix{2,2},sz1...),
+        )
+
+        @test_throws ArgumentError("Sizes must match") MappedGrid(
+            equidistant_grid((0,0), (1,1), sz1...),
+            rand(SVector{2},sz2...),
+            rand(SMatrix{2,2},sz1...),
+        )
+
+        @test_throws ArgumentError("Sizes must match") MappedGrid(
+            equidistant_grid((0,0), (1,1), sz1...),
+            rand(SVector{2},sz1...),
+            rand(SMatrix{2,2},sz2...),
+        )
+
+        err_str = "The size of the jacobian must match the dimensions of the grid and coordinates"
+        @test_throws ArgumentError(err_str) MappedGrid(
+            equidistant_grid((0,0), (1,1), 10, 11),
+            rand(SVector{3}, 10, 11),
+            rand(SMatrix{3,4}, 10, 11),
+        )
+
+        @test_throws ArgumentError(err_str) MappedGrid(
+            equidistant_grid((0,0), (1,1), 10, 11),
+            rand(SVector{3}, 10, 11),
+            rand(SMatrix{4,2}, 10, 11),
+        )
+    end
+
+    @testset "Indexing Interface" begin
+        lg = equidistant_grid((0,0), (1,1), 11, 21)
+        x̄ = map(ξ̄ -> 2ξ̄, lg)
+        J = map(ξ̄ -> @SArray(fill(2., 2, 2)), lg)
+        mg = MappedGrid(lg, x̄, J)
+        @test mg[1,1] == [0.0, 0.0]
+        @test mg[4,2] == [0.6, 0.1]
+        @test mg[6,10] == [1., 0.9]
+
+        @test mg[begin, begin] == [0.0, 0.0]
+        @test mg[end,end] == [2.0, 2.0]
+        @test mg[begin,end] == [0., 2.]
+
+        @test axes(mg) == (1:11, 1:21)
+
+        @testset "cartesian indexing" begin
+            cases = [
+                 (1,1) ,
+                 (3,5) ,
+                 (10,6),
+                 (1,1) ,
+                 (3,2) ,
+            ]
+
+            @testset "i = $is" for (lg, is) ∈ cases
+                @test mg[CartesianIndex(is...)] == mg[is...]
+            end
+        end
+
+        @testset "eachindex" begin
+            @test eachindex(mg) == CartesianIndices((11,21))
+        end
+
+        @testset "firstindex" begin
+            @test firstindex(mg, 1) == 1
+            @test firstindex(mg, 2) == 1
+        end
+
+        @testset "lastindex" begin
+            @test lastindex(mg, 1) == 11
+            @test lastindex(mg, 2) == 21
+        end
+    end
+
+    @testset "Iterator interface" begin
+        lg = equidistant_grid((0,0), (1,1), 11, 21)
+        x̄ = map(ξ̄ -> 2ξ̄, lg)
+        J = map(ξ̄ -> @SArray(fill(2., 2, 2)), lg)
+
+        mg = MappedGrid(lg, x̄, J)
+
+        lg2 = equidistant_grid((0,0), (1,1), 15, 11)
+        sg = MappedGrid(
+            equidistant_grid((0,0), (1,1), 15, 11),
+            map(ξ̄ -> @SArray[ξ̄[1], ξ̄[2], -ξ̄[1]], lg2), rand(SMatrix{3,2,Float64},15,11)
+        )
+
+        @test eltype(mg) == SVector{2,Float64}
+        @test eltype(sg) == SVector{3,Float64}
+
+        @test eltype(typeof(mg)) == SVector{2,Float64}
+        @test eltype(typeof(sg)) == SVector{3,Float64}
+
+        @test size(mg) == (11,21)
+        @test size(sg) == (15,11)
+
+        @test size(mg,2) == 21
+        @test size(sg,2) == 11
+
+        @test length(mg) == 231
+        @test length(sg) == 165
+
+        @test Base.IteratorSize(mg) == Base.HasShape{2}()
+        @test Base.IteratorSize(typeof(mg)) == Base.HasShape{2}()
+
+        @test Base.IteratorSize(sg) == Base.HasShape{2}()
+        @test Base.IteratorSize(typeof(sg)) == Base.HasShape{2}()
+
+        element, state = iterate(mg)
+        @test element == lg[1,1].*2
+        element, _ =  iterate(mg, state)
+        @test element == lg[2,1].*2
+
+        element, state = iterate(sg)
+        @test element == sg.physicalcoordinates[1,1]
+        element, _ = iterate(sg, state)
+        @test element == sg.physicalcoordinates[2,1]
+
+        @test collect(mg) == 2 .* lg
+    end
+
+    @testset "Base" begin
+        lg = equidistant_grid((0,0), (1,1), 11, 21)
+        x̄ = map(ξ̄ -> 2ξ̄, lg)
+        J = map(ξ̄ -> @SArray(fill(2., 2, 2)), lg)
+        mg = MappedGrid(lg, x̄, J)
+
+        @test ndims(mg) == 2
+    end
+
+    @testset "==" begin
+        sz = (15,11)
+        lg = equidistant_grid((0,0), (1,1), sz...)
+        x = rand(SVector{3,Float64}, sz...)
+        J = rand(SMatrix{3,2,Float64}, sz...)
+
+        sg = MappedGrid(lg, x, J)
+
+        sg1 = MappedGrid(equidistant_grid((0,0), (1,1), sz...), copy(x), copy(J))
+
+        sz2 = (15,12)
+        lg2 = equidistant_grid((0,0), (1,1), sz2...)
+        x2 = rand(SVector{3,Float64}, sz2...)
+        J2 = rand(SMatrix{3,2,Float64}, sz2...)
+        sg2 = MappedGrid(lg2, x2, J2)
+
+        sg3 = MappedGrid(lg, rand(SVector{3,Float64}, sz...), J)
+        sg4 = MappedGrid(lg, x, rand(SMatrix{3,2,Float64}, sz...))
+
+        @test sg == sg1
+        @test sg != sg2 # Different size
+        @test sg != sg3 # Different coordinates
+        @test sg != sg4 # Different jacobian
+    end
+
+    @testset "boundary_identifiers" begin
+        lg = equidistant_grid((0,0), (1,1), 11, 15)
+        x̄ = map(ξ̄ -> 2ξ̄, lg)
+        J = map(ξ̄ -> @SArray(fill(2., 2, 2)), lg)
+        mg = MappedGrid(lg, x̄, J)
+        @test boundary_identifiers(mg) == boundary_identifiers(lg)
+    end
+
+    @testset "boundary_indices" begin
+        lg = equidistant_grid((0,0), (1,1), 11, 15)
+        x̄ = map(ξ̄ -> 2ξ̄, lg)
+        J = map(ξ̄ -> @SArray(fill(2., 2, 2)), lg)
+        mg = MappedGrid(lg, x̄, J)
+
+        @test boundary_indices(mg, CartesianBoundary{1,LowerBoundary}()) == boundary_indices(lg,CartesianBoundary{1,LowerBoundary}())
+        @test boundary_indices(mg, CartesianBoundary{2,LowerBoundary}()) == boundary_indices(lg,CartesianBoundary{2,LowerBoundary}())
+        @test boundary_indices(mg, CartesianBoundary{1,UpperBoundary}()) == boundary_indices(lg,CartesianBoundary{1,UpperBoundary}())
+    end
+
+    @testset "boundary_grid" begin
+        x̄, J = _partially_curved_mapping()
+        mg = mapped_grid(x̄, J, 10, 11)
+        J1((ξ, η)) = @SMatrix[
+            1       ;
+            η*(2ξ-1);
+        ]
+        J2((ξ, η)) = @SMatrix[
+            0;
+            1+ξ*(ξ-1);
+        ]
+
+        function expected_bg(mg, bId, Jb)
+            lg = logical_grid(mg)
+            return MappedGrid(
+                boundary_grid(lg, bId),
+                map(x̄, boundary_grid(lg, bId)),
+                map(Jb, boundary_grid(lg, bId)),
+            )
+        end
+
+        let bid = TensorGridBoundary{1, LowerBoundary}()
+            @test boundary_grid(mg, bid) == expected_bg(mg, bid, J2)
+        end
+
+        let bid = TensorGridBoundary{1, UpperBoundary}()
+            @test boundary_grid(mg, bid) == expected_bg(mg, bid, J2)
+        end
+
+        let bid = TensorGridBoundary{2, LowerBoundary}()
+            @test boundary_grid(mg, bid) == expected_bg(mg, bid, J1)
+        end
+
+        let bid = TensorGridBoundary{2, UpperBoundary}()
+            @test boundary_grid(mg, bid) == expected_bg(mg, bid, J1)
+        end
+    end
+end
+
+@testset "mapped_grid" begin
+    x̄, J = _partially_curved_mapping()
+    mg = mapped_grid(x̄, J, 10, 11)
+    @test mg isa MappedGrid{SVector{2,Float64}, 2}
+
+    lg = equidistant_grid((0,0), (1,1), 10, 11)
+    @test logical_grid(mg) == lg
+    @test collect(mg) == map(x̄, lg)
+
+    @test mapped_grid(lg, x̄, J) == mg
+end
+
+@testset "metric_tensor" begin
+    x̄((ξ, η)) = @SVector[ξ*η, ξ + η^2]
+    J((ξ, η)) = @SMatrix[
+        η    ξ;
+        1   2η;
+    ]
+
+    g = mapped_grid(x̄, J, 10, 11)
+    G = map(logical_grid(g)) do (ξ,η)
+        @SMatrix[
+            1+η^2   ξ*η+2η;
+            ξ*η+2η  ξ^2 + 4η^2;
+        ]
+    end
+    @test metric_tensor(g) ≈ G
+end
+
+@testset "min_spacing" begin
+    let g = mapped_grid(identity, x->@SMatrix[1], 11)
+        @test min_spacing(g) ≈ 0.1
+    end
+
+    let g = mapped_grid(x->x+x.^2/2, x->@SMatrix[1 .+ x], 11)
+        @test min_spacing(g) ≈ 0.105
+    end
+
+    let g = mapped_grid(x->x + x.*(1 .- x)/2, x->@SMatrix[1.5 .- x], 11)
+        @test min_spacing(g) ≈ 0.055
+    end
+
+    let g = mapped_grid(identity, x->@SMatrix[1 0; 0 1], 11,11)
+        @test min_spacing(g) ≈ 0.1
+    end
+
+    let g = mapped_grid(identity, x->@SMatrix[1 0; 0 1], 11,21)
+        @test min_spacing(g) ≈ 0.05
+    end
+
+
+    @testset let a = @SVector[1,0], b = @SVector[1,1]/√2
+        g = mapped_grid(_skew_mapping(a,b)...,11,11)
+
+        @test min_spacing(g) ≈ 0.1*norm(b-a)
+    end
+
+    @testset let a = @SVector[1,0], b = @SVector[-1,1]/√2
+        g = mapped_grid(_skew_mapping(a,b)...,11,11)
+
+        @test min_spacing(g) ≈ 0.1*norm(a+b)
+    end
+end
+
+@testset "normal" begin
+    g = mapped_grid(_partially_curved_mapping()...,10, 11)
+
+    @test normal(g, CartesianBoundary{1,LowerBoundary}()) == fill(@SVector[-1,0], 11)
+    @test normal(g, CartesianBoundary{1,UpperBoundary}()) == fill(@SVector[1,0], 11)
+    @test normal(g, CartesianBoundary{2,LowerBoundary}()) == fill(@SVector[0,-1], 10)
+    @test normal(g, CartesianBoundary{2,UpperBoundary}()) ≈ map(boundary_grid(g,CartesianBoundary{2,UpperBoundary}())|>logical_grid) do ξ̄
+        α = 1-2ξ̄[1]
+        @SVector[α,1]/√(α^2 + 1)
+    end
+
+    g = mapped_grid(_fully_curved_mapping()...,5,4)
+
+    unit(v) = v/norm(v)
+    @testset let bId = CartesianBoundary{1,LowerBoundary}()
+        lbg = boundary_grid(logical_grid(g), bId)
+        @test normal(g, bId) ≈ map(lbg) do (ξ, η)
+            -unit(@SVector[1/2,  η/3-1/6])
+        end
+    end
+
+    @testset let bId = CartesianBoundary{1,UpperBoundary}()
+        lbg = boundary_grid(logical_grid(g), bId)
+        @test normal(g, bId) ≈ map(lbg) do (ξ, η)
+            unit(@SVector[7/2, 2η-1]/(5 + 3η + 2η^2))
+        end
+    end
+
+    @testset let bId = CartesianBoundary{2,LowerBoundary}()
+        lbg = boundary_grid(logical_grid(g), bId)
+        @test normal(g, bId) ≈ map(lbg) do (ξ, η)
+            -unit(@SVector[-2ξ, 2]/(6 + ξ^2 - 2ξ))
+        end
+    end
+
+    @testset let bId = CartesianBoundary{2,UpperBoundary}()
+        lbg = boundary_grid(logical_grid(g), bId)
+        @test normal(g, bId) ≈ map(lbg) do (ξ, η)
+            unit(@SVector[-3ξ, 2]/(6 + ξ^2 + 3ξ))
+        end
+    end
+end