diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index baa82d9b6f..5d1bae95ec 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -238,7 +238,8 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; :ODESystem, force = true) end defaults = todict(defaults) - defaults = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(defaults)) + defaults = Dict{Any, Any}(value(k) => value(v) + for (k, v) in pairs(defaults) if value(v) !== nothing) var_to_name = Dict() process_variables!(var_to_name, defaults, dvs′) process_variables!(var_to_name, defaults, ps′) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 5efdc2cb33..60070cf3ae 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -203,7 +203,8 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv :SDESystem, force = true) end defaults = todict(defaults) - defaults = Dict(value(k) => value(v) for (k, v) in pairs(defaults)) + defaults = Dict(value(k) => value(v) + for (k, v) in pairs(defaults) if value(v) !== nothing) var_to_name = Dict() process_variables!(var_to_name, defaults, dvs′) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 79c48af134..86d23acea0 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -149,7 +149,8 @@ function DiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; :DiscreteSystem, force = true) end defaults = todict(defaults) - defaults = Dict(value(k) => value(v) for (k, v) in pairs(defaults)) + defaults = Dict(value(k) => value(v) + for (k, v) in pairs(defaults) if value(v) !== nothing) var_to_name = Dict() process_variables!(var_to_name, defaults, dvs′) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index cdd5f1b2f8..536252fec4 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -176,7 +176,8 @@ function JumpSystem(eqs, iv, unknowns, ps; :JumpSystem, force = true) end defaults = todict(defaults) - defaults = Dict(value(k) => value(v) for (k, v) in pairs(defaults)) + defaults = Dict(value(k) => value(v) + for (k, v) in pairs(defaults) if value(v) !== nothing) unknowns, ps = value.(unknowns), value.(ps) var_to_name = Dict() diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 43d4bc8cc5..f854b28737 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -146,7 +146,8 @@ function NonlinearSystem(eqs, unknowns, ps; end jac = RefValue{Any}(EMPTY_JAC) defaults = todict(defaults) - defaults = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(defaults)) + defaults = Dict{Any, Any}(value(k) => value(v) + for (k, v) in pairs(defaults) if value(v) !== nothing) unknowns, ps = value.(unknowns), value.(ps) var_to_name = Dict() diff --git a/src/systems/optimization/constraints_system.jl b/src/systems/optimization/constraints_system.jl index 7fde00e2f4..afb5416aa5 100644 --- a/src/systems/optimization/constraints_system.jl +++ b/src/systems/optimization/constraints_system.jl @@ -133,7 +133,8 @@ function ConstraintsSystem(constraints, unknowns, ps; jac = RefValue{Any}(EMPTY_JAC) defaults = todict(defaults) - defaults = Dict(value(k) => value(v) for (k, v) in pairs(defaults)) + defaults = Dict(value(k) => value(v) + for (k, v) in pairs(defaults) if value(v) !== nothing) var_to_name = Dict() process_variables!(var_to_name, defaults, unknowns′) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 3a9f8aef74..21e82f15cc 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -111,7 +111,8 @@ function OptimizationSystem(op, unknowns, ps; throw(ArgumentError("System names must be unique.")) end defaults = todict(defaults) - defaults = Dict(value(k) => value(v) for (k, v) in pairs(defaults)) + defaults = Dict(value(k) => value(v) + for (k, v) in pairs(defaults) if value(v) !== nothing) var_to_name = Dict() process_variables!(var_to_name, defaults, unknowns′) diff --git a/src/utils.jl b/src/utils.jl index 86a28dcae6..5d6af76b77 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -252,7 +252,9 @@ end function collect_defaults!(defs, vars) for v in vars - (haskey(defs, v) || !hasdefault(unwrap(v))) && continue + if haskey(defs, v) || !hasdefault(unwrap(v)) || (def = getdefault(v)) === nothing + continue + end defs[v] = getdefault(v) end return defs diff --git a/test/initial_values.jl b/test/initial_values.jl index 3a7b48901d..4c2acd0aac 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -91,3 +91,20 @@ eqs = [D(D(z)) ~ ones(2, 2)] prob = ODEProblem(sys, [], (0.0, 1.0), [A1 => 0.3]) @test prob.ps[B1] == 0.3 @test prob.ps[B2] == 0.7 + +@testset "default=nothing is skipped" begin + @parameters p = nothing + @variables x(t)=nothing y(t) + for sys in [ + ODESystem(Equation[], t, [x, y], [p]; defaults = [y => nothing], name = :osys), + SDESystem(Equation[], [], t, [x, y], [p]; defaults = [y => nothing], name = :ssys), + JumpSystem(Equation[], t, [x, y], [p]; defaults = [y => nothing], name = :jsys), + NonlinearSystem(Equation[], [x, y], [p]; defaults = [y => nothing], name = :nsys), + OptimizationSystem( + Equation[], [x, y], [p]; defaults = [y => nothing], name = :optsys), + ConstraintsSystem( + Equation[], [x, y], [p]; defaults = [y => nothing], name = :conssys) + ] + @test isempty(ModelingToolkit.defaults(sys)) + end +end