Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unwanted type promotion in InternalITP #925

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,26 @@ end

# Use a generated function for type stability even when many callbacks are given
@inline function find_first_continuous_callback(integrator,
callbacks::Vararg{
AbstractContinuousCallback,
N}) where {N}
callbacks::Vararg{
AbstractContinuousCallback,
N}) where {N}
find_first_continuous_callback(integrator, tuple(callbacks...))
end
@generated function find_first_continuous_callback(integrator,
callbacks::NTuple{N,
AbstractContinuousCallback
}) where {N}
callbacks::NTuple{N,
AbstractContinuousCallback,
}) where {N}
ex = quote
tmin, upcrossing, event_occurred, event_idx = find_callback_time(integrator,
callbacks[1], 1)
callbacks[1], 1)
identified_idx = 1
end
for i in 2:N
ex = quote
$ex
tmin2, upcrossing2, event_occurred2, event_idx2 = find_callback_time(integrator,
callbacks[$i],
$i)
callbacks[$i],
$i)
if event_occurred2 && (tmin2 < tmin || !event_occurred)
tmin = tmin2
upcrossing = upcrossing2
Expand Down
18 changes: 9 additions & 9 deletions src/internal_falsi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem, alg::InternalFalsi, arg

if iszero(fr)
return SciMLBase.build_solution(prob, alg, right, fr;
retcode = ReturnCode.ExactSolutionLeft, left = left,
right = right)
retcode = ReturnCode.ExactSolutionLeft, left = left,
right = right)
end

i = 1
Expand Down Expand Up @@ -129,7 +129,7 @@ function scalar_nlsolve_ad(prob, alg::InternalFalsi, args...; kwargs...)
end

function SciMLBase.solve(prob::IntervalNonlinearProblem{uType, iip,
<:ForwardDiff.Dual{T, V, P}},
<:ForwardDiff.Dual{T, V, P}},
alg::InternalFalsi, args...;
kwargs...) where {uType, iip, T, V, P}
sol, partials = scalar_nlsolve_ad(prob, alg, args...; kwargs...)
Expand All @@ -140,15 +140,15 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem{uType, iip,
end

function SciMLBase.solve(prob::IntervalNonlinearProblem{uType, iip,
<:AbstractArray{
<:ForwardDiff.Dual{T,
V,
P},
}},
<:AbstractArray{
<:ForwardDiff.Dual{T,
V,
P},
}},
alg::InternalFalsi, args...;
kwargs...) where {uType, iip, T, V, P}
sol, partials = scalar_nlsolve_ad(prob, alg, args...; kwargs...)

return SciMLBase.build_solution(prob, alg, ForwardDiff.Dual{T, V, P}(sol.u, partials),
sol.resid; retcode = sol.retcode,
left = ForwardDiff.Dual{T, V, P}(sol.left, partials),
Expand Down
31 changes: 16 additions & 15 deletions src/internal_itp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
`InternalITP`: A non-allocating ITP method, internal to DiffEqBase for
simpler dependencies.
"""
struct InternalITP
struct InternalITP
k1::Float64
k2::Float64
n0::Int
end

InternalITP() = InternalITP(0.007, 1.5, 10)

function SciMLBase.solve(prob::IntervalNonlinearProblem{IP, Tuple{T,T}}, alg::InternalITP, args...;
function SciMLBase.solve(prob::IntervalNonlinearProblem{IP, Tuple{T, T}}, alg::InternalITP,
args...;
maxiters = 1000, kwargs...) where {IP, T}
f = Base.Fix2(prob.f, prob.p)
left, right = prob.tspan # a and b
Expand All @@ -26,9 +27,9 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem{IP, Tuple{T,T}}, alg::In
right = right)
end
#defining variables/cache
k1 = alg.k1
k2 = alg.k2
n0 = alg.n0
k1 = T(alg.k1)
k2 = T(alg.k2)
n0 = T(alg.n0)
n_h = ceil(log2(abs(right - left) / (2 * ϵ)))
mid = (left + right) / 2
x_f = (fr * left - fl * right) / (fr - fl)
Expand All @@ -46,7 +47,7 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem{IP, Tuple{T,T}}, alg::In
δ = k1 * (span^k2)

## Interpolation step ##
x_f = left + (right - left) * (fl/(fl - fr))
x_f = left + (right - left) * (fl / (fl - fr))

## Truncation step ##
σ = sign(mid - x_f)
Expand Down Expand Up @@ -79,8 +80,8 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem{IP, Tuple{T,T}}, alg::In
left = prevfloat_tdir(xp, prob.tspan...)
right = xp
return SciMLBase.build_solution(prob, alg, left, f(left);
retcode = ReturnCode.Success, left = left,
right = right)
retcode = ReturnCode.Success, left = left,
right = right)
end
i += 1
mid = (left + right) / 2
Expand Down Expand Up @@ -127,7 +128,7 @@ function scalar_nlsolve_ad(prob, alg::InternalITP, args...; kwargs...)
end

function SciMLBase.solve(prob::IntervalNonlinearProblem{uType, iip,
<:ForwardDiff.Dual{T, V, P}},
<:ForwardDiff.Dual{T, V, P}},
alg::InternalITP, args...;
kwargs...) where {uType, iip, T, V, P}
sol, partials = scalar_nlsolve_ad(prob, alg, args...; kwargs...)
Expand All @@ -138,15 +139,15 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem{uType, iip,
end

function SciMLBase.solve(prob::IntervalNonlinearProblem{uType, iip,
<:AbstractArray{
<:ForwardDiff.Dual{T,
V,
P},
}},
<:AbstractArray{
<:ForwardDiff.Dual{T,
V,
P},
}},
alg::InternalITP, args...;
kwargs...) where {uType, iip, T, V, P}
sol, partials = scalar_nlsolve_ad(prob, alg, args...; kwargs...)

return SciMLBase.build_solution(prob, alg, ForwardDiff.Dual{T, V, P}(sol.u, partials),
sol.resid; retcode = sol.retcode,
left = ForwardDiff.Dual{T, V, P}(sol.left, partials),
Expand Down
10 changes: 6 additions & 4 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
be double checked.
"""

struct NoiseSizeIncompatabilityError <: Exception
struct NoiseSizeIncompatabilityError <: Exception
prototypesize::Int
noisesize::Int
end
Expand Down Expand Up @@ -1025,7 +1025,7 @@
end
end
function solve(prob::SciMLBase.WeightedEnsembleProblem, args...; kwargs...)
SciMLBase.WeightedEnsembleSolution(solve(prob.ensembleprob), prob.weights)
SciMLBase.WeightedEnsembleSolution(solve(prob.ensembleprob), prob.weights)

Check warning on line 1028 in src/solve.jl

View check run for this annotation

Codecov / codecov/patch

src/solve.jl#L1028

Added line #L1028 was not covered by tests
end
function solve(prob::AbstractNoiseProblem, args...; kwargs...)
__solve(prob, args...; kwargs...)
Expand Down Expand Up @@ -1307,8 +1307,10 @@
end

if prob isa SDEProblem && prob.noise_rate_prototype !== nothing &&
prob.noise !== nothing && size(prob.noise_rate_prototype,2) != length(prob.noise.W[1])
throw(NoiseSizeIncompatabilityError(size(prob.noise_rate_prototype,2), length(prob.noise.W[1])))
prob.noise !== nothing &&
size(prob.noise_rate_prototype, 2) != length(prob.noise.W[1])
throw(NoiseSizeIncompatabilityError(size(prob.noise_rate_prototype, 2),
length(prob.noise.W[1])))
end

# Complex number support comes before arbitrary number support for a more direct
Expand Down
34 changes: 17 additions & 17 deletions test/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ struct EmptyIntegrator
u::Vector{Float64}
end
function DiffEqBase.find_callback_time(integrator::EmptyIntegrator,
callback::ContinuousCallback, counter)
callback::ContinuousCallback, counter)
1.0 + counter, 0.9 + counter, true, counter
end
function DiffEqBase.find_callback_time(integrator::EmptyIntegrator,
callback::VectorContinuousCallback, counter)
callback::VectorContinuousCallback, counter)
1.0 + counter, 0.9 + counter, true, counter
end
find_first_integrator = EmptyIntegrator([1.0, 2.0])
Expand All @@ -82,21 +82,21 @@ cond_9(u, t, integrator) = t - 1.8
cond_10(u, t, integrator) = t - 1.9
# Setup a lot of callbacks so the recursive inference failure happens
callbacks = (ContinuousCallback(cond_1, affect!),
ContinuousCallback(cond_2, affect!),
ContinuousCallback(cond_3, affect!),
ContinuousCallback(cond_4, affect!),
ContinuousCallback(cond_5, affect!),
ContinuousCallback(cond_6, affect!),
ContinuousCallback(cond_7, affect!),
ContinuousCallback(cond_8, affect!),
ContinuousCallback(cond_9, affect!),
ContinuousCallback(cond_10, affect!),
VectorContinuousCallback(cond_1, vector_affect!, 2),
VectorContinuousCallback(cond_2, vector_affect!, 2),
VectorContinuousCallback(cond_3, vector_affect!, 2),
VectorContinuousCallback(cond_4, vector_affect!, 2),
VectorContinuousCallback(cond_5, vector_affect!, 2),
VectorContinuousCallback(cond_6, vector_affect!, 2));
ContinuousCallback(cond_2, affect!),
ContinuousCallback(cond_3, affect!),
ContinuousCallback(cond_4, affect!),
ContinuousCallback(cond_5, affect!),
ContinuousCallback(cond_6, affect!),
ContinuousCallback(cond_7, affect!),
ContinuousCallback(cond_8, affect!),
ContinuousCallback(cond_9, affect!),
ContinuousCallback(cond_10, affect!),
VectorContinuousCallback(cond_1, vector_affect!, 2),
VectorContinuousCallback(cond_2, vector_affect!, 2),
VectorContinuousCallback(cond_3, vector_affect!, 2),
VectorContinuousCallback(cond_4, vector_affect!, 2),
VectorContinuousCallback(cond_5, vector_affect!, 2),
VectorContinuousCallback(cond_6, vector_affect!, 2));
function test_find_first_callback(callbacks, int)
@timed(DiffEqBase.find_first_continuous_callback(int, callbacks...))
end
Expand Down
7 changes: 6 additions & 1 deletion test/downstream/solve_error_handling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@ function g(du, u, p, t)
du[2, 4] = 1.8u[2]
end

prob = SDEProblem(f, g, randn(ComplexF64,2), (0.0, 1.0), noise_rate_prototype =complex(zeros(2, 4)),noise=StochasticDiffEq.RealWienerProcess(0.0,zeros(3)))
prob = SDEProblem(f,
g,
randn(ComplexF64, 2),
(0.0, 1.0),
noise_rate_prototype = complex(zeros(2, 4)),
noise = StochasticDiffEq.RealWienerProcess(0.0, zeros(3)))
@test_throws DiffEqBase.NoiseSizeIncompatabilityError solve(prob, LambaEM())
3 changes: 2 additions & 1 deletion test/forwarddiff_dual_detection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ p_possibilities17 = [
(Mod, ForwardDiff.Dual(2.0)), (() -> 2.0, ForwardDiff.Dual(2.0)),
(Base.pointer([2.0]), ForwardDiff.Dual(2.0)),
]
VERSION >= v"1.7" && push!(p_possibilities17, Returns((a = 2, b = 1.3, c = ForwardDiff.Dual(2.0f0))))
VERSION >= v"1.7" &&
push!(p_possibilities17, Returns((a = 2, b = 1.3, c = ForwardDiff.Dual(2.0f0))))

for p in p_possibilities17
@show p
Expand Down
4 changes: 2 additions & 2 deletions test/internal_rootfinder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ for Rootfinder in (InternalFalsi, InternalITP)

# https://github.com/SciML/DiffEqBase.jl/issues/916
inp = IntervalNonlinearProblem((t, p) -> min(-1.0 + 0.001427344607477125 * t, 1e-9),
(699.0079267259368, 700.6176418816023))
(699.0079267259368, 700.6176418816023))
@test solve(inp, rf).u ≈ 700.6016590257979

# Flipped signs & reversed tspan test for bracketing algorithms
Expand All @@ -36,4 +36,4 @@ for Rootfinder in (InternalFalsi, InternalITP)
@test abs.(solve(inp3, rf).u) ≈ sqrt.(p)
@test abs.(solve(inp4, rf).u) ≈ sqrt.(p)
end
end
end
Loading