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

Commit aabfced

Browse files
simplified coefficient function handling
1 parent 6b3194f commit aabfced

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/derivative_operators/abstract_operator_functions.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Base.:/(A::AbstractDerivativeOperator, B::AbstractVecOrMat) = Array(A) / B
128128
The Inf opnorm can be calculated easily using the stencil coeffiicents, while other opnorms
129129
default to compute from the full matrix form.
130130
=#
131-
function LinearAlgebra.opnorm(A::DerivativeOperator{T,S}, p::Real=2) where {T,S}
131+
function LinearAlgebra.opnorm(A::DerivativeOperator, p::Real=2)
132132
if p == Inf
133133
sum(abs.(A.stencil_coefs)) / A.dx^A.derivative_order
134134
else
@@ -167,6 +167,23 @@ end
167167

168168
################################################################################
169169

170+
function *(coeff_func::Function, A::DerivativeOperator{T}) where T
171+
coefficients = A.coefficients === nothing ? Vector{T}(undef,A.len) : A.coefficients
172+
DerivativeOperator{T,typeof(A.dx),typeof(A.stencil_coefs),
173+
typeof(A.low_boundary_coefs),typeof(coefficients),
174+
typeof(coeff_func)}(
175+
A.derivative_order, A.approximation_order,
176+
A.dx, A.len, A.stencil_length,
177+
A.stencil_coefs,
178+
A.boundary_stencil_length,
179+
A.boundary_point_count,
180+
A.low_boundary_coefs,
181+
A.high_boundary_coefs,coefficients,coeff_func,A.use_winding
182+
)
183+
end
184+
185+
################################################################################
186+
170187
function DiffEqBase.update_coefficients!(A::AbstractDerivativeOperator,u,p,t)
171188
if A.coeff_func !== nothing
172189
A.coeff_func(A.coefficients,u,p,t)

src/derivative_operators/derivative_operator.jl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ end
1616

1717
function CenteredDifference(derivative_order::Int,
1818
approximation_order::Int, dx::T,
19-
len::Int) where {T<:Real}
19+
len::Int, coeff_func=nothing) where {T<:Real}
2020

2121
stencil_length = derivative_order + approximation_order - 1 + (derivative_order+approximation_order)%2
2222
boundary_stencil_length = derivative_order + approximation_order
@@ -32,21 +32,22 @@ function CenteredDifference(derivative_order::Int,
3232
low_boundary_coefs = convert(SVector{boundary_point_count},_low_boundary_coefs)
3333
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]))
3434

35+
coefficients = coeff_func isa Nothing ? nothing : Vector{T}(undef,len)
3536
DerivativeOperator{T,T,typeof(stencil_coefs),
36-
typeof(low_boundary_coefs),Nothing,
37-
Nothing}(
37+
typeof(low_boundary_coefs),typeof(coefficients),
38+
typeof(coeff_func)}(
3839
derivative_order, approximation_order, dx, len, stencil_length,
3940
stencil_coefs,
4041
boundary_stencil_length,
4142
boundary_point_count,
4243
low_boundary_coefs,
43-
high_boundary_coefs,nothing,nothing,false
44+
high_boundary_coefs,coefficients,coeff_func,false
4445
)
4546
end
4647

4748
function CenteredDifference(derivative_order::Int,
4849
approximation_order::Int, dx::AbstractVector{T},
49-
len::Int) where {T<:Real}
50+
len::Int, coeff_func=nothing) where {T<:Real}
5051

5152
stencil_length = derivative_order + approximation_order - 1 + (derivative_order+approximation_order)%2
5253
boundary_stencil_length = derivative_order + approximation_order
@@ -62,22 +63,24 @@ function CenteredDifference(derivative_order::Int,
6263
low_boundary_coefs = convert(SVector{boundary_point_count},_low_boundary_coefs)
6364
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]))
6465

66+
coefficients = coeff_func isa Nothing ? nothing : Vector{T}(undef,len)
67+
6568
DerivativeOperator{T,typeof(dx),typeof(stencil_coefs),
66-
typeof(low_boundary_coefs),Nothing,
67-
Nothing}(
69+
typeof(low_boundary_coefs),typeof(coefficients),
70+
typeof(coeff_func)}(
6871
derivative_order, approximation_order, dx,
6972
len, stencil_length,
7073
stencil_coefs,
7174
boundary_stencil_length,
7275
boundary_point_count,
7376
low_boundary_coefs,
74-
high_boundary_coefs,nothing,nothing,false
77+
high_boundary_coefs,coefficients,coeff_func,false
7578
)
7679
end
7780

7881
function UpwindDifference(derivative_order::Int,
7982
approximation_order::Int, dx::T,
80-
len::Int, coeff_func) where {T<:Real}
83+
len::Int, coeff_func=nothing) where {T<:Real}
8184

8285
stencil_length = derivative_order + approximation_order - 1 + (derivative_order+approximation_order)%2
8386
boundary_stencil_length = derivative_order + approximation_order
@@ -109,7 +112,7 @@ end
109112

110113
function UpwindDifference(derivative_order::Int,
111114
approximation_order::Int, dx::AbstractVector{T},
112-
len::Int, coeff_func) where {T<:Real}
115+
len::Int, coeff_func=nothing) where {T<:Real}
113116

114117
stencil_length = derivative_order + approximation_order - 1 + (derivative_order+approximation_order)%2
115118
boundary_stencil_length = derivative_order + approximation_order

0 commit comments

Comments
 (0)