changeset 2080:0f949681d3d3 refactor/sbp_operators/direction_check tip

Check that direction of first/second derivative operators is within the dimension of the grid. Add 1D functions for first/second derivative operators that take a direction.
author Vidar Stiernström <vidar.stiernstrom@gmail.com>
date Fri, 20 Feb 2026 12:01:05 +0100
parents 118c09b168f5
children
files src/SbpOperators/volumeops/derivatives/first_derivative.jl src/SbpOperators/volumeops/derivatives/second_derivative.jl src/SbpOperators/volumeops/derivatives/second_derivative_variable.jl test/SbpOperators/volumeops/derivatives/first_derivative_test.jl test/SbpOperators/volumeops/derivatives/second_derivative_test.jl test/SbpOperators/volumeops/derivatives/second_derivative_variable_test.jl
diffstat 6 files changed, 41 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/SbpOperators/volumeops/derivatives/first_derivative.jl	Wed Feb 18 21:33:00 2026 +0100
+++ b/src/SbpOperators/volumeops/derivatives/first_derivative.jl	Fri Feb 20 12:01:05 2026 +0100
@@ -14,10 +14,17 @@
 See also: [`VolumeOperator`](@ref), [`LazyTensors.inflate`](@ref).
 """
 function first_derivative(g::TensorGrid, stencil_set, direction)
+    if direction ∉ Interval(0, ndims(g))
+        throw(DomainError(direction, "Direction must be inside [0, $(ndims(g))]."))
+    end
     D₁ = first_derivative(g.grids[direction], stencil_set)
     return LazyTensors.inflate(D₁, size(g), direction)
 end
 
+function first_derivative(g::EquidistantGrid, stencil_set, direction)
+    return first_derivative(TensorGrid(g), stencil_set, direction)
+end
+
 """
     first_derivative(g::EquidistantGrid, stencil_set::StencilSet)
 
--- a/src/SbpOperators/volumeops/derivatives/second_derivative.jl	Wed Feb 18 21:33:00 2026 +0100
+++ b/src/SbpOperators/volumeops/derivatives/second_derivative.jl	Fri Feb 20 12:01:05 2026 +0100
@@ -1,5 +1,6 @@
 """
-    second_derivative(g::EquidistantGrid, stencil_set, direction)
+second_derivative(g::TensorGrid, stencil_set, direction)    
+second_derivative(g::EquidistantGrid, stencil_set, direction)
 
 Creates the second derivative operator `D2` as a `LazyTensor`
 
@@ -9,10 +10,17 @@
 See also: [`VolumeOperator`](@ref), [`LazyTensors.inflate`](@ref).
 """
 function second_derivative(g::TensorGrid, stencil_set, direction)
+    if direction ∉ Interval(0, ndims(g))
+        throw(DomainError(direction, "Direction must be inside [0, $(ndims(g))]."))
+    end
     D₂ = second_derivative(g.grids[direction], stencil_set)
     return LazyTensors.inflate(D₂, size(g), direction)
 end
 
+function second_derivative(g::EquidistantGrid, stencil_set::StencilSet, direction)
+    return second_derivative(TensorGrid(g), stencil_set, direction)
+end
+
 """
     second_derivative(g::EquidistantGrid, stencil_set::::StencilSet)
 
--- a/src/SbpOperators/volumeops/derivatives/second_derivative_variable.jl	Wed Feb 18 21:33:00 2026 +0100
+++ b/src/SbpOperators/volumeops/derivatives/second_derivative_variable.jl	Fri Feb 20 12:01:05 2026 +0100
@@ -9,24 +9,31 @@
 """
 function second_derivative_variable end
 
-function second_derivative_variable(g::TensorGrid, coeff, stencil_set, dir::Int)
+function second_derivative_variable(g::TensorGrid, coeff, stencil_set, direction::Int)
+    if direction ∉ Interval(0, ndims(g))
+        throw(DomainError(direction, "Direction must be inside [0, $(ndims(g))]."))
+    end
     inner_stencil    = parse_nested_stencil(eltype(coeff), stencil_set["D2variable"]["inner_stencil"])
     closure_stencils = parse_nested_stencil.(eltype(coeff), stencil_set["D2variable"]["closure_stencils"])
 
-    return second_derivative_variable(g, coeff, inner_stencil, closure_stencils, dir)
+    return second_derivative_variable(g, coeff, inner_stencil, closure_stencils, direction)
+end
+
+function second_derivative_variable(g::EquidistantGrid, coeff, stencil_set, direction)
+    return second_derivative_variable(TensorGrid(g), coeff, stencil_set, direction)
 end
 
 function second_derivative_variable(g::EquidistantGrid, coeff, stencil_set)
-    return second_derivative_variable(TensorGrid(g), coeff, stencil_set, 1)
+    return second_derivative_variable(g::EquidistantGrid, coeff, stencil_set, 1)
 end
 
-function second_derivative_variable(g::TensorGrid, coeff, inner_stencil::NestedStencil, closure_stencils, dir)
+function second_derivative_variable(g::TensorGrid, coeff, inner_stencil::NestedStencil, closure_stencils, direction)
     check_coefficient(g, coeff)
 
-    Δxᵢ = spacing(g.grids[dir])
+    Δxᵢ = spacing(g.grids[direction])
     scaled_inner_stencil = scale(inner_stencil, 1/Δxᵢ^2)
     scaled_closure_stencils = scale.(Tuple(closure_stencils), 1/Δxᵢ^2)
-    return SecondDerivativeVariable(coeff, scaled_inner_stencil, scaled_closure_stencils, dir)
+    return SecondDerivativeVariable(coeff, scaled_inner_stencil, scaled_closure_stencils, direction)
 end
 
 function check_coefficient(g, coeff)
--- a/test/SbpOperators/volumeops/derivatives/first_derivative_test.jl	Wed Feb 18 21:33:00 2026 +0100
+++ b/test/SbpOperators/volumeops/derivatives/first_derivative_test.jl	Fri Feb 20 12:01:05 2026 +0100
@@ -16,6 +16,9 @@
         
         @test first_derivative(g₁, stencil_set) isa LazyTensor{Float64,1,1}
         @test first_derivative(g₂, stencil_set, 2) isa LazyTensor{Float64,2,2}
+        
+        @test first_derivative(g₁, stencil_set) == first_derivative(g₁, stencil_set, 1)
+        @test_throws DomainError(3, "Direction must be inside [0, 1].") first_derivative(g₁, stencil_set, 3)
 
         interior_stencil = CenteredStencil(-1,0,1)
         closure_stencils = [Stencil(-1,1, center=1)]
--- a/test/SbpOperators/volumeops/derivatives/second_derivative_test.jl	Wed Feb 18 21:33:00 2026 +0100
+++ b/test/SbpOperators/volumeops/derivatives/second_derivative_test.jl	Fri Feb 20 12:01:05 2026 +0100
@@ -21,8 +21,11 @@
     @testset "Constructors" begin
         @testset "1D" begin
             Dₓₓ = second_derivative(g_1D, stencil_set)
+            @test Dₓₓ == second_derivative(g_1D, stencil_set, 1)
             @test Dₓₓ == second_derivative(g_1D, inner_stencil, closure_stencils)
             @test Dₓₓ isa LazyTensor{Float64,1,1}
+
+            @test_throws DomainError(3, "Direction must be inside [0, 1].") second_derivative(g_1D, stencil_set, 3)
         end
         @testset "2D" begin
             Dₓₓ = second_derivative(g_2D,stencil_set,1)
--- a/test/SbpOperators/volumeops/derivatives/second_derivative_variable_test.jl	Wed Feb 18 21:33:00 2026 +0100
+++ b/test/SbpOperators/volumeops/derivatives/second_derivative_variable_test.jl	Fri Feb 20 12:01:05 2026 +0100
@@ -41,6 +41,12 @@
             @test apply_to_functions(v=x->x,   c=x-> -x ) == -ones(11)
             @test apply_to_functions(v=x->x^2, c=x->  1.) == 2ones(11)
         end
+
+        @testset "checking direction" begin
+            c = rand(size(g)...)
+            second_derivative_variable(g, c, stencil_set, 1) == second_derivative_variable(g, c, stencil_set)
+            @test_throws DomainError(2, "Direction must be inside [0, 1].") second_derivative_variable(g, c, stencil_set, 2)
+        end
     end
 
     @testset "2D" begin