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

Commit cd3dcda

Browse files
Merge pull request #132 from JuliaDiffEq/nnlib_convolutions
2d multiplication for combinations
2 parents 3766556 + 532c2cf commit cd3dcda

File tree

5 files changed

+911
-22
lines changed

5 files changed

+911
-22
lines changed

src/derivative_operators/convolutions.jl

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# mul! done by convolutions
2-
function LinearAlgebra.mul!(x_temp::AbstractVector{T}, A::DerivativeOperator, x::AbstractVector{T}) where T<:Real
3-
convolve_BC_left!(x_temp, x, A)
4-
convolve_interior!(x_temp, x, A)
5-
convolve_BC_right!(x_temp, x, A)
2+
function LinearAlgebra.mul!(x_temp::AbstractVector{T}, A::DerivativeOperator, x::AbstractVector{T}; overwrite = true) where T<:Real
3+
convolve_BC_left!(x_temp, x, A, overwrite = overwrite)
4+
convolve_interior!(x_temp, x, A, overwrite = overwrite)
5+
convolve_BC_right!(x_temp, x, A, overwrite = overwrite)
66
end
77

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) where {T<:Real}
11+
function convolve_interior!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator; overwrite = true) where {T<:Real}
1212
@assert length(x_temp)+2 == length(x)
1313
stencil = A.stencil_coefs
1414
coeff = A.coefficients
@@ -21,11 +21,11 @@ function convolve_interior!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::
2121
for idx in 1:A.stencil_length
2222
xtempi += cur_coeff * cur_stencil[idx] * x[i - mid + idx]
2323
end
24-
x_temp[i] = xtempi
24+
x_temp[i] = xtempi + !overwrite*x_temp[i]
2525
end
2626
end
2727

28-
function convolve_BC_left!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator) where {T<:Real}
28+
function convolve_BC_left!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator; overwrite = true) where {T<:Real}
2929
stencil = A.low_boundary_coefs
3030
coeff = A.coefficients
3131
for i in 1 : A.boundary_point_count
@@ -36,11 +36,11 @@ function convolve_BC_left!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::D
3636
for idx in 2:A.boundary_stencil_length
3737
xtempi += cur_coeff * cur_stencil[idx] * x[idx]
3838
end
39-
x_temp[i] = xtempi
39+
x_temp[i] = xtempi + !overwrite*x_temp[i]
4040
end
4141
end
4242

43-
function convolve_BC_right!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator) where {T<:Real}
43+
function convolve_BC_right!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator; overwrite = true) where {T<:Real}
4444
stencil = A.high_boundary_coefs
4545
coeff = A.coefficients
4646
for i in 1 : A.boundary_point_count
@@ -51,14 +51,14 @@ function convolve_BC_right!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::
5151
for idx in (A.boundary_stencil_length-1):-1:1
5252
xtempi += cur_coeff * cur_stencil[end-idx] * x[end-idx]
5353
end
54-
x_temp[end-A.boundary_point_count+i] = xtempi
54+
x_temp[end-A.boundary_point_count+i] = xtempi + !overwrite*x_temp[end-A.boundary_point_count+i]
5555
end
5656
end
5757

5858
###########################################
5959

6060
# Against A BC-padded vector, specialize the computation to explicitly use the left, right, and middle parts
61-
function convolve_interior!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector, A::DerivativeOperator) where {T<:Real}
61+
function convolve_interior!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector, A::DerivativeOperator; overwrite = true) where {T<:Real}
6262
stencil = A.stencil_coefs
6363
coeff = A.coefficients
6464
x = _x.u
@@ -72,11 +72,11 @@ function convolve_interior!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector,
7272
@inbounds for idx in 1:A.stencil_length
7373
xtempi += cur_coeff * cur_stencil[idx] * x[(i-1) - (mid-idx) + 1]
7474
end
75-
x_temp[i] = xtempi
75+
x_temp[i] = xtempi + !overwrite*x_temp[i]
7676
end
7777
end
7878

79-
function convolve_BC_left!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector, A::DerivativeOperator) where {T<:Real}
79+
function convolve_BC_left!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector, A::DerivativeOperator; overwrite = true) where {T<:Real}
8080
stencil = A.low_boundary_coefs
8181
coeff = A.coefficients
8282
for i in 1 : A.boundary_point_count
@@ -87,7 +87,7 @@ function convolve_BC_left!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector,
8787
@inbounds for idx in 2:A.boundary_stencil_length
8888
xtempi += cur_coeff * cur_stencil[idx] * _x.u[idx-1]
8989
end
90-
x_temp[i] = xtempi
90+
x_temp[i] = xtempi + !overwrite*x_temp[i]
9191
end
9292
# need to account for x.l in first interior
9393
mid = div(A.stencil_length,2) + 1
@@ -101,10 +101,10 @@ function convolve_BC_left!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector,
101101
@inbounds for idx in 2:A.stencil_length
102102
xtempi += cur_coeff * cur_stencil[idx] * x[(i-1) - (mid-idx) + 1]
103103
end
104-
x_temp[i] = xtempi
104+
x_temp[i] = xtempi + !overwrite*x_temp[i]
105105
end
106106

107-
function convolve_BC_right!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector, A::DerivativeOperator) where {T<:Real}
107+
function convolve_BC_right!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector, A::DerivativeOperator; overwrite = true) where {T<:Real}
108108
stencil = A.high_boundary_coefs
109109
coeff = A.coefficients
110110
bc_start = length(_x.u) - A.boundary_point_count
@@ -120,7 +120,7 @@ function convolve_BC_right!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector,
120120
@inbounds for idx in 1:A.stencil_length-1
121121
xtempi += cur_coeff * cur_stencil[idx] * x[(i-1) - (mid-idx) + 1]
122122
end
123-
x_temp[i] = xtempi
123+
x_temp[i] = xtempi + !overwrite*x_temp[i]
124124
for i in 1 : A.boundary_point_count
125125
cur_stencil = stencil[i]
126126
cur_coeff = typeof(coeff) <: AbstractVector ? coeff[bc_start + i] : coeff isa Number ? coeff : true
@@ -129,6 +129,25 @@ function convolve_BC_right!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector,
129129
@inbounds for idx in A.stencil_length:-1:1
130130
xtempi += cur_coeff * cur_stencil[end-idx] * _x.u[end-idx+1]
131131
end
132-
x_temp[bc_start + i] = xtempi
132+
x_temp[bc_start + i] = xtempi + !overwrite*x_temp[bc_start + i]
133+
end
134+
end
135+
136+
###########################################
137+
138+
function convolve_interior_add_range!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator, offset::Int) where {T<:Real}
139+
@assert length(x_temp)+2 == length(x)
140+
stencil = A.stencil_coefs
141+
coeff = A.coefficients
142+
mid = div(A.stencil_length,2)
143+
for i in [(1+A.boundary_point_count):(A.boundary_point_count+offset); (length(x_temp)-A.boundary_point_count-offset+1):(length(x_temp)-A.boundary_point_count)]
144+
xtempi = zero(T)
145+
cur_stencil = eltype(stencil) <: AbstractVector ? stencil[i] : stencil
146+
cur_coeff = typeof(coeff) <: AbstractVector ? coeff[i] : coeff isa Number ? coeff : true
147+
cur_stencil = use_winding(A) && cur_coeff < 0 ? reverse(cur_stencil) : cur_stencil
148+
for idx in 1:A.stencil_length
149+
xtempi += cur_coeff * cur_stencil[idx] * x[i - mid + idx]
150+
end
151+
x_temp[i] += xtempi
133152
end
134153
end

src/derivative_operators/derivative_operator.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,4 @@ end
166166
CenteredDifference(args...) = CenteredDifference{1}(args...)
167167
UpwindDifference(args...) = UpwindDifference{1}(args...)
168168
use_winding(A::DerivativeOperator{T,N,Wind}) where {T,N,Wind} = Wind
169+
diff_axis(A::DerivativeOperator{T,N}) where {T,N} = N

0 commit comments

Comments
 (0)