Skip to content
Open
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
6 changes: 5 additions & 1 deletion lib/ModelingToolkitBase/src/systems/problem_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1113,8 +1113,12 @@ function (recon::MTKParametersReconstructor)(src, dst)
if !iszero(diffcache_buffer_idx)
@set! nonnumerics[diffcache_buffer_idx] = DiffCacheAllocatorAPIWrapper{ForwardDiff.valtype(eltype(initialvals))}.(nonnumerics[diffcache_buffer_idx])
end
# This `convert` exists because a `Real` discrete might get its value from an
# integer function of integer parameters/discretes. This ends up creating a
# `BlockedArray{Int, ...}` instead of a `BlockedArray{Float64, ...}`.
return MTKParameters(
tunablevals, initialvals, recon.discretes_fn(src),
tunablevals, initialvals,
convert(typeof(parameter_values(dst).discrete), recon.discretes_fn(src)),
recon.consts_fn(src), nonnumerics, oldcache isa Tuple{} ? () : copy.(oldcache)
)
end
Expand Down
20 changes: 20 additions & 0 deletions lib/ModelingToolkitBase/test/initial_values.jl
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,24 @@ if !@isdefined(ModelingToolkit)
sys′ = ModelingToolkitBase.subset_tunables(mtkcompile(sys), [])
@test_nowarn ODEProblem(sys′, [], (0.0, 1.0))
end

@testset "`Real` discrete obtained via `Int` in initialization" begin
@variables x(t)
@parameters a::Int b
@discretes d(t)
# `d` must be a discrete, it should be bound to an integer, and the
# initialization must be non-trivial.
@mtkcompile sys = System(
[D(x) ~ d + a + b], t;
bindings = [d => a],
continuous_events = ModelingToolkitBase.SymbolicContinuousCallback(
[x ~ 3], [d ~ Pre(d) + 1]; discrete_parameters = d
), initialization_eqs = [b^2 ~ 2a]
)
prob = ODEProblem(sys, [x => 1, a => 1], (0.0, 3.0))
# This would build a `BlockedVector{Int, ...}` for the discretes, since `d`
# is obtained via `a`. However, `prob.p` contains a `BlockedVector{Float64, ..}`
# and this type mismatch causes an error.
@test_nowarn solve(prob, Tsit5())
end
end
Loading