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

Commit 6cdf435

Browse files
committed
Separating implementation of convolve functions for Array by dispatch
1 parent e102e06 commit 6cdf435

File tree

2 files changed

+95
-4
lines changed

2 files changed

+95
-4
lines changed

src/derivative_operators/convolutions.jl

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ end
88
################################################
99

1010
# Against a standard vector, assume already padded and just apply the stencil
11-
function convolve_interior!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator; overwrite = true) where {T<:Real, N}
11+
function convolve_interior!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator{T,N,false}; overwrite = true) where {T<:Real, N}
1212
@assert length(x_temp)+2 == length(x)
1313
stencil = A.stencil_coefs
1414
coeff = A.coefficients
@@ -25,7 +25,7 @@ function convolve_interior!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::
2525
end
2626
end
2727

28-
function convolve_BC_left!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator; overwrite = true) where {T<:Real, N}
28+
function convolve_BC_left!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator{T,N,false}; overwrite = true) where {T<:Real, N}
2929
stencil = A.low_boundary_coefs
3030
coeff = A.coefficients
3131
for i in 1 : A.boundary_point_count
@@ -40,7 +40,7 @@ function convolve_BC_left!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::D
4040
end
4141
end
4242

43-
function convolve_BC_right!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator; overwrite = true) where {T<:Real, N}
43+
function convolve_BC_right!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator{T,N,false}; overwrite = true) where {T<:Real, N}
4444
stencil = A.high_boundary_coefs
4545
coeff = A.coefficients
4646
for i in 1 : A.boundary_point_count
@@ -55,6 +55,97 @@ function convolve_BC_right!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::
5555
end
5656
end
5757

58+
# Against a standard vector, assume already padded and just apply the stencil
59+
function convolve_interior!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator{T,N,true}; overwrite = true) where {T<:Real, N}
60+
@assert length(x_temp)+2 == length(x)
61+
stencil = A.stencil_coefs
62+
coeff = A.coefficients
63+
64+
# Upwind operators have a non-centred stencil
65+
if use_winding(A)
66+
mid = 1 + A.stencil_length%2
67+
else
68+
mid = div(A.stencil_length,2)
69+
end
70+
71+
for i in (1+A.boundary_point_count) : (length(x_temp)-A.boundary_point_count)
72+
xtempi = zero(T)
73+
cur_stencil = eltype(stencil) <: AbstractVector ? stencil[i-A.boundary_point_count] : stencil
74+
cur_coeff = typeof(coeff) <: AbstractVector ? coeff[i] : coeff isa Number ? coeff : true
75+
cur_stencil = use_winding(A) && cur_coeff < 0 ? reverse(cur_stencil) : cur_stencil
76+
for idx in 1:A.stencil_length
77+
x_idx = use_winding(A) && cur_coeff < 0 ? x[i + mid - idx] : x[i - mid + idx]
78+
xtempi += cur_coeff * cur_stencil[idx] * x_idx
79+
end
80+
x_temp[i] = xtempi + !overwrite*x_temp[i]
81+
end
82+
end
83+
84+
function convolve_BC_left!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator{T,N,true}; overwrite = true) where {T<:Real, N}
85+
stencil = A.low_boundary_coefs
86+
coeff = A.coefficients
87+
88+
_bpc = A.boundary_point_count
89+
use_interior_stencil = false
90+
if isempty(stencil)
91+
_bpc = A.boundary_point_count + 1
92+
use_interior_stencil = true
93+
end
94+
95+
for i in 1 : _bpc
96+
if use_interior_stencil == true
97+
cur_stencil = A.stencil_coefs
98+
slen = length(A.stencil_coefs)
99+
else
100+
cur_stencil = stencil[i]
101+
slen = length(cur_stencil)
102+
end
103+
104+
cur_coeff = typeof(coeff) <: AbstractVector ? coeff[i] : coeff isa Number ? coeff : true
105+
cur_stencil = use_winding(A) && cur_coeff < 0 ? reverse(cur_stencil) : cur_stencil
106+
xtempi = cur_coeff*cur_stencil[1]*x[1]
107+
for idx in 2:slen
108+
xtempi += cur_coeff * cur_stencil[idx] * x[idx]
109+
end
110+
x_temp[i] = xtempi + !overwrite*x_temp[i]
111+
end
112+
end
113+
114+
function convolve_BC_right!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator{T,N,true}; overwrite = true) where {T<:Real, N}
115+
stencil = A.high_boundary_coefs
116+
coeff = A.coefficients
117+
x_len = length(x)
118+
L = A.boundary_stencil_length
119+
120+
_bpc = A.boundary_point_count
121+
use_interior_stencil = false
122+
123+
if isempty(stencil)
124+
_bpc = 1
125+
use_interior_stencil = true
126+
end
127+
128+
for i in 1 : _bpc
129+
if use_interior_stencil == true
130+
cur_stencil = A.stencil_coefs
131+
slen = length(A.stencil_coefs)
132+
L = A.stencil_length
133+
else
134+
cur_stencil = stencil[i]
135+
slen = length(cur_stencil)
136+
end
137+
138+
cur_coeff = typeof(coeff) <: AbstractVector ? coeff[i] : coeff isa Number ? coeff : true
139+
cur_stencil = use_winding(A) && cur_coeff < 0 ? reverse(cur_stencil) : cur_stencil
140+
xtempi = zero(T)
141+
for idx in 1:slen
142+
xtempi += cur_coeff * cur_stencil[idx] * x[x_len-L+idx]
143+
end
144+
x_temp[end-_bpc+i] = xtempi + !overwrite*x_temp[end-_bpc+i]
145+
end
146+
end
147+
148+
58149
###########################################
59150

60151
# Against A BC-padded vector, specialize the computation to explicitly use the left, right, and middle parts

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ import Base: isapprox
1717
@time @safetestset "Differentiation Dimension" begin include("differentiation_dimension.jl") end
1818
@time @safetestset "2D and 3D fast multiplication" begin include("2D_3D_fast_multiplication.jl") end
1919
@time @safetestset "Higher Dimensional Concretization" begin include("concretization.jl") end
20-
@time @safetestset "Upwind Operator Interface" begin include("upwind_operators_interface.jl") end
20+
@time @safetestset "Upwind Operator Interface" begin include("upwind_operators_interface.jl") end

0 commit comments

Comments
 (0)