changeset 940:b15f39ae1643 feature/variable_derivatives

Add size checking for the coefficient
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 10 Mar 2022 08:18:29 +0100
parents d8da3a1060b7
children 8b0ff2fddc32
files src/SbpOperators/volumeops/derivatives/second_derivative_variable.jl test/SbpOperators/volumeops/derivatives/second_derivative_variable_test.jl
diffstat 2 files changed, 23 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/SbpOperators/volumeops/derivatives/second_derivative_variable.jl	Wed Mar 09 10:14:16 2022 +0100
+++ b/src/SbpOperators/volumeops/derivatives/second_derivative_variable.jl	Thu Mar 10 08:18:29 2022 +0100
@@ -21,6 +21,8 @@
 end
 
 function SecondDerivativeVariable(grid::EquidistantGrid, coeff::AbstractArray, inner_stencil, closure_stencils, dir)
+    check_coefficient(grid, coeff)
+
     Δxᵢ = spacing(grid)[dir]
     scaled_inner_stencil = scale(inner_stencil, 1/Δxᵢ^2)
     scaled_closure_stencils = scale.(Tuple(closure_stencils), 1/Δxᵢ^2)
@@ -51,13 +53,23 @@
 ```
 on ``(0,1)⨯(0,1)`` represented by `g`.
 """
-function SecondDerivativeVariable(grid::EquidistantGrid, coeff::AbstractArray, stencil_set, dir)
+function SecondDerivativeVariable(grid::EquidistantGrid, coeff::AbstractArray, stencil_set, dir::Int)
     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 SecondDerivativeVariable(grid, coeff, inner_stencil, closure_stencils, dir)
 end
 
+function check_coefficient(grid, coeff)
+    if dimension(grid) != ndims(coeff)
+        throw(ArgumentError("The coefficient has dimension $(ndims(coeff)) while the grid is dimension $(dimension(grid))"))
+    end
+
+    if size(grid) != size(coeff)
+        throw(DimensionMismatch("the size $(size(coeff)) of the coefficient does not match the size $(size(grid)) of the grid"))
+    end
+end
+
 derivative_direction(::SecondDerivativeVariable{Dir}) where {Dir} = Dir
 
 closure_size(op::SecondDerivativeVariable) = length(op.closure_stencils)
--- a/test/SbpOperators/volumeops/derivatives/second_derivative_variable_test.jl	Wed Mar 09 10:14:16 2022 +0100
+++ b/test/SbpOperators/volumeops/derivatives/second_derivative_variable_test.jl	Thu Mar 10 08:18:29 2022 +0100
@@ -27,6 +27,16 @@
 
             stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order = 2)
             @test SecondDerivativeVariable(g, c, stencil_set, 1) isa SecondDerivativeVariable
+
+            @testset "checking c" begin
+                c_short = rand(5)
+                c_long = rand(16)
+                c_higher_dimension = rand(11,11)
+
+                @test_throws DimensionMismatch("the size (5,) of the coefficient does not match the size (11,) of the grid") SecondDerivativeVariable(g, c_short, interior_stencil, closure_stencils)
+                @test_throws DimensionMismatch("the size (16,) of the coefficient does not match the size (11,) of the grid") SecondDerivativeVariable(g, c_long, interior_stencil, closure_stencils)
+                @test_throws ArgumentError("The coefficient has dimension 2 while the grid is dimension 1") SecondDerivativeVariable(g, c_higher_dimension, interior_stencil, closure_stencils,1)
+            end
         end
 
         @testset "sizes" begin