|
1 | 1 | # mul! done by convolutions |
2 | | -function LinearAlgebra.mul!(x_temp::AbstractVector{T}, A::Union{DerivativeOperator{T},UpwindOperator{T}}, x::AbstractVector{T}) where T<:Real |
| 2 | +function LinearAlgebra.mul!(x_temp::AbstractVector{T}, A::DerivativeOperator, x::AbstractVector{T}) where T<:Real |
3 | 3 | convolve_BC_left!(x_temp, x, A) |
4 | 4 | convolve_interior!(x_temp, x, A) |
5 | 5 | convolve_BC_right!(x_temp, x, A) |
|
9 | 9 | ################################################ |
10 | 10 |
|
11 | 11 | # Against a standard vector, assume already padded and just apply the stencil |
12 | | -function convolve_interior!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator{T,S}) where {T<:Real,S<:SVector} |
| 12 | +function convolve_interior!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator) where {T<:Real} |
13 | 13 | @assert length(x_temp)+2 == length(x) |
14 | | - coeffs = A.stencil_coefs |
| 14 | + stencil = A.stencil_coefs |
| 15 | + coeff = A.coefficients |
15 | 16 | mid = div(A.stencil_length,2) |
16 | 17 | for i in (1+A.boundary_point_count) : (length(x_temp)-A.boundary_point_count) |
17 | 18 | xtempi = zero(T) |
| 19 | + cur_stencil = eltype(stencil) <: AbstractVector ? stencil[i] : stencil |
| 20 | + cur_coeff = eltype(coeff) <: AbstractVector ? coeff[i] : true |
| 21 | + cur_stencil = cur_coeff < 0 && A.use_winding ? reverse(cur_stencil) : cur_stencil |
18 | 22 | for idx in 1:A.stencil_length |
19 | | - xtempi += coeffs[idx] * x[i - mid + idx] |
| 23 | + xtempi += cur_coeff * cur_stencil[idx] * x[i - mid + idx] |
20 | 24 | end |
21 | 25 | x_temp[i] = xtempi |
22 | 26 | end |
23 | 27 | end |
24 | 28 |
|
25 | | -function convolve_BC_left!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator{T,S}) where {T<:Real,S<:SVector} |
26 | | - coeffs = A.low_boundary_coefs |
| 29 | +function convolve_BC_left!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator) where {T<:Real} |
| 30 | + stencil = A.low_boundary_coefs |
| 31 | + coeff = A.coefficients |
27 | 32 | for i in 1 : A.boundary_point_count |
28 | | - xtempi = coeffs[i][1]*x[1] |
| 33 | + xtempi = stencil[i][1]*x[1] |
| 34 | + cur_stencil = stencil[i] |
| 35 | + cur_coeff = eltype(coeff) <: AbstractVector ? coeff[i] : true |
| 36 | + cur_stencil = cur_coeff < 0 && A.use_winding ? reverse(cur_stencil) : cur_stencil |
29 | 37 | for idx in 2:A.boundary_stencil_length |
30 | | - xtempi += coeffs[i][idx] * x[idx] |
| 38 | + xtempi += cur_coeff * cur_stencil[idx] * x[idx] |
31 | 39 | end |
32 | 40 | x_temp[i] = xtempi |
33 | 41 | end |
34 | 42 | end |
35 | 43 |
|
36 | | -function convolve_BC_right!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator{T,S}) where {T<:Real,S<:SVector} |
37 | | - coeffs = A.high_boundary_coefs |
| 44 | +function convolve_BC_right!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::DerivativeOperator) where {T<:Real} |
| 45 | + stencil = A.high_boundary_coefs |
| 46 | + coeff = A.coefficients |
38 | 47 | for i in 1 : A.boundary_point_count |
39 | | - xtempi = coeffs[i][end]*x[end] |
| 48 | + xtempi = stencil[i][end]*x[end] |
| 49 | + cur_stencil = stencil[i] |
| 50 | + cur_coeff = eltype(coeff) <: AbstractVector ? coeff[i] : true |
| 51 | + cur_stencil = cur_coeff < 0 && A.use_winding ? reverse(cur_stencil) : cur_stencil |
40 | 52 | for idx in (A.boundary_stencil_length-1):-1:1 |
41 | | - xtempi += coeffs[i][end-idx] * x[end-idx] |
| 53 | + xtempi += cur_coeff * cur_stencil[end-idx] * x[end-idx] |
42 | 54 | end |
43 | 55 | x_temp[end-A.boundary_point_count+i] = xtempi |
44 | 56 | end |
45 | 57 | end |
46 | 58 |
|
47 | 59 | ########################################### |
48 | | -# NOT NEEDED ANYMORE |
49 | 60 |
|
50 | 61 | # Against A BC-padded vector, specialize the computation to explicitly use the left, right, and middle parts |
51 | | -function convolve_interior!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector, A::DerivativeOperator{T,S}) where {T<:Real,S<:SVector} |
52 | | - coeffs = A.stencil_coefs |
| 62 | +function convolve_interior!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector, A::DerivativeOperator) where {T<:Real} |
| 63 | + stencil = A.stencil_coefs |
| 64 | + coeff = A.coefficients |
53 | 65 | x = _x.u |
54 | 66 | mid = div(A.stencil_length,2) + 1 |
55 | 67 | # Just do the middle parts |
56 | 68 | for i in (1+A.boundary_length) : (length(x_temp)-A.boundary_length) |
57 | 69 | xtempi = zero(T) |
| 70 | + cur_stencil = eltype(stencil) <: AbstractVector ? stencil[i-A.boundary_length] : stencil |
| 71 | + cur_coeff = eltype(coeff) <: AbstractVector ? coeff[i-A.boundary_length] : true |
| 72 | + cur_stencil = cur_coeff < 0 && A.use_winding ? reverse(cur_stencil) : cur_stencil |
58 | 73 | @inbounds for idx in 1:A.stencil_length |
59 | | - xtempi += coeffs[idx] * x[i - (mid-idx) + 1] |
| 74 | + xtempi += cur_coeff * cur_stencil[idx] * x[i - (mid-idx) + 1] |
60 | 75 | end |
61 | 76 | x_temp[i] = xtempi |
62 | 77 | end |
63 | 78 | end |
64 | 79 |
|
65 | | -function convolve_BC_left!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector, A::DerivativeOperator{T,S}) where {T<:Real,S<:SVector} |
66 | | - coeffs = A.low_boundary_coefs |
| 80 | +function convolve_BC_left!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector, A::DerivativeOperator) where {T<:Real} |
| 81 | + stencil = A.low_boundary_coefs |
| 82 | + coeff = A.coefficients |
67 | 83 | for i in 1 : A.boundary_length |
68 | | - xtempi = coeffs[i][1]*x.l |
| 84 | + xtempi = stencil[i][1]*x.l |
| 85 | + cur_stencil = stencil[i] |
| 86 | + cur_coeff = eltype(coeff) <: AbstractVector ? coeff[i-A.boundary_length] : true |
| 87 | + cur_stencil = cur_coeff < 0 && A.use_winding ? reverse(cur_stencil) : cur_stencil |
69 | 88 | @inbounds for idx in 2:A.stencil_length |
70 | | - xtempi += coeffs[i][idx] * x.u[idx-1] |
| 89 | + xtempi += cur_coeff * cur_stencil[idx] * x.u[idx-1] |
71 | 90 | end |
72 | 91 | x_temp[i] = xtempi |
73 | 92 | end |
74 | 93 | end |
75 | 94 |
|
76 | | -function convolve_BC_right!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector, A::DerivativeOperator{T,S}) where {T<:Real,S<:SVector} |
77 | | - coeffs = A.low_boundary_coefs |
| 95 | +function convolve_BC_right!(x_temp::AbstractVector{T}, _x::BoundaryPaddedVector, A::DerivativeOperator) where {T<:Real} |
| 96 | + stencil = A.low_boundary_coefs |
| 97 | + coeff = A.coefficients |
78 | 98 | bc_start = length(x.u) - A.stencil_length |
79 | 99 | for i in 1 : A.boundary_length |
80 | | - xtempi = coeffs[i][end]*x.r |
| 100 | + xtempi = stencil[i][end]*x.r |
| 101 | + cur_stencil = stencil[i] |
| 102 | + cur_coeff = eltype(coeff) <: AbstractVector ? coeff[i-A.boundary_length] : true |
| 103 | + cur_stencil = cur_coeff < 0 && A.use_winding ? reverse(cur_stencil) : cur_stencil |
81 | 104 | @inbounds for idx in A.stencil_length:-1:2 |
82 | | - xtempi += coeffs[i][end-idx] * x.u[end-idx+1] |
| 105 | + xtempi += cur_coeff * cur_stencil[end-idx] * x.u[end-idx+1] |
83 | 106 | end |
84 | 107 | x_temp[i] = xtempi |
85 | 108 | end |
86 | 109 | end |
87 | | - |
88 | | -########################################## |
89 | | - |
90 | | -#= INTERIOR CONVOLUTION =# |
91 | | -function convolve_interior!(x_temp::AbstractVector{T}, x::AbstractVector{T}, A::UpwindOperator{T,S,LBC,RBC}) where {T<:Real,S<:SVector,LBC,RBC} |
92 | | - N = length(x) |
93 | | - stencil_length = length(A.up_stencil_coefs) |
94 | | - stencil_rem = 1 - stencil_length%2 |
95 | | - Threads.@threads for i in A.boundary_point_count[1]+1 : N-A.boundary_point_count[2] |
96 | | - xtempi = zero(T) |
97 | | - if A.directions[][i] == false |
98 | | - @inbounds for j in 1 : length(A.up_stencil_coefs) |
99 | | - xtempi += A.up_stencil_coefs[j] * x[i+j-1-stencil_rem] |
100 | | - end |
101 | | - else |
102 | | - @inbounds for j in -length(A.down_stencil_coefs)+1 : 0 |
103 | | - xtempi += A.down_stencil_coefs[j+stencil_length] * x[i+j+stencil_rem] |
104 | | - # println("i = $i, j = $j, s_idx = $(j+stencil_length), x_idx = $(i+j+stencil_rem), $(A.down_stencil_coefs[j+stencil_length]) * $(x[i+j+stencil_rem]), xtempi = $xtempi") |
105 | | - end |
106 | | - end |
107 | | - |
108 | | - x_temp[i] = xtempi |
109 | | - end |
110 | | -end |
0 commit comments