Skip to content

Commit

Permalink
Merge pull request #16 from dingraha/master
Browse files Browse the repository at this point in the history
Make Akima complex stepable
  • Loading branch information
andrewning authored Apr 19, 2022
2 parents 647a7c6 + 3a9c055 commit 9dba266
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FLOWMath"
uuid = "6cb5d3fb-0fe8-4cc2-bd89-9fe0b19a99d3"
authors = ["Andrew Ning <[email protected]>"]
version = "0.3.1"
version = "0.3.2"

[deps]
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Expand Down
3 changes: 3 additions & 0 deletions src/FLOWMath.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module FLOWMath

include("cs_safe.jl")
export abs_cs_safe

include("quadrature.jl")
export trapz

Expand Down
7 changes: 7 additions & 0 deletions src/cs_safe.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function abs_cs_safe(x::T) where {T<:Complex}
return x*sign(real(x))
end

function abs_cs_safe(x)
return abs(x)
end
4 changes: 2 additions & 2 deletions src/interpolate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ between xvec[i] and xvec[i+1]
function findindex(xvec, x)

n = length(xvec)
i = searchsortedlast(xvec, x)
i = searchsortedlast(real(xvec), real(x))

# this version allows extrapolation
if i == 0
Expand Down Expand Up @@ -93,7 +93,7 @@ function Akima(xdata::AbstractVector{TFX}, ydata::AbstractVector{TFY}, delta_x=0
m4 = m[i+1]
w1 = abs_smooth(m4 - m3, delta_x)
w2 = abs_smooth(m2 - m1, delta_x)
if (w1 < eps && w2 < eps)
if (real(w1) < eps && real(w2) < eps)
t[i] = 0.5*(m2 + m3) # special case to avoid divide by zero
else
t[i] = (w1*m2 + w2*m3) / (w1 + w2)
Expand Down
4 changes: 2 additions & 2 deletions src/smooth.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Typically usage is with gradient-based optimization.
"""
function abs_smooth(x, delta_x)

if x < delta_x && x > -delta_x
if real(x) < delta_x && real(x) > -delta_x
return x^2/(2*delta_x) + delta_x/2
else
return abs(x)
return abs_cs_safe(x)
end

end
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,17 @@ J = ForwardDiff.jacobian(wrapper, y)
J2 = FiniteDiff.finite_difference_jacobian(wrapper, y)
@test maximum(abs.(J - J2)) < 1e-6

J2 = FiniteDiff.finite_difference_jacobian(wrapper, y, Val(:complex))
@test maximum(abs.(J - J2)) < 1e-12

wrapper2(y) = Akima(x, y, 0.1)(xpt)
J = ForwardDiff.jacobian(wrapper2, x)
J2 = FiniteDiff.finite_difference_jacobian(wrapper2, x)
@test maximum(abs.(J - J2)) < 1e-6

J2 = FiniteDiff.finite_difference_jacobian(wrapper2, x, Val(:complex))
@test maximum(abs.(J - J2)) < 1e-12

# ---------------------------

# ----- 1D linear interpolation ------
Expand Down

2 comments on commit 9dba266

@andrewning
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/58764

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.2 -m "<description of version>" 9dba266c242acd239033e01fdb48ce922c09e668
git push origin v0.3.2

Please sign in to comment.