changeset 1283:54c3ed752730 refactor/grids

Make tests for normal_derivative work
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 01 Mar 2023 08:28:14 +0100
parents 11b08b242e48
children 7d52c4835d15
files src/SbpOperators/boundaryops/normal_derivative.jl test/SbpOperators/boundaryops/normal_derivative_test.jl
diffstat 2 files changed, 24 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/SbpOperators/boundaryops/normal_derivative.jl	Mon Feb 27 15:39:13 2023 +0100
+++ b/src/SbpOperators/boundaryops/normal_derivative.jl	Wed Mar 01 08:28:14 2023 +0100
@@ -1,5 +1,5 @@
 """
-    normal_derivative(grid, closure_stencil::Stencil, boundary)
+    normal_derivative(g, closure_stencil::Stencil, boundary)
 
 Creates the normal derivative boundary operator `d` as a `LazyTensor`
 
@@ -10,17 +10,16 @@
 
 See also: [`BoundaryOperator`](@ref), [`LazyTensors.inflate`](@ref).
 """
-function normal_derivative(grid, closure_stencil, boundary)
-    direction = dim(boundary)
-    h_inv = inverse_spacing(grid)[direction]
-
-    op = BoundaryOperator(restrict(grid, dim(boundary)), scale(closure_stencil,h_inv), region(boundary))
-    return LazyTensors.inflate(op, size(grid), dim(boundary))
+#TODO: Check docstring
+function normal_derivative(g::TensorGrid, stencil_set::StencilSet, boundary::TensorGridBoundary)
+    op = normal_derivative(g.grids[grid_id(boundary)], stencil_set, boundary_id(boundary))
+    return LazyTensors.inflate(op, size(g), grid_id(boundary))
 end
 
-"""
-    normal_derivative(grid, stencil_set, boundary)
+function normal_derivative(g::EquidistantGrid, stencil_set::StencilSet, boundary)
+    closure_stencil = parse_stencil(stencil_set["d1"]["closure"])
+    h_inv = inverse_spacing(g)
 
-Creates a `normal_derivative` operator on `grid` given a `stencil_set`.
-"""
-normal_derivative(grid, stencil_set::StencilSet, boundary) = normal_derivative(grid, parse_stencil(stencil_set["d1"]["closure"]), boundary)
+    scaled_stencil = scale(closure_stencil,h_inv)
+    return BoundaryOperator(g, scaled_stencil, boundary)
+end
--- a/test/SbpOperators/boundaryops/normal_derivative_test.jl	Mon Feb 27 15:39:13 2023 +0100
+++ b/test/SbpOperators/boundaryops/normal_derivative_test.jl	Wed Mar 01 08:28:14 2023 +0100
@@ -7,24 +7,23 @@
 import Sbplib.SbpOperators.BoundaryOperator
 
 @testset "normal_derivative" begin
-    g_1D = EquidistantGrid(11, 0.0, 1.0)
-    g_2D = EquidistantGrid((11,12), (0.0, 0.0), (1.0,1.0))
+    g_1D = equidistant_grid(11, 0.0, 1.0)
+    g_2D = equidistant_grid((11,12), (0.0, 0.0), (1.0,1.0))
     @testset "normal_derivative" begin
     	stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order=4)
-    	d_closure = parse_stencil(stencil_set["d1"]["closure"])
         @testset "1D" begin
-            d_l = normal_derivative(g_1D, d_closure, CartesianBoundary{1,Lower}())
-            @test d_l == normal_derivative(g_1D, stencil_set, CartesianBoundary{1,Lower}())
+            d_l = normal_derivative(g_1D, stencil_set, Lower())
+            @test d_l == normal_derivative(g_1D, stencil_set, Lower())
             @test d_l isa BoundaryOperator{T,Lower} where T
             @test d_l isa LazyTensor{T,0,1} where T
         end
         @testset "2D" begin
-            d_w = normal_derivative(g_2D, d_closure, CartesianBoundary{1,Lower}())
-            d_n = normal_derivative(g_2D, d_closure, CartesianBoundary{2,Upper}())
+            d_w = normal_derivative(g_2D, stencil_set, CartesianBoundary{1,Lower}())
+            d_n = normal_derivative(g_2D, stencil_set, CartesianBoundary{2,Upper}())
             Ix = IdentityTensor{Float64}((size(g_2D)[1],))
             Iy = IdentityTensor{Float64}((size(g_2D)[2],))
-            d_l = normal_derivative(restrict(g_2D,1),d_closure,CartesianBoundary{1,Lower}())
-            d_r = normal_derivative(restrict(g_2D,2),d_closure,CartesianBoundary{1,Upper}())
+            d_l = normal_derivative(g_2D.grids[1], stencil_set, Lower())
+            d_r = normal_derivative(g_2D.grids[2], stencil_set, Upper())
             @test d_w == normal_derivative(g_2D, stencil_set, CartesianBoundary{1,Lower}())
             @test d_w ==  d_l⊗Iy
             @test d_n ==  Ix⊗d_r
@@ -33,14 +32,13 @@
         end
     end
     @testset "Accuracy" begin
-        v = evalOn(g_2D, (x,y)-> x^2 + (y-1)^2 + x*y)
-        v∂x = evalOn(g_2D, (x,y)-> 2*x + y)
-        v∂y = evalOn(g_2D, (x,y)-> 2*(y-1) + x)
+        v = eval_on(g_2D, (x,y)-> x^2 + (y-1)^2 + x*y)
+        v∂x = eval_on(g_2D, (x,y)-> 2*x + y)
+        v∂y = eval_on(g_2D, (x,y)-> 2*(y-1) + x)
         # TODO: Test for higher order polynomials?
         @testset "2nd order" begin
         	stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order=2)
-        	d_closure = parse_stencil(stencil_set["d1"]["closure"])
-            d_w, d_e, d_s, d_n = normal_derivative.(Ref(g_2D), Ref(d_closure), boundary_identifiers(g_2D))
+            d_w, d_e, d_s, d_n = normal_derivative.(Ref(g_2D), Ref(stencil_set), boundary_identifiers(g_2D))
 
             @test d_w*v ≈ -v∂x[1,:] atol = 1e-13
             @test d_e*v ≈ v∂x[end,:] atol = 1e-13
@@ -50,8 +48,7 @@
 
         @testset "4th order" begin
             stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order=4)
-        	d_closure = parse_stencil(stencil_set["d1"]["closure"])
-            d_w, d_e, d_s, d_n = normal_derivative.(Ref(g_2D), Ref(d_closure), boundary_identifiers(g_2D))
+            d_w, d_e, d_s, d_n = normal_derivative.(Ref(g_2D), Ref(stencil_set), boundary_identifiers(g_2D))
             
             @test d_w*v ≈ -v∂x[1,:] atol = 1e-13
             @test d_e*v ≈ v∂x[end,:] atol = 1e-13