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: validate u0map and pmap #3350

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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: 4 additions & 2 deletions src/systems/parameter_buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ function MTKParameters(
else
error("Cannot create MTKParameters if system does not have index_cache")
end

all_ps = Set(unwrap.(parameters(sys)))
union!(all_ps, default_toterm.(unwrap.(parameters(sys))))
if p isa Vector && !(eltype(p) <: Pair) && !isempty(p)
ps = parameters(sys)
length(p) == length(ps) || error("Invalid parameters")
length(p) == length(ps) || error("The number of parameter values is not equal to the number of parameters.")
p = ps .=> p
end
if p isa SciMLBase.NullParameters || isempty(p)
p = Dict()
end
p = todict(p)

defs = Dict(default_toterm(unwrap(k)) => v for (k, v) in defaults(sys))
if eltype(u0) <: Pair
u0 = todict(u0)
Expand Down Expand Up @@ -761,7 +763,7 @@ end

function Base.showerror(io::IO, e::MissingParametersError)
println(io, MISSING_PARAMETERS_MESSAGE)
println(io, e.vars)
println(io, join(e.vars, ", "))
end

function InvalidParameterSizeException(param, val)
Expand Down
42 changes: 40 additions & 2 deletions src/systems/problem_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -684,12 +684,17 @@ function process_SciMLProblem(

u0Type = typeof(u0map)
pType = typeof(pmap)
_u0map = u0map
Copy link
Contributor Author

Choose a reason for hiding this comment

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

_u0map and pmap seems to not be used anywhere


u0map = to_varmap(u0map, dvs)
symbols_to_symbolics!(sys, u0map)
_pmap = pmap
check_keys(sys, u0map)

pmap = to_varmap(pmap, ps)
symbols_to_symbolics!(sys, pmap)
check_keys(sys, pmap)
badkeys = filter(k -> symbolic_type(k) === NotSymbolic(), keys(pmap))
isempty(badkeys) || throw(BadKeyError(collect(badkeys)))

defs = add_toterms(recursive_unwrap(defaults(sys)))
cmap, cs = get_cmap(sys)
kwargs = NamedTuple(kwargs)
Expand Down Expand Up @@ -778,6 +783,39 @@ function process_SciMLProblem(
implicit_dae ? (f, du0, u0, p) : (f, u0, p)
end

# Check that the keys of a u0map or pmap are valid
# (i.e. are symbolic keys, and are defined for the system.)
function check_keys(sys, map)
badkeys = Any[]
for k in keys(map)
if symbolic_type(k) === NotSymbolic()
push!(badkeys, k)
elseif k isa Symbol
!hasproperty(sys, k) && push!(badkeys, k)
elseif k ∉ Set(parameters(sys)) && k ∉ Set(unknowns(sys))
push!(badkeys, k)
end
end

isempty(badkeys) || throw(BadKeyError(collect(badkeys)))
end

const BAD_KEY_MESSAGE = """
Undefined keys found in the parameter or initial condition maps.
The following keys are either invalid or not parameters/states of the system:
"""

struct BadKeyError <: Exception
vars::Any
end

function Base.showerror(io::IO, e::BadKeyError)
println(io, BAD_KEY_MESSAGE)
println(io, join(e.vars, ", "))
end



##############
# Legacy functions for backward compatibility
##############
Expand Down
25 changes: 25 additions & 0 deletions test/problem_validation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D

@testset "Input map validation" begin
@variables X(t)
@parameters p d
eqs = [D(X) ~ p - d*X]
@mtkbuild osys = ODESystem(eqs, t)

p = "I accidentally renamed p"
u0 = [X => 1.0]
ps = [p => 1.0, d => 0.5]
@test_throws ModelingToolkit.BadKeyError oprob = ODEProblem(osys, u0, (0.0, 1.0), ps)

@parameters p d
ps = [p => 1.0, d => 0.5, "Random stuff" => 3.0]
@test_throws ModelingToolkit.BadKeyError oprob = ODEProblem(osys, u0, (0.0, 1.0), ps)

u0 = [:X => 1.0, "random" => 3.0]
@test_throws ModelingToolkit.BadKeyError oprob = ODEProblem(osys, u0, (0.0, 1.0), ps)

@parameters k
ps = [p => 1., d => 0.5, k => 3.]
@test_throws ModelingToolkit.BadKeyError oprob = ODEProblem(osys, u0, (0.0, 1.0), ps)
end
Loading