Mercurial > repos > public > sbplib_julia
changeset 1688:72776d3d5fd6 feature/grids/curvilinear
Add min_spacing for 2D mapped grids
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 22 Aug 2024 08:14:04 +0200 |
parents | 3ac94e8f28b3 |
children | e11b5b6940a2 |
files | src/Grids/mapped_grid.jl test/Grids/mapped_grid_test.jl |
diffstat | 2 files changed, 47 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/Grids/mapped_grid.jl Wed Aug 21 19:23:42 2024 +0200 +++ b/src/Grids/mapped_grid.jl Thu Aug 22 08:14:04 2024 +0200 @@ -92,6 +92,28 @@ 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)
--- a/test/Grids/mapped_grid_test.jl Wed Aug 21 19:23:42 2024 +0200 +++ b/test/Grids/mapped_grid_test.jl Thu Aug 22 08:14:04 2024 +0200 @@ -2,6 +2,7 @@ using Sbplib.RegionIndices using Test using StaticArrays +using LinearAlgebra @testset "MappedGrid" begin lg = equidistant_grid((0,0), (1,1), 11, 11) # TODO: Change dims of the grid to be different @@ -182,6 +183,30 @@ 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 + + skew_grid(a,b, sz...) = mapped_grid(ξ̄->ξ̄[1]*a + ξ̄[2]*b, ξ̄->[a b], sz...) + + @testset let a = @SVector[1,0], b = @SVector[1,1]/√2 + g = skew_grid(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 = skew_grid(a,b,11,11) + + @test min_spacing(g) ≈ 0.1*norm(a+b) + end + + # Skevt nät end end