Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/derivative_operators/derivative_operator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct CenteredDifference{N} end
function CenteredDifference{N}(derivative_order::Int,
approximation_order::Int, dx::T,
len::Int, coeff_func=nothing) where {T<:Real,N}

@assert approximation_order>1 "approximation_order must be greater than 1."
stencil_length = derivative_order + approximation_order - 1 + (derivative_order+approximation_order)%2
boundary_stencil_length = derivative_order + approximation_order
dummy_x = -div(stencil_length,2) : div(stencil_length,2)
Expand All @@ -30,8 +30,8 @@ function CenteredDifference{N}(derivative_order::Int,
deriv_spots = (-div(stencil_length,2)+1) : -1
boundary_deriv_spots = boundary_x[2:div(stencil_length,2)]

stencil_coefs = convert(SVector{stencil_length, T}, calculate_weights(derivative_order, zero(T), dummy_x))
_low_boundary_coefs = SVector{boundary_stencil_length, T}[convert(SVector{boundary_stencil_length, T}, calculate_weights(derivative_order, oneunit(T)*x0, boundary_x)) for x0 in boundary_deriv_spots]
stencil_coefs = convert(SVector{stencil_length, T}, (1/dx^derivative_order) * calculate_weights(derivative_order, zero(T), dummy_x))
_low_boundary_coefs = SVector{boundary_stencil_length, T}[convert(SVector{boundary_stencil_length, T}, (1/dx^derivative_order) * calculate_weights(derivative_order, oneunit(T)*x0, boundary_x)) for x0 in boundary_deriv_spots]
low_boundary_coefs = convert(SVector{boundary_point_count},_low_boundary_coefs)
high_boundary_coefs = convert(SVector{boundary_point_count},reverse(SVector{boundary_stencil_length, T}[reverse(low_boundary_coefs[i]) for i in 1:boundary_point_count]))

Expand Down
28 changes: 28 additions & 0 deletions test/error_benchmarking.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using DiffEqOperators, Plots

n = 10000
@show n
x = range(0.0; length = n, stop = 2π)
dx = x[2]-x[1]
y = exp.(π*x)
y_ = y[2:(end-1)]

for dor in 1:4, aor in 2:6

D1 = CenteredDifference(dor,aor,dx,length(x))

#test result
@show dor
@show aor
#take derivatives
err_abs = abs.(D1*y .- (π^dor)*y_)
err_percent = 100*err_abs./abs.(((π^dor)*y_))
max_error = maximum(err_percent) # test operator with known derivative of exp(kx)
avg_error = sum(err_percent)/length(err_percent)

@show max_error
@show avg_error
plot(x[2:(end-1)], err_percent, title="Percentage Error, n=$n aor = $aor, dor = $dor")

#test result
end