Skip to content

fix: fix get_mtkparameters_reconstructor handling of nonnumerics #3796

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
40 changes: 32 additions & 8 deletions src/systems/problem_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,8 @@ end
$(TYPEDEF)

A callable struct which applies `p_constructor` to possibly nested arrays. It also
ensures that views (including nested ones) are concretized.
ensures that views (including nested ones) are concretized. This is implemented manually
of using `narrow_buffer_type` to preserve type-stability.
"""
struct PConstructorApplicator{F}
p_constructor::F
Expand All @@ -721,10 +722,18 @@ function (pca::PConstructorApplicator)(x::AbstractArray)
pca.p_constructor(x)
end

function (pca::PConstructorApplicator)(x::AbstractArray{Bool})
pca.p_constructor(BitArray(x))
end

function (pca::PConstructorApplicator{typeof(identity)})(x::SubArray)
collect(x)
end

function (pca::PConstructorApplicator{typeof(identity)})(x::SubArray{Bool})
BitArray(x)
end

function (pca::PConstructorApplicator{typeof(identity)})(x::SubArray{<:AbstractArray})
collect(pca.(x))
end
Expand All @@ -749,6 +758,7 @@ takes a value provider of `srcsys` and a value provider of `dstsys` and returns
"""
function get_mtkparameters_reconstructor(srcsys::AbstractSystem, dstsys::AbstractSystem;
initials = false, unwrap_initials = false, p_constructor = identity)
_p_constructor = p_constructor
p_constructor = PConstructorApplicator(p_constructor)
# if we call `getu` on this (and it were able to handle empty tuples) we get the
# fields of `MTKParameters` except caches.
Expand Down Expand Up @@ -802,14 +812,24 @@ function get_mtkparameters_reconstructor(srcsys::AbstractSystem, dstsys::Abstrac
Base.Fix1(broadcast, p_constructor) ∘
getu(srcsys, syms[3])
end
rest_getters = map(Base.tail(Base.tail(Base.tail(syms)))) do buf
if buf == ()
return Returns(())
else
return Base.Fix1(broadcast, p_constructor) ∘ getu(srcsys, buf)
end
const_getter = if syms[4] == ()
Returns(())
else
Base.Fix1(broadcast, p_constructor) ∘ getu(srcsys, syms[4])
end
getters = (tunable_getter, initials_getter, discs_getter, rest_getters...)
nonnumeric_getter = if syms[5] == ()
Returns(())
else
ic = get_index_cache(dstsys)
buftypes = Tuple(map(ic.nonnumeric_buffer_sizes) do bufsize
Vector{bufsize.type}
end)
# nonnumerics retain the assigned buffer type without narrowing
Base.Fix1(broadcast, _p_constructor) ∘
Base.Fix1(Broadcast.BroadcastFunction(call), buftypes) ∘ getu(srcsys, syms[5])
end
getters = (
tunable_getter, initials_getter, discs_getter, const_getter, nonnumeric_getter)
getter = let getters = getters
function _getter(valp, initprob)
oldcache = parameter_values(initprob).caches
Expand All @@ -822,6 +842,10 @@ function get_mtkparameters_reconstructor(srcsys::AbstractSystem, dstsys::Abstrac
return getter
end

function call(f, args...)
f(args...)
end

"""
$(TYPEDSIGNATURES)

Expand Down
20 changes: 20 additions & 0 deletions test/initializationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1670,3 +1670,23 @@ end
prob = ODEProblem(sys, [x[1] => nothing, x[2] => 1], (0.0, 1.0))
@test SciMLBase.initialization_status(prob) == SciMLBase.FULLY_DETERMINED
end

@testset "Nonnumerics aren't narrowed" begin
@mtkmodel Foo begin
@variables begin
x(t) = 1.0
end
@parameters begin
p::AbstractString
r = 1.0
end
@equations begin
D(x) ~ r * x
end
end
@mtkbuild sys = Foo(p = "a")
prob = ODEProblem(sys, [], (0.0, 1.0))
@test prob.p.nonnumeric[1] isa Vector{AbstractString}
integ = init(prob)
@test integ.p.nonnumeric[1] isa Vector{AbstractString}
end
Loading