-
-
Notifications
You must be signed in to change notification settings - Fork 74
(WIP) Safer handling of DerivativeOperator.coefficients #244
base: master
Are you sure you want to change the base?
Changes from 8 commits
6a228b5
7530b13
965bea4
5c24d99
fcdf775
f80886e
514a99b
c0b85cd
db02dba
2d173b5
10850b5
edd7abd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
``` | ||
init_coefficients(coeff_func, len::Int) | ||
``` | ||
|
||
Return the initial value of an operator's `coefficients` field based on the type | ||
of `coeff_func`. | ||
""" | ||
init_coefficients(coeff_func::Nothing, len::Int) = nothing | ||
|
||
init_coefficients(coeff_func::Number, len::Int) = coeff_func * ones(typeof(coeff_func), len) | ||
|
||
function init_coefficients(coeff_func::AbstractVector{T}, len::Int) where T <: Number | ||
coeff_func | ||
end | ||
|
||
init_coefficients(coeff_func::Function, len::Int) = ones(Float64, len) | ||
|
||
|
||
|
||
""" | ||
``` | ||
get_coefficient(coefficients, index) | ||
``` | ||
""" | ||
get_coefficient(coefficients::AbstractVector, index::Int) = coeff[i] | ||
|
||
# FIXME: I don't think this case is used anymore | ||
get_coefficient(coefficients::Number, index::Int) = coefficients | ||
|
||
# FIXME: Why use "true" here for the value 1? | ||
get_coefficient(coefficients::Nothing, index::Int) = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ function Base.copyto!(L::AbstractMatrix{T}, A::DerivativeOperator{T}, N::Int) wh | |
|
||
coeff = A.coefficients | ||
get_coeff = if coeff isa AbstractVector | ||
i -> coeff[i] | ||
i = get_coefficient(coeff, i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you reference the variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed te |
||
elseif coeff isa Number | ||
i -> coeff | ||
else | ||
|
@@ -314,7 +314,7 @@ function LinearAlgebra.Array(A::DerivativeOperator{T,N,true}, len::Int=A.len) wh | |
stencils = A.stencil_coefs | ||
|
||
for i in 1:bpc | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
if cur_coeff >= 0 | ||
cur_stencil = stencils | ||
L[i,i+1:i+stl] = cur_coeff*cur_stencil | ||
|
@@ -325,7 +325,7 @@ function LinearAlgebra.Array(A::DerivativeOperator{T,N,true}, len::Int=A.len) wh | |
end | ||
|
||
for i in bpc+1:len-bpc | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
cur_stencil = stencils | ||
cur_stencil = cur_coeff >= 0 ? cur_stencil : ((-1)^A.derivative_order)*reverse(cur_stencil) | ||
if cur_coeff >= 0 | ||
|
@@ -336,7 +336,7 @@ function LinearAlgebra.Array(A::DerivativeOperator{T,N,true}, len::Int=A.len) wh | |
end | ||
|
||
for i in len-bpc+1:len | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very clean. Beautiful. |
||
if cur_coeff < 0 | ||
cur_stencil = stencils | ||
cur_stencil = ((-1)^A.derivative_order)*reverse(cur_stencil) | ||
|
@@ -358,7 +358,7 @@ function LinearAlgebra.Array(A::DerivativeOperator{T,N,true,M}, len::Int=A.len) | |
coeff = A.coefficients | ||
|
||
for i in 1:bpc | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
if cur_coeff >= 0 | ||
L[i,i+1:i+stl] = cur_coeff * A.low_boundary_coefs[1,i] | ||
else | ||
|
@@ -367,7 +367,7 @@ function LinearAlgebra.Array(A::DerivativeOperator{T,N,true,M}, len::Int=A.len) | |
end | ||
|
||
for i in bpc+1:len-bpc | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
if cur_coeff >= 0 | ||
L[i,i+1:i+stl] = cur_coeff * A.stencil_coefs[1,i-bpc] | ||
else | ||
|
@@ -376,7 +376,7 @@ function LinearAlgebra.Array(A::DerivativeOperator{T,N,true,M}, len::Int=A.len) | |
end | ||
|
||
for i in len-bpc+1:len | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
if cur_coeff < 0 | ||
L[i,i-stl+2:i+1] = cur_coeff * A.high_boundary_coefs[2,i-len+bpc] | ||
else | ||
|
@@ -403,7 +403,7 @@ function SparseArrays.SparseMatrixCSC(A::DerivativeOperator{T,N,true}, len::Int= | |
stencils = A.stencil_coefs | ||
|
||
for i in 1:bpc | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
if cur_coeff >= 0 | ||
cur_stencil = stencils | ||
L[i,i+1:i+stl] = cur_coeff*cur_stencil | ||
|
@@ -414,7 +414,7 @@ function SparseArrays.SparseMatrixCSC(A::DerivativeOperator{T,N,true}, len::Int= | |
end | ||
|
||
for i in bpc+1:len-bpc | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
cur_stencil = stencils | ||
cur_stencil = cur_coeff >= 0 ? cur_stencil : ((-1)^A.derivative_order)*reverse(cur_stencil) | ||
if cur_coeff >= 0 | ||
|
@@ -425,7 +425,7 @@ function SparseArrays.SparseMatrixCSC(A::DerivativeOperator{T,N,true}, len::Int= | |
end | ||
|
||
for i in len-bpc+1:len | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
if cur_coeff < 0 | ||
cur_stencil = stencils | ||
cur_stencil = ((-1)^A.derivative_order)*reverse(cur_stencil) | ||
|
@@ -447,7 +447,7 @@ function SparseArrays.SparseMatrixCSC(A::DerivativeOperator{T,N,true,M}, len::In | |
coeff = A.coefficients | ||
|
||
for i in 1:bpc | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
if cur_coeff >= 0 | ||
L[i,i+1:i+stl] = cur_coeff * A.low_boundary_coefs[1,i] | ||
else | ||
|
@@ -456,7 +456,7 @@ function SparseArrays.SparseMatrixCSC(A::DerivativeOperator{T,N,true,M}, len::In | |
end | ||
|
||
for i in bpc+1:len-bpc | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
if cur_coeff >= 0 | ||
L[i,i+1:i+stl] = cur_coeff * A.stencil_coefs[1,i-bpc] | ||
else | ||
|
@@ -465,7 +465,7 @@ function SparseArrays.SparseMatrixCSC(A::DerivativeOperator{T,N,true,M}, len::In | |
end | ||
|
||
for i in len-bpc+1:len | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
if cur_coeff < 0 | ||
L[i,i-stl+2:i+1] = cur_coeff * A.high_boundary_coefs[2,i-len+bpc] | ||
else | ||
|
@@ -492,7 +492,7 @@ function BandedMatrices.BandedMatrix(A::DerivativeOperator{T,N,true}, len::Int=A | |
stencils = A.stencil_coefs | ||
|
||
for i in 1:bpc | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
if cur_coeff >= 0 | ||
cur_stencil = stencils | ||
L[i,i+1:i+stl] = cur_coeff*cur_stencil | ||
|
@@ -503,7 +503,7 @@ function BandedMatrices.BandedMatrix(A::DerivativeOperator{T,N,true}, len::Int=A | |
end | ||
|
||
for i in bpc+1:len-bpc | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
cur_stencil = stencils | ||
cur_stencil = cur_coeff >= 0 ? cur_stencil : ((-1)^A.derivative_order)*reverse(cur_stencil) | ||
if cur_coeff >= 0 | ||
|
@@ -514,7 +514,7 @@ function BandedMatrices.BandedMatrix(A::DerivativeOperator{T,N,true}, len::Int=A | |
end | ||
|
||
for i in len-bpc+1:len | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
if cur_coeff < 0 | ||
cur_stencil = stencils | ||
cur_stencil = ((-1)^A.derivative_order)*reverse(cur_stencil) | ||
|
@@ -537,7 +537,7 @@ function BandedMatrices.BandedMatrix(A::DerivativeOperator{T,N,true,M}, len::Int | |
L = BandedMatrix{T}(Zeros(len, len+2), (stl-2, stl)) | ||
|
||
for i in 1:bpc | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
if cur_coeff >= 0 | ||
L[i,i+1:i+stl] = cur_coeff * A.low_boundary_coefs[1,i] | ||
else | ||
|
@@ -546,7 +546,7 @@ function BandedMatrices.BandedMatrix(A::DerivativeOperator{T,N,true,M}, len::Int | |
end | ||
|
||
for i in bpc+1:len-bpc | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
if cur_coeff >= 0 | ||
L[i,i+1:i+stl] = cur_coeff * A.stencil_coefs[1,i-bpc] | ||
else | ||
|
@@ -555,7 +555,7 @@ function BandedMatrices.BandedMatrix(A::DerivativeOperator{T,N,true,M}, len::Int | |
end | ||
|
||
for i in len-bpc+1:len | ||
cur_coeff = coeff[i] | ||
cur_coeff = get_coefficient(coeff, i) | ||
if cur_coeff < 0 | ||
L[i,i-stl+2:i+1] = cur_coeff * A.high_boundary_coefs[2,i-len+bpc] | ||
else | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This case was covered by a subset of convolution functions, but I don't know what the use-case is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's for a constant coefficient, like D*A so a constant diffusion. If the coefficient is a number, then the whole setup is actually a bitstype so it'll compile to the GPU and distribute much better, and this is a common case so it's worth supporting. I think that one function is really all that's necessary to support it.