changeset 2096:5af7534e5b3c feature/sbp_operators/laplace_curvilinear tip

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 02 Mar 2026 15:58:27 +0100
parents 87157cfca640 (current diff) 99577c24a5a5 (diff)
children
files Project.toml src/SbpOperators/volumeops/derivatives/second_derivative_variable.jl
diffstat 9 files changed, 77 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/.hgtags	Wed Feb 25 14:33:42 2026 +0100
+++ b/.hgtags	Mon Mar 02 15:58:27 2026 +0100
@@ -2,3 +2,4 @@
 b00eb94848baf86f3877c3ecef4d77503e8dfe4f v0.1.4
 b09b0da220728402f2d49adc467ee96982550bc1 v0.1.5
 8aa0cf7f9e4f5eeaaf6f14cb60b25c546a928b5b v0.1.6
+23e13150ac5accf9b07692be21996d994357ffaf v0.1.7
--- a/Project.toml	Wed Feb 25 14:33:42 2026 +0100
+++ b/Project.toml	Mon Mar 02 15:58:27 2026 +0100
@@ -1,7 +1,7 @@
 name = "Diffinitive"
 uuid = "5a373a26-915f-4769-bcab-bf03835de17b"
 authors = ["Jonatan Werpers <jonatan@werpers.com>", "Vidar Stiernström <vidar.stiernstrom@gmail.com>, and contributors"]
-version = "0.1.6"
+version = "0.1.7"
 
 [workspace]
 projects = ["test", "docs", "benchmark", "notebooks"]
--- a/src/LazyTensors/lazy_tensor_operations.jl	Wed Feb 25 14:33:42 2026 +0100
+++ b/src/LazyTensors/lazy_tensor_operations.jl	Mon Mar 02 15:58:27 2026 +0100
@@ -99,15 +99,19 @@
 end
 
 function apply(tmBinOp::TensorSum{T,R,D}, v::AbstractArray{<:Any,D}, I::Vararg{Any,R}) where {T,R,D}
-    return sum(tmBinOp.tms) do tm
+    vs = map(tmBinOp.tms) do tm
         apply(tm,v,I...)
     end
+
+    return +(vs...)
 end
 
 function apply_transpose(tmBinOp::TensorSum{T,R,D}, v::AbstractArray{<:Any,D}, I::Vararg{Any,R}) where {T,R,D}
-    return sum(tmBinOp.tms) do tm
+    vs = map(tmBinOp.tms) do tm
         apply_transpose(tm,v,I...)
     end
+
+    return +(vs...)
 end
 
 range_size(tmBinOp::TensorSum) = range_size(tmBinOp.tms[1])
--- a/src/SbpOperators/volumeops/derivatives/first_derivative.jl	Wed Feb 25 14:33:42 2026 +0100
+++ b/src/SbpOperators/volumeops/derivatives/first_derivative.jl	Mon Mar 02 15:58:27 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	Wed Feb 25 14:33:42 2026 +0100
+++ b/src/SbpOperators/volumeops/derivatives/second_derivative.jl	Mon Mar 02 15:58:27 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	Wed Feb 25 14:33:42 2026 +0100
+++ b/src/SbpOperators/volumeops/derivatives/second_derivative_variable.jl	Mon Mar 02 15:58:27 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	Wed Feb 25 14:33:42 2026 +0100
+++ b/test/SbpOperators/volumeops/derivatives/first_derivative_test.jl	Mon Mar 02 15:58:27 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	Wed Feb 25 14:33:42 2026 +0100
+++ b/test/SbpOperators/volumeops/derivatives/second_derivative_test.jl	Mon Mar 02 15:58:27 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	Wed Feb 25 14:33:42 2026 +0100
+++ b/test/SbpOperators/volumeops/derivatives/second_derivative_variable_test.jl	Mon Mar 02 15:58:27 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