Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 7f89237

Browse files
rename to CenteredDifference and simplify constructor
1 parent 271d5b1 commit 7f89237

File tree

8 files changed

+47
-79
lines changed

8 files changed

+47
-79
lines changed

src/DiffEqOperators.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ end
4444

4545
export MatrixFreeOperator
4646
export DiffEqScalar, DiffEqArrayOperator, DiffEqIdentity, JacVecOperator, getops
47-
export AbstractDerivativeOperator, DerivativeOperator, UpwindOperator, FiniteDifference
48-
export RobinBC
47+
export AbstractDerivativeOperator, DerivativeOperator, CenteredDifference, UpwindOperator, FiniteDifference
48+
export RobinBC, GeneralBC
4949
end # module

src/derivative_operators/derivative_operator.jl

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,33 @@ struct DerivativeOperator{T<:Real,S1<:SVector,S2<:SVector} <: AbstractDerivative
99
boundary_point_count :: Int
1010
low_boundary_coefs :: Vector{S2}
1111
high_boundary_coefs :: Vector{S2}
12+
end
1213

13-
function DerivativeOperator{T,S1,S2}(derivative_order::Int,
14-
approximation_order::Int, dx::T,
15-
dimension::Int) where
16-
{T<:Real,S1<:SVector,S2<:SVector}
17-
dimension = dimension
18-
dx = dx
19-
stencil_length = derivative_order + approximation_order - 1 + (derivative_order+approximation_order)%2
20-
boundary_stencil_length = derivative_order + approximation_order
21-
dummy_x = -div(stencil_length,2) : div(stencil_length,2)
22-
boundary_x = -boundary_stencil_length+1:0
23-
boundary_point_count = div(stencil_length,2) - 1 # -1 due to the ghost point
24-
# Because it's a N x (N+2) operator, the last stencil on the sides are the [b,0,x,x,x,x] stencils, not the [0,x,x,x,x,x] stencils, since we're never solving for the derivative at the boundary point.
25-
deriv_spots = (-div(stencil_length,2)+1) : -1
26-
boundary_deriv_spots = boundary_x[2:div(stencil_length,2)]
14+
function CenteredDifference(derivative_order::Int,
15+
approximation_order::Int, dx::T,
16+
dimension::Int) where {T<:Real}
17+
dimension = dimension
18+
dx = dx
19+
stencil_length = derivative_order + approximation_order - 1 + (derivative_order+approximation_order)%2
20+
boundary_stencil_length = derivative_order + approximation_order
21+
dummy_x = -div(stencil_length,2) : div(stencil_length,2)
22+
boundary_x = -boundary_stencil_length+1:0
23+
boundary_point_count = div(stencil_length,2) - 1 # -1 due to the ghost point
24+
# Because it's a N x (N+2) operator, the last stencil on the sides are the [b,0,x,x,x,x] stencils, not the [0,x,x,x,x,x] stencils, since we're never solving for the derivative at the boundary point.
25+
deriv_spots = (-div(stencil_length,2)+1) : -1
26+
boundary_deriv_spots = boundary_x[2:div(stencil_length,2)]
2727

28-
stencil_coefs = convert(SVector{stencil_length, T}, calculate_weights(derivative_order, zero(T), dummy_x))
29-
low_boundary_coefs = [convert(SVector{boundary_stencil_length, T}, calculate_weights(derivative_order, oneunit(T)*x0, boundary_x)) for x0 in boundary_deriv_spots]
30-
high_boundary_coefs = reverse!(copy([convert(SVector{boundary_stencil_length, T}, reverse(low_boundary_coefs[i])) for i in 1:boundary_point_count]))
28+
stencil_coefs = convert(SVector{stencil_length, T}, calculate_weights(derivative_order, zero(T), dummy_x))
29+
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]
30+
high_boundary_coefs = reverse([reverse(low_boundary_coefs[i]) for i in 1:boundary_point_count])
3131

32-
new(derivative_order, approximation_order, dx, dimension, stencil_length,
33-
stencil_coefs,
34-
boundary_stencil_length,
35-
boundary_point_count,
36-
low_boundary_coefs,
37-
high_boundary_coefs
38-
)
39-
end
40-
DerivativeOperator{T}(dorder::Int,aorder::Int,dx::T,dim::Int) where {T<:Real} =
41-
DerivativeOperator{T, SVector{dorder+aorder-1+(dorder+aorder)%2,T}, SVector{dorder+aorder,T}}(dorder, aorder, dx, dim)
32+
DerivativeOperator(derivative_order, approximation_order, dx, dimension, stencil_length,
33+
stencil_coefs,
34+
boundary_stencil_length,
35+
boundary_point_count,
36+
low_boundary_coefs,
37+
high_boundary_coefs
38+
)
4239
end
4340

4441
#=

test/2nd_order_check.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ using DiffEqOperators, Test, LinearAlgebra
55
Δx = 0.025π
66
N = Int(2*/Δx)) -2
77
x = -π:Δx:π-Δx
8-
D1 = DerivativeOperator{Float64}(1,ord,Δx,N,:periodic,:periodic); # 1nd Derivative
9-
D2 = DerivativeOperator{Float64}(2,ord,Δx,N,:periodic,:periodic); # 2nd Derivative
8+
D1 = CenteredDifference(1,ord,Δx,N,:periodic,:periodic); # 1nd Derivative
9+
D2 = CenteredDifference(2,ord,Δx,N,:periodic,:periodic); # 2nd Derivative
1010

1111
u0 = cos.(x)
1212
du_true = -cos.(x)

test/KdV.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ using DiffEqOperators, OrdinaryDiffEq
1717
const du3 = zeros(size(x));
1818
const temp = zeros(size(x));
1919

20-
# A = DerivativeOperator{Float64}(1,2,Δx,length(x),:Dirichlet0,:Dirichlet0);
20+
# A = CenteredDifference(1,2,Δx,length(x),:Dirichlet0,:Dirichlet0);
2121
A = UpwindOperator{Float64}(1,3,Δx,length(x),true.|BitVector(undef,length(x)),
2222
:Dirichlet0,:Dirichlet0);
23-
# C = DerivativeOperator{Float64}(3,2,Δx,length(x),:Dirichlet0,:Dirichlet0);
23+
# C = CenteredDifference(3,2,Δx,length(x),:Dirichlet0,:Dirichlet0);
2424
C = UpwindOperator{Float64}(3,3,Δx,length(x),true.|BitVector(undef,length(x)),
2525
:Dirichlet0,:Dirichlet0);
2626

test/derivative_operators_interface.jl

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ end
5353
# Do not modify the following test-set unless you are completely certain of your changes.
5454
@testset "Correctness of Stencils" begin
5555
N = 20
56-
L = DerivativeOperator{Float64}(4,4, 1.0, N)
56+
L = CenteredDifference(4,4, 1.0, N)
5757
correct = fourth_deriv_approx_stencil(N)
5858

5959
# Check that stencils (according to convert_by_multiplication) agree with correct
@@ -64,7 +64,7 @@ end
6464
@test sparse(L) correct
6565
@test BandedMatrix(L) correct
6666

67-
L = DerivativeOperator{Float64}(2,4, 1.0, N)
67+
L = CenteredDifference(2,4, 1.0, N)
6868
correct = second_deriv_fourth_approx_stencil(N)
6969

7070
# Check that stencils (according to convert_by_multiplication) agree with correct
@@ -82,7 +82,7 @@ end
8282
d_order = 2
8383
approx_order = 2
8484
correct = second_derivative_stencil(N)
85-
A = DerivativeOperator{Float64}(d_order,approx_order,1.0,N)
85+
A = CenteredDifference(d_order,approx_order,1.0,N)
8686

8787
@test convert_by_multiplication(Array,A,N) == correct
8888
@test Array(A) == second_derivative_stencil(N)
@@ -95,7 +95,7 @@ end
9595
N = 20
9696
d_order = 4
9797
approx_order = 4
98-
A = DerivativeOperator{Float64}(d_order,approx_order,1.0,N)
98+
A = CenteredDifference(d_order,approx_order,1.0,N)
9999
correct = convert_by_multiplication(Array,A,N)
100100

101101
@test Array(A) correct
@@ -105,7 +105,7 @@ end
105105
N = 26
106106
d_order = 8
107107
approx_order = 8
108-
A = DerivativeOperator{Float64}(d_order,approx_order,1.0,N)
108+
A = CenteredDifference(d_order,approx_order,1.0,N)
109109
correct = convert_by_multiplication(Array,A,N)
110110

111111
@test Array(A) correct
@@ -119,12 +119,11 @@ end
119119
y = collect(1:1.0:N+2).^4 - 2*collect(1:1.0:N+2).^3 + collect(1:1.0:N+2).^2;
120120
y = convert(Array{BigFloat, 1}, y)
121121

122-
A = DerivativeOperator{BigFloat}(d_order,approx_order,one(BigFloat),N)
122+
A = CenteredDifference(d_order,approx_order,one(BigFloat),N)
123123
correct = convert_by_multiplication(Array,A,N)
124124
@test Array(A) correct
125125
@test sparse(A) correct
126126
@test BandedMatrix(A) correct
127-
128127
@test A*y Array(A)*y
129128
end
130129

@@ -133,7 +132,7 @@ end
133132
d_order = 4
134133
approx_order = 10
135134

136-
A = DerivativeOperator{Float64}(d_order,approx_order,1.0,N)
135+
A = CenteredDifference(d_order,approx_order,1.0,N)
137136
@test A[1,1] == Array(A)[1,1]
138137
@test A[10,20] == 0
139138

@@ -147,7 +146,7 @@ end
147146
d_order = 2
148147
approx_order = 2
149148

150-
A = DerivativeOperator{Float64}(d_order,approx_order,1.0,N)
149+
A = CenteredDifference(d_order,approx_order,1.0,N)
151150
M = Array(A,1000)
152151
@test A[1,1] == M[1,1]
153152
@test A[1:4,1] == M[1:4,1]
@@ -167,8 +166,8 @@ end
167166
dy = yarr[2]-yarr[1]
168167
F = [x^2+y for x = xarr, y = yarr]
169168

170-
A = DerivativeOperator{Float64}(d_order,approx_order,dx,length(xarr)-2)
171-
B = DerivativeOperator{Float64}(d_order,approx_order,dy,length(yarr))
169+
A = CenteredDifference(d_order,approx_order,dx,length(xarr)-2)
170+
B = CenteredDifference(d_order,approx_order,dy,length(yarr))
172171

173172

174173
@test A*F 2*ones(N-2,M) atol=1e-2
@@ -186,7 +185,7 @@ end
186185
# Only tests the additional functionality defined in "operator_combination.jl"
187186
N = 10
188187
Random.seed!(0); LA = DiffEqArrayOperator(rand(N,N))
189-
LD = DerivativeOperator{Float64}(2,2,1.0,N)
188+
LD = CenteredDifference(2,2,1.0,N)
190189
@test_broken begin
191190
L = 1.1*LA - 2.2*LD + 3.3*I
192191
# Builds convert(L) the brute-force way

test/generic_operator_validation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ y_ = y[2:(end-1)]
1111

1212
@test_broken for dor in 1:6, aor in 1:8
1313

14-
D1 = DerivativeOperator{Float64}(dor,aor,dx[1],length(x))
14+
D1 = CenteredDifference(dor,aor,dx[1],length(x))
1515
D2 = DiffEqOperators.FiniteDifference{Float64}(dor,aor,dx,length(x))
1616
D = (D1,D2)
1717
@test_broken convert(Array, D1) convert(Array, D2)

test/heat_eqn.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using OrdinaryDiffEq
44
@testset "Parabolic Heat Equation with Dirichlet BCs" begin
55
x = collect(-pi : 2pi/511 : pi)
66
u0 = @. -(x - 0.5).^2 + 1/12
7-
A = DerivativeOperator{Float64}(2,2,2π/511,512,:Dirichlet,:Dirichlet;BC=(u0[1],u0[end]))
7+
A = CenteredDifference(2,2,2π/511,512,:Dirichlet,:Dirichlet;BC=(u0[1],u0[end]))
88
heat_eqn = ODEProblem(A, u0, (0.,10.))
99
soln = solve(heat_eqn,Tsit5(),dense=false,tstops=0:0.01:10)
1010

@@ -20,10 +20,10 @@ end
2020
dx = 2π/(N-1)
2121
x = collect(-pi : dx : pi)
2222
u0 = @. -(x - 0.5)^2 + 1/12
23-
B = DerivativeOperator{Float64}(1,2,dx,N,:None,:None)
23+
B = CenteredDifference(1,2,dx,N,:None,:None)
2424
deriv_start, deriv_end = (B*u0)[1], (B*u0)[end]
2525

26-
A = DerivativeOperator{Float64}(2,2,dx,N,:Neumann,:Neumann;BC=(deriv_start,deriv_end))
26+
A = CenteredDifference(2,2,dx,N,:Neumann,:Neumann;BC=(deriv_start,deriv_end))
2727

2828
heat_eqn = ODEProblem(A, u0, (0.,10.))
2929
soln = solve(heat_eqn,Tsit5(),dense=false,tstops=0:0.01:10)
@@ -42,14 +42,14 @@ end
4242
dx = 2π/(N-1)
4343
x = collect(-pi : dx : pi)
4444
u0 = @. -(x - 0.5).^2 + 1/12
45-
B = DerivativeOperator{Float64}(1,2,dx,N,:None,:None)
45+
B = CenteredDifference(1,2,dx,N,:None,:None)
4646
deriv_start, deriv_end = (B*u0)[1], (B*u0)[end]
4747
params = [1.0,0.5]
4848

4949
left_RBC = params[1]*u0[1] - params[2]*deriv_start
5050
right_RBC = params[1]*u0[end] + params[2]*deriv_end
5151

52-
A = DerivativeOperator{Float64}(2,2,dx,N,:Robin,:Dirichlet;BC=((params[1],params[2],left_RBC),u0[end]));
52+
A = CenteredDifference(2,2,dx,N,:Robin,:Dirichlet;BC=((params[1],params[2],left_RBC),u0[end]));
5353

5454
heat_eqn = ODEProblem(A, u0, (0.,10.));
5555
soln = solve(heat_eqn,Tsit5(),dense=false,tstops=0:0.01:10);

test/runtests.jl

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,3 @@ import Base: isapprox
1111
#@time @safetestset "KdV" begin include("KdV.jl") end # KdV times out and all fails
1212
#@time @safetestset "Heat Equation" begin include("heat_eqn.jl") end
1313
@time @safetestset "Matrix-Free Operators" begin include("matrixfree.jl") end
14-
15-
#=
16-
using StaticArrays, DiffEqOperators
17-
function isapprox(x::DerivativeOperator{T,S,LBC,RBC},y::FiniteDifference{T,S,LBC,RBC}; kwargs...) where {T<:Real,S<:StaticArrays.SVector,LBC,RBC}
18-
der_order = (x,y) -> x.derivative_order == y.derivative_order
19-
approximation_order = (x,y) -> x.approximation_order == y.approximation_order
20-
dx = (x,y) -> !any(x.dx .!= y.dx)
21-
dimension = (x,y) -> x.dimension == y.dimension
22-
stencil_length = (x,y) -> x.stencil_length == y.stencil_length
23-
stencil_coefs = (x,y) -> !any(!isapprox.(x.stencil_coefs,y.stencil_coefs; kwargs...))
24-
boundary_point_count= (x,y) -> x.boundary_point_count == y.boundary_point_count
25-
boundary_length = (x,y) -> x.boundary_length == y.boundary_length
26-
low_boundary_coefs = (x,y) -> !any(!isapprox.(x.low_boundary_coefs,y.low_boundary_coefs; kwargs...))
27-
high_boundary_coefs = (x,y) -> !any(!isapprox.(x.high_boundary_coefs,y.high_boundary_coefs; kwargs...))
28-
boundary_condition = (x,y) -> !any(!isapprox.(x.high_boundary_coefs,y.high_boundary_coefs; kwargs...))
29-
t = (x,y) -> x.t ≈ y.t
30-
31-
#in order of fast to slow comparison
32-
for f in (der_order,approximation_order,dimension,t,boundary_point_count,boundary_length,dx,boundary_condition,low_boundary_coefs,high_boundary_coefs,stencil_coefs)
33-
if !f(x,y)
34-
return false
35-
end
36-
end
37-
return true
38-
end
39-
40-
isapprox(x::FiniteDifference{T,S,LBC,RBC},y::DerivativeOperator{T,S,LBC,RBC}; kwargs...) where {T<:Real,S<:StaticArrays.SVector,LBC,RBC} = isapprox(y,x; kwargs...)
41-
=#

0 commit comments

Comments
 (0)