changeset 2093:17a41f0eddb0

Merge refactor/sbp_operators/direction_check
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 02 Mar 2026 15:54:31 +0100
parents 53e5887457b9 (current diff) b1085efb78bf (diff)
children 23e13150ac5a
files
diffstat 6 files changed, 69 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/SbpOperators/volumeops/derivatives/first_derivative.jl	Mon Mar 02 13:51:56 2026 +0100
+++ b/src/SbpOperators/volumeops/derivatives/first_derivative.jl	Mon Mar 02 15:54:31 2026 +0100
@@ -1,23 +1,27 @@
 """
-    first_derivative(g, ..., [direction])
+    first_derivative(g, ..., [dim])
 
 The first derivative operator `D1` as a `LazyTensor` on the given grid.
 
 `D1` approximates the first-derivative d/dξ on `g` along the coordinate
-dimension specified by `direction`.
+dimension specified by `dim`.
 """
 function first_derivative end
 
 """
-    first_derivative(g::TensorGrid, stencil_set, direction)
+    first_derivative(g::TensorGrid, stencil_set, dim)
 
 See also: [`VolumeOperator`](@ref), [`LazyTensors.inflate`](@ref).
 """
-function first_derivative(g::TensorGrid, stencil_set, direction)
-    D₁ = first_derivative(g.grids[direction], stencil_set)
-    return LazyTensors.inflate(D₁, size(g), direction)
+function first_derivative(g::TensorGrid, stencil_set, dim)
+    if dim ∉ 1:ndims(g)
+        throw(DomainError(dim, "Derivative direction must be in 1:$(ndims(g))."))
+    end
+    D₁ = first_derivative(g.grids[dim], stencil_set)
+    return LazyTensors.inflate(D₁, size(g), dim)
 end
 
+
 """
     first_derivative(g::EquidistantGrid, stencil_set::StencilSet)
 
@@ -30,6 +34,13 @@
     return first_derivative(g, inner_stencil, closure_stencils);
 end
 
+function first_derivative(g::EquidistantGrid, stencil_set, dim)
+    if dim != 1
+        throw(DomainError(dim, "Derivative direction must be 1."))
+    end
+    return first_derivative(g, stencil_set)
+end
+
 """
     first_derivative(g::EquidistantGrid, inner_stencil::Stencil, closure_stencils)
 
--- a/src/SbpOperators/volumeops/derivatives/second_derivative.jl	Mon Mar 02 13:51:56 2026 +0100
+++ b/src/SbpOperators/volumeops/derivatives/second_derivative.jl	Mon Mar 02 15:54:31 2026 +0100
@@ -1,16 +1,19 @@
 """
-    second_derivative(g::EquidistantGrid, stencil_set, direction)
+    second_derivative(g::TensorGrid, stencil_set, dim)
 
 Creates the second derivative operator `D2` as a `LazyTensor`
 
 `D2` approximates the second-derivative d²/dξ² on `g` along the coordinate
-dimension specified by `direction`.
+dimension specified by `dim`.
 
 See also: [`VolumeOperator`](@ref), [`LazyTensors.inflate`](@ref).
 """
-function second_derivative(g::TensorGrid, stencil_set, direction)
-    D₂ = second_derivative(g.grids[direction], stencil_set)
-    return LazyTensors.inflate(D₂, size(g), direction)
+function second_derivative(g::TensorGrid, stencil_set, dim)
+    if dim ∉ 1:ndims(g)
+        throw(DomainError(dim, "Derivative direction must be in 1:$(ndims(g))."))
+    end
+    D₂ = second_derivative(g.grids[dim], stencil_set)
+    return LazyTensors.inflate(D₂, size(g), dim)
 end
 
 """
@@ -25,6 +28,13 @@
     return second_derivative(g, inner_stencil, closure_stencils)
 end
 
+function second_derivative(g::EquidistantGrid, stencil_set::StencilSet, dim)
+    if dim != 1
+        throw(DomainError(dim, "Derivative direction must be 1."))
+    end
+    return second_derivative(g, stencil_set)
+end
+
 """
     second_derivative(g::EquidistantGrid, inner_stencil::Stencil, closure_stencils)
 
--- a/src/SbpOperators/volumeops/derivatives/second_derivative_variable.jl	Mon Mar 02 13:51:56 2026 +0100
+++ b/src/SbpOperators/volumeops/derivatives/second_derivative_variable.jl	Mon Mar 02 15:54:31 2026 +0100
@@ -1,32 +1,42 @@
 """
-    second_derivative_variable(g, coeff ..., [direction])
+    second_derivative_variable(g, coeff, ..., [dim])
 
 The variable second derivative operator as a `LazyTensor` on the given grid.
 `coeff` is a grid function of the variable coefficient.
 
 Approximates the d/dξ c d/dξ on `g` along the coordinate dimension specified
-by `direction`.
+by `dim`.
 """
 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, dim::Int)
+    if dim ∉ 1:ndims(g)
+        throw(DomainError(dim, "Derivative direction must be in 1:$(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, dim)
 end
 
 function second_derivative_variable(g::EquidistantGrid, coeff, stencil_set)
     return second_derivative_variable(TensorGrid(g), coeff, stencil_set, 1)
 end
 
-function second_derivative_variable(g::TensorGrid, coeff, inner_stencil::NestedStencil, closure_stencils, dir)
+function second_derivative_variable(g::EquidistantGrid, coeff, stencil_set, dim)
+    if dim != 1
+        throw(DomainError(dim, "Derivative direction must be 1."))
+    end
+    return second_derivative_variable(g, coeff, stencil_set)
+end
+
+function second_derivative_variable(g::TensorGrid, coeff, inner_stencil::NestedStencil, closure_stencils, dim)
     check_coefficient(g, coeff)
 
-    Δxᵢ = spacing(g.grids[dir])
+    Δxᵢ = spacing(g.grids[dim])
     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, dim)
 end
 
 function check_coefficient(g, coeff)
--- a/test/SbpOperators/volumeops/derivatives/first_derivative_test.jl	Mon Mar 02 13:51:56 2026 +0100
+++ b/test/SbpOperators/volumeops/derivatives/first_derivative_test.jl	Mon Mar 02 15:54:31 2026 +0100
@@ -16,6 +16,10 @@
         
         @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, "Derivative direction must be 1.") first_derivative(g₁, stencil_set, 3)
+        @test_throws DomainError(3, "Derivative direction must be in 1:2.") 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	Mon Mar 02 13:51:56 2026 +0100
+++ b/test/SbpOperators/volumeops/derivatives/second_derivative_test.jl	Mon Mar 02 15:54:31 2026 +0100
@@ -21,12 +21,18 @@
     @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, "Derivative direction must be 1.") second_derivative(g_1D, stencil_set, 3)
         end
         @testset "2D" begin
             Dₓₓ = second_derivative(g_2D,stencil_set,1)
             @test Dₓₓ isa LazyTensor{Float64,2,2}
+
+
+            @test_throws DomainError(3, "Derivative direction must be in 1:2.") second_derivative(g_2D, stencil_set, 3)
         end
     end
 
--- a/test/SbpOperators/volumeops/derivatives/second_derivative_variable_test.jl	Mon Mar 02 13:51:56 2026 +0100
+++ b/test/SbpOperators/volumeops/derivatives/second_derivative_variable_test.jl	Mon Mar 02 15:54:31 2026 +0100
@@ -41,12 +41,22 @@
             @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)...)
+            @test second_derivative_variable(g, c, stencil_set, 1) == second_derivative_variable(g, c, stencil_set)
+            @test_throws DomainError(2, "Derivative direction must be 1.") second_derivative_variable(g, c, stencil_set, 2)
+        end
     end
 
     @testset "2D" begin
         g = equidistant_grid((0.,0.), (10.,8.), 11, 9) # h = 1
         c = eval_on(g, (x,y)->x+y)
 
+        @testset "checking direction" begin
+            @test_throws DomainError(3, "Derivative direction must be in 1:2.") second_derivative_variable(g, c, stencil_set, 3)
+        end
+
         @testset "application" begin
             function apply_to_functions(dir; v, c)
                 g = equidistant_grid((0.,0.), (10.,8.), 11, 9) # h = 1