From be21e4b3281af492eb72723fda8e455a94048e48 Mon Sep 17 00:00:00 2001 From: Vincent Du <54586336+vyudu@users.noreply.github.com> Date: Fri, 7 Mar 2025 16:20:00 -0500 Subject: [PATCH 01/13] fix: propagate `tofloat`, `use_union` to `better_varmap_to_vars` fix mergre conflict --- src/systems/problem_utils.jl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index e9b836ddf7..4b612e62c7 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -363,7 +363,7 @@ Keyword arguments: - `is_initializeprob, guesses`: Used to determine whether the system is missing guesses. """ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; - tofloat = true, use_union = true, container_type = Array, + tofloat = true, container_type = Array, toterm = default_toterm, promotetoconcrete = nothing, check = true, allow_symbolic = false, is_initializeprob = false) isempty(vars) && return nothing @@ -841,8 +841,13 @@ function process_SciMLProblem( evaluate_varmap!(op, dvs; limit = substitution_limit) u0 = better_varmap_to_vars( +<<<<<<< HEAD op, dvs; tofloat = true, use_union = false, container_type = u0Type, allow_symbolic = symbolic_u0, is_initializeprob) +======= + op, dvs; tofloat, use_union, + container_type = u0Type, allow_symbolic = symbolic_u0) +>>>>>>> e31ae1bcc9 (fix: propagate `tofloat`, `use_union` to `better_varmap_to_vars`) if u0 !== nothing u0 = u0_constructor(u0) @@ -875,7 +880,7 @@ function process_SciMLProblem( du0map = to_varmap(du0map, ddvs) merge!(op, du0map) du0 = varmap_to_vars(op, ddvs; toterm = identity, - tofloat = true) + tofloat) kwargs = merge(kwargs, (; ddvs)) else du0 = nothing From adf70fba23b70281a36a322ef4b407dc9007828c Mon Sep 17 00:00:00 2001 From: Vincent Du <54586336+vyudu@users.noreply.github.com> Date: Fri, 7 Mar 2025 16:24:43 -0500 Subject: [PATCH 02/13] test: add typecheck --- test/jumpsystem.jl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 0fd6dc4af0..7d525c871e 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -533,3 +533,21 @@ let Xsamp /= Nsims @test abs(Xsamp - Xf(0.2, p) < 0.05 * Xf(0.2, p)) end + +@testset "JumpProcess simulation should be Int64 valued (#3446)" begin + @parameters p d + @variables X(t) + rate1 = p + rate2 = X*d + affect1 = [X ~ X + 1] + affect2 = [X ~ X - 1] + j1 = ConstantRateJump(rate1, affect1) + j2 = ConstantRateJump(rate2, affect2) + + # Works. + @mtkbuild js = JumpSystem([j1, j2], t, [X], [p,d]) + dprob = DiscreteProblem(js, [X => 15], (0.0, 10.), [p => 2.0, d => 0.5]) + jprob = JumpProblem(js, dprob, Direct()) + sol = solve(jprob, SSAStepper()) + @test eltype(sol[X]) === Int64 +end From 4aa47d6cfb883ea72929050fc3a17ca9f26f2838 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 13 Mar 2025 15:23:55 -0400 Subject: [PATCH 03/13] format --- test/jumpsystem.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 7d525c871e..d77e37f516 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -537,16 +537,16 @@ end @testset "JumpProcess simulation should be Int64 valued (#3446)" begin @parameters p d @variables X(t) - rate1 = p - rate2 = X*d + rate1 = p + rate2 = X * d affect1 = [X ~ X + 1] affect2 = [X ~ X - 1] j1 = ConstantRateJump(rate1, affect1) j2 = ConstantRateJump(rate2, affect2) # Works. - @mtkbuild js = JumpSystem([j1, j2], t, [X], [p,d]) - dprob = DiscreteProblem(js, [X => 15], (0.0, 10.), [p => 2.0, d => 0.5]) + @mtkbuild js = JumpSystem([j1, j2], t, [X], [p, d]) + dprob = DiscreteProblem(js, [X => 15], (0.0, 10.0), [p => 2.0, d => 0.5]) jprob = JumpProblem(js, dprob, Direct()) sol = solve(jprob, SSAStepper()) @test eltype(sol[X]) === Int64 From fca98e08e0e3edf8229b1cee594bc0e98b8be6ba Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 14 Mar 2025 09:48:57 -0400 Subject: [PATCH 04/13] fix: don't propagate for u0 --- src/systems/problem_utils.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 4b612e62c7..334944d50a 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -351,11 +351,10 @@ in `varmap`. Does not perform symbolic substitution in the values of `varmap`. Keyword arguments: - `tofloat`: Convert values to floating point numbers using `float`. -- `use_union`: Use a `Union`-typed array if the values have heterogeneous types. - `container_type`: The type of container to use for the values. - `toterm`: The `toterm` method to use for converting symbolics. - `promotetoconcrete`: whether the promote to a concrete buffer (respecting - `tofloat` and `use_union`). Defaults to `container_type <: AbstractArray`. + `tofloat`). Defaults to `container_type <: AbstractArray`. - `check`: Error if any variables in `vars` do not have a mapping in `varmap`. Uses [`missingvars`](@ref) to perform the check. - `allow_symbolic` allows the returned array to contain symbolic values. If this is `true`, @@ -393,7 +392,7 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; promotetoconcrete === nothing && (promotetoconcrete = container_type <: AbstractArray) if promotetoconcrete && !allow_symbolic - vals = promote_to_concrete(vals; tofloat = tofloat, use_union = use_union) + vals = promote_to_concrete(vals; tofloat = tofloat, use_union = false) end if isempty(vals) @@ -841,11 +840,15 @@ function process_SciMLProblem( evaluate_varmap!(op, dvs; limit = substitution_limit) u0 = better_varmap_to_vars( +<<<<<<< HEAD <<<<<<< HEAD op, dvs; tofloat = true, use_union = false, container_type = u0Type, allow_symbolic = symbolic_u0, is_initializeprob) ======= op, dvs; tofloat, use_union, +======= + op, dvs; tofloat, use_union = false, +>>>>>>> 6951e652f2 (fix: don't propagate for u0) container_type = u0Type, allow_symbolic = symbolic_u0) >>>>>>> e31ae1bcc9 (fix: propagate `tofloat`, `use_union` to `better_varmap_to_vars`) From 9c959704162b4b3ec9b514db62230d70b08c3d16 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 17 Mar 2025 10:42:41 -0400 Subject: [PATCH 05/13] remove instances of --- .../discrete_system/discrete_system.jl | 1 - .../implicit_discrete_system.jl | 1 - src/systems/jumps/jumpsystem.jl | 11 ++----- .../optimization/optimizationsystem.jl | 14 ++++----- src/systems/parameter_buffer.jl | 2 +- src/systems/problem_utils.jl | 31 +++++++++++++------ src/variables.jl | 4 +-- test/odesystem.jl | 6 ++-- test/split_parameters.jl | 2 +- 9 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 5c7d77ec83..952c8b71b0 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -302,7 +302,6 @@ function SciMLBase.DiscreteProblem( parammap = SciMLBase.NullParameters(); eval_module = @__MODULE__, eval_expression = false, - use_union = false, kwargs... ) if !iscomplete(sys) diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index 327c82c47f..a9cddcef49 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -321,7 +321,6 @@ function SciMLBase.ImplicitDiscreteProblem( parammap = SciMLBase.NullParameters(); eval_module = @__MODULE__, eval_expression = false, - use_union = false, kwargs... ) if !iscomplete(sys) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 81f0e1371d..b95b035bf6 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -383,7 +383,6 @@ end ```julia DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan, parammap = DiffEqBase.NullParameters; - use_union = true, kwargs...) ``` @@ -403,7 +402,6 @@ dprob = DiscreteProblem(complete(js), u₀map, tspan, parammap) """ function DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothing}, parammap = DiffEqBase.NullParameters(); - use_union = true, eval_expression = false, eval_module = @__MODULE__, kwargs...) @@ -416,7 +414,7 @@ function DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, end _f, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; - t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, check_length = false, build_initializeprob = false) + t = tspan === nothing ? nothing : tspan[1], tofloat = false, check_length = false, build_initializeprob = false) f = DiffEqBase.DISCRETE_INPLACE_DEFAULT observedfun = ObservedFunctionCache( @@ -451,14 +449,13 @@ struct DiscreteProblemExpr{iip} end function DiscreteProblemExpr{iip}(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothing}, parammap = DiffEqBase.NullParameters(); - use_union = true, kwargs...) where {iip} if !iscomplete(sys) error("A completed `JumpSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblemExpr`") end _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; - t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, check_length = false) + t = tspan === nothing ? nothing : tspan[1], tofloat = false, check_length = false) # identity function to make syms works quote f = DiffEqBase.DISCRETE_INPLACE_DEFAULT @@ -475,7 +472,6 @@ end ```julia DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan, parammap = DiffEqBase.NullParameters; - use_union = true, kwargs...) ``` @@ -497,7 +493,6 @@ oprob = ODEProblem(complete(js), u₀map, tspan, parammap) """ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothing}, parammap = DiffEqBase.NullParameters(); - use_union = false, eval_expression = false, eval_module = @__MODULE__, kwargs...) @@ -517,7 +512,7 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi build_initializeprob = false, kwargs...) else _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; - t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, + t = tspan === nothing ? nothing : tspan[1], tofloat = false, check_length = false) f = (du, u, p, t) -> (du .= 0; nothing) observedfun = ObservedFunctionCache(sys; eval_expression, eval_module, diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index c1c2237852..4d01bd31c9 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -295,7 +295,6 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, cons_sparse = false, checkbounds = false, linenumbers = true, parallel = SerialForm(), eval_expression = false, eval_module = @__MODULE__, - use_union = false, checks = true, kwargs...) where {iip} if !iscomplete(sys) @@ -338,10 +337,10 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, elseif has_index_cache(sys) && get_index_cache(sys) !== nothing p = MTKParameters(sys, parammap, u0map) else - p = varmap_to_vars(parammap, ps; defaults = defs, tofloat = false, use_union) + p = varmap_to_vars(parammap, ps; defaults = defs, tofloat = false) end - lb = varmap_to_vars(dvs .=> lb, dvs; defaults = defs, tofloat = false, use_union) - ub = varmap_to_vars(dvs .=> ub, dvs; defaults = defs, tofloat = false, use_union) + lb = varmap_to_vars(dvs .=> lb, dvs; defaults = defs, tofloat = false) + ub = varmap_to_vars(dvs .=> ub, dvs; defaults = defs, tofloat = false) if !isnothing(lb) && all(lb .== -Inf) && !isnothing(ub) && all(ub .== Inf) lb = nothing @@ -538,7 +537,6 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0map, checkbounds = false, linenumbers = false, parallel = SerialForm(), eval_expression = false, eval_module = @__MODULE__, - use_union = false, kwargs...) where {iip} if !iscomplete(sys) error("A completed `OptimizationSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `OptimizationProblemExpr`") @@ -578,10 +576,10 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0map, if has_index_cache(sys) && get_index_cache(sys) !== nothing p = MTKParameters(sys, parammap, u0map) else - p = varmap_to_vars(parammap, ps; defaults = defs, tofloat = false, use_union) + p = varmap_to_vars(parammap, ps; defaults = defs, tofloat = false) end - lb = varmap_to_vars(dvs .=> lb, dvs; defaults = defs, tofloat = false, use_union) - ub = varmap_to_vars(dvs .=> ub, dvs; defaults = defs, tofloat = false, use_union) + lb = varmap_to_vars(dvs .=> lb, dvs; defaults = defs, tofloat = false) + ub = varmap_to_vars(dvs .=> ub, dvs; defaults = defs, tofloat = false) if !isnothing(lb) && all(lb .== -Inf) && !isnothing(ub) && all(ub .== Inf) lb = nothing diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index e132a5e596..4f1bd1a234 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -27,7 +27,7 @@ This requires that `complete` has been called on the system (usually via the default behavior). """ function MTKParameters( - sys::AbstractSystem, p, u0 = Dict(); tofloat = false, use_union = false, + sys::AbstractSystem, p, u0 = Dict(); tofloat = false, t0 = nothing, substitution_limit = 1000) ic = if has_index_cache(sys) && get_index_cache(sys) !== nothing get_index_cache(sys) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 334944d50a..a5d9b02dbb 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -363,7 +363,11 @@ Keyword arguments: """ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; tofloat = true, container_type = Array, +<<<<<<< HEAD toterm = default_toterm, promotetoconcrete = nothing, check = true, allow_symbolic = false, is_initializeprob = false) +======= + toterm = default_toterm, promotetoconcrete = nothing, check = true, allow_symbolic = false) +>>>>>>> a5e1e0239a (remove instances of) isempty(vars) && return nothing if check @@ -730,8 +734,12 @@ Keyword arguments: - `fully_determined`: Override whether the initialization system is fully determined. - `check_initialization_units`: Enable or disable unit checks when constructing the initialization problem. +<<<<<<< HEAD - `tofloat`, `use_union`, `is_initializeprob`: Passed to [`better_varmap_to_vars`](@ref) for building `u0` (and possibly `p`). +======= +- `tofloat`: Passed to [`better_varmap_to_vars`](@ref) for building `u0` (and possibly `p`). +>>>>>>> a5e1e0239a (remove instances of) - `u0_constructor`: A function to apply to the `u0` value returned from `better_varmap_to_vars` to construct the final `u0` value. - `du0map`: A map of derivatives to values. See `implicit_dae`. @@ -761,7 +769,7 @@ function process_SciMLProblem( implicit_dae = false, t = nothing, guesses = AnyDict(), warn_initialize_determined = true, initialization_eqs = [], eval_expression = false, eval_module = @__MODULE__, fully_determined = nothing, - check_initialization_units = false, tofloat = true, use_union = false, + check_initialization_units = false, tofloat = true, u0_constructor = identity, du0map = nothing, check_length = true, symbolic_u0 = false, warn_cyclic_dependency = false, circular_dependency_max_cycle_length = length(all_symbols(sys)), @@ -841,6 +849,7 @@ function process_SciMLProblem( u0 = better_varmap_to_vars( <<<<<<< HEAD +<<<<<<< HEAD <<<<<<< HEAD op, dvs; tofloat = true, use_union = false, container_type = u0Type, allow_symbolic = symbolic_u0, is_initializeprob) @@ -851,6 +860,9 @@ function process_SciMLProblem( >>>>>>> 6951e652f2 (fix: don't propagate for u0) container_type = u0Type, allow_symbolic = symbolic_u0) >>>>>>> e31ae1bcc9 (fix: propagate `tofloat`, `use_union` to `better_varmap_to_vars`) +======= + op, dvs; tofloat, container_type = u0Type, allow_symbolic = symbolic_u0) +>>>>>>> a5e1e0239a (remove instances of) if u0 !== nothing u0 = u0_constructor(u0) @@ -875,7 +887,7 @@ function process_SciMLProblem( if is_split(sys) p = MTKParameters(sys, op) else - p = better_varmap_to_vars(op, ps; tofloat, use_union, container_type = pType) + p = better_varmap_to_vars(op, ps; tofloat, container_type = pType) end if implicit_dae && du0map !== nothing @@ -944,7 +956,7 @@ end ############## """ - u0, p, defs = get_u0_p(sys, u0map, parammap; use_union=true, tofloat=true) + u0, p, defs = get_u0_p(sys, u0map, parammap; tofloat=true) Take dictionaries with initial conditions and parameters and convert them to numeric arrays `u0` and `p`. Also return the merged dictionary `defs` containing the entire operating point. """ @@ -952,7 +964,6 @@ function get_u0_p(sys, u0map, parammap = nothing; t0 = nothing, - use_union = true, tofloat = true, symbolic_u0 = false) dvs = unknowns(sys) @@ -991,11 +1002,11 @@ function get_u0_p(sys, end if symbolic_u0 - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = false, use_union = false) + u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = false) else - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true, use_union) + u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true) end - p = varmap_to_vars(parammap, ps; defaults = defs, tofloat, use_union) + p = varmap_to_vars(parammap, ps; defaults = defs, tofloat) p = p === nothing ? SciMLBase.NullParameters() : p t0 !== nothing && delete!(defs, get_iv(sys)) u0, p, defs @@ -1003,7 +1014,7 @@ end function get_u0( sys, u0map, parammap = nothing; symbolic_u0 = false, - toterm = default_toterm, t0 = nothing, use_union = true) + toterm = default_toterm, t0 = nothing) dvs = unknowns(sys) ps = parameters(sys) defs = defaults(sys) @@ -1026,9 +1037,9 @@ function get_u0( defs = mergedefaults(defs, obsmap, u0map, dvs) if symbolic_u0 u0 = varmap_to_vars( - u0map, dvs; defaults = defs, tofloat = false, use_union = false, toterm) + u0map, dvs; defaults = defs, tofloat = false, toterm) else - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true, use_union, toterm) + u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true, toterm) end t0 !== nothing && delete!(defs, get_iv(sys)) return u0, defs diff --git a/src/variables.jl b/src/variables.jl index 3f070fea3c..dae044f1ec 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -158,7 +158,7 @@ applicable. """ function varmap_to_vars(varmap, varlist; defaults = Dict(), check = true, toterm = default_toterm, promotetoconcrete = nothing, - tofloat = true, use_union = true) + tofloat = true) varlist = collect(map(unwrap, varlist)) # Edge cases where one of the arguments is effectively empty. @@ -194,7 +194,7 @@ function varmap_to_vars(varmap, varlist; defaults = Dict(), check = true, promotetoconcrete === nothing && (promotetoconcrete = container_type <: AbstractArray) if promotetoconcrete - vals = promote_to_concrete(vals; tofloat = tofloat, use_union = use_union) + vals = promote_to_concrete(vals; tofloat = tofloat, use_union = false) end if isempty(vals) diff --git a/test/odesystem.jl b/test/odesystem.jl index 970a9afaad..7c4c4e7ead 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -736,7 +736,7 @@ let pmap = [k1 => 1, k2 => 1] tspan = (0.0, 1.0) - prob = ODEProblem(sys, u0map, tspan, pmap) + prob = ODEProblem(sys, u0map, tspan, pmap); @test eltype(vcat(prob.p...)) === Float64 prob = ODEProblem(sys, u0map, tspan, pmap) @@ -745,7 +745,7 @@ let # No longer supported, Tuple used instead # pmap = Pair{Any, Union{Int, Float64}}[k1 => 1, k2 => 1.0] # tspan = (0.0, 1.0) - # prob = ODEProblem(sys, u0map, tspan, pmap, use_union = true) + # prob = ODEProblem(sys, u0map, tspan, pmap) # @test eltype(prob.p) === Union{Float64, Int} end @@ -1208,7 +1208,7 @@ end sys = structural_simplify(ODESystem([D(x) ~ P], t, [x], [P]; name = :sys)) function x_at_1(P) - prob = ODEProblem(sys, [x => P], (0.0, 1.0), [sys.P => P], use_union = false) + prob = ODEProblem(sys, [x => P], (0.0, 1.0), [sys.P => P]) return solve(prob, Tsit5())(1.0) end diff --git a/test/split_parameters.jl b/test/split_parameters.jl index 9b00097d54..2ecf6d3aca 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -123,7 +123,7 @@ sol = solve(prob, ImplicitEuler()); # ------------------------ Mixed Type Conserved prob = ODEProblem( - sys, [], tspan, []; tofloat = false, use_union = true, build_initializeprob = false) + sys, [], tspan, []; tofloat = false, build_initializeprob = false) @test prob.p isa Vector{Union{Float64, Int64}} sol = solve(prob, ImplicitEuler()); From 58f93c45dd2c01cee8e9f3b4059802e5bf0ec0f8 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 17 Mar 2025 10:59:29 -0400 Subject: [PATCH 06/13] revert --- src/variables.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/variables.jl b/src/variables.jl index dae044f1ec..f3dd16819d 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -158,7 +158,7 @@ applicable. """ function varmap_to_vars(varmap, varlist; defaults = Dict(), check = true, toterm = default_toterm, promotetoconcrete = nothing, - tofloat = true) + tofloat = true, use_union = true) varlist = collect(map(unwrap, varlist)) # Edge cases where one of the arguments is effectively empty. @@ -186,15 +186,14 @@ function varmap_to_vars(varmap, varlist; defaults = Dict(), check = true, vals = if eltype(varmap) <: Pair # `varmap` is a dict or an array of pairs varmap = todict(varmap) - _varmap_to_vars(varmap, varlist; defaults = defaults, check = check, - toterm = toterm) + _varmap_to_vars(varmap, varlist; defaults, check, toterm) else # plain array-like initialization varmap end promotetoconcrete === nothing && (promotetoconcrete = container_type <: AbstractArray) if promotetoconcrete - vals = promote_to_concrete(vals; tofloat = tofloat, use_union = false) + vals = promote_to_concrete(vals; tofloat, use_union) end if isempty(vals) From 97161d43d026a630998d2a5241fffc9d33d06d5a Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 17 Mar 2025 11:44:17 -0400 Subject: [PATCH 07/13] test_broken for now --- test/split_parameters.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/split_parameters.jl b/test/split_parameters.jl index 2ecf6d3aca..1c1701fb2d 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -125,7 +125,7 @@ sol = solve(prob, ImplicitEuler()); prob = ODEProblem( sys, [], tspan, []; tofloat = false, build_initializeprob = false) -@test prob.p isa Vector{Union{Float64, Int64}} +@test_broken prob.p isa Vector{Union{Float64, Int64}} sol = solve(prob, ImplicitEuler()); @test sol.retcode == ReturnCode.Success From af4dc93a84a4e69628bcdc7a1001474817857ad3 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 18 Mar 2025 10:56:58 -0400 Subject: [PATCH 08/13] revert legacy functions, remove test --- src/systems/problem_utils.jl | 15 ++++++++------- test/split_parameters.jl | 1 - 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index a5d9b02dbb..92d867db69 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -956,7 +956,7 @@ end ############## """ - u0, p, defs = get_u0_p(sys, u0map, parammap; tofloat=true) + u0, p, defs = get_u0_p(sys, u0map, parammap; use_union=true, tofloat=true) Take dictionaries with initial conditions and parameters and convert them to numeric arrays `u0` and `p`. Also return the merged dictionary `defs` containing the entire operating point. """ @@ -965,6 +965,7 @@ function get_u0_p(sys, parammap = nothing; t0 = nothing, tofloat = true, + use_union = true, symbolic_u0 = false) dvs = unknowns(sys) ps = parameters(sys; initial_parameters = true) @@ -1002,11 +1003,11 @@ function get_u0_p(sys, end if symbolic_u0 - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = false) + u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = false, use_union = false) else - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true) + u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true, use_union) end - p = varmap_to_vars(parammap, ps; defaults = defs, tofloat) + p = varmap_to_vars(parammap, ps; defaults = defs, tofloat, use_union) p = p === nothing ? SciMLBase.NullParameters() : p t0 !== nothing && delete!(defs, get_iv(sys)) u0, p, defs @@ -1014,7 +1015,7 @@ end function get_u0( sys, u0map, parammap = nothing; symbolic_u0 = false, - toterm = default_toterm, t0 = nothing) + toterm = default_toterm, t0 = nothing, use_union = true) dvs = unknowns(sys) ps = parameters(sys) defs = defaults(sys) @@ -1037,9 +1038,9 @@ function get_u0( defs = mergedefaults(defs, obsmap, u0map, dvs) if symbolic_u0 u0 = varmap_to_vars( - u0map, dvs; defaults = defs, tofloat = false, toterm) + u0map, dvs; defaults = defs, tofloat = false, use_union = false, toterm) else - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true, toterm) + u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true, use_union, toterm) end t0 !== nothing && delete!(defs, get_iv(sys)) return u0, defs diff --git a/test/split_parameters.jl b/test/split_parameters.jl index 1c1701fb2d..74f8d41d73 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -125,7 +125,6 @@ sol = solve(prob, ImplicitEuler()); prob = ODEProblem( sys, [], tspan, []; tofloat = false, build_initializeprob = false) -@test_broken prob.p isa Vector{Union{Float64, Int64}} sol = solve(prob, ImplicitEuler()); @test sol.retcode == ReturnCode.Success From eadea4031ed977763a75b5ac019f5b9adb206b2a Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 18 Mar 2025 10:58:42 -0400 Subject: [PATCH 09/13] format --- test/odesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index 7c4c4e7ead..78218c5107 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -736,7 +736,7 @@ let pmap = [k1 => 1, k2 => 1] tspan = (0.0, 1.0) - prob = ODEProblem(sys, u0map, tspan, pmap); + prob = ODEProblem(sys, u0map, tspan, pmap) @test eltype(vcat(prob.p...)) === Float64 prob = ODEProblem(sys, u0map, tspan, pmap) From 8805bb3a2a089cb63b9711cb203470a3f838f4b9 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 18 Mar 2025 11:06:54 -0400 Subject: [PATCH 10/13] format --- src/systems/diffeqs/abstractodesystem.jl | 3 +- src/systems/problem_utils.jl | 47 +++++++----------------- test/initial_values.jl | 6 ++- 3 files changed, 19 insertions(+), 37 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index f686ebcede..f39c76246d 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1504,5 +1504,6 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, else NonlinearLeastSquaresProblem end - TProb(isys, u0map, parammap; kwargs..., build_initializeprob = false, is_initializeprob = true) + TProb(isys, u0map, parammap; kwargs..., + build_initializeprob = false, is_initializeprob = true) end diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 92d867db69..0eaab993a3 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -330,17 +330,17 @@ struct MissingGuessError <: Exception vals::Vector{Any} end -function Base.showerror(io::IO, err::MissingGuessError) - println(io, - """ - Cyclic guesses detected in the system. Symbolic values were found for the following variables/parameters in the map: \ - """) +function Base.showerror(io::IO, err::MissingGuessError) + println(io, + """ + Cyclic guesses detected in the system. Symbolic values were found for the following variables/parameters in the map: \ + """) for (sym, val) in zip(err.syms, err.vals) println(io, "$sym => $val") end println(io, - """ - In order to resolve this, please provide additional numeric guesses so that the chain can be resolved to assign numeric values to each variable. """) + """ + In order to resolve this, please provide additional numeric guesses so that the chain can be resolved to assign numeric values to each variable. """) end """ @@ -363,11 +363,8 @@ Keyword arguments: """ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; tofloat = true, container_type = Array, -<<<<<<< HEAD - toterm = default_toterm, promotetoconcrete = nothing, check = true, allow_symbolic = false, is_initializeprob = false) -======= - toterm = default_toterm, promotetoconcrete = nothing, check = true, allow_symbolic = false) ->>>>>>> a5e1e0239a (remove instances of) + toterm = default_toterm, promotetoconcrete = nothing, check = true, + allow_symbolic = false, is_initializeprob = false) isempty(vars) && return nothing if check @@ -385,8 +382,8 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; end if !isempty(missingsyms) - is_initializeprob ? throw(MissingGuessError(missingsyms, missingvals)) : - throw(UnexpectedSymbolicValueInVarmap(missingsyms[1], missingvals[1])) + is_initializeprob ? throw(MissingGuessError(missingsyms, missingvals)) : + throw(UnexpectedSymbolicValueInVarmap(missingsyms[1], missingvals[1])) end end @@ -734,12 +731,7 @@ Keyword arguments: - `fully_determined`: Override whether the initialization system is fully determined. - `check_initialization_units`: Enable or disable unit checks when constructing the initialization problem. -<<<<<<< HEAD -- `tofloat`, `use_union`, `is_initializeprob`: Passed to [`better_varmap_to_vars`](@ref) for building `u0` (and - possibly `p`). -======= -- `tofloat`: Passed to [`better_varmap_to_vars`](@ref) for building `u0` (and possibly `p`). ->>>>>>> a5e1e0239a (remove instances of) +- `tofloat`, `is_initializeprob`: Passed to [`better_varmap_to_vars`](@ref) for building `u0` (and possibly `p`). - `u0_constructor`: A function to apply to the `u0` value returned from `better_varmap_to_vars` to construct the final `u0` value. - `du0map`: A map of derivatives to values. See `implicit_dae`. @@ -848,21 +840,8 @@ function process_SciMLProblem( evaluate_varmap!(op, dvs; limit = substitution_limit) u0 = better_varmap_to_vars( -<<<<<<< HEAD -<<<<<<< HEAD -<<<<<<< HEAD - op, dvs; tofloat = true, use_union = false, + op, dvs; tofloat = true, container_type = u0Type, allow_symbolic = symbolic_u0, is_initializeprob) -======= - op, dvs; tofloat, use_union, -======= - op, dvs; tofloat, use_union = false, ->>>>>>> 6951e652f2 (fix: don't propagate for u0) - container_type = u0Type, allow_symbolic = symbolic_u0) ->>>>>>> e31ae1bcc9 (fix: propagate `tofloat`, `use_union` to `better_varmap_to_vars`) -======= - op, dvs; tofloat, container_type = u0Type, allow_symbolic = symbolic_u0) ->>>>>>> a5e1e0239a (remove instances of) if u0 !== nothing u0 = u0_constructor(u0) diff --git a/test/initial_values.jl b/test/initial_values.jl index 66230557ab..f413c1a018 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -208,12 +208,14 @@ end x^2 + y^2 ~ 1] @mtkbuild pend = ODESystem(eqs, t) - @test_throws ModelingToolkit.MissingGuessError ODEProblem(pend, [x => 1], (0, 1), [g => 1], guesses = [y => λ, λ => y + 1]) + @test_throws ModelingToolkit.MissingGuessError ODEProblem( + pend, [x => 1], (0, 1), [g => 1], guesses = [y => λ, λ => y + 1]) ODEProblem(pend, [x => 1], (0, 1), [g => 1], guesses = [y => λ, λ => 0.5]) # Throw multiple if multiple are missing @variables a(t) b(t) c(t) d(t) e(t) eqs = [D(a) ~ b, D(b) ~ c, D(c) ~ d, D(d) ~ e, D(e) ~ 1] @mtkbuild sys = ODESystem(eqs, t) - @test_throws ["a(t)", "c(t)"] ODEProblem(sys, [e => 2, a => b, b => a + 1, c => d, d => c + 1], (0, 1)) + @test_throws ["a(t)", "c(t)"] ODEProblem( + sys, [e => 2, a => b, b => a + 1, c => d, d => c + 1], (0, 1)) end From 6b9dc98651ed6a908bba4357039e34b204ede7c8 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 18 Mar 2025 11:11:37 -0400 Subject: [PATCH 11/13] fix removed tofloat = true --- src/systems/problem_utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 0eaab993a3..f9f8f4df4c 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -874,7 +874,7 @@ function process_SciMLProblem( du0map = to_varmap(du0map, ddvs) merge!(op, du0map) du0 = varmap_to_vars(op, ddvs; toterm = identity, - tofloat) + tofloat = true) kwargs = merge(kwargs, (; ddvs)) else du0 = nothing From 408b86b0c9cf79077e4cac7f99f3828b00889c5a Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 18 Mar 2025 12:37:59 -0400 Subject: [PATCH 12/13] fix JumpSystem tofloat test --- src/systems/problem_utils.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index f9f8f4df4c..4964ac2986 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -840,7 +840,7 @@ function process_SciMLProblem( evaluate_varmap!(op, dvs; limit = substitution_limit) u0 = better_varmap_to_vars( - op, dvs; tofloat = true, + op, dvs; tofloat, container_type = u0Type, allow_symbolic = symbolic_u0, is_initializeprob) if u0 !== nothing @@ -874,7 +874,7 @@ function process_SciMLProblem( du0map = to_varmap(du0map, ddvs) merge!(op, du0map) du0 = varmap_to_vars(op, ddvs; toterm = identity, - tofloat = true) + tofloat) kwargs = merge(kwargs, (; ddvs)) else du0 = nothing @@ -984,7 +984,7 @@ function get_u0_p(sys, if symbolic_u0 u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = false, use_union = false) else - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true, use_union) + u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat, use_union) end p = varmap_to_vars(parammap, ps; defaults = defs, tofloat, use_union) p = p === nothing ? SciMLBase.NullParameters() : p @@ -1019,7 +1019,7 @@ function get_u0( u0 = varmap_to_vars( u0map, dvs; defaults = defs, tofloat = false, use_union = false, toterm) else - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true, use_union, toterm) + u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat, use_union, toterm) end t0 !== nothing && delete!(defs, get_iv(sys)) return u0, defs From cabd06063c3cdf7119d4b72cce10ed0a77b885ac Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 18 Mar 2025 16:21:37 -0400 Subject: [PATCH 13/13] fix get_u0 test --- src/systems/problem_utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 4964ac2986..b94a360f93 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1019,7 +1019,7 @@ function get_u0( u0 = varmap_to_vars( u0map, dvs; defaults = defs, tofloat = false, use_union = false, toterm) else - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat, use_union, toterm) + u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true, use_union, toterm) end t0 !== nothing && delete!(defs, get_iv(sys)) return u0, defs