diff src/SbpOperators/volumeops/derivatives/second_derivative_variable.jl @ 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 025a506ca2fa
children 3bb94ce74697
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)