This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 74
(WIP) Safer handling of DerivativeOperator.coefficients #244
Open
ghost
wants to merge
12
commits into
SciML:master
Choose a base branch
from
dangirsh:fixup-coeff-func
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6a228b5
removing coefficient_functions.jl and the corresponding test
dangirsh 7530b13
whitespace
dangirsh 965bea4
remove references to coefficient_functions.jl and related test
dangirsh 5c24d99
initialize DerivativeOperator.coefficients to ones instead of undef when
dangirsh fcdf775
add function to initialize coefficients based on coeff_func
dangirsh f80886e
consolidate coefficient handling to coeffcients.jl
dangirsh 514a99b
forgot two convolution function in last commit
dangirsh c0b85cd
use get_coefficient in concretization routines
dangirsh db02dba
small fixes. tests passing
dangirsh 2d173b5
Merge branch 'master' into fixup-coeff-func
dangirsh 10850b5
Merge branch 'master' into fixup-coeff-func
dangirsh edd7abd
small fix in Base.copyto! in concretization.jl (credit simenhu)
dangirsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) = coefficients[index] | ||
|
||
# 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.
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,12 @@ function Base.copyto!(L::AbstractMatrix{T}, A::DerivativeOperator{T}, N::Int) wh | |
bstl = A.boundary_stencil_length | ||
|
||
coeff = A.coefficients | ||
get_coeff = if coeff isa AbstractVector | ||
i -> coeff[i] | ||
get_coeff(i) = if coeff isa AbstractVector | ||
get_coefficient(coeff, i) | ||
elseif coeff isa Number | ||
i -> coeff | ||
coeff | ||
else | ||
i -> true | ||
true | ||
end | ||
|
||
for i in 1:bl | ||
|
@@ -451,7 +451,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 | ||
|
@@ -462,7 +462,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 | ||
|
@@ -473,7 +473,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) | ||
|
@@ -495,7 +495,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 | ||
|
@@ -504,7 +504,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 | ||
|
@@ -513,7 +513,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 | ||
|
@@ -540,7 +540,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 | ||
|
@@ -551,7 +551,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 | ||
|
@@ -562,7 +562,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) | ||
|
@@ -584,7 +584,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 | ||
|
@@ -593,7 +593,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 | ||
|
@@ -602,7 +602,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 | ||
|
@@ -629,7 +629,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 | ||
|
@@ -640,7 +640,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 | ||
|
@@ -651,7 +651,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) | ||
|
@@ -674,7 +674,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 | ||
|
@@ -683,7 +683,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 | ||
|
@@ -692,7 +692,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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.