Skip to content

Commit

Permalink
Deprecate Ferrite.value and Ferrite.derivative (#720)
Browse files Browse the repository at this point in the history
This patch deprecates the (internal) functions
`Ferrite.value(::Interpolation, ::Vec)` and
`Ferrite.derivative(::Interpolation, ::Vec)`. After #719 they are not
used internally. It is usually best to not allocate this vector at all
and instead request the value/gradient for each specific shape function.
  • Loading branch information
fredrikekre authored May 22, 2023
1 parent ba7c7b7 commit 240cd94
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 27 deletions.
3 changes: 3 additions & 0 deletions src/deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,6 @@ end
function Base.show(io::IO, ::CrouzeixRaviart{shape, order}) where {shape, order}
print(io, "CrouzeixRaviart{$(shape), $(order)}()")
end

@deprecate value(ip::Interpolation, ξ::Vec) [Ferrite.value(ip, i, ξ) for i in 1:getnbasefunctions(ip)] false
@deprecate derivative(ip::Interpolation, ξ::Vec) [Tensors.gradient(x -> Ferrite.value(ip, i, x), ξ) for i in 1:getnbasefunctions(ip)] false
21 changes: 0 additions & 21 deletions src/interpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,27 +176,6 @@ Return order of the interpolation.
"""
@inline getorder(::Interpolation{shape,order}) where {shape,order} = order

"""
Ferrite.value(ip::Interpolation, ξ::Vec)
Return a vector, of length [`getnbasefunctions(ip::Interpolation)`](@ref), with the value of each shape functions
of `ip`, evaluated in the reference coordinate `ξ`. This calls [`Ferrite.value(ip::Interpolation, i::Int, ξ::Vec)`](@ref), where `i`
is the shape function number, which each concrete interpolation should implement.
"""
function value(ip::InterpolationByDim{dim}, ξ::Vec{dim,T}) where {dim,T}
[value(ip, i, ξ) for i in 1:getnbasefunctions(ip)]
end

"""
Ferrite.derivative(ip::Interpolation, ξ::Vec)
Return a vector, of length [`getnbasefunctions(ip::Interpolation)`](@ref), with the derivative (w.r.t. the
reference coordinate) of each shape functions of `ip`, evaluated in the reference coordinate
`ξ`. This uses automatic differentiation and uses `ip`s implementation of [`Ferrite.value(ip::Interpolation, i::Int, ξ::Vec)`](@ref).
"""
function derivative(ip::InterpolationByDim{dim}, ξ::Vec{dim,T}) where {dim,T}
[gradient-> value(ip, i, ξ), ξ) for i in 1:getnbasefunctions(ip)]
end

function gradient_and_value(ip::Interpolation, i::Int, x::Vec)
return gradient-> value(ip, i, ξ), x, :all)
Expand Down
7 changes: 7 additions & 0 deletions test/test_deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@ end
@test (@test_deprecated r"RefTriangle" test_combo(FaceValues, 1, RefTetrahedron, (:legendre, 1), Lagrange{RefTriangle, 1}())) isa FaceValues
end

@testset "Ferrite.value and Ferrite.derivative" begin
ip = Lagrange{RefQuadrilateral, 1}()
ξ = zero(Vec{2})
@test (@test_deprecated Ferrite.value(ip, ξ)) == [Ferrite.value(ip, i, ξ) for i in 1:getnbasefunctions(ip)]
@test (@test_deprecated Ferrite.derivative(ip, ξ)) == [Tensors.gradient(x -> Ferrite.value(ip, i, x), ξ) for i in 1:getnbasefunctions(ip)]
end

end # testset deprecations
12 changes: 6 additions & 6 deletions test/test_interpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
# Check partition of unity at random point.
n_basefuncs = getnbasefunctions(interpolation)
x = rand(Tensor{1, ref_dim})
f = (x) -> Ferrite.value(interpolation, Tensor{1, ref_dim}(x))
f = (x) -> [Ferrite.value(interpolation, i, Tensor{1, ref_dim}(x)) for i in 1:n_basefuncs]
@test vec(ForwardDiff.jacobian(f, Array(x))')
reinterpret(Float64, Ferrite.derivative(interpolation, x))
@test sum(Ferrite.value(interpolation, x)) 1.0
reinterpret(Float64, [Tensors.gradient(y -> Ferrite.value(interpolation, i, y), x) for i in 1:n_basefuncs])
@test sum([Ferrite.value(interpolation, i, x) for i in 1:n_basefuncs]) 1.0

# Check if the important functions are consistent
coords = Ferrite.reference_coordinates(interpolation)
Expand Down Expand Up @@ -97,12 +97,12 @@

# Check for dirac delta property of interpolation
@testset "dirac delta property of dof $dof" for dof in 1:n_basefuncs
N_dof = Ferrite.value(interpolation, coords[dof])
for k in 1:n_basefuncs
N_dof = Ferrite.value(interpolation, k, coords[dof])
if k == dof
@test N_dof[k] 1.0
@test N_dof 1.0
else
@test N_dof[k] 0.0 atol=4eps(Float64) #broken=typeof(interpolation)==Lagrange{2, RefTetrahedron, 5}&&dof==4&&k==18
@test N_dof 0.0 atol=4eps(Float64) #broken=typeof(interpolation)==Lagrange{2, RefTetrahedron, 5}&&dof==4&&k==18
end
end
end
Expand Down

0 comments on commit 240cd94

Please sign in to comment.